From e48bcd2dce615f467ddeccc7bf000ae487b6837c Mon Sep 17 00:00:00 2001 From: 1natsu <1natsu172@users.noreply.github.com> Date: Mon, 13 Jan 2025 01:59:42 +0900 Subject: [PATCH] fix: #1349 auto-mount not mounting if already exists an anchor at initialization --- .../ui/__tests__/index.test.ts | 19 +++++++++++++++++++ .../src/client/content-scripts/ui/index.ts | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/packages/wxt/src/client/content-scripts/ui/__tests__/index.test.ts b/packages/wxt/src/client/content-scripts/ui/__tests__/index.test.ts index 108c351cb..4913265f5 100644 --- a/packages/wxt/src/client/content-scripts/ui/__tests__/index.test.ts +++ b/packages/wxt/src/client/content-scripts/ui/__tests__/index.test.ts @@ -546,6 +546,25 @@ describe('Content Script UIs', () => { ] as const)( 'built-in UI type: $name', ({ name, createUiFunction, uiSelector }) => { + it('should mount if an anchor already exists at the initialization', async () => { + const onMount = vi.fn(appendTestApp); + ui = await createUiFunction(ctx, { + position: 'inline', + onMount, + anchor: `#parent > #${DYNAMIC_CHILD_ID}`, + page: name === 'iframe' ? '/page.html' : undefined, + name: 'test-component', + }); + + appendTestElement({ id: DYNAMIC_CHILD_ID }); + ui.autoMount(); + await runMicrotasks(); + + await expect + .poll(() => document.querySelector(uiSelector)) + .not.toBeNull(); + }); + it('should mount when an anchor is dynamically added and unmount when an anchor is removed', async () => { const onMount = vi.fn(appendTestApp); const onRemove = vi.fn(); diff --git a/packages/wxt/src/client/content-scripts/ui/index.ts b/packages/wxt/src/client/content-scripts/ui/index.ts index a7d91a865..49182bb40 100644 --- a/packages/wxt/src/client/content-scripts/ui/index.ts +++ b/packages/wxt/src/client/content-scripts/ui/index.ts @@ -355,6 +355,11 @@ function autoMountUi( async function observeElement(selector: string | null | undefined) { let isAnchorExist = !!getAnchor(options); + // Mount if anchor exists at initialization. + if (isAnchorExist) { + uiCallbacks.mount(); + } + while (!abortController.signal.aborted) { try { const changedAnchor = await waitElement(selector ?? 'body', {