-
Notifications
You must be signed in to change notification settings - Fork 74
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
75 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
import * as React from "react"; | ||
import "./Slider.tsx"; | ||
import { TimeFrameText } from "../../../components/Texts"; | ||
|
||
interface Props { | ||
min: number; | ||
max: number; | ||
value?: number; | ||
step?: number; | ||
onChange: (e: React.ChangeEvent<HTMLInputElement>) => void; | ||
onPointerDown?: () => void; | ||
onPointerUp?: () => void; | ||
thumbVisible?: boolean; | ||
nowValue?: number; | ||
} | ||
|
||
const THUMB_WIDTH = 14; | ||
|
||
/** | ||
* The browser's native slider is difficult to style and leads | ||
* to very small tap targets on mobile, so instead we render a | ||
* transparent slider and use a custom UI in front of it. | ||
*/ | ||
const Slider: React.FC<Props> = ({ | ||
min, | ||
max, | ||
onChange, | ||
onPointerDown, | ||
onPointerUp, | ||
step, | ||
thumbVisible = true, | ||
value, | ||
nowValue, | ||
}) => { | ||
const nowValuePercent = | ||
nowValue !== undefined ? ((nowValue - min) / max) * 100 : undefined; | ||
return ( | ||
<div className="relative z-10"> | ||
<Slider | ||
min={min} | ||
max={max} | ||
value={value} | ||
step={step} | ||
onChange={onChange} | ||
onPointerDown={onPointerDown} | ||
onPointerUp={onPointerUp} | ||
thumbVisible={thumbVisible} | ||
/> | ||
<div | ||
className={` | ||
relative flex | ||
-translate-x-1/2 select-none flex-col | ||
items-center | ||
${nowValuePercent === undefined ? "invisible" : "visible"} | ||
`} | ||
style={{ | ||
// Positions the marker along the track whilst compensating for the thumb width as the browser natively does. 7 being half the thumb width. | ||
left: `calc(${nowValuePercent}% - ${ | ||
(((nowValuePercent ?? 0) / 100) * 2 - 1) * 7 | ||
}px)`, | ||
}} | ||
> | ||
<div className="-mt-0.5 h-2 w-0.5 rounded-b-full bg-slateus-200"></div> | ||
<TimeFrameText className="mt-0.5 text-slateus-200">now</TimeFrameText> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Slider; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters