3.3.0 β€’ Published 3 months ago

react-slot-counter v3.3.0

Weekly downloads
-
License
MIT
Repository
github
Last release
3 months ago

React Slot Counter 🎰 - Make Your UI Count! ✨

πŸš€ Elevate Your UI with Dynamic, Eye-Catching Counters - React Slot Counter

Make Your Numbers Pop 🌟: Simple and Dynamic Counters for Your UI

NPM License Size NPM Downloads Deploy to GitHub Pages

πŸš€ What's New in 3.0.0?

Key Changes

  • New Prop: speed: Adjust animation speed more intuitively with the new speed prop.
  • New Prop: delay: Each column's animation start can now be delayed using the delay prop.

πŸ’‘ Why React Slot Counter?

React Slot Counter is your go-to solution for adding animated, interactive counters to your web applications. With customizable animations, diverse input compatibility, and easy integration, it's designed to make your numbers not just visible but visually striking.

🌟 Key Features

  • Versatile Inputs: Beyond numbersβ€”strings, JSX elements, and more.
  • Animation on Demand: Updates animate only the changed elements.
  • Sequential Animation: For that extra touch of order and purpose.
  • Total Customizability: Tailor the look and feel to fit your app perfectly.

πŸ“¦ Quick Installation

npm install react-slot-counter

πŸ›  Easy Usage

Import SlotCounter and use it in your component. Here's a simple example:

import React from 'react';
import SlotCounter from 'react-slot-counter';

function App() {
  return (
    <>
      <SlotCounter value={123456} />
      <SlotCounter value={36.5} />
      <SlotCounter value="1,234,567" />
      <SlotCounter value={['1', '2', '3', '4', '5', '6']} />
      <SlotCounter value="??????" />
    </>
  );
}

export default App;

πŸŽ₯ Live Demo

Explore more at the demo page.

πŸ“ Comprehensive Props

Detailed props for customizing SlotCounter to fit your UI needs:

  • value (required): Display numbers, strings, or JSX elements.
  • duration: Control the speed of the animation.
  • animateUnchanged: Choose to animate all or only changed characters.
  • And many more!
PropTypeDefaultDescription
value (required)number | string | string[] | JSX.Element[]The value to be displayed. It can be a number or a string with numbers and commas.
startValuenumber | string | string[] | JSX.Element[]The initial value to be displayed before the animation starts. It sets the beginning of the slot machine animation.
startValueOncebooleanfalseIf set to true, the animation starts from the startValue only for the first render. For subsequent animations, it starts from the last value.
durationnumber0.7The duration of the animation in seconds.
speednumber1.4The speed of counter when running.
delaynumberThe delay time of each columns
dummyCharactersstring[] | JSX.Element[]Defaults to random numbers from 0 to 9An array of dummy characters to be used in the animation.
dummyCharacterCountnumber6The number of dummy characters to be displayed in the animation before reaching the target character.
autoAnimationStartbooleantrueDetermines whether the animation should start automatically when the component is first mounted.
animateUnchangedbooleanfalseDetermines whether to animate only the characters that have changed.
hasInfiniteListbooleanfalseDetermines whether the list should appear as continuous, with the end of the target character seamlessly connected to the beginning.
containerClassNamestringThe class name of container.
charClassNamestringThe class name of each character.
separatorClassNamestringThe class name of the separator character (. or ,).
valueClassNamestringThe class name for the value of the slot, making it possible to customize the styling and visibility of the value.
numberSlotClassNamestringThe class name for the number slot, allowing you to customize the styling of the number slot.
numberClassNamestringThe class name for the number, allowing you to customize the styling of the number.
sequentialAnimationModebooleanfalseDetermines if the animation should increment or decrement sequentially from the startValue to value instead of random animation.
useMonospaceWidthbooleanfalseEnsures that all numeric characters occupy the same horizontal space, just like they would in a monospace font.
direction'bottom-top' | 'top-bottom''bottom-top'Sets the direction of the slot machine animation. Accepted values are 'bottom-top' and 'top-bottom'.
debounceDelaynumber0Specifies the delay in milliseconds for debouncing animations. When the value changes rapidly, it allows the animation to execute smoothly.
animateOnVisibleboolean | rootMargin: string, triggerOnce: booleanfalse | rootMargin: '0px', triggerOnce: falseActivates the animation when the component is visible in the viewport. rootMargin sets the margin around the viewport for triggering the animation, while triggerOnce determines if the animation should occur only once (true) or every time the component becomes visible (false).
startFromLastDigitbooleanfalseDetermines the order in which the digits animate. When set to true, the animation will start from the last digit and progress backward to the first digit. When false, the animation will start from the first digit and proceed forward.
onAnimationStart() => voidCallback function that is called when the animation starts.
onAnimationEnd() => voidCallback function that is called when the animation completes.
separatorCharactersstring[]undefinedDefines custom separator characters. When provided, only these characters will be treated as separators. For example, separatorCharacters={[':']} will treat colons as separators in "12:34:56".
isSeparatorCharacter((value: string | JSX.Element) => boolean) | nulldefaultIsSeparatorCharacterA function to determine if a value should be treated as a separator. Set to null to disable all separators. When provided along with separatorCharacters, separatorCharacters takes precedence.
slotPeeknumberControls the visibility of adjacent numbers in the slot machine effect. Set the value in pixels to determine how much of the previous and next numbers should be visible. Higher values will show more of the adjacent numbers.

πŸ€– Advanced Ref Usage

Manipulate the behavior with refreshStyles and startAnimation methods.

MethodTypeDescription
refreshStyles() => voidRecalculates the styles for the SlotCounter component. Useful for scenarios where the font size changes or the window is resized, forcing a re-render to apply the new styles.
startAnimation(options?: Options) => voidInitiates the animation of the component with optional customization parameters.
reload() => voidForces a complete re-render of the SlotCounter component. Useful for scenarios where you need to reset the component's state or refresh its appearance after dynamic style changes.

Options for startAnimation Method

PropertyTypeOptionalDefaultDescription
durationnumberYesNoneA number representing the duration of the animation in seconds. Overrides the duration prop if provided.
dummyCharacterCountnumberYesNoneA number indicating how many dummy characters should be shown before the target character. Overrides the dummyCharacterCount prop if provided.
directionstringYes'bottom-top'Sets the direction of the slot machine animation. Accepted values: 'bottom-top', 'top-bottom'. Overrides the direction prop if provided.

Ref Example:

import React, { useRef } from 'react';
import SlotCounter from 'react-slot-counter';

function App() {
  const counterRef = useRef(null);

  const handleStartClick = () => {
    counterRef.current?.startAnimation();
  };

  return (
    <>
      <SlotCounter value={123456} ref={counterRef} />
      <button onClick={handleStartClick}>Start</button>
    </>
  );
}

export default App;

πŸ“œ Stay Updated

Check out our CHANGELOG.md for the latest updates.

πŸ‘¨β€πŸ’» Join the Community

Your contributions are welcome! Let's make this project even better together.

✨ Contributors

Thanks go to these wonderful people:

❀️ Like Our Work?

Support us with a star ⭐ on GitHub!

πŸ“„ License

This project is proudly licensed under the MIT License.

3.0.4

6 months ago

3.0.3

6 months ago

3.2.0

4 months ago

3.0.2

6 months ago

3.0.5

5 months ago

3.3.0

3 months ago

3.1.0

5 months ago

3.0.1

11 months ago

3.0.0

12 months ago

2.3.3

1 year ago

3.0.0-rc.0

1 year ago

2.3.2

1 year ago

2.3.0

1 year ago

2.3.1

1 year ago

2.2.5

1 year ago

2.2.4

1 year ago

2.2.3

1 year ago

2.2.2

1 year ago

2.2.1

2 years ago

2.2.0

2 years ago

1.15.0

2 years ago

2.0.3

2 years ago

2.1.1

2 years ago

2.0.2

2 years ago

2.1.0

2 years ago

2.0.1

2 years ago

2.0.0

2 years ago

1.14.0

2 years ago

1.13.1

2 years ago

1.13.0

2 years ago

1.12.0

2 years ago

1.11.1

2 years ago

1.7.3

2 years ago

1.9.0

2 years ago

1.8.1

2 years ago

1.8.0

2 years ago

1.7.1

2 years ago

1.7.0

2 years ago

1.6.0

2 years ago

1.5.0

2 years ago

1.11.0

2 years ago

1.10.0

2 years ago

1.4.3

2 years ago

1.4.2

2 years ago

1.4.1

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.2.0

2 years ago

1.1.2

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago