Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TBCCT-201] slider: displays current value while dragging the thumb #183

Merged
merged 1 commit into from
Dec 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading