|
1 | | -import React from 'react'; |
| 1 | +import React, {useState, useEffect} from 'react'; |
2 | 2 | import styled from 'styled-components'; |
| 3 | +import {useAppSelector} from 'src/store/hooks'; |
| 4 | +import {getShowSliderValues} from 'src/store/settingsSlice'; |
3 | 5 |
|
4 | | -const Container = styled.span` |
5 | | - display: inline-block; |
| 6 | +const Container = styled.span<{$showValue?: boolean}>` |
| 7 | + display: inline-flex; |
| 8 | + align-items: center; |
6 | 9 | line-height: initial; |
7 | | - width: 200px; |
| 10 | + gap: ${(props) => (props.$showValue ? '10px' : '0')}; |
| 11 | + width: ${(props) => (props.$showValue ? 'auto' : '200px')}; |
8 | 12 | `; |
9 | 13 |
|
10 | 14 | const SliderInput = styled.input.attrs({type: 'range'})<any>` |
11 | 15 | accent-color: var(--color_accent); |
12 | | - width: 100%; |
| 16 | + width: ${(props) => (props.$showValue ? '200px' : '100%')}; |
| 17 | + flex: none; |
| 18 | +`; |
| 19 | + |
| 20 | +const ValueDisplay = styled.span` |
| 21 | + text-align: right; |
| 22 | + font-size: 20px; |
| 23 | + color: var(--color_label_highlighted); |
| 24 | + white-space: nowrap; |
| 25 | + min-width: 40px; |
13 | 26 | `; |
14 | 27 |
|
15 | 28 | export const AccentRange: React.FC< |
16 | 29 | Omit<React.InputHTMLAttributes<HTMLInputElement>, 'onChange'> & { |
17 | 30 | onChange: (x: number) => void; |
18 | 31 | } |
19 | | -> = (props) => ( |
20 | | - <Container> |
21 | | - <SliderInput |
22 | | - {...props} |
23 | | - onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
24 | | - props.onChange && props.onChange(+e.target.value); |
25 | | - }} |
26 | | - /> |
27 | | - </Container> |
28 | | -); |
| 32 | +> = (props) => { |
| 33 | + // Get the dynamic value from Redux store |
| 34 | + const showValue = useAppSelector(getShowSliderValues); |
| 35 | + |
| 36 | + const [currentValue, setCurrentValue] = useState<number>( |
| 37 | + Number(props.defaultValue || props.value || props.min || 0), |
| 38 | + ); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + const newValue = Number( |
| 42 | + props.defaultValue || props.value || props.min || 0, |
| 43 | + ); |
| 44 | + setCurrentValue(newValue); |
| 45 | + }, [props.defaultValue, props.value, props.min]); |
| 46 | + |
| 47 | + return ( |
| 48 | + <Container $showValue={showValue}> |
| 49 | + {showValue && <ValueDisplay>{currentValue}</ValueDisplay>} |
| 50 | + <SliderInput |
| 51 | + {...props} |
| 52 | + $showValue={showValue} |
| 53 | + onChange={(e: React.ChangeEvent<HTMLInputElement>) => { |
| 54 | + const newValue = +e.target.value; |
| 55 | + setCurrentValue(newValue); |
| 56 | + props.onChange && props.onChange(newValue); |
| 57 | + }} |
| 58 | + /> |
| 59 | + </Container> |
| 60 | + ); |
| 61 | +}; |
0 commit comments