I want to create a follow effect. currently the when i move the slider.thumb the tool tip moves instantly with the slider.thumb. but i want when to create a pulling effect with slight delay.
️ Here is the Video Showing Desired Effect
this is my current code. & stackblitz link
import * as Slider from '@radix-ui/react-slider'
import { useState } from 'react'
export const InputRange = () => {
const [value, setValue] = useState(2)
return (
<form>
<Slider.Root
className="relative flex items-center select-none touch-none w-full h-5 "
defaultValue={[value]}
min={1}
max={30}
step={1}
aria-label="Volume"
onValueChange={(e) => setValue(e[0])}
>
<Slider.Track className="bg-slate-200 relative grow rounded-full h-[3px]">
<Slider.Range className="absolute bg-black rounded-full h-full" />
</Slider.Track>
<Slider.Thumb className="relative group duration-150 block w-6 h-6 bg-black rounded-full active:scale-125 outline-none border-none cursor-pointer " >
<h1 className='absolute ease-in-out duration-150 opacity-0 group-active:opacity-100 group-active:-translate-y-12 -translate-x-8 -translate-y-6 px-5 py-2 bg-black text-center rounded-full text-white whitespace-nowrap text-xs font-bold'>{value} {value === 1 ? "night" : "nights"} </h1>
</Slider.Thumb>
</Slider.Root>
</form>
)
}
I tried to update the position of the tooltip for the radix-ui/react-slider component, but it caused the tooltip to move off the screen when I moved the slider.
const updateTooltipPosition = () => {
const thumb = thumbRef.current.getBoundingClientRect()
const tooltip = tooltipRef.current.getBoundingClientRect()
// console.log(thumb.x );
const tooltipCenter = tooltip.left + tooltip.width / 2
console.log(tooltip.left , tooltipCenter)
tooltipRef.current.style.transform = `translate(${}px, -120%)`
}
tried using useEffect but did nothing.
const [value, setValue] = useState(2)
const tooltipRef = useRef(null)
useEffect(() => {
if (tooltipRef.current) {
const tooltipWidth = tooltipRef.current.offsetWidth
const thumbPosition = ((value - 1) / 29) * 100 // calculate the position of the thumb
const tooltipPosition = thumbPosition - tooltipWidth / 2 // adjust for the width of the tooltip
tooltipRef.current.style.left = `${tooltipPosition}%` // set the left position of the tooltip
}
}, [value])
other possible ways i wanted to try like using a library like react-spring, framer motion but don't know how it will solve the issue.