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

refactor: button & card ripple #4284

Merged
merged 4 commits into from
Dec 10, 2024
Merged

Conversation

sudongyuer
Copy link
Member

@sudongyuer sudongyuer commented Dec 8, 2024

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.

Copy link

changeset-bot bot commented Dec 8, 2024

🦋 Changeset detected

Latest commit: dce6aca

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.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Dec 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextui-docs-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 8, 2024 7:07pm
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Dec 8, 2024 7:07pm

Copy link

vercel bot commented Dec 8, 2024

@sudongyuer is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Dec 8, 2024

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

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
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, 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 for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Outside diff range and nitpick comments (2)
packages/components/button/src/use-button.ts (1)

152-153: Consider adding JSDoc deprecation notice for onClick

While the code handles both event types correctly, adding a formal deprecation notice would help guide users towards using onPress.

    {
      elementType: as,
      isDisabled,
+     /** @deprecated Use onPress instead of onClick */
      onClick: onClick,
      onPress: chain(onPress, handlePress),
      ...otherProps,
    }
packages/components/card/src/use-card.ts (1)

111-111: Consider adding type safety for onClick deprecation

To maintain consistency with the button component, consider adding similar onClick deprecation handling.

    {
+     /** @deprecated Use onPress instead of onClick */
      onClick: onClick,
      onPress: chain(onPress, handlePress),
      elementType: as,
      isDisabled: !originalProps.isPressable,
      allowTextSelectionOnPress,
      ...otherProps,
    }

Also applies to: 114-114

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 3b390fb and dce6aca.

📒 Files selected for processing (4)
  • .changeset/rude-cobras-grab.md (1 hunks)
  • packages/components/button/src/use-button.ts (2 hunks)
  • packages/components/card/src/use-card.ts (3 hunks)
  • packages/components/ripple/src/use-ripple.ts (3 hunks)
✅ Files skipped from review due to trivial changes (1)
  • .changeset/rude-cobras-grab.md
🔇 Additional comments (2)
packages/components/button/src/use-button.ts (1)

138-138: LGTM! Clean transition to PressEvent handling

The implementation correctly handles both the new onPress and deprecated onClick events, maintaining backward compatibility while moving towards the more accessible PressEvent pattern.

Also applies to: 140-145

packages/components/card/src/use-card.ts (1)

99-99: LGTM! Consistent implementation with button component

The card component's press handling implementation correctly mirrors the button component's approach, ensuring consistent behavior across components.

Also applies to: 101-107

Comment on lines +17 to +18
const onPress = useCallback((event: PressEvent) => {
const trigger = event.target;
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Consider using event.currentTarget instead of event.target

Using event.target might lead to incorrect ripple positioning when clicking on child elements within the button/card. The target could be a nested element, while currentTarget always refers to the element the event handler is attached to.

-  const trigger = event.target;
+  const trigger = event.currentTarget;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const onPress = useCallback((event: PressEvent) => {
const trigger = event.target;
const onPress = useCallback((event: PressEvent) => {
const trigger = event.currentTarget;

Comment on lines +27 to +28
x: event.x - size / 2,
y: event.y - size / 2,
Copy link
Contributor

Choose a reason for hiding this comment

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

💡 Codebase verification

⚠️ Potential issue

Scroll position needs to be accounted for in ripple coordinates

The current implementation uses event.x and event.y which are viewport-relative coordinates. This will cause misaligned ripples when the page is scrolled since these coordinates don't account for scroll position. The suggested fix in the review comment using event.pageX, event.pageY and getBoundingClientRect() is the correct approach as it properly handles scroll position.

  • packages/components/ripple/src/use-ripple.ts: Replace viewport coordinates with page-relative coordinates that account for scroll position and element position.
🔗 Analysis chain

Verify scroll position handling in coordinate calculation

The direct use of event.x and event.y might not account for scroll position, potentially causing misaligned ripples when the page is scrolled.

-    x: event.x - size / 2,
-    y: event.y - size / 2,
+    x: event.pageX - trigger.getBoundingClientRect().left - size / 2,
+    y: event.pageY - trigger.getBoundingClientRect().top - size / 2,

Let me gather more context about the ripple implementation to verify the coordinate calculation.


Let me try a different approach to examine the ripple implementation.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Search for scroll position handling in ripple implementations
rg -A 5 "scroll(Left|Top)" 
ast-grep --pattern 'event.x' 

Length of output: 2846


Script:

#!/bin/bash
# Check the complete ripple implementation and event handling
ast-grep --pattern 'function useRipple' -A 30
rg -l "getBoundingClientRect|pageX|pageY" packages/components/ripple/

Length of output: 119


Script:

#!/bin/bash
# Check the complete ripple implementation file content
cat packages/components/ripple/src/use-ripple.ts

# Also check for any test files that might show the expected behavior
fd -e test.ts -e test.tsx -e spec.ts -e spec.tsx ripple packages/components/

Length of output: 1169

@macci001
Copy link
Contributor

macci001 commented Dec 9, 2024

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

@wingkwong wingkwong added this to the v2.6.6 milestone Dec 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants