Skip to content

Commit

Permalink
adds tooltip to thumb slider
Browse files Browse the repository at this point in the history
  • Loading branch information
agnlez committed Nov 14, 2024
1 parent a1ef257 commit a150045
Showing 1 changed file with 45 additions and 16 deletions.
61 changes: 45 additions & 16 deletions client/src/components/ui/slider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ import * as SliderPrimitive from "@radix-ui/react-slider";

import { cn } from "@/lib/utils";

import {
Tooltip,
TooltipContent,
TooltipTrigger,
} from "@/components/ui/tooltip";

const Thumb = React.forwardRef<
React.ElementRef<typeof SliderPrimitive.Thumb>,
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Thumb>
Expand Down Expand Up @@ -49,22 +55,45 @@ Slider.displayName = SliderPrimitive.Root.displayName;
const RangeSlider = React.forwardRef<
React.ElementRef<typeof SliderPrimitive.Root>,
React.ComponentPropsWithoutRef<typeof SliderPrimitive.Root>
>(({ className, ...props }, ref) => (
<SliderPrimitive.Root
ref={ref}
className={cn(
"relative flex w-full touch-none select-none items-center",
className,
)}
{...props}
>
<SliderPrimitive.Track className={SLIDER_TRACK_STYLES}>
<SliderPrimitive.Range className="absolute h-full bg-primary" />
</SliderPrimitive.Track>
<Thumb />
<Thumb />
</SliderPrimitive.Root>
));
>(({ className, ...props }, ref) => {
const [values, setValues] = React.useState([
props.defaultValue?.[0] || 0,
props.defaultValue?.[1] || 0,
]);

return (
<SliderPrimitive.Root
ref={ref}
className={cn(
"relative flex w-full touch-none select-none items-center",
className,
)}
{...props}
onValueChange={setValues}
>
<SliderPrimitive.Track className={SLIDER_TRACK_STYLES}>
<SliderPrimitive.Range className="absolute h-full bg-primary" />
</SliderPrimitive.Track>
<Tooltip>
<TooltipTrigger asChild>
<Thumb />
</TooltipTrigger>
<TooltipContent side="top" align="center">
{values[0]}
</TooltipContent>
</Tooltip>

<Tooltip>
<TooltipTrigger asChild>
<Thumb />
</TooltipTrigger>
<TooltipContent side="top" align="center">
{values[1]}
</TooltipContent>
</Tooltip>
</SliderPrimitive.Root>
);
});

RangeSlider.displayName = SliderPrimitive.Root.displayName;

Expand Down

0 comments on commit a150045

Please sign in to comment.