fix(core): prevent SlotController.getSlotted() returning empty array prior to initialization#3093
Conversation
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) <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: e90ca21 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
✅ Deploy Preview for patternfly-elements ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
✅ Commitlint tests passed!More Info{
"valid": true,
"errors": [],
"warnings": [],
"input": "fix(core): prevent SlotController.getSlotted() returning empty array prior to initialization"
} |
|
Alternative approach: synchronous The current fix adds a fallback in The gap also affects Why Both Suggested change Make hostConnected(): void {
this.#mo.observe(this.host, { childList: true });
this.#slotRecords.clear();
}
hostUpdated(): void {
if (this.#slotRecords.size === 0) {
for (let slotName of this.#slotNames.concat(Object.values(this.#deprecations))) {
slotName ||= SlotController.default;
const slot = this.#getSlotElement(slotName);
if (slot) {
this.#slotRecords.set(slotName, new SlotRecord(slot, slotName, this.host));
}
}
if (this.#slotRecords.size > 0) {
this.host.requestUpdate();
}
}
}The MutationObserver callback becomes: #mo = new MutationObserver(() => this.host.requestUpdate());And This also requires updating the SlotControllerPublicAPI declaration to match: hostConnected?(): void;Why this works
What this fixes that the current PR doesn't
Trade-off The two-render pattern on initial connection is inherent We can spin this off to another issue if we feel the need |
zeroedin
left a comment
There was a problem hiding this comment.
This looks good to me as is, the alternative comment can be broken out into its own issue and is not a blocker to this PR. Tested artifact down stream in RHDS.
Summary
Fixed
getSlotted()returning empty arrays when called before asyncslot record initialization completes. Falls back to querying slot
elements directly when records aren't yet populated.
Closes #2946
Test plan
getSlotted()returns correct elements in downstream consumers