Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changeset/fix-slot-controller.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@patternfly/pfe-core": patch
---

`SlotController`: fixed `getSlotted()` returning empty arrays in
certain timing scenarios.
17 changes: 15 additions & 2 deletions core/pfe-core/controllers/slot-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -215,13 +215,26 @@ export class SlotController implements SlotControllerPublicAPI {
*/
public getSlotted<T extends Element = Element>(...slotNames: string[] | [null]): T[] {
if (!slotNames.length || slotNames.length === 1 && slotNames.at(0) === null) {
return (this.#slotRecords.get(SlotController.default)?.elements ?? []) as T[];
return (this.#getAssignedElements(SlotController.default)) as T[];
} else {
return slotNames.flatMap(slotName =>
this.#slotRecords.get(slotName ?? SlotController.default)?.elements ?? []) as T[];
this.#getAssignedElements(slotName ?? SlotController.default)) as T[];
}
}

/**
* Returns the assigned elements for a given slot name, falling back to
* querying the slot element directly if the slot record hasn't been
* initialized yet.
*/
#getAssignedElements(slotId: string | symbol): Element[] {
const record = this.#slotRecords.get(slotId);
if (record) {
return record.elements;
}
return this.#getSlotElement(slotId)?.assignedElements?.() ?? [];
}

/**
* Returns a boolean statement of whether or not any of those slots exists in the light DOM.
* @param names The slot names to check.
Expand Down
Loading