Wheel Selector
A wheel selector component to select a number from a range.
Made by lucasNumber Selector
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
Fruit Selector
Apple
Banana
Cherry
Date
Elderberry
Fig
Grape
Try to grab it
"use client";
import WheelSelector from "@/components/targetblank/components/wheel-selector";
import React from "react";
const WheelSelectorDemo = () => {
const numberItems = React.useMemo(() => {
const min = 100;
const max = 200;
return Array.from({ length: max - min + 1 }, (_, i) => i + min);
}, []);
const [numberValue, setNumberValue] = React.useState(numberItems[43]); // 143
React.useEffect(() => {
const interval = setInterval(() => {
const newValue =
numberItems[Math.floor(Math.random() * numberItems.length)];
setNumberValue(newValue);
}, 3000);
return () => clearInterval(interval);
}, [numberItems]);
const fruitItems = React.useMemo(
() => ["Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape"],
[],
);
const [fruitValue, setFruitValue] = React.useState(fruitItems[2]);
return (
<div className="flex flex-col gap-8 justify-center items-center">
<div>
<h3 className="mb-2 font-semibold text-center">Number Selector</h3>
<WheelSelector<number>
items={numberItems}
value={numberValue}
onChange={setNumberValue}
className="w-48"
/>
</div>
<div>
<h3 className="mb-2 font-semibold text-center">Fruit Selector</h3>
<WheelSelector<string>
items={fruitItems}
value={fruitValue}
onChange={setFruitValue}
width={100}
className="w-80"
renderItem={({ item, isSelected }) => (
<div
className={`flex h-full w-full items-center justify-center rounded-lg font-bold transition-all duration-200 ${
isSelected
? "bg-primary text-primary-foreground shadow-md"
: "bg-muted text-muted-foreground hover:bg-muted/80"
}`}
>
{item}
</div>
)}
/>
<p className="text-xs text-center text-muted-foreground mt-2">
Try to grab it
</p>
</div>
</div>
);
};
export default WheelSelectorDemo;
Installation
Install the following dependencies:
Copy and paste the following code into your project:
"use client";
import { cn } from "@/lib/utils";
import { animate, motion, useMotionValue } from "motion/react";
import * as React from "react";
function WheelSelector<T>({
className,
frameClassName,
gap = 14,
height = 30,
items,
numbersContainerClassName,
value,
width = 36,
onChange,
renderItem,
}: {
className?: string;
frameClassName?: string;
gap?: number;
height?: number;
items: T[];
numbersContainerClassName?: string;
value?: T;
width?: number;
onChange?: (value: T) => void;
renderItem?: ({
item,
isSelected,
}: {
item: T;
isSelected: boolean;
}) => React.ReactNode;
}) {
const x = useMotionValue(0);
const containerRef = React.useRef<HTMLDivElement>(null);
const [isGrabbing, setIsGrabbing] = React.useState(false);
const [dragProps, setDragProps] = React.useState({});
const itemWidth = width + gap;
const selectedIndex = React.useMemo(() => {
if (!value) return -1;
return items.indexOf(value);
}, [items, value]);
React.useLayoutEffect(() => {
if (containerRef.current && selectedIndex !== -1) {
const containerCenter = containerRef.current.offsetWidth / 2;
const targetX = containerCenter - selectedIndex * itemWidth - width / 2;
animate(x, targetX, {
type: "spring",
damping: 30,
stiffness: 400,
});
setDragProps({
dragConstraints: {
right: containerCenter - width / 2,
left: containerCenter - (items.length - 1) * itemWidth - width / 2,
},
dragTransition: {
power: 0.1,
timeConstant: 250,
modifyTarget: (target: number) => {
const targetIndex = Math.round(
(containerCenter - target - width / 2) / itemWidth,
);
const clampedIndex = Math.max(
0,
Math.min(targetIndex, items.length - 1),
);
const newValue = items[clampedIndex];
onChange?.(newValue);
const newTarget =
containerCenter - clampedIndex * itemWidth - width / 2;
return newTarget;
},
},
});
}
}, [items, selectedIndex, itemWidth, x, onChange, width, gap]);
return (
<div
ref={containerRef}
className={cn(
"relative h-16 cursor-grab overflow-hidden",
"bg-gradient-to-r from-white/50 via-transparent to-white/50",
"dark:from-black/50 dark:via-transparent dark:to-black/50",
isGrabbing && "cursor-grabbing",
className,
)}
style={{
mask: `linear-gradient(to right, rgba(0, 0, 0, 0) 0%, rgb(0, 0, 1) 50%, rgba(0, 0, 0, 0) 100%)`,
}}
>
<motion.div
drag="x"
style={{
x,
gap,
}}
className={cn(
"flex absolute top-1/2 items-center -translate-y-1/2",
numbersContainerClassName,
)}
{...dragProps}
onDragStart={() => setIsGrabbing(true)}
onDragEnd={() => setIsGrabbing(false)}
>
{items.map((item, index) => {
const isSelected = selectedIndex === index;
return (
<div
key={index}
className="flex-shrink-0"
style={{
width,
height,
}}
>
{renderItem ? (
renderItem({ item, isSelected })
) : (
<span
className="flex justify-center items-center w-full h-full text-foreground rounded-md transition-opacity"
style={{
opacity: isSelected ? 1 : 0.4,
}}
>
{String(item)}
</span>
)}
</div>
);
})}
</motion.div>
<div className="absolute inset-0 pointer-events-none">
<div
className={cn(
"absolute top-1/2 left-1/2 rounded-md border-2 -translate-x-1/2 -translate-y-1/2 size-9",
"border-neutral-300 dark:border-neutral-600",
frameClassName,
)}
style={{
width,
height,
}}
/>
</div>
</div>
);
}
export default WheelSelector;
Update the import paths to match your project setup.
Usage
<WheelSelector items={items} value={value} onChange={onChange} />
Props
Prop | Type | Default |
---|---|---|
className? | string | - |
frameClassName? | string | - |
gap? | number | 14 |
height? | number | 30 |
items | T[] | - |
numbersContainerClassName? | string | - |
value | T | - |
width? | number | 36 |
onChange? | (value: T) => void | - |
renderItem? | ({ item, isSelected }: { item: T; isSelected: boolean }) => React.ReactNode | - |