-
Notifications
You must be signed in to change notification settings - Fork 6.8k
feat(cdk-experimental/toolbar): add toolbar directive and demo #31676
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
23fad80
feat(cdk-experimental/toolbar): add toolbar directive and demo
demanr 0b8e7ec
fix(cdk-experimental/toolbar): lint correction
demanr 59860c5
fix(cdk-experimental/toolbar): focus mode removal
demanr 84dcffa
fix(cdk-experimental/toolbar): skip disabled default false
demanr c70b034
fix(cdk-experimental/toolbar): uregister wording
demanr File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
load("//tools:defaults.bzl", "ng_project") | ||
|
||
package(default_visibility = ["//visibility:public"]) | ||
|
||
ng_project( | ||
name = "toolbar", | ||
srcs = glob( | ||
["**/*.ts"], | ||
exclude = ["**/*.spec.ts"], | ||
), | ||
deps = [ | ||
"//:node_modules/@angular/core", | ||
"//src/cdk-experimental/ui-patterns", | ||
"//src/cdk/a11y", | ||
"//src/cdk/bidi", | ||
], | ||
) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export * from './public-api'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
export {CdkToolbar, CdkToolbarWidget} from './toolbar'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/** | ||
* @license | ||
* Copyright Google LLC All Rights Reserved. | ||
* | ||
* Use of this source code is governed by an MIT-style license that can be | ||
* found in the LICENSE file at https://angular.dev/license | ||
*/ | ||
|
||
import { | ||
afterRenderEffect, | ||
Directive, | ||
ElementRef, | ||
inject, | ||
computed, | ||
input, | ||
booleanAttribute, | ||
signal, | ||
Signal, | ||
OnInit, | ||
OnDestroy, | ||
} from '@angular/core'; | ||
import {ToolbarPattern, RadioButtonPattern, ToolbarWidgetPattern} from '../ui-patterns'; | ||
import {Directionality} from '@angular/cdk/bidi'; | ||
import {_IdGenerator} from '@angular/cdk/a11y'; | ||
|
||
/** Interface for a radio button that can be used with a toolbar. Based on radio-button in ui-patterns */ | ||
interface CdkRadioButtonInterface<V> { | ||
/** The HTML element associated with the radio button. */ | ||
element: Signal<HTMLElement>; | ||
/** Whether the radio button is disabled. */ | ||
disabled: Signal<boolean>; | ||
|
||
pattern: RadioButtonPattern<V>; | ||
} | ||
|
||
interface HasElement { | ||
element: Signal<HTMLElement>; | ||
} | ||
|
||
/** | ||
* Sort directives by their document order. | ||
*/ | ||
function sortDirectives(a: HasElement, b: HasElement) { | ||
return (a.element().compareDocumentPosition(b.element()) & Node.DOCUMENT_POSITION_PRECEDING) > 0 | ||
? 1 | ||
: -1; | ||
} | ||
|
||
/** | ||
* A toolbar widget container. | ||
* | ||
* Widgets such as radio groups or buttons are nested within a toolbar to allow for a single | ||
* place of reference for focus and navigation. The CdkToolbar is meant to be used in conjunction | ||
* with CdkToolbarWidget and CdkRadioGroup as follows: | ||
* | ||
* ```html | ||
* <div cdkToolbar> | ||
* <button cdkToolbarWidget>Button</button> | ||
* <div cdkRadioGroup> | ||
* <label cdkRadioButton value="1">Option 1</label> | ||
* <label cdkRadioButton value="2">Option 2</label> | ||
* <label cdkRadioButton value="3">Option 3</label> | ||
* </div> | ||
* </div> | ||
* ``` | ||
*/ | ||
@Directive({ | ||
selector: '[cdkToolbar]', | ||
exportAs: 'cdkToolbar', | ||
host: { | ||
'role': 'toolbar', | ||
'class': 'cdk-toolbar', | ||
'[attr.tabindex]': 'pattern.tabindex()', | ||
'[attr.aria-disabled]': 'pattern.disabled()', | ||
'[attr.aria-orientation]': 'pattern.orientation()', | ||
'[attr.aria-activedescendant]': 'pattern.activedescendant()', | ||
'(keydown)': 'pattern.onKeydown($event)', | ||
'(pointerdown)': 'pattern.onPointerdown($event)', | ||
'(focusin)': 'onFocus()', | ||
}, | ||
}) | ||
export class CdkToolbar<V> { | ||
/** The CdkTabList nested inside of the container. */ | ||
private readonly _cdkWidgets = signal(new Set<CdkRadioButtonInterface<V> | CdkToolbarWidget>()); | ||
|
||
/** A signal wrapper for directionality. */ | ||
textDirection = inject(Directionality).valueSignal; | ||
|
||
/** Sorted UIPatterns of the child widgets */ | ||
items = computed(() => | ||
[...this._cdkWidgets()].sort(sortDirectives).map(widget => widget.pattern), | ||
); | ||
|
||
/** Whether the toolbar is vertically or horizontally oriented. */ | ||
orientation = input<'vertical' | 'horizontal'>('horizontal'); | ||
|
||
/** Whether disabled items in the group should be skipped when navigating. */ | ||
skipDisabled = input(false, {transform: booleanAttribute}); | ||
|
||
/** Whether the toolbar is disabled. */ | ||
disabled = input(false, {transform: booleanAttribute}); | ||
|
||
/** Whether focus should wrap when navigating. */ | ||
readonly wrap = input(true, {transform: booleanAttribute}); | ||
|
||
/** The toolbar UIPattern. */ | ||
pattern: ToolbarPattern<V> = new ToolbarPattern<V>({ | ||
...this, | ||
activeItem: signal(undefined), | ||
textDirection: this.textDirection, | ||
focusMode: signal('roving'), | ||
}); | ||
|
||
/** Whether the toolbar has received focus yet. */ | ||
private _hasFocused = signal(false); | ||
|
||
onFocus() { | ||
this._hasFocused.set(true); | ||
} | ||
|
||
constructor() { | ||
afterRenderEffect(() => { | ||
if (!this._hasFocused()) { | ||
this.pattern.setDefaultState(); | ||
} | ||
}); | ||
|
||
afterRenderEffect(() => { | ||
if (typeof ngDevMode === 'undefined' || ngDevMode) { | ||
const violations = this.pattern.validate(); | ||
for (const violation of violations) { | ||
console.error(violation); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
register(widget: CdkRadioButtonInterface<V> | CdkToolbarWidget) { | ||
const widgets = this._cdkWidgets(); | ||
if (!widgets.has(widget)) { | ||
widgets.add(widget); | ||
this._cdkWidgets.set(new Set(widgets)); | ||
} | ||
} | ||
|
||
unregister(widget: CdkRadioButtonInterface<V> | CdkToolbarWidget) { | ||
const widgets = this._cdkWidgets(); | ||
if (widgets.delete(widget)) { | ||
this._cdkWidgets.set(new Set(widgets)); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* A widget within a toolbar. | ||
* | ||
* A widget is anything that is within a toolbar. It should be applied to any native HTML element | ||
* that has the purpose of acting as a widget navigatable within a toolbar. | ||
*/ | ||
@Directive({ | ||
selector: '[cdkToolbarWidget]', | ||
exportAs: 'cdkToolbarWidget', | ||
host: { | ||
'role': 'button', | ||
'class': 'cdk-toolbar-widget', | ||
'[class.cdk-active]': 'pattern.active()', | ||
'[attr.tabindex]': 'pattern.tabindex()', | ||
'[attr.inert]': 'hardDisabled() ? true : null', | ||
'[attr.disabled]': 'hardDisabled() ? true : null', | ||
'[attr.aria-disabled]': 'pattern.disabled()', | ||
'[id]': 'pattern.id()', | ||
}, | ||
}) | ||
export class CdkToolbarWidget implements OnInit, OnDestroy { | ||
/** A reference to the widget element. */ | ||
private readonly _elementRef = inject(ElementRef); | ||
|
||
/** The parent CdkToolbar. */ | ||
private readonly _cdkToolbar = inject(CdkToolbar); | ||
|
||
/** A unique identifier for the widget. */ | ||
private readonly _generatedId = inject(_IdGenerator).getId('cdk-toolbar-widget-'); | ||
|
||
/** A unique identifier for the widget. */ | ||
protected id = computed(() => this._generatedId); | ||
|
||
/** The parent Toolbar UIPattern. */ | ||
protected parentToolbar = computed(() => this._cdkToolbar.pattern); | ||
|
||
/** A reference to the widget element to be focused on navigation. */ | ||
element = computed(() => this._elementRef.nativeElement); | ||
|
||
/** Whether the widget is disabled. */ | ||
disabled = input(false, {transform: booleanAttribute}); | ||
|
||
readonly hardDisabled = computed( | ||
() => this.pattern.disabled() && this._cdkToolbar.skipDisabled(), | ||
); | ||
|
||
pattern = new ToolbarWidgetPattern({ | ||
...this, | ||
id: this.id, | ||
element: this.element, | ||
disabled: computed(() => this._cdkToolbar.disabled() || this.disabled()), | ||
parentToolbar: this.parentToolbar, | ||
}); | ||
|
||
ngOnInit() { | ||
this._cdkToolbar.register(this); | ||
} | ||
|
||
ngOnDestroy() { | ||
this._cdkToolbar.unregister(this); | ||
} | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.