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
9 changes: 2 additions & 7 deletions packages/react-aria/exports/private/utils/domHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,2 @@
export {
addEvent,
getOwnerDocument,
getOwnerWindow,
isDocument,
isShadowRoot
} from '../../../src/utils/domHelpers';
export {addEvent, getOwnerDocument, getOwnerWindow} from '../../../src/utils/domHelpers';
export {isDocument, isShadowRoot} from '../../../src/utils/typeHelpers';

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not strictly necessary, but I think it's a nice bit of reorganisation. Technically theses are on private paths, so we could just omit them from re-export here, but this seems like a nice thing to do

1 change: 1 addition & 0 deletions packages/react-aria/exports/private/utils/typeHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {isWindow, isDocument, isShadowRoot} from '../../../src/utils/typeHelpers';
3 changes: 2 additions & 1 deletion packages/react-aria/src/interactions/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@
import {FocusableElement} from '@react-types/shared';
import {focusWithoutScrolling} from '../utils/focusWithoutScrolling';
import {getActiveElement, getEventTarget, nodeContains} from '../utils/shadowdom/DOMFunctions';
import {getOwnerWindow, isNode, isShadowRoot} from '../utils/domHelpers';
import {getOwnerWindow} from '../utils/domHelpers';
import {isFocusable} from '../utils/isFocusable';
import {isNode, isShadowRoot} from '../utils/typeHelpers';
import {FocusEvent as ReactFocusEvent, SyntheticEvent, useCallback, useRef} from 'react';
import {useLayoutEffect} from '../utils/useLayoutEffect';

Expand Down
4 changes: 2 additions & 2 deletions packages/react-aria/src/overlays/ariaHideOutside.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
*/

import {createShadowTreeWalker} from '../utils/shadowdom/ShadowTreeWalker';

import {getOwnerDocument, getOwnerWindow, isShadowRoot} from '../utils/domHelpers';
import {getOwnerDocument, getOwnerWindow} from '../utils/domHelpers';
import {isShadowRoot} from '../utils/typeHelpers';
import {nodeContains} from '../utils/shadowdom/DOMFunctions';
import {shadowDOM} from 'react-stately/private/flags/flags';

Expand Down
39 changes: 1 addition & 38 deletions packages/react-aria/src/utils/domHelpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
*/

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

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

/**
* Type guard that checks if a value is a Node. Verifies the presence and type of the nodeType
* property.
*/
export function isNode(value: unknown): value is Node {
return (
value !== null &&
typeof value === 'object' &&
'nodeType' in value &&
typeof value.nodeType === 'number'
);
}

/**
* Type guard that checks if a value is a Window. Uses window self reference checks to
* distinguish Window from other values.
*/
function isWindow(value: unknown): value is Window & typeof globalThis {
return typeof value === 'object' && value != null && 'window' in value && value.window === value;
}

/**
* Type guard that checks if a value is a Document. Uses nodeType and host property checks to
* distinguish Document from other values.
*/
export function isDocument(value: unknown): value is Document {
return isNode(value) && value.nodeType === 9;
}

/**
* Type guard that checks if a value is a ShadowRoot. Uses nodeType and host property checks to
* distinguish ShadowRoot from other values.
*/
export function isShadowRoot(value: unknown): value is ShadowRoot {
// 11 = DOCUMENT_FRAGMENT_NODE
return isNode(value) && value.nodeType === 11 && 'host' in value;
}

/**
* Attaches an event listener on target(s) and returns a cleanup function.
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/react-aria/src/utils/shadowdom/DOMFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
/* eslint-disable rsp-rules/no-non-shadow-contains, rsp-rules/safe-event-target */

import {EventTargetType} from '@react-types/shared';
import {getOwnerWindow, isShadowRoot} from '../domHelpers';
import {getOwnerWindow} from '../domHelpers';
import {isShadowRoot} from '../typeHelpers';
import {shadowDOM} from 'react-stately/private/flags/flags';
import type {SyntheticEvent} from 'react';

Expand Down
49 changes: 49 additions & 0 deletions packages/react-aria/src/utils/typeHelpers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* Copyright 2026 Adobe. All rights reserved.
* This file is licensed to you under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. You may obtain a copy
* of the License at http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under
* the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR REPRESENTATIONS
* OF ANY KIND, either express or implied. See the License for the specific language
* governing permissions and limitations under the License.
*/

/**
* Type guard that checks if a value is a Node. Verifies the presence and type of the nodeType
* property.
*/
export function isNode(value: unknown): value is Node {
return (
value !== null &&
typeof value === 'object' &&
'nodeType' in value &&
typeof value.nodeType === 'number'
);
}

/**
* Type guard that checks if a value is a Window. Uses window self reference checks to
* distinguish Window from other values.
*/
export function isWindow(value: unknown): value is Window & typeof globalThis {
return typeof value === 'object' && value != null && 'window' in value && value.window === value;
}

/**
* Type guard that checks if a value is a Document. Uses nodeType and host property checks to
* distinguish Document from other values.
*/
export function isDocument(value: unknown): value is Document {
return isNode(value) && value.nodeType === 9;
}

/**
* Type guard that checks if a value is a ShadowRoot. Uses nodeType and host property checks to
* distinguish ShadowRoot from other values.
*/
export function isShadowRoot(value: unknown): value is ShadowRoot {
// 11 = DOCUMENT_FRAGMENT_NODE
return isNode(value) && value.nodeType === 11 && 'host' in value;
}
Loading