Open History to see the last commit
1import React from 'react'
2import { createRoot } from 'react-dom/client'
3import { useRanger, Ranger } from '@tanstack/react-ranger'
4β
5function App() {
6 const rangerRef = React.useRef<HTMLDivElement>(null)
7 const [values, setValues] = React.useState<ReadonlyArray<number>>([
8 10, 15, 50,
9 ])
10β
11 const rangerInstance = useRanger<HTMLDivElement>({
12 getRangerElement: () => rangerRef.current,
13 values,
14 min: 0,
15 max: 100,
16 stepSize: 5,
17 onChange: (instance: Ranger<HTMLDivElement>) =>
18 setValues(instance.sortedValues),
19 })
20β
21 return (
22 <div className="App" style={{ padding: 10 }}>
23 <h1>Basic Range</h1>
24 <span>Active Index: {rangerInstance.activeHandleIndex}</span>
25 <br />
26 <br />
27 <div
28 ref={rangerRef}
29 style={{
30 position: 'relative',
31 userSelect: 'none',
32 height: '4px',
33 background: '#ddd',
34 boxShadow: 'inset 0 1px 2px rgba(0,0,0,.6)',
35 borderRadius: '2px',
36 }}
37 >
38 {rangerInstance
39 .handles()
40 .map(
41 (
42 {
43 value,
44 onKeyDownHandler,
45 onMouseDownHandler,
46 onTouchStart,
47 isActive,
48 },
49 i,
50 ) => (
51 <button
52 key={i}
53 onKeyDown={onKeyDownHandler}
54 onMouseDown={onMouseDownHandler}
55 onTouchStart={onTouchStart}
56 role="slider"
57 aria-valuemin={rangerInstance.options.min}
58 aria-valuemax={rangerInstance.options.max}
59 aria-valuenow={value}
60 style={{
61 position: 'absolute',
62 top: '50%',
63 left: `${rangerInstance.getPercentageForValue(value)}%`,
64 zIndex: isActive ? '1' : '0',
65 transform: 'translate(-50%, -50%)',
66 width: '14px',
67 height: '14px',
68 outline: 'none',
69 borderRadius: '100%',
70 background: 'linear-gradient(to bottom, #eee 45%, #ddd 55%)',
71 border: 'solid 1px #888',
72 }}
73 />
74 ),
75 )}
76 </div>
77 <br />
78 <br />
79 <br />
80 <pre
81 style={{
82 display: 'inline-block',
83 textAlign: 'left',
84 }}
85 >
86 <code>
87 {JSON.stringify({
88 values,
89 })}
90 </code>
91 </pre>
92 </div>
93 )
94}
95β
96const root = createRoot(document.getElementById('root')!)
97β
98root.render(
99 <React.StrictMode>
100 <App />
101 </React.StrictMode>,
102)