Skip to content

refactor: rewrite ClickListener #18962

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
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
11 changes: 6 additions & 5 deletions packages/react/src/components/OverflowMenu/OverflowMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
cloneElement,
forwardRef,
isValidElement,
KeyboardEvent,
MouseEvent,
useCallback,
useContext,
useEffect,
useRef,
useState,
type ElementType,
type KeyboardEvent,
type MouseEvent,
type ReactElement,
type ReactNode,
type Ref,
Expand All @@ -26,7 +26,7 @@
import classNames from 'classnames';
import invariant from 'invariant';
import PropTypes from 'prop-types';
import ClickListener from '../../internal/ClickListener';
import { ClickListener } from '../../internal/ClickListener';
import {
DIRECTION_BOTTOM,
DIRECTION_TOP,
Expand Down Expand Up @@ -369,11 +369,12 @@
}
};

const handleClickOutside = (evt: MouseEvent<Document>) => {
const handleClickOutside = (evt: globalThis.MouseEvent) => {
if (
open &&
(!menuBodyRef.current ||
!menuBodyRef.current.contains(evt.target as Node))
(evt.target instanceof Node &&
!menuBodyRef.current.contains(evt.target)))

Check warning on line 377 in packages/react/src/components/OverflowMenu/OverflowMenu.tsx

View check run for this annotation

Codecov / codecov/patch

packages/react/src/components/OverflowMenu/OverflowMenu.tsx#L376-L377

Added lines #L376 - L377 were not covered by tests
) {
closeMenu();
}
Expand Down
79 changes: 0 additions & 79 deletions packages/react/src/internal/ClickListener.js

This file was deleted.

64 changes: 64 additions & 0 deletions packages/react/src/internal/ClickListener.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/**
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React, {
cloneElement,
useEffect,
useRef,
type ReactElement,
} from 'react';

interface ClickListenerProps {
children: ReactElement;
onClickOutside: (event: MouseEvent) => void;
}

export const ClickListener = ({
children,
onClickOutside,
}: ClickListenerProps) => {
const elementRef = useRef<HTMLElement | null>(null);

const getEventTarget = (event: MouseEvent) => {
if (event.composed && typeof event.composedPath === 'function') {
return event.composedPath()[0];
}

return event.target;
};

const handleDocumentClick = (event: MouseEvent) => {
if (elementRef.current?.contains) {
const eventTarget = getEventTarget(event);

if (
eventTarget instanceof Node &&
!elementRef.current.contains(eventTarget)
) {
onClickOutside(event);
}
}
};

useEffect(() => {
document.addEventListener('click', handleDocumentClick);

return () => {
document.removeEventListener('click', handleDocumentClick);
};
}, [onClickOutside]);

const handleRef = (el: HTMLElement | null) => {
elementRef.current = el;

if ('ref' in children && typeof children.ref === 'function') {
children.ref(el);
}
};

return cloneElement(children, { ref: handleRef });
};
22 changes: 6 additions & 16 deletions packages/react/src/internal/__tests__/ClickListener-test.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
/**
* Copyright IBM Corp. 2016, 2023
* Copyright IBM Corp. 2016, 2025
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

import React from 'react';
import ClickListener from '../ClickListener';

import { render } from '@testing-library/react';
import React, { forwardRef } from 'react';
import { ClickListener } from '../ClickListener';

describe('ClickListener', () => {
let onClickOutside;
let handleRefSpy;

beforeEach(() => {
onClickOutside = jest.fn();
handleRefSpy = jest.spyOn(ClickListener.prototype, 'handleRef');
});

afterEach(() => {
handleRefSpy.mockRestore();
});

describe('renders as expected - Component API', () => {
Expand Down Expand Up @@ -56,17 +49,14 @@ describe('ClickListener', () => {

it('should not overwrite any children function refs', () => {
const mockRef = jest.fn();
class Child extends React.Component {
render() {
return <div />;
}
}
const Child = forwardRef((props, ref) => <div ref={ref} {...props} />);

render(
<ClickListener onClickOutside={onClickOutside}>
<Child ref={mockRef} />
</ClickListener>
);
expect(handleRefSpy).toHaveBeenCalledTimes(1);

expect(mockRef).toHaveBeenCalledTimes(1);
});
});
Expand Down
Loading