Pull Request Number: 4284 Title: refactor: button & card ripple Base Branch: canary Head Branch: eng-1188 Author: sudongyuer URL: https://github.com/nextui-org/nextui/pull/4284 State: OPEN Created At: 2024-12-08T19:01:33Z Merged At: None Participants: sudongyuer, macci001 Description: Closes # 📝 Description Refactor Button & Card ripple In the latest version of react-aria they added the pointer events to the usePress hook so we no longer need to hack this in the components that use ripple https://react-spectrum.adobe.com/react-aria/examples/ripple-button.html ⛳️ Current behavior (updates) 🚀 New behavior 2024-12-09.03.00.23.mov 💣 Is this a breaking change (Yes/No): 📝 Additional Information Summary by CodeRabbit New Features Enhanced button and card components to support press events, improving interaction clarity and accessibility. Bug Fixes Updated event handling for ripple effects to ensure they trigger correctly under specified conditions. Refactor Refined internal logic for button and card components to distinguish between click and press events, streamlining event handling. Documentation Updated method signatures to reflect changes in event handling for better clarity. Commits: - chore: update use-button.ts\n- chore: update use-ripple.ts\n- chore: update use-card.ts\n- chore: add changeset\n Labels: Comments: - changeset-bot: ### 🦋 Changeset detected Latest commit: dce6acace482d82c1622361a6cf8d5db4e9823e1 **The changes in this PR will be included in the next version bump.**
This PR includes changesets to release 12 packages | Name | Type | | ------------------------ | ----- | | @nextui-org/button | Patch | | @nextui-org/ripple | Patch | | @nextui-org/card | Patch | | @nextui-org/alert | Patch | | @nextui-org/autocomplete | Patch | | @nextui-org/calendar | Patch | | @nextui-org/date-picker | Patch | | @nextui-org/dropdown | Patch | | @nextui-org/popover | Patch | | @nextui-org/select | Patch | | @nextui-org/snippet | Patch | | @nextui-org/react | Patch |
Not sure what this means? [Click here to learn what changesets are](https://github.com/changesets/changesets/blob/main/docs/adding-a-changeset.md). [Click here if you're a maintainer who wants to add another changeset to this PR](https://github.com/sudongyuer/nextui/new/eng-1188?filename=.changeset/soft-meals-obey.md&value=---%0A%22%40nextui-org%2Fbutton%22%3A%20patch%0A%22%40nextui-org%2Fcard%22%3A%20patch%0A%22%40nextui-org%2Fripple%22%3A%20patch%0A---%0A%0Arefactor%3A%20button%20%26%20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90YWwiOjAsImxpbmsiOiJuZXh0dWktZG9jcy12Mi1naXQtZm9yay1zdWRvbmd5dWVyLWVuZy0xMTg4LW5leHR1aS1vcmcudmVyY2VsLmFwcCJ9fV19 **The latest updates on your projects**. Learn more about [Vercel for Git ↗︎](https://vercel.link/github-learn-more) | Name | Status | Preview | Comments | Updated (UTC) | | :--- | :----- | :------ | :------- | :------ | | **nextui-docs-v2** | ✅ Ready ([Inspect](https://vercel.com/nextui-org/nextui-docs-v2/8wfi6QPHRVkhu589kgDQBGifXGNw)) | [Visit Preview](https://nextui-docs-v2-git-fork-sudongyuer-eng-1188-nextui-org.vercel.app) | 💬 [**Add feedback**](https://vercel.live/open-feedback/nextui-docs-v2-git-fork-sudongyuer-eng-1188-nextui-org.vercel.app?via=pr-comment-feedback-link) | Dec 8, 2024 7:07pm | | **nextui-storybook-v2** | ✅ Ready ([Inspect](https://vercel.com/nextui-org/nextui-storybook-v2/uAH5BdY7tuw8zJC54DtL45bgxkQQ)) | [Visit Preview](https://nextui-storybook-v2-git-fork-sudongyuer-eng-1188-nextui-org.vercel.app) | 💬 [**Add feedback**](https://vercel.live/open-feedback/nextui-storybook-v2-git-fork-sudongyuer-eng-1188-nextui-org.vercel.app?via=pr-comment-feedback-link) | Dec 8, 2024 7:07pm | \n- vercel: @sudongyuer is attempting to deploy a commit to the **NextUI Inc** Team on [Vercel](https://vercel.com). A member of the Team first needs to [authorize it](https://vercel.com/git/authorize?team=NextUI%20Inc&type=github&job=%7B%22headInfo%22%3A%7B%22sha%22%3A%22dce6acace482d82c1622361a6cf8d5db4e9823e1%22%7D%2C%22id%22%3A%22QmQc9h7wK6Qbh7VQgRmjcLPVnpQvr9ocdtohctkoXhJowf%22%2C%22org%22%3A%22nextui-org%22%2C%22prId%22%3A4284%2C%22repo%22%3A%22nextui%22%7D). \n- coderabbitai: ## Walkthrough This pull request introduces a patch for the `@nextui-org/button`, `@nextui-org/ripple`, and `@nextui-org/card` packages. It includes a refactor of the Button and Card Ripple components, focusing on updating event handling from `onClick` to `onPress`. The changes involve modifying the internal logic of the `use-button`, `use-card`, and `use-ripple` hooks to enhance the handling of button and card interactions, aligning them with new event management practices. ## Changes | Files | Change Summary | |------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------| | `packages/components/button/src/use-button.ts` | Refactored button interaction handling; renamed `handleClick` to `handlePress`, updated event types from `MouseEvent` to `PressEvent`, and adjusted `useAriaButton` parameters accordingly. | | `packages/components/card/src/use-card.ts` | Updated card interaction logic; replaced `handleClick` with `handlePress`, modified event handling to use `PressEvent`, and adjusted the return structure of the `useCard` function. | | `packages/components/ripple/src/use-ripple.ts` | Changed event handler from `onClick` to `onPress`, simplified ripple coordinate calculations, and updated the return statement to reflect the new event handler. | ## Possibly related PRs - **#3439**: This PR addresses the `Ripple` component in the Button documentation, which is relevant as the main PR also involves changes to the Button component and its interaction with the ripple effect. - **#3569**: This PR fixes a deprecation warning related to the `onClick` event in the Button component, which aligns with the main PR's changes to event handling in the Button component. - **#4168**: This PR ensures that components like `ListboxItem`, `MenuItem`, and `PaginationItem` correctly wrap their content in `
  • ` tags when using the `href` prop, which is relevant to the overall structure and semantics of components that may interact with the Button and Card components in the main PR. ## Suggested labels `📋 Scope : Docs` ## Suggested reviewers - wingkwong - jrgarciadev --- Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?
    ❤️ Share - [X](https://twitter.com/intent/tweet?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A&url=https%3A//coderabbit.ai) - [Mastodon](https://mastodon.social/share?text=I%20just%20used%20%40coderabbitai%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20the%20proprietary%20code.%20Check%20it%20out%3A%20https%3A%2F%2Fcoderabbit.ai) - [Reddit](https://www.reddit.com/submit?title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&text=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code.%20Check%20it%20out%3A%20https%3A//coderabbit.ai) - [LinkedIn](https://www.linkedin.com/sharing/share-offsite/?url=https%3A%2F%2Fcoderabbit.ai&mini=true&title=Great%20tool%20for%20code%20review%20-%20CodeRabbit&summary=I%20just%20used%20CodeRabbit%20for%20my%20code%20review%2C%20and%20it%27s%20fantastic%21%20It%27s%20free%20for%20OSS%20and%20offers%20a%20free%20trial%20for%20proprietary%20code)
    🪧 Tips ### Chat There are 3 ways to chat with [CodeRabbit](https://coderabbit.ai): - Review comments: Directly reply to a review comment made by CodeRabbit. Example: - `I pushed a fix in commit , please review it.` - `Generate unit testing code for this file.` - `Open a follow-up GitHub issue for this discussion.` - Files and specific lines of code (under the "Files changed" tab): Tag `@coderabbitai` in a new review comment at the desired location with your query. Examples: - `@coderabbitai generate unit testing code for this file.` - `@coderabbitai modularize this function.` - PR comments: Tag `@coderabbitai` in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples: - `@coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.` - `@coderabbitai read src/utils.ts and generate unit testing code.` - `@coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.` - `@coderabbitai help me debug CodeRabbit configuration file.` Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments. ### CodeRabbit Commands (Invoked using PR comments) - `@coderabbitai pause` to pause the reviews on a PR. - `@coderabbitai resume` to resume the paused reviews. - `@coderabbitai review` to trigger an incremental review. This is useful when automatic reviews are disabled for the repository. - `@coderabbitai full review` to do a full review from scratch and review all the files again. - `@coderabbitai summary` to regenerate the summary of the PR. - `@coderabbitai resolve` resolve all the CodeRabbit review comments. - `@coderabbitai configuration` to show the current CodeRabbit configuration for the repository. - `@coderabbitai help` to get help. ### Other keywords and placeholders - Add `@coderabbitai ignore` anywhere in the PR description to prevent this PR from being reviewed. - Add `@coderabbitai summary` to generate the high-level summary at a specific location in the PR description. - Add `@coderabbitai` anywhere in the PR title to generate the title automatically. ### Documentation and Community - Visit our [Documentation](https://docs.coderabbit.ai) for detailed information on how to use CodeRabbit. - Join our [Discord Community](http://discord.gg/coderabbit) to get help, request features, and share feedback. - Follow us on [X/Twitter](https://twitter.com/coderabbitai) for updates and announcements.
    \n- macci001: The PR fixes the ripple issue on touch devices, which we saw after upgrading the RA. Also side-note: seems like after moving to onPress from onClick, we can also get rid of this issue #4223\n Files Changed: - .changeset/rude-cobras-grab.md (added, 7 changes)\n Patch: @@ -0,0 +1,7 @@ +--- +"@nextui-org/button": patch +"@nextui-org/ripple": patch +"@nextui-org/card": patch +--- + +Refactor Button & Card Ripple\n- packages/components/button/src/use-button.ts (modified, 16 changes)\n Patch: @@ -14,7 +14,7 @@ import {useDOMRef, filterDOMProps} from "@nextui-org/react-utils"; import {button} from "@nextui-org/theme"; import {isValidElement, cloneElement, useMemo} from "react"; import {useAriaButton} from "@nextui-org/use-aria-button"; -import {useHover} from "@react-aria/interactions"; +import {PressEvent, useHover} from "@react-aria/interactions"; import {SpinnerProps} from "@nextui-org/spinner"; import {useRipple} from "@nextui-org/ripple"; @@ -135,22 +135,22 @@ export function useButton(props: UseButtonProps) { ], ); - const {onClick: onRippleClickHandler, onClear: onClearRipple, ripples} = useRipple(); + const {onPress: onRipplePressHandler, onClear: onClearRipple, ripples} = useRipple(); - const handleClick = useCallback( - (e: React.MouseEvent) => { + const handlePress = useCallback( + (e: PressEvent) => { if (disableRipple || isDisabled || disableAnimation) return; - domRef.current && onRippleClickHandler(e); + domRef.current && onRipplePressHandler(e); }, - [disableRipple, isDisabled, disableAnimation, domRef, onRippleClickHandler], + [disableRipple, isDisabled, disableAnimation, domRef, onRipplePressHandler], ); const {buttonProps: ariaButtonProps, isPressed} = useAriaButton( { elementType: as, isDisabled, - onPress, - onClick: chain(onClick, handleClick), + onPress: chain(onPress, handlePress), + onClick: onClick, ...otherProps, } as AriaButtonProps, domRef,\n- packages/components/card/src/use-card.ts (modified, 24 changes)\n Patch: @@ -4,10 +4,10 @@ import type {AriaButtonProps} from "@nextui-org/use-aria-button"; import type {RippleProps} from "@nextui-org/ripple"; import {card} from "@nextui-org/theme"; -import {MouseEvent, ReactNode, useCallback, useMemo} from "react"; +import {ReactNode, useCallback, useMemo} from "react"; import {chain, mergeProps} from "@react-aria/utils"; import {useFocusRing} from "@react-aria/focus"; -import {useHover} from "@react-aria/interactions"; +import {PressEvent, useHover} from "@react-aria/interactions"; import {useAriaButton} from "@nextui-org/use-aria-button"; import { HTMLNextUIProps, @@ -96,20 +96,22 @@ export function useCard(originalProps: UseCardProps) { const baseStyles = clsx(classNames?.base, className); - const {onClick: onRippleClickHandler, onClear: onClearRipple, ripples} = useRipple(); + const {onClear: onClearRipple, onPress: onRipplePressHandler, ripples} = useRipple(); - const handleClick = (e: MouseEvent) => { - if (!disableAnimation && !disableRipple && domRef.current) { - onRippleClickHandler(e); - } - }; + const handlePress = useCallback( + (e: PressEvent) => { + if (disableRipple || disableAnimation) return; + domRef.current && onRipplePressHandler(e); + }, + [disableRipple, disableAnimation, domRef, onRipplePressHandler], + ); const {buttonProps, isPressed} = useAriaButton( { - onPress, + onPress: chain(onPress, handlePress), elementType: as, isDisabled: !originalProps.isPressable, - onClick: chain(onClick, handleClick), + onClick: onClick, allowTextSelectionOnPress, ...otherProps, } as unknown as AriaButtonProps<"button">, @@ -209,7 +211,7 @@ export function useCard(originalProps: UseCardProps) { isPressable: originalProps.isPressable, isHoverable: originalProps.isHoverable, disableRipple, - handleClick, + handlePress, isFocusVisible, getCardProps, getRippleProps,\n- packages/components/ripple/src/use-ripple.ts (modified, 12 changes)\n Patch: @@ -1,5 +1,6 @@ import {getUniqueID} from "@nextui-org/shared-utils"; import React, {useCallback, useState} from "react"; +import {PressEvent} from "@react-types/shared"; export type RippleType = { key: React.Key; @@ -13,19 +14,18 @@ export interface UseRippleProps {} export function useRipple(props: UseRippleProps = {}) { const [ripples, setRipples] = useState([]); - const onClick = useCallback((event: React.MouseEvent) => { - const trigger = event.currentTarget; + const onPress = useCallback((event: PressEvent) => { + const trigger = event.target; const size = Math.max(trigger.clientWidth, trigger.clientHeight); - const rect = trigger.getBoundingClientRect(); setRipples((prevRipples) => [ ...prevRipples, { key: getUniqueID(prevRipples.length.toString()), size, - x: event.clientX - rect.left - size / 2, - y: event.clientY - rect.top - size / 2, + x: event.x - size / 2, + y: event.y - size / 2, }, ]); }, []); @@ -34,7 +34,7 @@ export function useRipple(props: UseRippleProps = {}) { setRipples((prevState) => prevState.filter((ripple) => ripple.key !== key)); }, []); - return {ripples, onClick, onClear, ...props}; + return {ripples, onClear, onPress, ...props}; } export type UseRippleReturn = ReturnType;\n