mirror of
https://github.com/ionic-team/ionic-framework.git
synced 2025-11-10 00:27:41 +08:00
Issue number: N/A --------- <!-- Please do not submit updates to dependencies unless it fixes an issue. --> <!-- Please try to limit your pull request to one type (bugfix, feature, etc). Submit multiple pull requests if needed. --> ## What is the current behavior? <!-- Please describe the current behavior that you are modifying. --> the stencil team has been working on fixing multiple issues with slots elements in the stencil code base. to make these changes backwards compatible and communicate that they were volatile, we added two configuration flags for these fixes that were prefixed with the word "experimental". now that the effort to provide these fixes has largely solidified, the features behind these flags are slightly less volatile. while the "experimental" aspect still technically holds true, we've requested the Framework team to enable these flags in a v8 beta. the stencil team expects these flags to be set to `true` by default in stencil v5, which ought to help prepare for future migrations the ionic framework has to undergo. Previously, Stencil would allow content to project through to a component even when a slot was not present. However, with the changes in `extras.experimentalScopedSlotChanges`, this behavior was changed to hide elements without a destination slot - matching the behavior of a shadow encapsulated component. As such, elements projected through the `ion-label` or `ion-buttons` components would no longer be visible in rendered output. ## What is the new behavior? <!-- Please describe the behavior or changes that are being added by this PR. --> This commit adds an explicit `slot` tag to components in core leveraging "scoped" encapsulation with no rendered content (i.e. elements with only a `Host` tag and styles). `subtree` was added to a mutation observer. This fixes an issue with the `ion-input` component not re-rendering in some cases when using the label slot functionality. HTML element patches in Stencil that are enabled by the `experimentalSlotFixes` flag result in DOM manipulations that won't trigger the current mutation observer configuration and callback. ## Does this introduce a breaking change? - [] Yes - [x] No <!-- If this introduces a breaking change: 1. Describe the impact and migration path for existing applications below. 2. Update the BREAKING.md file with the breaking change. 3. Add "BREAKING CHANGE: [...]" to the commit description when merging. See https://github.com/ionic-team/ionic-framework/blob/main/.github/CONTRIBUTING.md#footer for more information. --> ## Other information Would you like us to update the commit message to include the `BREAKING CHANGE:` comment? Unsure of the actual impact on end users here. Similarly, would you like us to update the commit message here from `chore()` to something else? <!-- Any other information that is important to this PR such as screenshots of how the component looks before and after the change. --> --------- Co-authored-by: Tanner Reits <47483144+tanner-reits@users.noreply.github.com> Co-authored-by: Liam DeBeasi <liamdebeasi@users.noreply.github.com> Co-authored-by: Tanner Reits <tanner@ionic.io> Co-authored-by: Maria Hutt <thetaPC@users.noreply.github.com>
133 lines
4.3 KiB
TypeScript
133 lines
4.3 KiB
TypeScript
import { win } from '@utils/browser';
|
|
import { raf } from '@utils/helpers';
|
|
/**
|
|
* Used to update a scoped component that uses emulated slots. This fires when
|
|
* content is passed into the slot or when the content inside of a slot changes.
|
|
* This is not needed for components using native slots in the Shadow DOM.
|
|
* @internal
|
|
* @param el The host element to observe
|
|
* @param slotName mutationCallback will fire when nodes on these slot(s) change
|
|
* @param mutationCallback The callback to fire whenever the slotted content changes
|
|
*/
|
|
export const createSlotMutationController = (
|
|
el: HTMLElement,
|
|
slotName: string | string[],
|
|
mutationCallback: () => void
|
|
): SlotMutationController => {
|
|
let hostMutationObserver: MutationObserver | undefined;
|
|
let slottedContentMutationObserver: MutationObserver | undefined;
|
|
|
|
if (win !== undefined && 'MutationObserver' in win) {
|
|
const slots = Array.isArray(slotName) ? slotName : [slotName];
|
|
|
|
hostMutationObserver = new MutationObserver((entries) => {
|
|
for (const entry of entries) {
|
|
for (const node of entry.addedNodes) {
|
|
/**
|
|
* Check to see if the added node
|
|
* is our slotted content.
|
|
*/
|
|
if (node.nodeType === Node.ELEMENT_NODE && slots.includes((node as HTMLElement).slot)) {
|
|
/**
|
|
* If so, we want to watch the slotted
|
|
* content itself for changes. This lets us
|
|
* detect when content inside of the slot changes.
|
|
*/
|
|
mutationCallback();
|
|
|
|
/**
|
|
* Adding the listener in an raf
|
|
* waits until Stencil moves the slotted element
|
|
* into the correct place in the event that
|
|
* slotted content is being added.
|
|
*/
|
|
raf(() => watchForSlotChange(node as HTMLElement));
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
hostMutationObserver.observe(el, {
|
|
childList: true,
|
|
/**
|
|
* This fixes an issue with the `ion-input` and
|
|
* `ion-textarea` not re-rendering in some cases
|
|
* when using the label slot functionality.
|
|
*
|
|
* HTML element patches in Stencil that are enabled
|
|
* by the `experimentalSlotFixes` flag in Stencil v4
|
|
* result in DOM manipulations that won't trigger
|
|
* the current mutation observer configuration and
|
|
* callback.
|
|
*/
|
|
subtree: true,
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Listen for changes inside of the slotted content.
|
|
* We can listen for subtree changes here to be
|
|
* informed of text within the slotted content
|
|
* changing. Doing this on the host is possible
|
|
* but it is much more expensive to do because
|
|
* it also listens for changes to the internals
|
|
* of the component.
|
|
*/
|
|
const watchForSlotChange = (slottedEl: HTMLElement) => {
|
|
if (slottedContentMutationObserver) {
|
|
slottedContentMutationObserver.disconnect();
|
|
slottedContentMutationObserver = undefined;
|
|
}
|
|
|
|
slottedContentMutationObserver = new MutationObserver((entries) => {
|
|
mutationCallback();
|
|
|
|
for (const entry of entries) {
|
|
for (const node of entry.removedNodes) {
|
|
/**
|
|
* If the element was removed then we
|
|
* need to destroy the MutationObserver
|
|
* so the element can be garbage collected.
|
|
*/
|
|
if (node.nodeType === Node.ELEMENT_NODE && (node as HTMLElement).slot === slotName) {
|
|
destroySlottedContentObserver();
|
|
}
|
|
}
|
|
}
|
|
});
|
|
|
|
/**
|
|
* Listen for changes inside of the element
|
|
* as well as anything deep in the tree.
|
|
* We listen on the parentElement so that we can
|
|
* detect when slotted element itself is removed.
|
|
*/
|
|
slottedContentMutationObserver.observe(slottedEl.parentElement ?? slottedEl, { subtree: true, childList: true });
|
|
};
|
|
|
|
const destroy = () => {
|
|
if (hostMutationObserver) {
|
|
hostMutationObserver.disconnect();
|
|
hostMutationObserver = undefined;
|
|
}
|
|
|
|
destroySlottedContentObserver();
|
|
};
|
|
|
|
const destroySlottedContentObserver = () => {
|
|
if (slottedContentMutationObserver) {
|
|
slottedContentMutationObserver.disconnect();
|
|
slottedContentMutationObserver = undefined;
|
|
}
|
|
};
|
|
|
|
return {
|
|
destroy,
|
|
};
|
|
};
|
|
|
|
export type SlotMutationController = {
|
|
destroy: () => void;
|
|
};
|