Created
April 14, 2023 15:06
-
-
Save barrybtw/aa03e690b3dc102413a77700776050a1 to your computer and use it in GitHub Desktop.
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
@import '@radix-ui/colors/blackA.css'; | |
@import '@radix-ui/colors/violet.css'; | |
.SliderRoot { | |
position: relative; | |
display: flex; | |
align-items: center; | |
user-select: none; | |
touch-action: none; | |
width: 200px; | |
height: 20px; | |
} | |
.SliderTrack { | |
background-color: var(--blackA10); | |
position: relative; | |
flex-grow: 1; | |
border-radius: 9999px; | |
height: 3px; | |
} | |
.SliderRange { | |
position: absolute; | |
background-color: white; | |
border-radius: 9999px; | |
height: 100%; | |
} | |
.SliderThumb { | |
display: block; | |
width: 20px; | |
height: 20px; | |
background-color: white; | |
box-shadow: 0 2px 10px var(--blackA7); | |
border-radius: 10px; | |
} | |
.SliderThumb:hover { | |
background-color: var(--violet3); | |
} | |
.SliderThumb:focus { | |
outline: none; | |
box-shadow: 0 0 0 5px var(--blackA8); | |
} |
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
import './App.css'; | |
import * as Slider from '@radix-ui/react-slider'; | |
import { useState } from 'react'; | |
function App() { | |
const currentMilitaryTime = new Date().getHours() * 100; | |
const [time, setTime] = useState([currentMilitaryTime]); | |
function fromMilitaryTime(militaryTime: number) { | |
const atLeastFourLong = militaryTime.toString().padStart(4, '0'); | |
const hours = atLeastFourLong.slice(0, 2); | |
const minutes = Math.floor( | |
(parseInt(atLeastFourLong.slice(2, 4)) / 100) * 60, | |
); | |
return `${hours}:${minutes.toString().padEnd(2, '0')}`; | |
} | |
return ( | |
<div className='App'> | |
<Slider.Root | |
className='SliderRoot' | |
defaultValue={[currentMilitaryTime]} | |
max={2400} | |
min={100} | |
step={50} | |
aria-label='Time' | |
value={time} | |
onValueChange={setTime} | |
> | |
<Slider.Track className='SliderTrack'> | |
<Slider.Range className='SliderRange' /> | |
</Slider.Track> | |
<Slider.Thumb className='SliderThumb' /> | |
</Slider.Root> | |
{fromMilitaryTime(time[0])} | |
</div> | |
); | |
} | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment