Skip to content

Commit 6fc7d92

Browse files
authored
fix(content-script-ui): Properly assign and unassign mounted value (#598)
1 parent 2e0e104 commit 6fc7d92

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

Diff for: src/client/content-scripts/ui/__tests__/index.test.ts

+58
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,62 @@ describe('Content Script UIs', () => {
404404
});
405405
});
406406
});
407+
408+
describe('mounted value', () => {
409+
describe('integrated', () => {
410+
it('should set the mounted value based on the onMounted return value', () => {
411+
const expected = Symbol();
412+
413+
const ui = createIntegratedUi(new ContentScriptContext('test'), {
414+
position: 'inline',
415+
onMount: () => expected,
416+
});
417+
expect(ui.mounted).toBeUndefined();
418+
419+
ui.mount();
420+
expect(ui.mounted).toBe(expected);
421+
422+
ui.remove();
423+
expect(ui.mounted).toBeUndefined();
424+
});
425+
});
426+
427+
describe('iframe', () => {
428+
it('should set the mounted value based on the onMounted return value', async () => {
429+
const expected = Symbol();
430+
431+
const ui = createIframeUi(new ContentScriptContext('test'), {
432+
page: '',
433+
position: 'inline',
434+
onMount: () => expected,
435+
});
436+
expect(ui.mounted).toBeUndefined();
437+
438+
ui.mount();
439+
expect(ui.mounted).toBe(expected);
440+
441+
ui.remove();
442+
expect(ui.mounted).toBeUndefined();
443+
});
444+
});
445+
446+
describe('shadow-root', () => {
447+
it('should set the mounted value based on the onMounted return value', async () => {
448+
const expected = Symbol();
449+
450+
const ui = await createShadowRootUi(new ContentScriptContext('test'), {
451+
name: 'test',
452+
position: 'inline',
453+
onMount: () => expected,
454+
});
455+
expect(ui.mounted).toBeUndefined();
456+
457+
ui.mount();
458+
expect(ui.mounted).toBe(expected);
459+
460+
ui.remove();
461+
expect(ui.mounted).toBeUndefined();
462+
});
463+
});
464+
});
407465
});

Diff for: src/client/content-scripts/ui/index.ts

+14-4
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,15 @@ export function createIntegratedUi<TMounted>(
3535
const remove = () => {
3636
options.onRemove?.(mounted);
3737
wrapper.remove();
38+
mounted = undefined;
3839
};
3940

4041
ctx.onInvalidated(remove);
4142

4243
return {
43-
mounted,
44+
get mounted() {
45+
return mounted;
46+
},
4447
wrapper,
4548
mount,
4649
remove,
@@ -71,12 +74,15 @@ export function createIframeUi<TMounted>(
7174
const remove = () => {
7275
options.onRemove?.(mounted);
7376
wrapper.remove();
77+
mounted = undefined;
7478
};
7579

7680
ctx.onInvalidated(remove);
7781

7882
return {
79-
mounted,
83+
get mounted() {
84+
return mounted;
85+
},
8086
iframe,
8187
wrapper,
8288
mount,
@@ -116,7 +122,7 @@ export async function createShadowRootUi<TMounted>(
116122
});
117123
shadowHost.setAttribute('data-wxt-shadow-root', '');
118124

119-
let mounted: TMounted;
125+
let mounted: TMounted | undefined;
120126

121127
const mount = () => {
122128
// Add shadow root element to DOM
@@ -134,6 +140,8 @@ export async function createShadowRootUi<TMounted>(
134140
// Remove children from uiContainer
135141
while (uiContainer.lastChild)
136142
uiContainer.removeChild(uiContainer.lastChild);
143+
// Clear mounted value
144+
mounted = undefined;
137145
};
138146

139147
ctx.onInvalidated(remove);
@@ -144,7 +152,9 @@ export async function createShadowRootUi<TMounted>(
144152
uiContainer,
145153
mount,
146154
remove,
147-
mounted: mounted!,
155+
get mounted() {
156+
return mounted;
157+
},
148158
};
149159
}
150160

0 commit comments

Comments
 (0)