Skip to content
This repository was archived by the owner on Mar 25, 2025. It is now read-only.

Commit d712d27

Browse files
Brian Ochanboilund
authored andcommitted
feat(slider): slider with indicator props
- we would like to build a custom slider with an indicator popup above the knob showing what the value of the slider is when pressed and when the knob is being moved. - this PR adds two optional props `onPressed` and `onKnobMoved` that would allow us to archive this behavior in our project.
1 parent ae2da82 commit d712d27

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

packages/core/src/Slider/index.tsx

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,6 +302,14 @@ export interface SliderProps extends BaseProps {
302302
* Configuration for displaying ticks in the slider
303303
*/
304304
readonly tickConfig?: TickConfig
305+
/**
306+
* Executes JavaScript when a user presses the knob
307+
*/
308+
readonly onPressed?: (pressed: boolean) => void
309+
/**
310+
* Returns the current knob position in the X-axis
311+
*/
312+
readonly onKnobMove?: (value: number) => void
305313
}
306314

307315
export const Meta = styled.div`
@@ -334,6 +342,8 @@ export const Slider: FC<SliderProps> = ({
334342
onPointerDown,
335343
onPointerUp,
336344
onFocus,
345+
onPressed,
346+
onKnobMove,
337347
tickConfig = {
338348
ticks: [],
339349
},
@@ -357,6 +367,11 @@ export const Slider: FC<SliderProps> = ({
357367
[max, min, value]
358368
)
359369

370+
const sliderPosition = useMemo(
371+
() => fraction * sliderWidth,
372+
[fraction, sliderWidth]
373+
)
374+
360375
// Generate the ticks with its position along the x-axis
361376
const tickMarkers = useMemo(
362377
() =>
@@ -441,6 +456,16 @@ export const Slider: FC<SliderProps> = ({
441456
}
442457
}, [handleClick, pressed])
443458

459+
// Trigger callback when the knob is moved along the X-axis
460+
useEffect(() => {
461+
onKnobMove?.(sliderPosition)
462+
}, [sliderPosition])
463+
464+
// Trigger callback when knob is pressed
465+
useEffect(() => {
466+
onPressed?.(pressed)
467+
}, [pressed])
468+
444469
const getNextSnap = useCallback(
445470
() => snapValues.find(snapValue => snapValue > fraction),
446471
[snapValues, fraction]
@@ -597,9 +622,7 @@ export const Slider: FC<SliderProps> = ({
597622
fraction={fraction}
598623
pressed={pressed}
599624
style={{
600-
transform: `translateX(-50%) translateX(${
601-
fraction * sliderWidth
602-
}px)`,
625+
transform: `translateX(-50%) translateX(${sliderPosition}px)`,
603626
}}
604627
ref={knobRef}
605628
>

0 commit comments

Comments
 (0)