Skip to content

Commit 338cf2e

Browse files
authored
chore: move type guards from domHelpers into typeHelpers (#10832)
1 parent e84abb9 commit 338cf2e

7 files changed

Lines changed: 59 additions & 49 deletions

File tree

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
export {
2-
addEvent,
3-
getOwnerDocument,
4-
getOwnerWindow,
5-
isDocument,
6-
isShadowRoot
7-
} from '../../../src/utils/domHelpers';
1+
export {addEvent, getOwnerDocument, getOwnerWindow} from '../../../src/utils/domHelpers';
2+
export {isDocument, isShadowRoot} from '../../../src/utils/typeHelpers';
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export {isWindow, isDocument, isShadowRoot} from '../../../src/utils/typeHelpers';

‎packages/react-aria/src/interactions/utils.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@
1313
import {FocusableElement} from '@react-types/shared';
1414
import {focusWithoutScrolling} from '../utils/focusWithoutScrolling';
1515
import {getActiveElement, getEventTarget, nodeContains} from '../utils/shadowdom/DOMFunctions';
16-
import {getOwnerWindow, isNode, isShadowRoot} from '../utils/domHelpers';
16+
import {getOwnerWindow} from '../utils/domHelpers';
1717
import {isFocusable} from '../utils/isFocusable';
18+
import {isNode, isShadowRoot} from '../utils/typeHelpers';
1819
import {FocusEvent as ReactFocusEvent, SyntheticEvent, useCallback, useRef} from 'react';
1920
import {useLayoutEffect} from '../utils/useLayoutEffect';
2021

‎packages/react-aria/src/overlays/ariaHideOutside.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
*/
1212

1313
import {createShadowTreeWalker} from '../utils/shadowdom/ShadowTreeWalker';
14-
15-
import {getOwnerDocument, getOwnerWindow, isShadowRoot} from '../utils/domHelpers';
14+
import {getOwnerDocument, getOwnerWindow} from '../utils/domHelpers';
15+
import {isShadowRoot} from '../utils/typeHelpers';
1616
import {nodeContains} from '../utils/shadowdom/DOMFunctions';
1717
import {shadowDOM} from 'react-stately/private/flags/flags';
1818

‎packages/react-aria/src/utils/domHelpers.ts‎

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
*/
1212

1313
import type {EventMapType} from '@react-types/shared';
14+
import {isDocument, isWindow} from './typeHelpers';
1415

1516
export const getOwnerDocument = (target?: EventTarget | null): Document => {
1617
if (isWindow(target)) return target.document;
@@ -28,44 +29,6 @@ export const getOwnerWindow = (target?: EventTarget | null): Window & typeof glo
2829
return ownerDocument?.defaultView ?? (typeof window !== 'undefined' ? window : undefined);
2930
};
3031

31-
/**
32-
* Type guard that checks if a value is a Node. Verifies the presence and type of the nodeType
33-
* property.
34-
*/
35-
export function isNode(value: unknown): value is Node {
36-
return (
37-
value !== null &&
38-
typeof value === 'object' &&
39-
'nodeType' in value &&
40-
typeof value.nodeType === 'number'
41-
);
42-
}
43-
44-
/**
45-
* Type guard that checks if a value is a Window. Uses window self reference checks to
46-
* distinguish Window from other values.
47-
*/
48-
function isWindow(value: unknown): value is Window & typeof globalThis {
49-
return typeof value === 'object' && value != null && 'window' in value && value.window === value;
50-
}
51-
52-
/**
53-
* Type guard that checks if a value is a Document. Uses nodeType and host property checks to
54-
* distinguish Document from other values.
55-
*/
56-
export function isDocument(value: unknown): value is Document {
57-
return isNode(value) && value.nodeType === 9;
58-
}
59-
60-
/**
61-
* Type guard that checks if a value is a ShadowRoot. Uses nodeType and host property checks to
62-
* distinguish ShadowRoot from other values.
63-
*/
64-
export function isShadowRoot(value: unknown): value is ShadowRoot {
65-
// 11 = DOCUMENT_FRAGMENT_NODE
66-
return isNode(value) && value.nodeType === 11 && 'host' in value;
67-
}
68-
6932
/**
7033
* Attaches an event listener on target(s) and returns a cleanup function.
7134
*/

‎packages/react-aria/src/utils/shadowdom/DOMFunctions.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22
/* eslint-disable rsp-rules/no-non-shadow-contains, rsp-rules/safe-event-target */
33

44
import {EventTargetType} from '@react-types/shared';
5-
import {getOwnerWindow, isShadowRoot} from '../domHelpers';
5+
import {getOwnerWindow} from '../domHelpers';
6+
import {isShadowRoot} from '../typeHelpers';
67
import {shadowDOM} from 'react-stately/private/flags/flags';
78
import type {SyntheticEvent} from 'react';
89

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* Copyright 2026 Adobe. All rights reserved.
3+
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
4+
* you may not use this file except in compliance with the License. You may obtain a copy
5+
* of the License at http://www.apache.org/licenses/LICENSE-2.0
6+
*
7+
* Unless required by applicable law or agreed to in writing, software distributed under
8+
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
9+
* OF ANY KIND, either express or implied. See the License for the specific language
10+
* governing permissions and limitations under the License.
11+
*/
12+
13+
/**
14+
* Type guard that checks if a value is a Node. Verifies the presence and type of the nodeType
15+
* property.
16+
*/
17+
export function isNode(value: unknown): value is Node {
18+
return (
19+
value !== null &&
20+
typeof value === 'object' &&
21+
'nodeType' in value &&
22+
typeof value.nodeType === 'number'
23+
);
24+
}
25+
26+
/**
27+
* Type guard that checks if a value is a Window. Uses window self reference checks to
28+
* distinguish Window from other values.
29+
*/
30+
export function isWindow(value: unknown): value is Window & typeof globalThis {
31+
return typeof value === 'object' && value != null && 'window' in value && value.window === value;
32+
}
33+
34+
/**
35+
* Type guard that checks if a value is a Document. Uses nodeType and host property checks to
36+
* distinguish Document from other values.
37+
*/
38+
export function isDocument(value: unknown): value is Document {
39+
return isNode(value) && value.nodeType === 9;
40+
}
41+
42+
/**
43+
* Type guard that checks if a value is a ShadowRoot. Uses nodeType and host property checks to
44+
* distinguish ShadowRoot from other values.
45+
*/
46+
export function isShadowRoot(value: unknown): value is ShadowRoot {
47+
// 11 = DOCUMENT_FRAGMENT_NODE
48+
return isNode(value) && value.nodeType === 11 && 'host' in value;
49+
}

0 commit comments

Comments
 (0)