-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from gov4git/fix-scroller-bubble
Fix scroller bubble
- Loading branch information
Showing
6 changed files
with
149 additions
and
107 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,5 @@ | ||
--- | ||
'gov4git-desktop-app': patch | ||
--- | ||
|
||
Fix scroller bubble position |
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,39 @@ | ||
import { makeStyles, shorthands } from '@fluentui/react-components' | ||
|
||
import { gov4GitTokens } from '../App/theme/tokens.js' | ||
|
||
export const useBubbleSliderStyles = makeStyles({ | ||
sliderArea: { | ||
position: 'relative', | ||
marginBottom: '36px', | ||
width: '90%', | ||
}, | ||
slider: { | ||
display: 'block', | ||
appearance: 'none', | ||
height: '4px', | ||
width: '100%', | ||
backgroundColor: gov4GitTokens.g4gColorPrimaryGreen2, | ||
...shorthands.outline('none'), | ||
...shorthands.borderRadius(gov4GitTokens.borderRadiusLarge), | ||
'::-webkit-slider-thumb': { | ||
content: 'hello', | ||
appearance: 'none', | ||
backgroundColor: gov4GitTokens.g4gColorSecondaryGreen8, | ||
width: '20px', | ||
height: '16px', | ||
...shorthands.borderRadius(gov4GitTokens.borderRadiusCircular), | ||
...shorthands.border('2px', 'solid', gov4GitTokens.g4gColorNeutral), | ||
}, | ||
}, | ||
bubble: { | ||
fontSize: '0.7rem', | ||
color: gov4GitTokens.g4gColorNeutral, | ||
backgroundColor: gov4GitTokens.g4gColorSecondaryGreen8, | ||
...shorthands.padding('1px', '6px'), | ||
...shorthands.borderRadius(gov4GitTokens.borderRadiusMedium), | ||
position: 'absolute', | ||
top: '0.8rem', | ||
transform: 'translateX(-50%)', | ||
}, | ||
}) |
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,87 @@ | ||
import { | ||
type ChangeEventHandler, | ||
type FC, | ||
useCallback, | ||
useEffect, | ||
useMemo, | ||
useState, | ||
} from 'react' | ||
|
||
import { formatDecimal } from '../lib/numbers.js' | ||
import { useBubbleSliderStyles } from './BubbleSlider.styles.js' | ||
|
||
export type BubbleSliderProps = { | ||
value: number | ||
min?: number | ||
max?: number | ||
step?: string | ||
onChange: (value: number) => void | ||
ariaLabel: string | ||
hideBubble?: boolean | ||
} | ||
|
||
export const BubbleSlider: FC<BubbleSliderProps> = function BubbleSilder({ | ||
value: v = 0, | ||
min = 0, | ||
max = 100, | ||
step = '1', | ||
onChange, | ||
ariaLabel, | ||
hideBubble = false, | ||
}) { | ||
const styles = useBubbleSliderStyles() | ||
const [value, setValue] = useState(v) | ||
const [left, setLeft] = useState('0%') | ||
|
||
useEffect(() => { | ||
setValue(v) | ||
}, [v]) | ||
|
||
const bubbleStyles = useMemo(() => { | ||
return { | ||
left: left, | ||
} | ||
}, [left]) | ||
|
||
const calculateLeft = useCallback( | ||
(value: number) => { | ||
const slideVal = Number(((value - min) * 100) / (max - min)) | ||
const newPosition = 10 - slideVal * 0.2 | ||
return `calc(${slideVal}% + (${newPosition}px))` | ||
}, | ||
[min, max], | ||
) | ||
|
||
useEffect(() => { | ||
const left = calculateLeft(value) | ||
setLeft(left) | ||
}, [calculateLeft, setLeft, value]) | ||
|
||
const onSlide: ChangeEventHandler<HTMLInputElement> = useCallback( | ||
(e) => { | ||
const rangeVal = +e.target.value | ||
onChange(rangeVal) | ||
}, | ||
[onChange], | ||
) | ||
|
||
return ( | ||
<div className={styles.sliderArea}> | ||
<input | ||
className={styles.slider} | ||
type="range" | ||
aria-label={ariaLabel} | ||
step={step} | ||
min={min} | ||
max={max} | ||
value={value} | ||
onChange={onSlide} | ||
/> | ||
{!hideBubble && ( | ||
<div style={bubbleStyles} className={styles.bubble}> | ||
{formatDecimal(value)} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
} |
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