-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
refactor: button & card ripple #4284
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| --- | ||
| "@nextui-org/button": patch | ||
| "@nextui-org/ripple": patch | ||
| "@nextui-org/card": patch | ||
| --- | ||
|
|
||
| Refactor Button & Card Ripple |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<RippleType[]>([]); | ||
|
|
||
| const onClick = useCallback((event: React.MouseEvent<HTMLElement, 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, | ||
|
Comment on lines
+27
to
+28
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Codebase verification Scroll position needs to be accounted for in ripple coordinates The current implementation uses
🔗 Analysis chainVerify scroll position handling in coordinate calculation The direct use of - 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 executedThe 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 |
||
| }, | ||
| ]); | ||
| }, []); | ||
|
|
@@ -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<typeof useRipple>; | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider using event.currentTarget instead of event.target
Using
event.targetmight lead to incorrect ripple positioning when clicking on child elements within the button/card. Thetargetcould be a nested element, whilecurrentTargetalways refers to the element the event handler is attached to.📝 Committable suggestion