Skip to content

The all-in-one local AI studio for your desktop: chat, image and video generation and a coding agent in one free, open source app. Windows and Linux. No Docker, no terminal, no cloud required.

2.2 KB
Raw Blame
import { useState } from 'react'
import { HelpMark } from '../ui/HelpMark'

interface Props {
  label: string
  /** Explanation behind a "?" next to the label: hover, or tap to show it below. */
  help?: { text: string; label: string }
  value: number
  min: number
  max: number
  step: number
  onChange: (value: number) => void
  /** What to show for the value when it is not the number itself. */
  display?: string
}

export function SliderControl({ label, help, value, min, max, step, onChange, display }: Props) {
  const [helpOpen, setHelpOpen] = useState(false)
  const pct = ((value - min) / (max - min)) * 100

  return (
    <div className="space-y-1">
      {/* Label + value on one line */}
      <div className="flex items-center justify-between">
        <span className="flex items-center gap-1 text-[0.7rem] text-gray-500 dark:text-gray-400">
          {label}
          {help && <HelpMark text={help.text} label={help.label} open={helpOpen} onToggle={() => setHelpOpen((v) => !v)} />}
        </span>
        <span className="text-[0.65rem] font-mono text-gray-400 dark:text-gray-500">{display ?? value}</span>
      </div>
      {/* Slider full width below */}
      <div className="relative h-5 flex items-center">
        {/* Track background */}
        <div className="absolute inset-x-0 h-1 rounded-full bg-white/10" />
        {/* Track fill */}
        <div className="absolute left-0 h-1 rounded-full bg-gray-400" style={{ width: `${pct}%` }} />
        {/* Native input (invisible but captures all mouse/touch events) */}
        <input
          type="range"
          min={min}
          max={max}
          step={step}
          value={value}
          aria-label={label}
          aria-valuetext={display}
          onChange={(e) => onChange(parseFloat(e.target.value))}
          className="absolute inset-x-0 w-full cursor-pointer"
          style={{ zIndex: 2, opacity: 0, top: '-4px', height: '28px' }}
        />
        {/* Custom thumb */}
        <div
          className="absolute w-3 h-3 rounded-full bg-gray-300 border border-gray-500 pointer-events-none"
          style={{ left: `calc(${pct}% - 6px)`, zIndex: 1 }}
        />
      </div>
      {help && helpOpen && <p className="t-micro leading-relaxed text-gray-500">{help.text}</p>}
    </div>
  )
}