Skip to content

feat: support React 19 and update focusout event mapping to onBlur 👌 #97

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

Merged
merged 6 commits into from
Apr 12, 2025
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
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 32 additions & 15 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {
Ref,
useRef,
useEffect,
RefCallback,
Expand All @@ -25,19 +26,36 @@ interface Props extends HTMLAttributes<HTMLElement> {
const eventTypeMapping = {
click: 'onClick',
focusin: 'onFocus',
focusout: 'onFocus',
focusout: 'onBlur',
mousedown: 'onMouseDown',
mouseup: 'onMouseUp',
touchstart: 'onTouchStart',
touchend: 'onTouchEnd'
};

const reactMajorVersion = parseInt(React.version.split('.')[0], 10);

const mergeRefs = <T extends any>(
refs: Array<Ref<T> | undefined | null>
): RefCallback<T> => {
return (value) => {
refs.forEach((ref) => {
if (typeof ref === 'function') {
ref(value);
} else if (ref != null) {
(ref as MutableRefObject<T | null>).current = value;
}
});
};
};

const ClickAwayListener: FunctionComponent<Props> = ({
children,
onClickAway,
focusEvent = 'focusin',
mouseEvent = 'click',
touchEvent = 'touchend'
touchEvent = 'touchend',
...rest
}) => {
const node = useRef<HTMLElement | null>(null);
const bubbledEventTarget = useRef<EventTarget | null>(null);
Expand Down Expand Up @@ -69,19 +87,17 @@ const ClickAwayListener: FunctionComponent<Props> = ({
}
};

const handleChildRef = (childRef: HTMLElement) => {
node.current = childRef;
let childRef: React.Ref<any> | null = null;

let { ref } = children as typeof children & {
ref: RefCallback<HTMLElement> | MutableRefObject<HTMLElement>;
};
// For React 19+, we get the ref via props.ref
if (reactMajorVersion >= 19) {
childRef = children.props?.ref || null;
} else if ('ref' in children) {
childRef = (children as any).ref;
}

if (typeof ref === 'function') {
ref(childRef);
} else if (ref) {
ref.current = childRef;
}
};
// Create a combined ref handler
const combinedRef = mergeRefs([node, childRef]);

useEffect(() => {
const nodeDocument = node.current?.ownerDocument ?? document;
Expand Down Expand Up @@ -117,10 +133,11 @@ const ClickAwayListener: FunctionComponent<Props> = ({

return React.Children.only(
cloneElement(children as ReactElement<any>, {
ref: handleChildRef,
ref: combinedRef,
[mappedFocusEvent]: handleBubbledEvents(mappedFocusEvent),
[mappedMouseEvent]: handleBubbledEvents(mappedMouseEvent),
[mappedTouchEvent]: handleBubbledEvents(mappedTouchEvent)
[mappedTouchEvent]: handleBubbledEvents(mappedTouchEvent),
...rest
})
);
};
Expand Down
Loading