Skip to content

Commit

Permalink
Link, ButtonLink: support to experimental onMouseEnter, onMouseLeave …
Browse files Browse the repository at this point in the history
…to support prefetching logic (#3933)
  • Loading branch information
AlbertCarreras authored Jan 13, 2025
1 parent 2723345 commit ed9cb16
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 0 deletions.
22 changes: 22 additions & 0 deletions packages/gestalt/src/ButtonLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,15 @@ type ButtonProps = {
event: React.MouseEvent<HTMLAnchorElement> | React.KeyboardEvent<HTMLAnchorElement>;
dangerouslyDisableOnNavigation: () => void;
}) => void;
/**
* Callback fired when a mouse pointer moves in a ButtonLink component.
*/
_onMouseEnter?: (arg1: { event: React.MouseEvent<HTMLAnchorElement> }) => void;
/**
* Callback fired when a mouse pointer moves out a ButtonLink component.
*/
_onMouseLeave?: (arg1: { event: React.MouseEvent<HTMLAnchorElement> }) => void;

/**
* sm: 32px, md: 40px, lg: 48px
*/
Expand Down Expand Up @@ -117,6 +126,9 @@ const ButtonLinkWithForwardRef = forwardRef<HTMLAnchorElement, ButtonProps>(func
iconEnd,
iconStart,
onClick,
_onMouseEnter,
_onMouseLeave,

tabIndex = 0,
size = 'md',
text,
Expand Down Expand Up @@ -176,6 +188,14 @@ const ButtonLinkWithForwardRef = forwardRef<HTMLAnchorElement, ButtonProps>(func
})
: undefined;

const handleOnMouseEnter: ComponentProps<typeof InternalLink>['onMouseEnter'] = ({ event }) => {
// @ts-expect-error - TS2322 - Type 'MouseEvent<HTMLAnchorElement, MouseEvent> | MouseEvent<HTMLDivElement, MouseEvent>' is not assignable to type 'MouseEvent<HTMLAnchorElement, MouseEvent>'.
_onMouseEnter?.({ event });
};
const handleOnMouseLeave: ComponentProps<typeof InternalLink>['onMouseLeave'] = ({ event }) =>
// @ts-expect-error - TS2322 - Type 'MouseEvent<HTMLAnchorElement, MouseEvent> | MouseEvent<HTMLDivElement, MouseEvent>' is not assignable to type 'MouseEvent<HTMLAnchorElement, MouseEvent>'.
_onMouseLeave?.({ event });

return (
<InternalLink
ref={innerRef}
Expand All @@ -187,6 +207,8 @@ const ButtonLinkWithForwardRef = forwardRef<HTMLAnchorElement, ButtonProps>(func
fullWidth={fullWidth}
href={href}
onClick={handleClick}
onMouseEnter={handleOnMouseEnter}
onMouseLeave={handleOnMouseLeave}
rel={rel}
selected={false}
size={size}
Expand Down
12 changes: 12 additions & 0 deletions packages/gestalt/src/Link.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ type Props = {
* Callback triggered when when the element loses focus.
*/
onBlur?: (arg1: { event: React.FocusEvent<HTMLAnchorElement> }) => void;
/**
* Callback fired when a mouse pointer moves in a Link component.
*/
_onMouseEnter?: (arg1: { event: React.MouseEvent<HTMLAnchorElement> }) => void;
/**
* Callback fired when a mouse pointer moves out a Link component.
*/
_onMouseLeave?: (arg1: { event: React.MouseEvent<HTMLAnchorElement> }) => void;
/**
* Callback fired when Link is clicked (pressed and released) with a mouse or keyboard. See [GlobalEventsHandlerProvider](https://gestalt.pinterest.systems/web/utilities/globaleventshandlerprovider#Link-handlers) to learn more about link navigation.
*/
Expand Down Expand Up @@ -137,6 +145,8 @@ const LinkWithForwardRef = forwardRef<HTMLAnchorElement, Props>(function Link(
href,
id,
onBlur,
_onMouseEnter,
_onMouseLeave,
onClick,
onFocus,
rel = 'none',
Expand Down Expand Up @@ -276,6 +286,8 @@ const LinkWithForwardRef = forwardRef<HTMLAnchorElement, Props>(function Link(
}}
onKeyPress={handleKeyPress}
onMouseDown={handleMouseDown}
onMouseEnter={(event) => _onMouseEnter?.({ event })}
onMouseLeave={(event) => _onMouseLeave?.({ event })}
onMouseUp={handleMouseUp}
onTouchCancel={handleTouchCancel}
onTouchEnd={handleTouchEnd}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ Array [
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -205,6 +207,8 @@ Array [
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -333,6 +337,8 @@ Array [
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -441,6 +447,8 @@ Array [
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down
4 changes: 4 additions & 0 deletions packages/gestalt/src/__snapshots__/BannerSlim.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ exports[`BannerSlim renders complex message 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -400,6 +402,8 @@ exports[`BannerSlim renders simple message with helper link 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down
22 changes: 22 additions & 0 deletions packages/gestalt/src/__snapshots__/Link.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ exports[`default 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -29,6 +31,8 @@ exports[`external link with nofollow 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -68,6 +72,8 @@ exports[`inline and auto underline 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -88,6 +94,8 @@ exports[`inline and overriden underline="hover" 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -108,6 +116,8 @@ exports[`inline and overriden underline="none" 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -128,6 +138,8 @@ exports[`regular 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -148,6 +160,8 @@ exports[`target blank 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -169,6 +183,8 @@ exports[`target null 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -189,6 +205,8 @@ exports[`target self 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -210,6 +228,8 @@ exports[`with custom rounding, and tapStyle 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand All @@ -230,6 +250,8 @@ exports[`with onClick 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down
2 changes: 2 additions & 0 deletions packages/gestalt/src/__snapshots__/PageHeader.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -633,6 +633,8 @@ exports[`PageHeader renders secondary action 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down
2 changes: 2 additions & 0 deletions packages/gestalt/src/__snapshots__/Status.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,8 @@ exports[`Status renders with rich title 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down
6 changes: 6 additions & 0 deletions packages/gestalt/src/__snapshots__/Toast.test.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ exports[`<Toast /> Text + _dangerouslySetPrimaryAction 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -215,6 +217,8 @@ exports[`<Toast /> Text + Image + Button 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down Expand Up @@ -337,6 +341,8 @@ exports[`<Toast /> Text + Image 1`] = `
onFocus={[Function]}
onKeyPress={[Function]}
onMouseDown={[Function]}
onMouseEnter={[Function]}
onMouseLeave={[Function]}
onMouseUp={[Function]}
onTouchCancel={[Function]}
onTouchEnd={[Function]}
Expand Down

0 comments on commit ed9cb16

Please sign in to comment.