Skip to content

Commit 1575647

Browse files
fix(item): replace mutation observer with slot change listeners
1 parent 36b17fe commit 1575647

File tree

1 file changed

+11
-25
lines changed

1 file changed

+11
-25
lines changed

core/src/components/item/item.tsx

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
3232
private labelColorStyles = {};
3333
private itemStyles = new Map<string, CssClassMap>();
3434
private inheritedAriaAttributes: Attributes = {};
35-
private observer?: MutationObserver;
3635

3736
@Element() el!: HTMLIonItemElement;
3837

@@ -165,19 +164,6 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
165164

166165
connectedCallback() {
167166
this.hasStartEl();
168-
169-
// create MutationObserver to watch for changes in nested content
170-
// and update state to reflect change in behavior and rerender
171-
if (typeof MutationObserver !== 'undefined' && !this.observer) {
172-
this.observer = new MutationObserver(() => {
173-
this.setIsInteractive();
174-
this.setMultipleInputs();
175-
});
176-
177-
this.observer.observe(this.el, {
178-
childList: true,
179-
});
180-
}
181167
}
182168

183169
componentWillLoad() {
@@ -187,17 +173,11 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
187173
componentDidLoad() {
188174
raf(() => {
189175
this.setMultipleInputs();
176+
this.setIsInteractive();
190177
this.focusable = this.isFocusable();
191178
});
192179
}
193180

194-
disconnectedCallback(): void {
195-
// disconnect MutationObserver when component is disconnected from the DOM
196-
if (this.observer) {
197-
this.observer.disconnect();
198-
}
199-
}
200-
201181
private totalNestedInputs() {
202182
// The following elements have a clickable cover that is relative to the entire item
203183
const covers = this.el.querySelectorAll('ion-checkbox, ion-datetime, ion-select, ion-radio');
@@ -237,9 +217,15 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
237217
// If item contains any interactive children, set isInteractive to `true`
238218
const { covers, inputs, clickables } = this.totalNestedInputs();
239219

240-
this.isInteractive = !!(covers.length + inputs.length + clickables.length);
220+
this.isInteractive = covers.length > 0 || inputs.length > 0 || clickables.length > 0;
241221
}
242222

223+
// slot change listener updates state to reflect how/if item should be interactive
224+
private updateInteractivityOnSlotChange = () => {
225+
this.setIsInteractive();
226+
this.setMultipleInputs();
227+
};
228+
243229
// If the item contains an input including a checkbox, datetime, select, or radio
244230
// then the item will have a clickable input cover that covers the item
245231
// that should get the hover, focused and activated states UNLESS it has multiple
@@ -403,12 +389,12 @@ export class Item implements ComponentInterface, AnchorInterface, ButtonInterfac
403389
disabled={disabled}
404390
{...clickFn}
405391
>
406-
<slot name="start"></slot>
392+
<slot name="start" onSlotchange={this.updateInteractivityOnSlotChange}></slot>
407393
<div class="item-inner">
408394
<div class="input-wrapper">
409-
<slot></slot>
395+
<slot onSlotchange={this.updateInteractivityOnSlotChange}></slot>
410396
</div>
411-
<slot name="end"></slot>
397+
<slot name="end" onSlotchange={this.updateInteractivityOnSlotChange}></slot>
412398
{showDetail && (
413399
<ion-icon
414400
icon={detailIcon}

0 commit comments

Comments
 (0)