Skip to content

Commit

Permalink
slider: displays current value while dragging the thumb
Browse files Browse the repository at this point in the history
  • Loading branch information
andresgnlez committed Dec 18, 2024
1 parent be3da98 commit fd506b7
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions client/src/components/ui/slider.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"use client";

import * as React from "react";
import { useState } from "react";

import * as SliderPrimitive from "@radix-ui/react-slider";

Expand Down Expand Up @@ -58,6 +59,10 @@ const RangeSlider = React.forwardRef<
format?: (v: number) => number | string;
}
>(({ className, format = (v: number) => v, ...props }, ref) => {
const [isDragging, setIsDragging] = useState({
left: false,
right: false,
});
const [values, setValues] = React.useState([
props.defaultValue?.[0] || 0,
props.defaultValue?.[1] || 0,
Expand All @@ -79,18 +84,32 @@ const RangeSlider = React.forwardRef<
<SliderPrimitive.Track className={SLIDER_TRACK_STYLES}>
<SliderPrimitive.Range className="absolute h-full bg-primary" />
</SliderPrimitive.Track>
<Tooltip>
<Tooltip {...(isDragging["left"] && { open: true })}>
<TooltipTrigger asChild>
<Thumb />
<Thumb
onPointerDown={() => {
setIsDragging((prevState) => ({ ...prevState, left: true }));
}}
onPointerUp={() => {
setIsDragging((prevState) => ({ ...prevState, left: false }));
}}
/>
</TooltipTrigger>
<TooltipContent side="top" align="center">
{format(values[0])}
</TooltipContent>
</Tooltip>

<Tooltip>
<Tooltip {...(isDragging["right"] && { open: true })}>
<TooltipTrigger asChild>
<Thumb />
<Thumb
onPointerDown={() => {
setIsDragging((prevState) => ({ ...prevState, right: true }));
}}
onPointerUp={() => {
setIsDragging((prevState) => ({ ...prevState, right: false }));
}}
/>
</TooltipTrigger>
<TooltipContent side="top" align="center">
{format(values[1])}
Expand Down

0 comments on commit fd506b7

Please sign in to comment.