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

feat: support auxiliary clicks #45

Open
wants to merge 1 commit into
base: main
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
5 changes: 5 additions & 0 deletions .changeset/flat-coins-unite.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@timhettler/radix-card": minor
---

Support auxiliary clicks, i.e. clicking with the middle mouse button to open the link in a new tab
9 changes: 8 additions & 1 deletion lib/src/Card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,20 @@ interface CardProps extends PrimitiveDivProps {}
const Card = React.forwardRef<CardElement, CardProps>(
(props: ScopedProps<CardProps>, forwardedRef) => {
const { __scopeCard, ...cardProps } = props;
const { targetRef, handleRedundantClick } = useRedundantClick();
const { targetRef, handleRedundantClick, handleAuxiliaryClick } =
useRedundantClick();
const [targetHasFocus, setTargetHasFocus] = React.useState<"" | null>(null);

const handleClick = composeEventHandlers(
props.onClick,
handleRedundantClick
);

const handleAuxClick = composeEventHandlers(
props.onAuxClick,
handleAuxiliaryClick
);

const handleFocus = composeEventHandlers(props.onFocus, (event) => {
setTargetHasFocus(event.target === targetRef.current ? "" : null);
});
Expand All @@ -59,6 +65,7 @@ const Card = React.forwardRef<CardElement, CardProps>(
{...cardProps}
ref={forwardedRef}
onClick={handleClick}
onAuxClick={handleAuxClick}
onFocus={handleFocus}
onBlur={handleBlur}
data-target-focused={targetHasFocus}
Expand Down
10 changes: 10 additions & 0 deletions lib/src/useRedundantClick.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,19 @@ const useRedundantClick = <T extends HTMLElement = HTMLElement>() => {
targetRef.current.dispatchEvent(newEvent);
};

// Add to the container element
const handleAuxiliaryClick = (event: React.MouseEvent) => {
if (event.button !== 1) {
return;
}
Comment on lines +43 to +45
Copy link
Owner

Choose a reason for hiding this comment

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

My concern is that this is too specific for a library... Is this guard really necessary? Is there a downside to letting all aux events pass through?

Copy link
Contributor Author

@marcusforsberg marcusforsberg Aug 29, 2024

Choose a reason for hiding this comment

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

This is so that the handler specifically only handles middle mouse clicks. I believe if we removed it, it would start firing on the other mouse buttons as well (right click, browser back, browser forward), as per this list: https://developer.mozilla.org/en-US/docs/Web/API/MouseEvent/button#value (since the auxclick event fires for all non-primary mouse buttons).

That said I honestly only learned about the existence of auxclick when I set out to implement this, so I am in no way an expert! 😄


handleRedundantClick(event);
};

return {
targetRef,
handleRedundantClick,
handleAuxiliaryClick,
};
};

Expand Down
Loading