-
Notifications
You must be signed in to change notification settings - Fork 6.8k
refactor(aria/listbox): avoid circular references at runtime #32488
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
Open
crisbeto
wants to merge
1
commit into
angular:main
Choose a base branch
from
crisbeto:listbox-circular-deps-poc
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
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,83 @@ | ||
| /** | ||
| * @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 {booleanAttribute, computed, Directive, ElementRef, inject, input} from '@angular/core'; | ||
| import {_IdGenerator} from '@angular/cdk/a11y'; | ||
| import {OptionPattern} from '../private'; | ||
| import {LISTBOX} from './tokens'; | ||
|
|
||
| /** | ||
| * A selectable option in an `ngListbox`. | ||
| * | ||
| * This directive should be applied to an element (e.g., `<li>`, `<div>`) within an | ||
| * `ngListbox`. The `value` input is used to identify the option, and the `label` input provides | ||
| * the accessible name for the option. | ||
| * | ||
| * ```html | ||
| * <li ngOption value="item-id" label="Item Name"> | ||
| * Item Name | ||
| * </li> | ||
| * ``` | ||
| * | ||
| * @developerPreview 21.0 | ||
| */ | ||
| @Directive({ | ||
| selector: '[ngOption]', | ||
| exportAs: 'ngOption', | ||
| host: { | ||
| 'role': 'option', | ||
| '[attr.data-active]': 'active()', | ||
| '[attr.id]': '_pattern.id()', | ||
| '[attr.tabindex]': '_pattern.tabIndex()', | ||
| '[attr.aria-selected]': '_pattern.selected()', | ||
| '[attr.aria-disabled]': '_pattern.disabled()', | ||
| }, | ||
| }) | ||
| export class Option<V> { | ||
| /** A reference to the host element. */ | ||
| readonly element = inject(ElementRef).nativeElement as HTMLElement; | ||
|
|
||
| /** Whether the option is currently active (focused). */ | ||
| active = computed(() => this._pattern.active()); | ||
|
|
||
| /** The parent Listbox. */ | ||
| private readonly _listbox = inject(LISTBOX); | ||
|
|
||
| /** A unique identifier for the option. */ | ||
| readonly id = input(inject(_IdGenerator).getId('ng-option-', true)); | ||
|
|
||
| // TODO(wagnermaciel): See if we want to change how we handle this since textContent is not | ||
| // reactive. See https://github.com/angular/components/pull/30495#discussion_r1961260216. | ||
| /** The text used by the typeahead search. */ | ||
| protected searchTerm = computed(() => this.label() ?? this.element.textContent); | ||
|
|
||
| /** The parent Listbox UIPattern. */ | ||
| private readonly _listboxPattern = computed(() => this._listbox._pattern); | ||
|
|
||
| /** The value of the option. */ | ||
| value = input.required<V>(); | ||
|
|
||
| /** Whether an item is disabled. */ | ||
| disabled = input(false, {transform: booleanAttribute}); | ||
|
|
||
| /** The text used by the typeahead search. */ | ||
| label = input<string>(); | ||
|
|
||
| /** Whether the option is selected. */ | ||
| readonly selected = computed(() => this._pattern.selected()); | ||
|
|
||
| /** The Option UIPattern. */ | ||
| readonly _pattern = new OptionPattern<V>({ | ||
| ...this, | ||
| id: this.id, | ||
| value: this.value, | ||
| listbox: this._listboxPattern, | ||
| element: () => this.element, | ||
| searchTerm: () => this.searchTerm() ?? '', | ||
| }); | ||
| } | ||
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,12 @@ | ||
| /** | ||
| * @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 {InjectionToken} from '@angular/core'; | ||
| import type {Listbox} from './listbox'; | ||
|
|
||
| export const LISTBOX = new InjectionToken<Listbox<any>>('LISTBOX'); |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we use
'@angular/aria/private';or does that have no impact?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imports within the same npm package need to be relative after the recent dev infra changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we change listbox.ts to use the relative path then?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah yeah we should. Not sure why the build didn't break.