Working tree is now the canonical repo. Off-chain play-money + trustless on-chain (FPMM AMM + CLOB via UMA settlement), AI market factory, creator/social/distribution, mobile shell + push, one-command demo + deploy-kit. Secrets excluded (see ENV-AND-KEYS.md). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
212 lines
5.6 KiB
TypeScript
212 lines
5.6 KiB
TypeScript
import clsx from 'clsx'
|
|
import * as RxSlider from '@radix-ui/react-slider'
|
|
import { ReactNode } from 'react'
|
|
import { filterDefined } from 'common/util/array'
|
|
|
|
export const sliderColors = {
|
|
green: ['bg-teal-400', 'focus:outline-teal-600/30 bg-teal-600'],
|
|
'light-green': [
|
|
'bg-emerald-200/50 dark:bg-emerald-800/50',
|
|
'focus:outline-emerald-200/20 bg-emerald-200 dark:bg-teal-200',
|
|
],
|
|
red: ['bg-scarlet-400', 'focus:outline-scarlet-600/30 bg-scarlet-600'],
|
|
indigo: ['bg-primary-300', 'focus:outline-primary-500/30 bg-primary-500'],
|
|
violet: ['bg-violet-300', 'focus:outline-violet-500/30 bg-violet-500'],
|
|
azure: ['bg-azure-300', 'focus:outline-azure-500/30 bg-azure-500'],
|
|
sienna: ['bg-sienna-300', 'focus:outline-sienna-500/30 bg-sienna-500'],
|
|
gray: [
|
|
'dark:bg-ink-500 bg-ink-600',
|
|
'focus:outline-ink-800/30 dark:bg-ink-700 bg-ink-700',
|
|
],
|
|
// light: ['primary-200', 'primary-300']
|
|
} as const
|
|
|
|
export type SliderColor = keyof typeof sliderColors | `#${string}`
|
|
export type Mark = { value: number; label: string }
|
|
|
|
export function Slider(props: {
|
|
amount: number
|
|
onChange: (newAmount: number) => void
|
|
min?: number
|
|
max?: number
|
|
step?: number
|
|
marks?: Mark[]
|
|
color?: SliderColor
|
|
className?: string
|
|
disabled?: boolean
|
|
inverted?: boolean
|
|
fillToRight?: boolean
|
|
ariaLabel?: string
|
|
ariaValueText?: string
|
|
}) {
|
|
const {
|
|
amount,
|
|
onChange,
|
|
min = 0,
|
|
max = 100,
|
|
step,
|
|
marks,
|
|
className,
|
|
disabled,
|
|
color = 'indigo',
|
|
inverted,
|
|
fillToRight,
|
|
ariaLabel,
|
|
ariaValueText,
|
|
} = props
|
|
|
|
const isCustomColor = color.startsWith('#')
|
|
const [trackClasses, thumbClasses] = isCustomColor
|
|
? ['', '']
|
|
: sliderColors[color as keyof typeof sliderColors]
|
|
|
|
return (
|
|
<RxSlider.Root
|
|
aria-label={ariaLabel}
|
|
aria-valuetext={ariaValueText}
|
|
className={clsx(
|
|
className,
|
|
'relative flex touch-none select-none items-center',
|
|
marks ? 'h-[43px]' : 'h-5'
|
|
)}
|
|
value={filterDefined([amount, fillToRight ? max : undefined])}
|
|
onValueChange={([val]) => onChange(val)}
|
|
min={min}
|
|
max={max}
|
|
step={step}
|
|
disabled={disabled}
|
|
inverted={inverted}
|
|
>
|
|
<Track
|
|
className={trackClasses}
|
|
rangeStyle={isCustomColor ? { backgroundColor: color } : undefined}
|
|
>
|
|
<div className="absolute left-2.5 right-2.5 h-full">
|
|
{marks?.map(({ value, label }) => (
|
|
<div
|
|
className="absolute top-1/2 -translate-x-1/2 -translate-y-1/2"
|
|
style={{ left: `${(value / (max - min)) * 100}%` }}
|
|
key={value}
|
|
>
|
|
<div
|
|
className={clsx(
|
|
fillToRight
|
|
? amount <= value
|
|
? trackClasses
|
|
: 'bg-ink-300'
|
|
: amount >= value
|
|
? trackClasses
|
|
: 'bg-ink-400',
|
|
'h-2 w-2 rounded-full'
|
|
)}
|
|
style={
|
|
isCustomColor &&
|
|
(fillToRight ? amount <= value : amount >= value)
|
|
? { backgroundColor: color }
|
|
: undefined
|
|
}
|
|
/>
|
|
<span className="text-ink-400 absolute left-1/2 top-4 -translate-x-1/2 text-xs">
|
|
{label}
|
|
</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Track>
|
|
<Thumb
|
|
className={thumbClasses}
|
|
style={isCustomColor ? { backgroundColor: color } : undefined}
|
|
/>
|
|
</RxSlider.Root>
|
|
)
|
|
}
|
|
|
|
export function RangeSlider(props: {
|
|
lowValue: number
|
|
highValue: number
|
|
setValues: (low: number, high: number) => void
|
|
min?: number
|
|
max?: number
|
|
disabled?: boolean
|
|
step?: number
|
|
color?: keyof typeof sliderColors
|
|
handleSize?: number
|
|
className?: string
|
|
marks?: Mark[]
|
|
}) {
|
|
const {
|
|
lowValue,
|
|
highValue,
|
|
setValues,
|
|
min,
|
|
max,
|
|
step,
|
|
disabled,
|
|
color = 'indigo',
|
|
className,
|
|
marks,
|
|
} = props
|
|
|
|
const [trackClasses, thumbClasses] = sliderColors[color]
|
|
|
|
return (
|
|
<RxSlider.Root
|
|
className={clsx(
|
|
'relative flex h-7 touch-none select-none items-center',
|
|
className
|
|
)}
|
|
value={[lowValue, highValue]}
|
|
step={step ?? 1}
|
|
onValueChange={([low, high]) => setValues(low, high)}
|
|
min={min}
|
|
max={max}
|
|
disabled={disabled}
|
|
>
|
|
<Track className={trackClasses}>
|
|
<div>
|
|
{marks?.map(({ value, label }) => (
|
|
<div
|
|
className="absolute top-1/2 -translate-x-1/2 -translate-y-1/2"
|
|
style={{ left: `${value}%` }}
|
|
key={value}
|
|
>
|
|
<span className="text-ink-400 absolute left-1/2 top-4 -translate-x-1/2 text-xs">
|
|
{label}
|
|
</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
</Track>
|
|
<Thumb className={thumbClasses} />
|
|
<Thumb className={thumbClasses} />
|
|
</RxSlider.Root>
|
|
)
|
|
}
|
|
|
|
const Track = (props: {
|
|
className: string
|
|
children?: ReactNode
|
|
rangeStyle?: React.CSSProperties
|
|
}) => {
|
|
const { className, children, rangeStyle } = props
|
|
return (
|
|
<RxSlider.Track className="bg-ink-300 relative h-1 grow rounded-full">
|
|
{children}
|
|
<RxSlider.Range
|
|
className={clsx(className, 'absolute h-full rounded-full')}
|
|
style={rangeStyle}
|
|
/>
|
|
</RxSlider.Track>
|
|
)
|
|
}
|
|
|
|
const Thumb = (props: { className: string; style?: React.CSSProperties }) => (
|
|
<RxSlider.Thumb
|
|
className={clsx(
|
|
props.className,
|
|
'block h-5 w-5 cursor-col-resize rounded-full outline outline-4 outline-transparent transition-colors'
|
|
)}
|
|
style={props.style}
|
|
/>
|
|
)
|