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

Added host to hooks for instanceof check #100

Open
wants to merge 1 commit 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
8 changes: 6 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,19 @@ export type RoundingFunction = (n: number) => number;

function useResizeObserver<T extends Element>(
opts: {
host: Window & typeof globalThis,
ref?: RefObject<T> | T | null | undefined;
onResize?: ResizeHandler;
box?: ResizeObserverBoxOptions;
round?: RoundingFunction;
} = {}
} = {
host: window
}
): HookResponse<T> {
// Saving the callback as a ref. With this, I don't need to put onResize in the
// effect dep array, and just passing in an anonymous function without memoising
// will not reinstantiate the hook's ResizeObserver.
const onResize = opts.onResize;
const { onResize, host } = opts;
const onResizeRef = useRef<ResizeHandler | undefined>(undefined);
onResizeRef.current = onResize;
const round = opts.round || Math.round;
Expand Down Expand Up @@ -152,6 +155,7 @@ function useResizeObserver<T extends Element>(
},
[opts.box, round]
),
host,
opts.ref
);

Expand Down
5 changes: 3 additions & 2 deletions src/utils/useResolvedElement.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ type SubscriberResponse = SubscriberCleanupFunction | void;
// refs to such extent, but then composing hooks and components could not opt out of unnecessary renders.
export default function useResolvedElement<T extends Element>(
subscriber: (element: T) => SubscriberResponse,
refOrElement?: T | RefObject<T> | null
host: Window & typeof globalThis = window,
refOrElement?: T | RefObject<T> | null,
): RefCallback<T> {
const lastReportRef = useRef<{
element: T | null;
Expand All @@ -31,7 +32,7 @@ export default function useResolvedElement<T extends Element>(
const element: T | null = cbElement
? cbElement
: refOrElement
? refOrElement instanceof Element
? refOrElement instanceof host.Element
? refOrElement
: refOrElement.current
: null;
Expand Down