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

components: useHoverOverlay should compose props #87246

Merged
merged 2 commits into from
Mar 19, 2025
Merged
Changes from 1 commit
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
36 changes: 36 additions & 0 deletions static/app/utils/useHoverOverlay.spec.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import {render, screen, userEvent} from 'sentry-test/reactTestingLibrary';

import {useHoverOverlay} from 'sentry/utils/useHoverOverlay';

function Component(props: Partial<React.HTMLAttributes<HTMLInputElement>>) {
const {wrapTrigger} = useHoverOverlay('test', {skipWrapper: true});

return wrapTrigger(<input {...props} />);
}

describe('useHoverOverlay', () => {
it('skipWrapper=true composes handlers', async () => {
const componentProps = {
onPointerEnter: jest.fn(),
onPointerLeave: jest.fn(),
onFocus: jest.fn(),
onBlur: jest.fn(),
};

render(<Component {...componentProps} />);

const input = screen.getByRole('textbox');

await userEvent.hover(input);
expect(componentProps.onPointerEnter).toHaveBeenCalled();

await userEvent.unhover(input);
expect(componentProps.onPointerLeave).toHaveBeenCalled();

await userEvent.click(input);
expect(componentProps.onFocus).toHaveBeenCalled();

await userEvent.tab();
expect(componentProps.onBlur).toHaveBeenCalled();
});
});
46 changes: 37 additions & 9 deletions static/app/utils/useHoverOverlay.tsx
Original file line number Diff line number Diff line change
@@ -266,14 +266,35 @@ function useHoverOverlay(
*/
const wrapTrigger = useCallback(
(triggerChildren: React.ReactNode) => {
const props = {
const makeProps = (
componentProps: Partial<
Pick<
React.HTMLAttributes<any>,
'onFocus' | 'onBlur' | 'onPointerEnter' | 'onPointerLeave'
>
>
) => ({
// !!These props are always overriden!!
'aria-describedby': describeById,
ref: setTriggerElement,
Copy link
Member Author

Choose a reason for hiding this comment

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

This is unfortunate, but we need react 19 to be able to fix it. If I attempt to compose the ref it react to error as the ref is a "reserved prop"

onFocus: handleMouseEnter,
onBlur: handleMouseLeave,
onPointerEnter: handleMouseEnter,
onPointerLeave: handleMouseLeave,
};
// The following props are composed from the componentProps trigger props
onFocus: (e: React.FocusEvent<any>) => {
handleMouseEnter();
componentProps.onFocus?.(e);
},
onBlur: (e: React.FocusEvent<any>) => {
handleMouseLeave();
componentProps.onBlur?.(e);
},
onPointerEnter: (e: React.PointerEvent<any>) => {
handleMouseEnter();
componentProps.onPointerEnter?.(e);
},
onPointerLeave: (e: React.PointerEvent<any>) => {
handleMouseLeave();
componentProps.onPointerLeave?.(e);
},
});
Copy link
Member

Choose a reason for hiding this comment

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


// Use the `type` property of the react instance to detect whether we have
// a basic element (type=string) or a class/function component
@@ -291,20 +312,27 @@ function useHoverOverlay(

return cloneElement<any>(
triggerChildren,
Object.assign(props, {style: triggerStyle})
Object.assign(makeProps(triggerChildren.props), {style: triggerStyle})
);
}

// Basic DOM nodes can be cloned and have more props applied.
return cloneElement<any>(
triggerChildren,
Object.assign(props, {
Object.assign(makeProps(triggerChildren.props), {
style: triggerChildren.props.style,
})
);
}

const containerProps = Object.assign(props, {
const props =
isValidElement(triggerChildren) && 'props' in triggerChildren
? triggerChildren.props
: {};

// @ts-expect-error props is of type unknown at this point,
// and we cant infer signature what they could be
const containerProps = Object.assign(makeProps(props), {
style: {
...(showUnderline ? theme.tooltipUnderline(underlineColor) : {}),
...(containerDisplayMode ? {display: containerDisplayMode} : {}),