Skip to content

Commit 46daafd

Browse files
authored
Add DOM utilities to primitive package (#3625)
1 parent 13e76f0 commit 46daafd

File tree

4 files changed

+37
-4
lines changed

4 files changed

+37
-4
lines changed

.changeset/slick-ravens-eat.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@radix-ui/primitive': patch
3+
---
4+
5+
Added DOM utilities
Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
11
// @ts-check
22
import { configs } from '@repo/eslint-config/react-package';
33

4-
export default configs;
4+
/** @type {import("eslint").Linter.Config[]} */
5+
export default [
6+
...configs,
7+
{
8+
linterOptions: { reportUnusedDisableDirectives: false },
9+
},
10+
];
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { composeEventHandlers } from './primitive';
1+
export * from './primitive';

packages/core/primitive/src/primitive.tsx

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,12 @@
1-
function composeEventHandlers<E extends { defaultPrevented: boolean }>(
1+
/* eslint-disable no-restricted-globals */
2+
export const canUseDOM = !!(
3+
typeof window !== 'undefined' &&
4+
window.document &&
5+
window.document.createElement
6+
);
7+
/* eslint-enable no-restricted-globals */
8+
9+
export function composeEventHandlers<E extends { defaultPrevented: boolean }>(
210
originalEventHandler?: (event: E) => void,
311
ourEventHandler?: (event: E) => void,
412
{ checkForDefaultPrevented = true } = {}
@@ -12,4 +20,18 @@ function composeEventHandlers<E extends { defaultPrevented: boolean }>(
1220
};
1321
}
1422

15-
export { composeEventHandlers };
23+
export function getOwnerWindow(element: Element | null | undefined) {
24+
if (!canUseDOM) {
25+
throw new Error('Cannot access window outside of the DOM');
26+
}
27+
// eslint-disable-next-line no-restricted-globals
28+
return element?.ownerDocument?.defaultView ?? window;
29+
}
30+
31+
export function getOwnerDocument(element: Element | null | undefined) {
32+
if (!canUseDOM) {
33+
throw new Error('Cannot access document outside of the DOM');
34+
}
35+
// eslint-disable-next-line no-restricted-globals
36+
return element?.ownerDocument ?? document;
37+
}

0 commit comments

Comments
 (0)