@podoba/react 0.0.45 → 0.0.46
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/src/components/slider.tsx +97 -53
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@podoba/react",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.46",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "podoba React components — React Aria Components + Tailwind primitives + layout, built with uic.",
|
|
6
6
|
"repository": {
|
|
@@ -28,8 +28,8 @@
|
|
|
28
28
|
"typecheck": "tsc --build"
|
|
29
29
|
},
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@podoba/tokens": "^0.0.
|
|
32
|
-
"@podoba/tailwind": "^0.0.
|
|
31
|
+
"@podoba/tokens": "^0.0.46",
|
|
32
|
+
"@podoba/tailwind": "^0.0.46",
|
|
33
33
|
"react-aria-components": "1.18.0",
|
|
34
34
|
"class-variance-authority": "0.7.1",
|
|
35
35
|
"clsx": "2.1.1",
|
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { ReactNode } from 'react'
|
|
2
2
|
import { Label, Slider as RACSlider, type SliderProps as RACSliderProps, SliderThumb, SliderTrack } from 'react-aria-components'
|
|
3
3
|
|
|
4
|
+
export const SLIDER_SIZES = ['md', 'sm'] as const
|
|
5
|
+
export type SliderSize = (typeof SLIDER_SIZES)[number]
|
|
6
|
+
export const SLIDER_TONES = ['default', 'inverted'] as const
|
|
7
|
+
export type SliderTone = (typeof SLIDER_TONES)[number]
|
|
8
|
+
|
|
4
9
|
/**
|
|
5
10
|
* Slider — a draggable value selector (single value or a range when `value`/
|
|
6
11
|
* `defaultValue` is a two-number array). Built on React Aria Components `Slider`
|
|
@@ -8,62 +13,101 @@ import { Label, Slider as RACSlider, type SliderProps as RACSliderProps, SliderT
|
|
|
8
13
|
* thumb carries its current value next to it (respects `formatOptions`).
|
|
9
14
|
* `orientation="vertical"` renders a 12rem tall track with the minimum at the
|
|
10
15
|
* bottom and the value label to the right of each thumb.
|
|
16
|
+
*
|
|
17
|
+
* `size="sm"` is the compact toolbar slider: one 24px row, the label kept for
|
|
18
|
+
* screen readers only and no value label (show your own readout beside it).
|
|
19
|
+
* `tone="inverted"` draws it for a dark surface such as a `bg-brand-primary` pill.
|
|
11
20
|
*/
|
|
12
|
-
export type SliderProps<T extends number | number[]> = RACSliderProps<T> & {
|
|
13
|
-
/**
|
|
21
|
+
export type SliderProps<T extends number | number[]> = Omit<RACSliderProps<T>, 'className'> & {
|
|
22
|
+
/** Label (required for accessibility); visually hidden when `size="sm"`. */
|
|
14
23
|
label: ReactNode
|
|
15
|
-
/** Hide the value label next to each thumb. */
|
|
24
|
+
/** Hide the value label next to each thumb (always hidden when `size="sm"`). */
|
|
16
25
|
hideValue?: boolean
|
|
26
|
+
size?: SliderSize
|
|
27
|
+
tone?: SliderTone
|
|
28
|
+
/** Extra classes on the root, for layout such as a fixed width. */
|
|
29
|
+
className?: string
|
|
30
|
+
/**
|
|
31
|
+
* Drawn on the track between the rail and the fill, positioned absolutely
|
|
32
|
+
* against the track: e.g. a marked stretch of the range.
|
|
33
|
+
*/
|
|
34
|
+
trackAdornment?: ReactNode
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const RAIL = { md: { horizontal: 'h-1.5 w-full', vertical: 'h-full w-1.5' }, sm: { horizontal: 'h-1 w-full', vertical: 'h-full w-1' } }
|
|
38
|
+
const FILL = { md: { horizontal: 'h-1.5', vertical: 'w-1.5' }, sm: { horizontal: 'h-1', vertical: 'w-1' } }
|
|
39
|
+
const THUMB_SIZE = { md: 'h-4 w-4', sm: 'h-3.5 w-3.5' }
|
|
40
|
+
const THUMB_TONE = {
|
|
41
|
+
default: 'border-2 border-fg bg-surface data-[focus-visible]:ring-ring',
|
|
42
|
+
inverted: 'bg-fg-inverted data-[focus-visible]:ring-brand-green',
|
|
17
43
|
}
|
|
44
|
+
const RAIL_TONE = { default: 'bg-surface-muted', inverted: 'bg-fg-inverted/30' }
|
|
45
|
+
const TEXT_TONE = { default: 'text-fg', inverted: 'text-fg-inverted' }
|
|
18
46
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
47
|
+
function trackClassName(size: SliderSize, vertical: boolean): string {
|
|
48
|
+
if (vertical) return 'relative flex h-48 w-6 justify-center'
|
|
49
|
+
// mt-6 leaves room for the value label that sits above each thumb.
|
|
50
|
+
return size === 'sm' ? 'relative flex h-6 w-full items-center' : 'relative mt-6 flex h-6 w-full items-center'
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
export const Slider = <T extends number | number[]>({
|
|
54
|
+
label,
|
|
55
|
+
hideValue,
|
|
56
|
+
size = 'md',
|
|
57
|
+
tone = 'default',
|
|
58
|
+
className,
|
|
59
|
+
trackAdornment,
|
|
60
|
+
...props
|
|
61
|
+
}: SliderProps<T>) => {
|
|
62
|
+
const compact = size === 'sm'
|
|
63
|
+
const showValue = !hideValue && !compact
|
|
64
|
+
return (
|
|
65
|
+
<RACSlider
|
|
66
|
+
{...props}
|
|
67
|
+
className={`flex flex-col data-[disabled]:opacity-50${compact ? '' : ' gap-1.5'}${className ? ` ${className}` : ''}`}
|
|
29
68
|
>
|
|
30
|
-
{
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
)
|
|
69
|
+
<Label className={compact ? 'sr-only' : `text-heading5 font-medium ${TEXT_TONE[tone]}`}>{label}</Label>
|
|
70
|
+
<SliderTrack className={({ orientation }) => trackClassName(size, orientation === 'vertical')}>
|
|
71
|
+
{({ state }) => {
|
|
72
|
+
const isVertical = state.orientation === 'vertical'
|
|
73
|
+
const axis = isVertical ? 'vertical' : 'horizontal'
|
|
74
|
+
const start = state.values.length > 1 ? state.getThumbPercent(0) : 0
|
|
75
|
+
const end = state.getThumbPercent(state.values.length - 1)
|
|
76
|
+
return (
|
|
77
|
+
<>
|
|
78
|
+
<div className={`rounded-full ${RAIL_TONE[tone]} ${RAIL[size][axis]}`} />
|
|
79
|
+
{trackAdornment}
|
|
80
|
+
<div
|
|
81
|
+
className={`absolute rounded-full bg-brand-green ${FILL[size][axis]}`}
|
|
82
|
+
style={
|
|
83
|
+
isVertical
|
|
84
|
+
? { bottom: `${start * 100}%`, height: `${(end - start) * 100}%` }
|
|
85
|
+
: { left: `${start * 100}%`, width: `${(end - start) * 100}%` }
|
|
86
|
+
}
|
|
87
|
+
/>
|
|
88
|
+
{state.values.map((_, i) => (
|
|
89
|
+
<SliderThumb
|
|
90
|
+
// biome-ignore lint/suspicious/noArrayIndexKey: thumbs are positional and fixed-count
|
|
91
|
+
key={i}
|
|
92
|
+
index={i}
|
|
93
|
+
// React Aria positions the thumb along the track (`left` or, when
|
|
94
|
+
// vertical, `top`) with `translate(-50%, -50%)` but leaves the
|
|
95
|
+
// cross axis unset, so center it there ourselves.
|
|
96
|
+
className={`${isVertical ? 'left-1/2' : 'top-1/2'} ${THUMB_SIZE[size]} ${THUMB_TONE[tone]} rounded-full outline-none transition-transform data-[dragging]:scale-110 data-[focus-visible]:ring-2`}
|
|
97
|
+
>
|
|
98
|
+
{showValue ? (
|
|
99
|
+
<span
|
|
100
|
+
className={`pointer-events-none absolute whitespace-nowrap text-label font-medium tabular-nums ${TEXT_TONE[tone]} ${isVertical ? 'top-1/2 left-full ml-2 -translate-y-1/2' : '-top-6 left-1/2 -translate-x-1/2'}`}
|
|
101
|
+
>
|
|
102
|
+
{state.getThumbValueLabel(i)}
|
|
103
|
+
</span>
|
|
104
|
+
) : null}
|
|
105
|
+
</SliderThumb>
|
|
106
|
+
))}
|
|
107
|
+
</>
|
|
108
|
+
)
|
|
109
|
+
}}
|
|
110
|
+
</SliderTrack>
|
|
111
|
+
</RACSlider>
|
|
112
|
+
)
|
|
113
|
+
}
|