|
| 1 | +import { useCallback, useRef, CSSProperties, useEffect } from "react"; |
| 2 | +import { DynamicChildPositioner, SliderKnob } from "./components"; |
| 3 | +import { ProportionDetail, SliderKnobOptions } from "./components/types"; |
| 4 | +import { EventType, getClientX, clamp } from "./utilities"; |
| 5 | + |
| 6 | +export type ProportionSliderProps = { |
| 7 | + /** |
| 8 | + * Current values of the two proportions [left, right] |
| 9 | + * These should be positive numbers |
| 10 | + */ |
| 11 | + value: [number, number]; |
| 12 | + |
| 13 | + /** |
| 14 | + * Details for the two proportions [left, right] |
| 15 | + */ |
| 16 | + proportions: [ProportionDetail, ProportionDetail]; |
| 17 | + |
| 18 | + /** |
| 19 | + * Callback when values change |
| 20 | + * @param values The new values [left, right] |
| 21 | + */ |
| 22 | + onChange?: (values: [number, number]) => void; |
| 23 | + |
| 24 | + /** |
| 25 | + * Appearance of the slider knob |
| 26 | + */ |
| 27 | + knobOptions?: SliderKnobOptions; |
| 28 | + |
| 29 | + /** Height of the slider in pixels */ |
| 30 | + height?: number; |
| 31 | + /** Width of the slider in pixels */ |
| 32 | + width?: number; |
| 33 | + /** Whether the slider is disabled */ |
| 34 | + disabled?: boolean; |
| 35 | + /** Custom class name for the slider container */ |
| 36 | + className?: string; |
| 37 | + /** Custom styles for the slider container */ |
| 38 | + style?: CSSProperties; |
| 39 | + |
| 40 | + /** |
| 41 | + * Accessibility options |
| 42 | + */ |
| 43 | + ariaLabel?: string; |
| 44 | +}; |
| 45 | + |
| 46 | +export const ProportionSlider = ({ |
| 47 | + value, |
| 48 | + proportions, |
| 49 | + onChange, |
| 50 | + knobOptions, |
| 51 | + disabled, |
| 52 | + height, |
| 53 | + ariaLabel, |
| 54 | + className, |
| 55 | + style, |
| 56 | + width, |
| 57 | +}: ProportionSliderProps) => { |
| 58 | + const mergedKnobOptions = getMergeKnobOptions(knobOptions); |
| 59 | + |
| 60 | + const ref = useRef<HTMLDivElement | null>(null); |
| 61 | + const total = value[0] + value[1]; |
| 62 | + const sliderWidth = mergedKnobOptions.width + mergedKnobOptions.gap * 2; |
| 63 | + const refTotal = useRef<number>(total); |
| 64 | + const refSliderWidth = useRef<number>(sliderWidth); |
| 65 | + refTotal.current = total; |
| 66 | + refSliderWidth.current = sliderWidth; |
| 67 | + const refIsDragging = useRef<boolean>(false); |
| 68 | + |
| 69 | + const onChangeValueFromEvent = useCallback( |
| 70 | + (e: EventType, { width, left }: { width: number; left: number }) => { |
| 71 | + const x = getClientX(e); |
| 72 | + const knobWidth = refSliderWidth.current; |
| 73 | + const factor = (x - left - knobWidth / 2) / (width - knobWidth); |
| 74 | + const total = refTotal.current; |
| 75 | + const value1 = clamp(total * factor, 0, total); |
| 76 | + onChange?.([value1, total - value1]); |
| 77 | + }, |
| 78 | + [onChange] |
| 79 | + ); |
| 80 | + |
| 81 | + const onDown = useCallback( |
| 82 | + (e: EventType) => { |
| 83 | + const target = e.target as HTMLElement; |
| 84 | + const rect = ref.current?.getBoundingClientRect(); |
| 85 | + if ( |
| 86 | + (ref.current && !ref.current.contains(target)) || |
| 87 | + !rect || |
| 88 | + rect.width === 0 |
| 89 | + ) { |
| 90 | + console.log("returning because of rect width", rect?.width); |
| 91 | + return; |
| 92 | + } |
| 93 | + e.preventDefault(); |
| 94 | + refIsDragging.current = true; |
| 95 | + onChangeValueFromEvent(e, rect); |
| 96 | + return true; |
| 97 | + }, |
| 98 | + [onChangeValueFromEvent] |
| 99 | + ); |
| 100 | + |
| 101 | + const onMove = useCallback( |
| 102 | + (e: EventType) => { |
| 103 | + const rect = ref.current?.getBoundingClientRect(); |
| 104 | + if (!refIsDragging.current || !rect || rect.width === 0) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + e.preventDefault(); |
| 108 | + onChangeValueFromEvent(e, rect); |
| 109 | + return true; |
| 110 | + }, |
| 111 | + [onChangeValueFromEvent] |
| 112 | + ); |
| 113 | + |
| 114 | + const onUp = useCallback((e: EventType) => { |
| 115 | + if (!refIsDragging.current) return false; |
| 116 | + refIsDragging.current = false; |
| 117 | + e.preventDefault(); |
| 118 | + return true; |
| 119 | + }, []); |
| 120 | + |
| 121 | + useEffect(() => { |
| 122 | + window.addEventListener("mousedown", onDown); |
| 123 | + window.addEventListener("mousemove", onMove); |
| 124 | + window.addEventListener("mouseup", onUp); |
| 125 | + window.addEventListener("touchstart", onDown); |
| 126 | + window.addEventListener("touchmove", onMove); |
| 127 | + window.addEventListener("touchend", onUp); |
| 128 | + return () => { |
| 129 | + window.removeEventListener("mousedown", onDown); |
| 130 | + window.removeEventListener("mousemove", onMove); |
| 131 | + window.removeEventListener("mouseup", onUp); |
| 132 | + window.removeEventListener("touchstart", onDown); |
| 133 | + window.removeEventListener("touchmove", onMove); |
| 134 | + window.removeEventListener("touchend", onUp); |
| 135 | + }; |
| 136 | + }, [onDown, onMove, onUp]); |
| 137 | + return ( |
| 138 | + <div |
| 139 | + ref={ref} |
| 140 | + role="slider" |
| 141 | + aria-label={ariaLabel} |
| 142 | + aria-valuenow={Math.round((value[0] / total) * 100)} |
| 143 | + style={{ |
| 144 | + display: "flex", |
| 145 | + flexDirection: "row", |
| 146 | + height, |
| 147 | + width, |
| 148 | + opacity: disabled ? 0.6 : 1, |
| 149 | + cursor: disabled ? "not-allowed" : "pointer", |
| 150 | + ...style, |
| 151 | + }} |
| 152 | + className={className} |
| 153 | + > |
| 154 | + <DynamicChildPositioner |
| 155 | + detail={proportions[0]} |
| 156 | + valueLabel={`${Math.round((value[0] * 100) / total)}%`} |
| 157 | + width={`calc(${(value[0] * 100) / total}% - ${sliderWidth / 2}px)`} |
| 158 | + primaryNode="left" |
| 159 | + /> |
| 160 | + <SliderKnob disabled={disabled} {...mergedKnobOptions} /> |
| 161 | + <DynamicChildPositioner |
| 162 | + detail={proportions[1]} |
| 163 | + valueLabel={`${Math.round((value[1] * 100) / total)}%`} |
| 164 | + width={`calc(${(value[1] * 100) / total}% - ${sliderWidth / 2}px)`} |
| 165 | + primaryNode="right" |
| 166 | + /> |
| 167 | + </div> |
| 168 | + ); |
| 169 | +}; |
| 170 | + |
| 171 | +const DefaultKnobOptions: Required<SliderKnobOptions> = { |
| 172 | + width: 5, |
| 173 | + gap: 2, |
| 174 | + backgroundColor: "red", |
| 175 | + className: "slider-knob", |
| 176 | + style: {}, |
| 177 | +}; |
| 178 | + |
| 179 | +function getMergeKnobOptions( |
| 180 | + knobOptions: SliderKnobOptions | undefined = {} |
| 181 | +): Required<SliderKnobOptions> { |
| 182 | + return { |
| 183 | + width: knobOptions.width ?? DefaultKnobOptions.width, |
| 184 | + gap: knobOptions.gap ?? DefaultKnobOptions.gap, |
| 185 | + backgroundColor: |
| 186 | + knobOptions.backgroundColor ?? DefaultKnobOptions.backgroundColor, |
| 187 | + className: knobOptions.className ?? DefaultKnobOptions.className, |
| 188 | + style: knobOptions.style ?? DefaultKnobOptions.style, |
| 189 | + }; |
| 190 | +} |
0 commit comments