This repository has been archived by the owner on Nov 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
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
1 parent
e61ebd4
commit b2e4a6f
Showing
18 changed files
with
571 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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,113 @@ | ||
.container { | ||
padding-top: 30px; | ||
display: flex; | ||
height: 35px; | ||
} | ||
|
||
.slider { | ||
position: relative; | ||
width: 400px; | ||
} | ||
|
||
.slider__track, | ||
.slider__range, | ||
.slider__left-value, | ||
.slider__right-value, | ||
.slider__left-input, | ||
.slider__right-input { | ||
position: absolute; | ||
} | ||
|
||
.slider__track, | ||
.slider__range { | ||
border-radius: 3px; | ||
height: 5px; | ||
} | ||
|
||
.slider__track { | ||
background-color: #ced4da; | ||
width: 100%; | ||
z-index: 1; | ||
} | ||
|
||
.slider__range { | ||
background-color: #9fe5e1; | ||
z-index: 2; | ||
} | ||
|
||
.slider__left-value, | ||
.slider__right-value { | ||
font-size: 12px; | ||
margin-top: 20px; | ||
} | ||
|
||
.slider__left-input, | ||
.slider__right-input { | ||
font-size: 12px; | ||
margin-top: -36px; | ||
} | ||
|
||
|
||
.slider__left-input input, | ||
.slider__right-input input { | ||
width: 80px; | ||
} | ||
|
||
.slider__left-value, .slider__left-input { | ||
left: 6px; | ||
} | ||
|
||
.slider__right-value, .slider__right-input { | ||
right: -4px; | ||
} | ||
|
||
/* Removing the default appearance */ | ||
.thumb, | ||
.thumb::-webkit-slider-thumb { | ||
-webkit-appearance: none; | ||
-webkit-tap-highlight-color: transparent; | ||
} | ||
|
||
.thumb { | ||
pointer-events: none; | ||
position: absolute; | ||
height: 0; | ||
width: 400px; | ||
outline: none; | ||
} | ||
|
||
.thumb--left { | ||
z-index: 3; | ||
} | ||
|
||
.thumb--right { | ||
z-index: 4; | ||
} | ||
|
||
/* For Chrome browsers */ | ||
.thumb::-webkit-slider-thumb { | ||
background-color: #f1f5f7; | ||
border: none; | ||
border-radius: 50%; | ||
box-shadow: 0 0 1px 1px #ced4da; | ||
cursor: pointer; | ||
height: 18px; | ||
width: 18px; | ||
margin-top: 4px; | ||
pointer-events: all; | ||
position: relative; | ||
} | ||
|
||
/* For Firefox browsers */ | ||
.thumb::-moz-range-thumb { | ||
background-color: #f1f5f7; | ||
border: none; | ||
border-radius: 50%; | ||
box-shadow: 0 0 1px 1px #ced4da; | ||
cursor: pointer; | ||
height: 18px; | ||
width: 18px; | ||
margin-top: 4px; | ||
pointer-events: all; | ||
position: relative; | ||
} |
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,117 @@ | ||
import React, { useCallback, useEffect, useState, useRef } from "react"; | ||
import PropTypes from "prop-types"; | ||
import "./MultiSlider.css"; | ||
|
||
export interface SliderProps { | ||
min: number, | ||
max: number, | ||
onChange: (min: number, max: number) => void, | ||
labelFn?: (value: number) => string, | ||
} | ||
|
||
const MultiRangeSlider = ({ min, max, onChange, labelFn }: SliderProps) => { | ||
if (!labelFn) { | ||
labelFn = (_val) => ''; | ||
} | ||
const [minVal, setMinVal] = useState<number>(min); | ||
const [maxVal, setMaxVal] = useState<number>(max); | ||
const minValRef = useRef<number>(min); | ||
const maxValRef = useRef<number>(max); | ||
const range = useRef<HTMLInputElement | null>(null); | ||
|
||
// Convert to percentage | ||
const getPercent = useCallback( | ||
(value) => Math.round(((value - min) / (max - min)) * 100), | ||
[min, max] | ||
); | ||
|
||
// Set width of the range to decrease from the left side | ||
useEffect(() => { | ||
const minPercent = getPercent(minVal); | ||
const maxPercent = getPercent(maxValRef.current); | ||
|
||
if (range.current) { | ||
|
||
range.current.style.left = `${minPercent}%`; | ||
range.current.style.width = `${maxPercent - minPercent}%`; | ||
} | ||
}, [minVal, getPercent]); | ||
|
||
// Set width of the range to decrease from the right side | ||
useEffect(() => { | ||
const minPercent = getPercent(minValRef.current); | ||
const maxPercent = getPercent(maxVal); | ||
|
||
if (range.current) { | ||
range.current.style.width = `${maxPercent - minPercent}%`; | ||
} | ||
}, [maxVal, getPercent]); | ||
|
||
// Get min and max values when their state changes | ||
useEffect(() => { | ||
onChange(minVal, maxVal); | ||
}, [minVal, maxVal, onChange]); | ||
|
||
return ( | ||
<span className="container"> | ||
<input | ||
type="range" | ||
min={min} | ||
max={max} | ||
value={minVal} | ||
onChange={(event) => { | ||
const value = Math.min(Number(event.target.value), maxVal - 1); | ||
setMinVal(value); | ||
minValRef.current = value; | ||
}} | ||
className="thumb thumb--left" | ||
style={{ zIndex: (minVal > max - 100) ? "5" : undefined }} | ||
/> | ||
<input | ||
type="range" | ||
min={min} | ||
max={max} | ||
value={maxVal} | ||
onChange={(event) => { | ||
const value = Math.max(Number(event.target.value), minVal + 1); | ||
setMaxVal(value); | ||
maxValRef.current = value; | ||
}} | ||
className="thumb thumb--right" | ||
/> | ||
|
||
<span className="slider"> | ||
<span className="slider__left-input"> | ||
<input type="number" value={minVal} onChange={(event) => { | ||
const value = Math.max(Math.min(Number(event.target.value), maxVal - 1), min); | ||
setMinVal(value); | ||
minValRef.current = value; | ||
}}/> | ||
</span> | ||
<span className="slider__right-input"> | ||
<input type="number" value={maxVal} onChange={(event) => { | ||
const value = Math.min(Math.max(Number(event.target.value), minVal + 1), max); | ||
setMaxVal(value); | ||
maxValRef.current = value; | ||
}}/> | ||
</span> | ||
<span className="slider__track" /> | ||
<span ref={range} className="slider__range" /> | ||
<span className="slider__left-value"> | ||
{labelFn(minVal)} | ||
</span> | ||
<span className="slider__right-value"> | ||
{labelFn(maxVal)} | ||
</span> | ||
</span> | ||
</span> | ||
); | ||
}; | ||
|
||
MultiRangeSlider.propTypes = { | ||
min: PropTypes.number.isRequired, | ||
max: PropTypes.number.isRequired, | ||
onChange: PropTypes.func.isRequired | ||
}; | ||
|
||
export default MultiRangeSlider; |
Oops, something went wrong.