-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Standard slider replaced with a custom
- Loading branch information
1 parent
946cb5a
commit 6aeee40
Showing
6 changed files
with
115 additions
and
22 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
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
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,80 @@ | ||
import Style from '@/app/styles/slider.module.css'; | ||
import { useEffect, useState } from 'react'; | ||
|
||
|
||
const Slider = ({ initial, range, onChange }: { initial: number, range: number, onChange(val: number): void }) => { | ||
const [position, setPosition] = useState<number>(0); | ||
const [lastVal, setLastVal] = useState<number>(0); | ||
const [isDragging, setIsDragging] = useState<boolean>(false); | ||
|
||
const calcPosition = (range: number, y: number, ival?: number) => { | ||
const rect = document.getElementById('track').getBoundingClientRect(); | ||
const realRange = (rect.height - 16) / range; | ||
const value = ival ?? Math.min(Math.max(Math.floor((y - rect.top) / realRange), 0), range); | ||
const val = (value * realRange) / rect.height * 100; | ||
return { position: Math.min(val, ((rect.height - 16) / rect.height) * 100), value: value } | ||
} | ||
|
||
const mouseDown = (event: any) => { | ||
setIsDragging(true); | ||
document.body.style.userSelect = 'none'; | ||
const clientY = 'touches' in event ? event.touches[0].clientY : event.clientY; | ||
const position = calcPosition(range, clientY); | ||
setPosition(position.position); | ||
if (position.value !== lastVal) { | ||
onChange(position.value); | ||
setLastVal(position.value); | ||
} | ||
} | ||
|
||
const mouseMove = (event: any) => { | ||
const clientY = 'touches' in event ? event.touches[0].clientY : event.clientY; | ||
const position = calcPosition(range, clientY); | ||
setPosition(position.position); | ||
if (position.value !== lastVal) { | ||
onChange(position.value); | ||
setLastVal(position.value); | ||
} | ||
} | ||
|
||
const mouseUp = () => { | ||
setIsDragging(false); | ||
document.body.style.userSelect = 'auto'; | ||
} | ||
|
||
useEffect(() => { | ||
setPosition(calcPosition(range, 0, initial).position); | ||
}, []) | ||
|
||
return ( | ||
<> | ||
{isDragging && | ||
<div | ||
style={{ position: 'fixed', inset: 0, touchAction: 'none', cursor: 'pointer' }} | ||
onMouseMove={mouseMove} | ||
onMouseUp={mouseUp} | ||
onTouchMove={mouseMove} | ||
onTouchEnd={mouseUp} | ||
/> | ||
} | ||
<div | ||
className={Style.track} | ||
id='track' | ||
onMouseUp={mouseUp} | ||
onTouchEnd={mouseUp} | ||
onTouchMove={mouseMove} | ||
onMouseDown={mouseDown} | ||
> | ||
<div | ||
className={Style.thumb} | ||
style={{ top: `${position}%`, touchAction: 'none' }} | ||
onTouchMove={mouseMove} | ||
onMouseDown={mouseDown} | ||
onTouchStart={mouseDown} | ||
/> | ||
</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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
.track { | ||
width: 0.5rem; | ||
border-radius: 5px; | ||
border: 1px var(--main-element-color) solid; | ||
box-sizing: border-box; | ||
display: flex; | ||
flex-direction: column; | ||
align-items: center; | ||
margin-left: .5rem; | ||
margin-right: .5rem; | ||
} | ||
|
||
.thumb { | ||
background-image: url('../workshop/[id]/pepe.png'); | ||
background-size: contain; | ||
background-position: center center; | ||
background-repeat: no-repeat; | ||
width: 24px; | ||
height: 16px; | ||
position: relative; | ||
border-radius: 3px; | ||
cursor: pointer; | ||
transition: top 150ms; | ||
} |
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
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