Skip to content

Commit

Permalink
Merge pull request #4 from camiha/develop
Browse files Browse the repository at this point in the history
fix: Determine the result outside of the useEffect lifecycle
  • Loading branch information
camiha authored Dec 3, 2023
2 parents 8368d24 + 0da8380 commit c8be684
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,6 @@ dist
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.*

# OSX
.DS_Store
11 changes: 7 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,10 +64,13 @@ If you want to set styling globally, please refer to the StyleOption section. (I

### `useRoulette` Hook
#### Props
| Property | Type | Description |
|----------|---------------------|------------------------------------------------------|
| `items` | `RouletteItem[]` | Array of items to display on the roulette wheel. |
| `options`| `RouletteOptions` | Optional settings to customize the roulette wheel. |
| Property | Type | Description |
|-------------|-----------------------------|---------------------------------------------------------|
| `items` | `RouletteItem[]` | Array of items to display on the roulette wheel. |
| `onSpinUp` | `() => void` | Optional callback executed when the roulette starts spinning. |
| `onSpinDown`| `() => void` | Optional callback executed when the roulette starts slowing down. |
| `onComplete`| `(result: string) => void` | Optional callback executed when the roulette stops, returning the result. |
| `options` | `Partial<RouletteOptions>` | Optional settings to customize the roulette wheel. |

#### Return Values
| Property | Type | Description |
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-hook-roulette",
"version": "0.0.2",
"version": "0.0.3",
"description": "Minimal roulette wheel component. built with React Hooks.",
"homepage": "https://github.com/camiha/React-Hook-Roulette",
"publishConfig": {
Expand Down Expand Up @@ -35,6 +35,7 @@
"license": "MIT",
"devDependencies": {
"@biomejs/biome": "^1.4.1",
"typescript": "^5.3.2",
"unbuild": "^2.0.0"
},
"peerDependencies": {
Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 14 additions & 1 deletion src/hooks/use-roulette.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,15 @@ import { RouletteCanvas, RouletteItem } from "../types";

export const useRoulette = ({
items,
onSpinUp,
onSpinDown,
onComplete,
options = {},
}: {
items: RouletteItem[];
onSpinUp?: () => void;
onSpinDown?: () => void;
onComplete?: (result: string) => void;
options?: Partial<RouletteOptions>;
}): {
roulette: RouletteCanvas;
Expand Down Expand Up @@ -69,7 +75,7 @@ export const useRoulette = ({
const onStart = useCallback(() => {
if (status !== "stop") return;
setStatus("running");
setResult("");

}, [status]);

const onStop = useCallback(() => {
Expand All @@ -93,6 +99,12 @@ export const useRoulette = ({
useEffect(() => {
if (status !== "running" && status !== "ending") return;

if (status === "running") {
onSpinUp?.();
}
if (status === "ending") {
onSpinDown?.();
}
const { context } = setupCanvas(canvasRef.current);
const cancelAnimation = animateRoulette({
rouletteItemList: items,
Expand All @@ -104,6 +116,7 @@ export const useRoulette = ({
onFinish: (rouletteResult: string) => {
setStatus("stop");
setResult(rouletteResult);
onComplete?.(rouletteResult);
},
});
return () => {
Expand Down

0 comments on commit c8be684

Please sign in to comment.