Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: enhance DOM judgment #2723

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
25 changes: 25 additions & 0 deletions packages/hooks/src/utils/dom.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
/**
* Thx rc-util
* copied from https://github.com/react-component/util/blob/v5.44.3/src/Dom/findDOMNode.ts#L4-L23
*/

export function isDOM(node: any): node is HTMLElement | SVGElement {
// https://developer.mozilla.org/en-US/docs/Web/API/Element
// Since XULElement is also subclass of Element, we only need HTMLElement and SVGElement
return node instanceof HTMLElement || node instanceof SVGElement;
}

/**
* Retrieves a DOM node via a ref, and does not invoke `findDOMNode`.
*/
export function getDOM(node: any): HTMLElement | SVGElement | null {
if (node && typeof node === 'object' && isDOM(node.nativeElement)) {
return node.nativeElement;
}

if (isDOM(node)) {
return node as any;
}

return null;
}
3 changes: 2 additions & 1 deletion packages/hooks/src/utils/domTarget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { MutableRefObject } from 'react';
import { isFunction } from './index';
import isBrowser from './isBrowser';
import { getDOM } from './dom';

type TargetValue<T> = T | undefined | null;

Expand Down Expand Up @@ -30,5 +31,5 @@ export function getTargetElement<T extends TargetType>(target: BasicTarget<T>, d
targetElement = target;
}

return targetElement;
return getDOM(targetElement)
}