Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/click-outside-lite-fixes.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"click-outside-lite": patch
---

Clicks inside an element in a shadow root are no longer reported as outside (uses `composedPath()`), the `once` cleanup runs even if the callback throws, and an invalid `ignore` selector throws once at setup instead of on every event.
43 changes: 43 additions & 0 deletions packages/click-outside-lite/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,4 +150,47 @@ describe('onClickOutside', () => {
expect(callback).toHaveBeenCalled();
});
});

describe('shadow DOM, once and ignore validation', () => {
test('a click inside an element in a shadow root is not outside', () => {
const host = document.createElement('div');
document.body.appendChild(host);
const root = host.attachShadow({ mode: 'open' });
const inner = document.createElement('button');
root.appendChild(inner);

const callback = vi.fn();
const cleanup = onClickOutside(inner, callback);

inner.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, composed: true }));
expect(callback).not.toHaveBeenCalled();

outside.dispatchEvent(new MouseEvent('mousedown', { bubbles: true, composed: true }));
expect(callback).toHaveBeenCalledTimes(1);
cleanup();
});

test('once cleans up even when the callback throws', () => {
const callback = vi.fn(() => {
throw new Error('boom');
});
onClickOutside(target, callback, { once: true });

// jsdom reports an error thrown in a listener to the window instead of rethrowing
const onError = vi.fn((e: ErrorEvent) => e.preventDefault());
window.addEventListener('error', onError);
outside.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
outside.dispatchEvent(new MouseEvent('mousedown', { bubbles: true }));
window.removeEventListener('error', onError);

expect(callback).toHaveBeenCalledTimes(1);
});

test('an invalid ignore selector throws once at setup', () => {
const addSpy = vi.spyOn(document, 'addEventListener');
expect(() => onClickOutside(target, vi.fn(), { ignore: ['div['] })).toThrow();
expect(addSpy).not.toHaveBeenCalled();
addSpy.mockRestore();
});
});
});
20 changes: 14 additions & 6 deletions packages/click-outside-lite/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ export function onClickOutside(
return el;
};

// Throws once here for an invalid selector, not on every event
ignore.forEach(selector => document.querySelector(selector));

const targets = Array.isArray(target) ? target : [target];
const elements = targets.map(resolveElement).filter(Boolean) as HTMLElement[];

Expand All @@ -53,24 +56,29 @@ export function onClickOutside(
if (!isActive) return;

const target = event.target as Node;
// composedPath sees through shadow roots, where event.target is the host
const path = typeof event.composedPath === 'function' ? event.composedPath() : [];
const within = (el: Element) => path.includes(el) || el.contains(target);

// Check if click is inside any of the elements
const isInside = elements.some(el => el.contains(target));
const isInside = elements.some(within);
if (isInside) return;

// Check if click is on an ignored element
if (ignore.length > 0) {
const isIgnored = ignore.some(selector => {
const ignored = document.querySelectorAll(selector);
return Array.from(ignored).some(el => el.contains(target));
return Array.from(ignored).some(within);
});
if (isIgnored) return;
}

callback(event);

if (once) {
cleanup();
try {
callback(event);
} finally {
if (once) {
cleanup();
}
}
};

Expand Down
Loading