From 5e2f20b701d9363b9c03ec59e9ba0d5db307db48 Mon Sep 17 00:00:00 2001 From: Benny Powers Date: Wed, 15 Apr 2026 15:42:13 +0300 Subject: [PATCH] fix(core): SlotController.getSlotted() returning empty array Add fallback in getSlotted() to query slot elements directly when async slot record initialization hasn't completed yet. Closes #2946 Assisted-By: Claude Opus 4.6 (1M context) --- .changeset/fix-slot-controller.md | 6 ++++++ core/pfe-core/controllers/slot-controller.ts | 17 +++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) create mode 100644 .changeset/fix-slot-controller.md diff --git a/.changeset/fix-slot-controller.md b/.changeset/fix-slot-controller.md new file mode 100644 index 0000000000..f7742ab53f --- /dev/null +++ b/.changeset/fix-slot-controller.md @@ -0,0 +1,6 @@ +--- +"@patternfly/pfe-core": patch +--- + +`SlotController`: fixed `getSlotted()` returning empty arrays in +certain timing scenarios. diff --git a/core/pfe-core/controllers/slot-controller.ts b/core/pfe-core/controllers/slot-controller.ts index 85cb4d962e..9578975a4b 100644 --- a/core/pfe-core/controllers/slot-controller.ts +++ b/core/pfe-core/controllers/slot-controller.ts @@ -215,13 +215,26 @@ export class SlotController implements SlotControllerPublicAPI { */ public getSlotted(...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.