Skip to content

Commit 7872523

Browse files
authored
fix(multiple): expose element (#32328)
* fix(multiple): expose element * fixup! fix(multiple): expose element
1 parent 1629175 commit 7872523

File tree

11 files changed

+126
-80
lines changed

11 files changed

+126
-80
lines changed

src/aria/accordion/accordion.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,15 @@ export class AccordionTrigger {
147147
/** A reference to the trigger element. */
148148
private readonly _elementRef = inject(ElementRef);
149149

150+
/** A reference to the trigger element. */
151+
readonly element = this._elementRef.nativeElement as HTMLElement;
152+
150153
/** The parent AccordionGroup. */
151154
private readonly _accordionGroup = inject(AccordionGroup);
152155

153156
/** A unique identifier for the widget. */
154157
readonly id = input(inject(_IdGenerator).getId('ng-accordion-trigger-', true));
155158

156-
/** The host native element. */
157-
readonly element = computed(() => this._elementRef.nativeElement);
158-
159159
/** A local unique identifier for the trigger, used to match with its panel's `panelId`. */
160160
readonly panelId = input.required<string>();
161161

@@ -176,6 +176,7 @@ export class AccordionTrigger {
176176
...this,
177177
accordionGroup: computed(() => this._accordionGroup._pattern),
178178
accordionPanel: this._accordionPanel,
179+
element: () => this.element,
179180
});
180181

181182
/** Expands this item. */
@@ -242,6 +243,9 @@ export class AccordionGroup {
242243
/** A reference to the group element. */
243244
private readonly _elementRef = inject(ElementRef);
244245

246+
/** A reference to the group element. */
247+
readonly element = this._elementRef.nativeElement as HTMLElement;
248+
245249
/** The AccordionTriggers nested inside this group. */
246250
private readonly _triggers = contentChildren(AccordionTrigger, {descendants: true});
247251

@@ -251,9 +255,6 @@ export class AccordionGroup {
251255
/** The AccordionPanels nested inside this group. */
252256
private readonly _panels = contentChildren(AccordionPanel, {descendants: true});
253257

254-
/** The host native element. */
255-
readonly element = computed(() => this._elementRef.nativeElement);
256-
257258
/** The text direction (ltr or rtl). */
258259
readonly textDirection = inject(Directionality).valueSignal;
259260

@@ -280,6 +281,7 @@ export class AccordionGroup {
280281
// TODO(ok7sai): Investigate whether an accordion should support horizontal mode.
281282
orientation: () => 'vertical',
282283
getItem: e => this._getItem(e),
284+
element: () => this.element,
283285
});
284286

285287
constructor() {

src/aria/combobox/combobox.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,9 @@ export class Combobox<V> {
9191
/** The element that the combobox is attached to. */
9292
private readonly _elementRef = inject(ElementRef);
9393

94+
/** A reference to the combobox element. */
95+
readonly element = this._elementRef.nativeElement as HTMLElement;
96+
9497
/** The DeferredContentAware host directive. */
9598
private readonly _deferredContentAware = inject(DeferredContentAware, {optional: true});
9699

@@ -213,6 +216,9 @@ export class ComboboxInput {
213216
/** The element that the combobox is attached to. */
214217
private readonly _elementRef = inject<ElementRef<HTMLInputElement>>(ElementRef);
215218

219+
/** A reference to the input element. */
220+
readonly element = this._elementRef.nativeElement as HTMLElement;
221+
216222
/** The combobox that the input belongs to. */
217223
readonly combobox = inject(Combobox);
218224

@@ -330,7 +336,10 @@ export class ComboboxPopup<V> {
330336
})
331337
export class ComboboxDialog {
332338
/** The dialog element. */
333-
readonly element = inject(ElementRef<HTMLDialogElement>);
339+
private readonly _elementRef = inject(ElementRef<HTMLDialogElement>);
340+
341+
/** A reference to the dialog element. */
342+
readonly element = this._elementRef.nativeElement as HTMLElement;
334343

335344
/** The combobox that the dialog belongs to. */
336345
readonly combobox = inject(Combobox);
@@ -345,7 +354,7 @@ export class ComboboxDialog {
345354
constructor() {
346355
this._pattern = new ComboboxDialogPattern({
347356
id: () => '',
348-
element: () => this.element.nativeElement,
357+
element: () => this._elementRef.nativeElement,
349358
combobox: this.combobox._pattern,
350359
});
351360

@@ -354,10 +363,10 @@ export class ComboboxDialog {
354363
}
355364

356365
afterRenderEffect(() => {
357-
if (this.element) {
366+
if (this._elementRef) {
358367
this.combobox._pattern.expanded()
359-
? this.element.nativeElement.showModal()
360-
: this.element.nativeElement.close();
368+
? this._elementRef.nativeElement.showModal()
369+
: this._elementRef.nativeElement.close();
361370
}
362371
});
363372
}

src/aria/grid/grid.ts

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,9 @@ export class Grid {
6565
/** A reference to the host element. */
6666
private readonly _elementRef = inject(ElementRef);
6767

68+
/** A reference to the host element. */
69+
readonly element = this._elementRef.nativeElement as HTMLElement;
70+
6871
/** The rows that make up the grid. */
6972
private readonly _rows = contentChildren(GridRow, {descendants: true});
7073

@@ -76,9 +79,6 @@ export class Grid {
7679
/** Text direction. */
7780
readonly textDirection = inject(Directionality).valueSignal;
7881

79-
/** The host native element. */
80-
readonly element = computed(() => this._elementRef.nativeElement);
81-
8282
/** Whether selection is enabled for the grid. */
8383
readonly enableSelection = input(false, {transform: booleanAttribute});
8484

@@ -132,6 +132,7 @@ export class Grid {
132132
...this,
133133
rows: this._rowPatterns,
134134
getCell: e => this._getCell(e),
135+
element: () => this.element,
135136
});
136137

137138
constructor() {
@@ -186,6 +187,9 @@ export class GridRow {
186187
/** A reference to the host element. */
187188
private readonly _elementRef = inject(ElementRef);
188189

190+
/** A reference to the host element. */
191+
readonly element = this._elementRef.nativeElement as HTMLElement;
192+
189193
/** The cells that make up this row. */
190194
private readonly _cells = contentChildren(GridCell, {descendants: true});
191195

@@ -200,9 +204,6 @@ export class GridRow {
200204
/** The parent grid UI pattern. */
201205
readonly grid = computed(() => this._grid._pattern);
202206

203-
/** The host native element. */
204-
readonly element = computed(() => this._elementRef.nativeElement);
205-
206207
/** The index of this row within the grid. */
207208
readonly rowIndex = input<number>();
208209

@@ -250,6 +251,9 @@ export class GridCell {
250251
/** A reference to the host element. */
251252
private readonly _elementRef = inject(ElementRef);
252253

254+
/** A reference to the host element. */
255+
readonly element = this._elementRef.nativeElement as HTMLElement;
256+
253257
/** The widgets contained within this cell, if any. */
254258
private readonly _widgets = contentChildren(GridCellWidget, {descendants: true});
255259

@@ -267,9 +271,6 @@ export class GridCell {
267271
/** A unique identifier for the cell. */
268272
readonly id = input(inject(_IdGenerator).getId('ng-grid-cell-', true));
269273

270-
/** The host native element. */
271-
readonly element = computed(() => this._elementRef.nativeElement);
272-
273274
/** The ARIA role for the cell. */
274275
readonly role = input<'gridcell' | 'columnheader' | 'rowheader'>('gridcell');
275276

@@ -318,6 +319,7 @@ export class GridCell {
318319
row: () => this._row._pattern,
319320
widgets: this._widgetPatterns,
320321
getWidget: e => this._getWidget(e),
322+
element: () => this.element,
321323
});
322324

323325
constructor() {}
@@ -369,12 +371,12 @@ export class GridCellWidget {
369371
/** A reference to the host element. */
370372
private readonly _elementRef = inject(ElementRef);
371373

374+
/** A reference to the host element. */
375+
readonly element = this._elementRef.nativeElement as HTMLElement;
376+
372377
/** The parent cell. */
373378
private readonly _cell = inject(GridCell);
374379

375-
/** The host native element. */
376-
readonly element = computed(() => this._elementRef.nativeElement);
377-
378380
/** A unique identifier for the widget. */
379381
readonly id = input(inject(_IdGenerator).getId('ng-grid-cell-widget-', true));
380382

@@ -407,6 +409,7 @@ export class GridCellWidget {
407409
/** The UI pattern for the grid cell widget. */
408410
readonly _pattern = new GridCellWidgetPattern({
409411
...this,
412+
element: () => this.element,
410413
cell: () => this._cell._pattern,
411414
focusTarget: computed(() => {
412415
if (this.focusTarget() instanceof ElementRef) {

src/aria/listbox/listbox.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,12 @@ export class Listbox<V> {
7171
optional: true,
7272
});
7373

74-
/** A reference to the listbox element. */
74+
/** A reference to the host element. */
7575
private readonly _elementRef = inject(ElementRef);
7676

77+
/** A reference to the host element. */
78+
readonly element = this._elementRef.nativeElement as HTMLElement;
79+
7780
/** The directionality (LTR / RTL) context for the application (or a subtree of it). */
7881
private readonly _directionality = inject(Directionality);
7982

@@ -233,9 +236,12 @@ export class Listbox<V> {
233236
},
234237
})
235238
export class Option<V> {
236-
/** A reference to the option element. */
239+
/** A reference to the host element. */
237240
private readonly _elementRef = inject(ElementRef);
238241

242+
/** A reference to the host element. */
243+
readonly element = this._elementRef.nativeElement as HTMLElement;
244+
239245
/** The parent Listbox. */
240246
private readonly _listbox = inject(Listbox);
241247

@@ -245,14 +251,11 @@ export class Option<V> {
245251
// TODO(wagnermaciel): See if we want to change how we handle this since textContent is not
246252
// reactive. See https://github.com/angular/components/pull/30495#discussion_r1961260216.
247253
/** The text used by the typeahead search. */
248-
protected searchTerm = computed(() => this.label() ?? this.element().textContent);
254+
protected searchTerm = computed(() => this.label() ?? this.element.textContent);
249255

250256
/** The parent Listbox UIPattern. */
251257
protected listbox = computed(() => this._listbox._pattern);
252258

253-
/** A reference to the option element to be focused on navigation. */
254-
protected element = computed(() => this._elementRef.nativeElement);
255-
256259
/** The value of the option. */
257260
value = input.required<V>();
258261

@@ -271,7 +274,7 @@ export class Option<V> {
271274
id: this.id,
272275
value: this.value,
273276
listbox: this.listbox,
274-
element: this.element,
277+
element: () => this.element,
275278
searchTerm: this.searchTerm,
276279
});
277280
}

src/aria/menu/menu.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ import {Directionality} from '@angular/cdk/bidi';
6969
},
7070
})
7171
export class MenuTrigger<V> {
72-
/** A reference to the menu trigger element. */
72+
/** A reference to the host element. */
7373
private readonly _elementRef = inject(ElementRef);
7474

75+
/** A reference to the host element. */
76+
readonly element = this._elementRef.nativeElement as HTMLElement;
77+
7578
/** The directionality (LTR / RTL) context for the application (or a subtree of it). */
7679
readonly textDirection = inject(Directionality).valueSignal;
7780

78-
/** A reference to the menu element. */
79-
readonly element: HTMLButtonElement = this._elementRef.nativeElement;
80-
8181
/** The menu associated with the trigger. */
8282
menu = input<Menu<V> | undefined>(undefined);
8383

@@ -175,11 +175,11 @@ export class Menu<V> {
175175
this._allItems().filter(i => i.parent === this),
176176
);
177177

178-
/** A reference to the menu element. */
178+
/** A reference to the host element. */
179179
private readonly _elementRef = inject(ElementRef);
180180

181-
/** A reference to the menu element. */
182-
readonly element: HTMLElement = this._elementRef.nativeElement;
181+
/** A reference to the host element. */
182+
readonly element = this._elementRef.nativeElement as HTMLElement;
183183

184184
/** The directionality (LTR / RTL) context for the application (or a subtree of it). */
185185
readonly textDirection = inject(Directionality).valueSignal;
@@ -320,11 +320,11 @@ export class MenuBar<V> {
320320
readonly _items: SignalLike<MenuItem<V>[]> = () =>
321321
this._allItems().filter(i => i.parent === this);
322322

323-
/** A reference to the menu element. */
323+
/** A reference to the host element. */
324324
private readonly _elementRef = inject(ElementRef);
325325

326-
/** A reference to the menubar element. */
327-
readonly element: HTMLElement = this._elementRef.nativeElement;
326+
/** A reference to the host element. */
327+
readonly element = this._elementRef.nativeElement as HTMLElement;
328328

329329
/** Whether the menubar is disabled. */
330330
readonly disabled = input(false, {transform: booleanAttribute});
@@ -412,11 +412,11 @@ export class MenuBar<V> {
412412
},
413413
})
414414
export class MenuItem<V> {
415-
/** A reference to the menu item element. */
415+
/** A reference to the host element. */
416416
private readonly _elementRef = inject(ElementRef);
417417

418-
/** A reference to the menu element. */
419-
readonly element: HTMLElement = this._elementRef.nativeElement;
418+
/** A reference to the host element. */
419+
readonly element = this._elementRef.nativeElement as HTMLElement;
420420

421421
/** The unique ID of the menu item. */
422422
readonly id = input(inject(_IdGenerator).getId('ng-menu-item-', true));

0 commit comments

Comments
 (0)