forked from react/react
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathSuspenseScrubber.js
More file actions
86 lines (81 loc) · 2.01 KB
/
Copy pathSuspenseScrubber.js
File metadata and controls
86 lines (81 loc) · 2.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
/**
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
* @flow
*/
import typeof {SyntheticEvent} from 'react-dom-bindings/src/events/SyntheticEvent';
import * as React from 'react';
import {useRef} from 'react';
import styles from './SuspenseScrubber.css';
export default function SuspenseScrubber({
min,
max,
value,
onBlur,
onChange,
onFocus,
onHoverSegment,
onHoverLeave,
}: {
min: number,
max: number,
value: number,
onBlur: () => void,
onChange: (index: number) => void,
onFocus: () => void,
onHoverSegment: (index: number) => void,
onHoverLeave: () => void,
}): React$Node {
const inputRef = useRef();
function handleChange(event: SyntheticEvent) {
const newValue = +event.currentTarget.value;
onChange(newValue);
}
function handlePress(index: number, event: SyntheticEvent) {
event.preventDefault();
if (inputRef.current == null) {
throw new Error(
'The input should always be mounted while we can click things.',
);
}
inputRef.current.focus();
onChange(index);
}
const steps = [];
for (let index = min; index <= max; index++) {
steps.push(
<div
key={index}
className={styles.SuspenseScrubberStep}
onPointerDown={handlePress.bind(null, index)}
onMouseEnter={onHoverSegment.bind(null, index)}>
<div
className={
index <= value
? styles.SuspenseScrubberBeadSelected
: styles.SuspenseScrubberBead
}
/>
</div>,
);
}
return (
<div className={styles.SuspenseScrubber} onMouseLeave={onHoverLeave}>
<input
className={styles.SuspenseScrubberInput}
type="range"
min={min}
max={max}
value={value}
onBlur={onBlur}
onChange={handleChange}
onFocus={onFocus}
ref={inputRef}
/>
{steps}
</div>
);
}