Skip to content

Commit

Permalink
fix: autoMount not working if anchor is already present (#1350)
Browse files Browse the repository at this point in the history
  • Loading branch information
1natsu172 authored Jan 12, 2025
1 parent fb6a298 commit 5d7e251
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 0 deletions.
19 changes: 19 additions & 0 deletions packages/wxt/src/client/content-scripts/ui/__tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions packages/wxt/src/client/content-scripts/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down

0 comments on commit 5d7e251

Please sign in to comment.