Skip to content

Commit

Permalink
Merge pull request #41 from tuanngocptn/main
Browse files Browse the repository at this point in the history
Add 'speed' of counter and 'delay' for each colums.
  • Loading branch information
almond-bongbong authored Jun 16, 2024
2 parents 22945db + 357eef1 commit 50c55cc
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 8 deletions.
19 changes: 19 additions & 0 deletions .github/workflows/contribute-list.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
name: Contributors readme action

on:
push:
branches:
- main

jobs:
contrib-readme-job:
runs-on: ubuntu-latest
name: Automate contrib in readme
permissions:
contents: write
pull-requests: write
steps:
- name: Contribute List
uses: akhilmhdh/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
43 changes: 43 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ Detailed props for customizing SlotCounter to fit your UI needs:
| startValue | `number` \| `string` \| `string[]` \| `JSX.Element[]` | | The initial value to be displayed before the animation starts. It sets the beginning of the slot machine animation. |
| startValueOnce | `boolean` | `false` | If set to true, the animation starts from the `startValue` only for the first render. For subsequent animations, it starts from the last value. |
| duration | `number` | `0.7` | The duration of the animation in seconds. |
| speed | `number` | `1.4` | The speed of counter when running. |
| delay | `number` | | The delay time of each columns |
| dummyCharacters | `string[]` \| `JSX.Element[]` | Defaults to random numbers from 0 to 9 | An array of dummy characters to be used in the animation. |
| dummyCharacterCount | `number` | `6` | The number of dummy characters to be displayed in the animation before reaching the target character. |
| autoAnimationStart | `boolean` | `true` | Determines whether the animation should start automatically when the component is first mounted. |
Expand Down Expand Up @@ -152,6 +154,47 @@ Check out our [CHANGELOG.md](./CHANGELOG.md) for the latest updates.
Your contributions are welcome! Let's make this project even better together.
## ✨ Contributors
Thanks go to these wonderful people:
<!-- readme: collaborators,contributors -start -->
<table>
<tbody>
<tr>
<td align="center">
<a href="https://github.com/tuanngocptn">
<img src="https://avatars.githubusercontent.com/u/22292704?v=4" width="100;" alt="tuanngocptn"/>
<br />
<sub><b>Nick - Ngoc Pham</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/baronha">
<img src="https://avatars.githubusercontent.com/u/23580920?v=4" width="100;" alt="baronha"/>
<br />
<sub><b>Bảo Hà.</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/almond-bongbong">
<img src="https://avatars.githubusercontent.com/u/42146674?v=4" width="100;" alt="almond-bongbong"/>
<br />
<sub><b>Max</b></sub>
</a>
</td>
<td align="center">
<a href="https://github.com/larsjuvik">
<img src="https://avatars.githubusercontent.com/u/77640590?v=4" width="100;" alt="larsjuvik"/>
<br />
<sub><b>larsjuvik</b></sub>
</a>
</td>
</tr>
<tbody>
</table>
<!-- readme: collaborators,contributors -end -->
## ❤️ Like Our Work?
Support us with a star ⭐ on GitHub!
Expand Down
23 changes: 18 additions & 5 deletions src/components/Slot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ interface Props {
useMonospaceWidth: boolean;
maxNumberWidth?: number;
onFontHeightChange?: (fontHeight: number) => void;
speed: number;
duration: number;
}

function Slot({
Expand All @@ -36,6 +38,8 @@ function Slot({
isChanged,
effectiveDuration,
delay,
duration,
speed,
value,
startValue,
disableStartValue,
Expand Down Expand Up @@ -122,9 +126,18 @@ function Slot({
() => setLocalValue(value),
sequentialAnimationMode
? 0
: (effectiveDuration * 1000 * 1.3) / dummyList.length + delay * 1000,
: (effectiveDuration * speed * duration * 1000) / dummyList.length + delay * 1000,
);
}, [localActive, value, effectiveDuration, delay, dummyList.length, sequentialAnimationMode]);
}, [
localActive,
value,
effectiveDuration,
delay,
dummyList.length,
sequentialAnimationMode,
speed,
duration,
]);

useEffect(() => {
setDummyListState(hasSequentialDummyList ? dummyList : shuffle(dummyList));
Expand Down Expand Up @@ -173,9 +186,9 @@ function Slot({
transform: reverse ? `translateY(-${slotNumbersHeight}px)` : `translateY(0px)`,
...(localActive &&
isChanged && {
transform: reverse ? `translateY(0px)` : `translateY(-${slotNumbersHeight}px)`,
transition: `transform ${effectiveDuration}s ${delay}s ease-in-out`,
}),
transform: reverse ? `translateY(0px)` : `translateY(-${slotNumbersHeight}px)`,
transition: `transform ${effectiveDuration}s ${delay}s ease-in-out`,
}),
}}
>
{didMount ? (
Expand Down
15 changes: 12 additions & 3 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ interface Props {
startValue?: Value;
startValueOnce?: boolean;
duration?: number;
speed?: number;
delay?: number;
dummyCharacters?: string[] | JSX.Element[];
dummyCharacterCount?: number;
autoAnimationStart?: boolean;
Expand All @@ -66,6 +68,8 @@ function SlotCounter(
startValue,
startValueOnce = false,
duration = 0.7,
speed = 1.4,
delay,
dummyCharacters,
dummyCharacterCount = 6,
autoAnimationStart: _autoAnimationStart = true,
Expand Down Expand Up @@ -175,14 +179,14 @@ function SlotCounter(
*/
useEffect(() => {
setDummyList(
range(0, effectiveDummyCharacterCount - 1).map((i) => {
range(0, effectiveDummyCharacterCount * duration * speed - 1).map((i) => {
if (!dummyCharacters) return random(0, 10);

const index = i >= dummyCharacters.length ? random(0, dummyCharacters.length) : i;
return dummyCharacters[index];
}),
);
}, [dummyCharacters, effectiveDummyCharacterCount]);
}, [dummyCharacters, effectiveDummyCharacterCount, speed, duration]);

/**
* Update valueRef and prevValueRef when value is changed
Expand Down Expand Up @@ -232,8 +236,11 @@ function SlotCounter(
*/
const calculatedInterval = useMemo(() => {
const MAX_INTERVAL = 0.1;
if (delay) {
return delay;
}
return Math.min(MAX_INTERVAL, effectiveDuration / valueList.length);
}, [effectiveDuration, valueList.length]);
}, [effectiveDuration, valueList.length, delay]);

/**
* Start animation
Expand Down Expand Up @@ -492,6 +499,8 @@ function SlotCounter(
sequentialAnimationMode={sequentialAnimationMode}
useMonospaceWidth={useMonospaceWidth}
onFontHeightChange={handleFontHeightChange}
speed={speed}
duration={duration}
/>
);
})}
Expand Down

0 comments on commit 50c55cc

Please sign in to comment.