-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[WEB-4860] dev: propel animated counter component #7740
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f201c63
dev: animated counter added to propel
anmolsinghbhatia 26c1ab6
chore: animated counter story added
anmolsinghbhatia 60b9a0c
chore: propel config updated
anmolsinghbhatia e38e764
chore: code refactor
anmolsinghbhatia 8d0fa9d
chore: code refactor
anmolsinghbhatia e449009
fix: format error
anmolsinghbhatia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
packages/propel/src/animated-counter/animated-counter.stories.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { useState } from "react"; | ||
| import type { Meta, StoryObj } from "@storybook/react-vite"; | ||
| import { AnimatedCounter } from "./animated-counter"; | ||
|
|
||
| const meta: Meta<typeof AnimatedCounter> = { | ||
| title: "AnimatedCounter", | ||
| component: AnimatedCounter, | ||
| parameters: { | ||
| layout: "centered", | ||
| }, | ||
| tags: ["autodocs"], | ||
| argTypes: { | ||
| size: { | ||
| control: { type: "select" }, | ||
| options: ["sm", "md", "lg"], | ||
| }, | ||
| }, | ||
| }; | ||
|
|
||
| export default meta; | ||
| type Story = StoryObj<typeof AnimatedCounter>; | ||
|
|
||
| const AnimatedCounterDemo = (args: React.ComponentProps<typeof AnimatedCounter>) => { | ||
| const [count, setCount] = useState(args.count || 0); | ||
|
|
||
| return ( | ||
| <div className="space-y-6 p-4"> | ||
| <div className="flex items-center justify-center gap-6"> | ||
| <button | ||
| className="px-4 py-2 bg-red-500 text-white font-medium rounded-lg hover:bg-red-600 focus:outline-none focus:ring-2 focus:ring-red-400 focus:ring-offset-2 transition-colors shadow-md" | ||
| onClick={() => setCount((prev) => Math.max(0, prev - 1))} | ||
| > | ||
| -1 | ||
| </button> | ||
| <div className="flex items-center justify-center min-w-[60px] h-12 bg-gray-50 border border-gray-200 rounded-lg"> | ||
| <AnimatedCounter {...args} count={count} /> | ||
| </div> | ||
| <button | ||
| className="px-4 py-2 bg-green-500 text-white font-medium rounded-lg hover:bg-green-600 focus:outline-none focus:ring-2 focus:ring-green-400 focus:ring-offset-2 transition-colors shadow-md" | ||
| onClick={() => setCount((prev) => prev + 1)} | ||
| > | ||
| +1 | ||
| </button> | ||
| </div> | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export const Default: Story = { | ||
| render: (args) => <AnimatedCounterDemo {...args} />, | ||
| args: { | ||
| count: 5, | ||
| size: "md", | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,95 @@ | ||
| import { useState, useEffect } from "react"; | ||
| import { cn } from "../utils"; | ||
|
|
||
| export interface AnimatedCounterProps { | ||
| count: number; | ||
| className?: string; | ||
| size?: "sm" | "md" | "lg"; | ||
| } | ||
|
|
||
| const sizeClasses = { | ||
| sm: "text-xs h-4 w-4", | ||
| md: "text-sm h-5 w-5", | ||
| lg: "text-base h-6 w-6", | ||
| }; | ||
|
|
||
| export const AnimatedCounter: React.FC<AnimatedCounterProps> = ({ count, className, size = "md" }) => { | ||
| // states | ||
| const [displayCount, setDisplayCount] = useState(count); | ||
| const [prevCount, setPrevCount] = useState(count); | ||
| const [isAnimating, setIsAnimating] = useState(false); | ||
| const [direction, setDirection] = useState<"up" | "down" | null>(null); | ||
| const [animationKey, setAnimationKey] = useState(0); | ||
|
|
||
| useEffect(() => { | ||
| if (count !== prevCount) { | ||
| setDirection(count > prevCount ? "up" : "down"); | ||
| setIsAnimating(true); | ||
| setAnimationKey((prev) => prev + 1); | ||
|
|
||
| // Update the display count immediately, animation will show the transition | ||
| setDisplayCount(count); | ||
|
|
||
| // End animation after CSS transition | ||
| const timer = setTimeout(() => { | ||
| setIsAnimating(false); | ||
| setDirection(null); | ||
| setPrevCount(count); | ||
| }, 250); | ||
|
|
||
| return () => clearTimeout(timer); | ||
| } | ||
| }, [count, prevCount]); | ||
|
|
||
| const sizeClass = sizeClasses[size]; | ||
|
|
||
| return ( | ||
| <div className={cn("relative inline-flex items-center justify-center overflow-hidden", sizeClass)}> | ||
| {/* Previous number sliding out */} | ||
| {isAnimating && ( | ||
| <span | ||
| key={`prev-${animationKey}`} | ||
| className={cn( | ||
| "absolute inset-0 flex items-center justify-center font-medium", | ||
| "animate-[slideOut_0.25s_ease-out_forwards]", | ||
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| direction === "up" && "[--slide-out-dir:-100%]", | ||
| direction === "down" && "[--slide-out-dir:100%]", | ||
| sizeClass | ||
| )} | ||
| style={{ | ||
| animation: | ||
| direction === "up" | ||
| ? "slideOut 0.25s ease-out forwards, fadeOut 0.25s ease-out forwards" | ||
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| : "slideOutDown 0.25s ease-out forwards, fadeOut 0.25s ease-out forwards", | ||
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }} | ||
| > | ||
| {prevCount} | ||
| </span> | ||
| )} | ||
|
|
||
| {/* New number sliding in */} | ||
| <span | ||
| key={`current-${animationKey}`} | ||
| className={cn( | ||
| "flex items-center justify-center font-medium", | ||
| isAnimating && "animate-[slideIn_0.25s_ease-out_forwards]", | ||
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| !isAnimating && "opacity-100", | ||
| sizeClass, | ||
| className | ||
| )} | ||
| style={ | ||
| isAnimating | ||
| ? { | ||
| animation: | ||
| direction === "up" | ||
| ? "slideInFromBottom 0.25s ease-out forwards" | ||
| : "slideInFromTop 0.25s ease-out forwards", | ||
anmolsinghbhatia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
| : undefined | ||
| } | ||
| > | ||
| {displayCount} | ||
| </span> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| export { AnimatedCounter } from "./animated-counter"; | ||
| export type { AnimatedCounterProps } from "./animated-counter"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.