0.0.10 • Published 5 months ago

react-hook-roulette v0.0.10

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

React Hook Roulette

Minimal roulette wheel component. built with React Hooks.

Features

  • Easy Setup: Seamlessly integrates into React apps using React Hooks, simplifying embedding and state management.
  • Canvas-Based Rendering: Uses HTML Canvas for high-performance rendering.
  • Simple API: easy-to-use API for customization.

Demo

live demo available on stackblitz

demo

Setup

npm

npm install react-hook-roulette

pnpm

pnpm add react-hook-roulette

yarn

yarn add react-hook-roulette

Code Example

import { Roulette, useRoulette } from 'react-hook-roulette';

const App = () => {
	const items = [
		{ name: "label1" },
		{ name: "label2" },
		{ name: "label3" },
		{ name: "label4" },
		{ name: "label5" },
		{ name: "label6" },
	];
	const { roulette, onStart, onStop, result } = useRoulette({ items });

	return (
		<div>
			<Roulette roulette={roulette} />
			<button type="button" onClick={onStart}>Start</button>
			<button type="button" onClick={onStop}>Stop</button>
			{result && <p>Result: {result}</p>}
		</div>
	);
};

API References

RouletteItem

PropertyTypeDescription
namestringLabel for the roulette segment.
bgstring?Background color of the roulette segment.
colorstring?Text color for the segment label.
weightnumber?Determines the segment's size relative to others.

If you want to set styling globally, please refer to the StyleOption section. (If both are specified, the one specified in rouletteItem takes precedence.)

useRoulette Hook

Props

PropertyTypeDescription
itemsRouletteItem[]Array of items to display on the roulette wheel.
onSpinUp() => voidOptional callback executed when the roulette starts spinning.
onSpinDown() => voidOptional callback executed when the roulette starts slowing down.
onSpinEnd(result: string) => voidOptional callback executed when the roulette stops, returning the result.
optionsPartial<RouletteOptions>Optional settings to customize the roulette wheel.

Return Values

PropertyTypeDescription
rouletteRouletteCanvasContains the size of the roulette and a ref to the canvas element.
resultstringThe result after the wheel stops spinning.
onStart() => voidFunction to start the wheel spinning.
onStop() => voidFunction to stop the wheel spinning.

Options

PropertyTypeDefault ValueDescription
sizenumber400Diameter of the roulette wheel.
initialAnglenumber0Starting angle of the wheel.
rotationDirectionRotationDirection"clockwise"Rotation direction.
maxSpeednumber100Maximum rotation speed.
accelerationnumber1Acceleration rate until reaching max speed.
decelerationnumber1Deceleration rate after stopping.
determineAnglenumber45Angle for determining the selected item.
showArrowbooleantrueControls visibility of the selection arrow.
styleStyleOptionCustomize roulette stylings.

StyleOption

CanvasStyle
PropertyTypeDescription
bgstringBackground color of the canvas.
PieStyle
PropertyTypeDescription
borderbooleanif set true, set border for each pie segment
borderColorstring
borderWidthnumber
themePieTheme[]Array of theme options for the pie segments.
LabelStyle
PropertyTypeDescription
fontstringFont style and size for the label text.
defaultColorstringDefault text color for the label.
alignCanvasTextAlignHorizontal alignment of the label text.
baselineCanvasTextBaselineVertical alignment of the label text.
offsetnumberPosition offset of the label from the center.
ArrowStyle
PropertyTypeDescription
bgstringBackground color of the arrow.
sizenumberSize of the arrow.

Default config

const option = {
	size: 400,
	maxSpeed: 100,
	rotationDirection: "clockwise",
	acceleration: 1,
	deceleration: 1,
	initialAngle: 0,
	determineAngle: 45,
	showArrow: true,
	style: {
		canvas: {
			bg: "#fff",
		},
		arrow: {
			bg: "#000",
			size: 16,
		},
		label: {
			font: "16px Arial",
			align: "right",
			baseline: "middle",
			offset: 0.75,
			defaultColor: "#000",
		},
		pie: {
			border: false,
			borderColor: '#000',
			borderWidth: 2,
			theme: [
				{
					bg: "#e0e7ff",
				},
				{
					bg: "#a5b4fc",
				},
				{
					bg: "#6366f1",
					color: "#fff",
				},
				{
					bg: "#4338ca",
					color: "#fff",
				},
			],
		},
	},
}

License

MIT

0.0.10

5 months ago

0.0.9

5 months ago

0.0.8

5 months ago

0.0.7

5 months ago

0.0.6

5 months ago

0.0.5

5 months ago

0.0.4

5 months ago

0.0.3

5 months ago

0.0.2

5 months ago

0.0.1

5 months ago