|
| 1 | +import { useCallback, useRef, CSSProperties } from "react"; |
| 2 | +import { Proportion, SliderKnob } from "./components"; |
| 3 | +import { ProportionDetail, SliderKnobOptions } from "./components/types"; |
| 4 | + |
| 5 | +export type ProportionSliderProps = { |
| 6 | + /** |
| 7 | + * Current values of the two proportions [left, right] |
| 8 | + * These should be positive numbers |
| 9 | + */ |
| 10 | + value: [number, number]; |
| 11 | + |
| 12 | + /** |
| 13 | + * Details for the two proportions [left, right] |
| 14 | + */ |
| 15 | + proportions: [ProportionDetail, ProportionDetail]; |
| 16 | + |
| 17 | + /** |
| 18 | + * Callback when values change |
| 19 | + * @param values The new values [left, right] |
| 20 | + */ |
| 21 | + onChange?: (values: [number, number]) => void; |
| 22 | + |
| 23 | + /** |
| 24 | + * Appearance of the slider knob |
| 25 | + */ |
| 26 | + knobOptions?: SliderKnobOptions; |
| 27 | + |
| 28 | + /** Height of the slider in pixels */ |
| 29 | + height?: number; |
| 30 | + /** Whether the slider is disabled */ |
| 31 | + disabled?: boolean; |
| 32 | + /** Custom class name for the slider container */ |
| 33 | + className?: string; |
| 34 | + /** Custom styles for the slider container */ |
| 35 | + style?: CSSProperties; |
| 36 | + |
| 37 | + /** |
| 38 | + * Accessibility options |
| 39 | + */ |
| 40 | + ariaLabel?: string; |
| 41 | +}; |
| 42 | + |
| 43 | +export const ProportionSlider = ({ |
| 44 | + value, |
| 45 | + proportions, |
| 46 | + onChange, |
| 47 | + knobOptions, |
| 48 | + disabled, |
| 49 | + height, |
| 50 | + ariaLabel, |
| 51 | + className, |
| 52 | + style, |
| 53 | +}: ProportionSliderProps) => { |
| 54 | + const mergedKnobOptions = getMergeKnobOptions(knobOptions); |
| 55 | + |
| 56 | + const refWidth = useRef<number | null>(null); |
| 57 | + const total = value[0] + value[1]; |
| 58 | + |
| 59 | + const refStartX = useRef<number | null>(null); |
| 60 | + const refValue1Start = useRef<number | null>(null); |
| 61 | + const refValue2Start = useRef<number | null>(null); |
| 62 | + const sliderWidth = mergedKnobOptions.width + mergedKnobOptions.gap * 2; |
| 63 | + const onDragStart = useCallback( |
| 64 | + (px: number): void => { |
| 65 | + refStartX.current = px; |
| 66 | + refValue1Start.current = value[0]; |
| 67 | + refValue2Start.current = value[1]; |
| 68 | + }, |
| 69 | + [value] |
| 70 | + ); |
| 71 | + const onDragEnd = useCallback(() => { |
| 72 | + refStartX.current = null; |
| 73 | + refValue1Start.current = null; |
| 74 | + refValue2Start.current = null; |
| 75 | + }, []); |
| 76 | + const onDrag = useCallback( |
| 77 | + (px: number): void => { |
| 78 | + if (refStartX.current === null) return; |
| 79 | + const diffPx = px - refStartX.current; |
| 80 | + const totalWidthPx = refWidth.current! - sliderWidth; |
| 81 | + const total = refValue1Start.current! + refValue2Start.current!; |
| 82 | + let newValue1 = refValue1Start.current! + (diffPx / totalWidthPx) * total; |
| 83 | + newValue1 = Math.max(0, Math.min(total, newValue1)); |
| 84 | + onChange?.([newValue1, total - newValue1]); |
| 85 | + }, |
| 86 | + [onChange, sliderWidth] |
| 87 | + ); |
| 88 | + return ( |
| 89 | + <div |
| 90 | + ref={(el) => { |
| 91 | + if (el) { |
| 92 | + refWidth.current = el.getBoundingClientRect().width; |
| 93 | + } |
| 94 | + }} |
| 95 | + role="slider" |
| 96 | + aria-label={ariaLabel} |
| 97 | + aria-valuenow={Math.round((value[0] / total) * 100)} |
| 98 | + style={{ |
| 99 | + display: "flex", |
| 100 | + flexDirection: "row", |
| 101 | + height, |
| 102 | + opacity: disabled ? 0.6 : 1, |
| 103 | + cursor: disabled ? "not-allowed" : "pointer", |
| 104 | + ...style, |
| 105 | + }} |
| 106 | + className={className} |
| 107 | + > |
| 108 | + <Proportion |
| 109 | + detail={proportions[0]} |
| 110 | + valueLabel={`${Math.round((value[0] * 100) / total)}%`} |
| 111 | + width={`calc(${(value[0] * 100) / total}% - ${sliderWidth / 2}px)`} |
| 112 | + anchor="left" |
| 113 | + /> |
| 114 | + <SliderKnob |
| 115 | + onDragStart={disabled ? undefined : onDragStart} |
| 116 | + onDrag={disabled ? undefined : onDrag} |
| 117 | + onDragEnd={disabled ? undefined : onDragEnd} |
| 118 | + {...mergedKnobOptions} |
| 119 | + /> |
| 120 | + <Proportion |
| 121 | + detail={proportions[1]} |
| 122 | + valueLabel={`${Math.round((value[1] * 100) / total)}%`} |
| 123 | + width={`calc(${(value[1] * 100) / total}% - ${sliderWidth / 2}px)`} |
| 124 | + anchor="right" |
| 125 | + /> |
| 126 | + </div> |
| 127 | + ); |
| 128 | +}; |
| 129 | + |
| 130 | +const DefaultKnobOptions: Required<SliderKnobOptions> = { |
| 131 | + width: 5, |
| 132 | + gap: 2, |
| 133 | + backgroundColor: "red", |
| 134 | + className: "slider-knob", |
| 135 | + style: {}, |
| 136 | +}; |
| 137 | + |
| 138 | +function getMergeKnobOptions( |
| 139 | + knobOptions: SliderKnobOptions | undefined = {} |
| 140 | +): Required<SliderKnobOptions> { |
| 141 | + return { |
| 142 | + width: knobOptions.width ?? DefaultKnobOptions.width, |
| 143 | + gap: knobOptions.gap ?? DefaultKnobOptions.gap, |
| 144 | + backgroundColor: |
| 145 | + knobOptions.backgroundColor ?? DefaultKnobOptions.backgroundColor, |
| 146 | + className: knobOptions.className ?? DefaultKnobOptions.className, |
| 147 | + style: knobOptions.style ?? DefaultKnobOptions.style, |
| 148 | + }; |
| 149 | +} |
0 commit comments