@kolkrabbi/kol-component 0.311.3 → 0.313.0
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/index.js +2 -0
- package/src/molecules/VariantsControl.jsx +91 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kolkrabbi/kol-component",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.313.0",
|
|
4
4
|
"description": "The core component library",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|
|
@@ -26,8 +26,8 @@
|
|
|
26
26
|
"embla-carousel-react": "^8.6.0",
|
|
27
27
|
"pdfjs-dist": "^6.3.289",
|
|
28
28
|
"react-syntax-highlighter": "^16.1.1",
|
|
29
|
-
"@kolkrabbi/kol-
|
|
30
|
-
"@kolkrabbi/kol-
|
|
29
|
+
"@kolkrabbi/kol-markdown": "^0.2.0",
|
|
30
|
+
"@kolkrabbi/kol-search": "^0.3.0"
|
|
31
31
|
},
|
|
32
32
|
"peerDependencies": {
|
|
33
33
|
"@kolkrabbi/kol-icons": ">=0.22.0",
|
package/src/index.js
CHANGED
|
@@ -67,6 +67,8 @@ export { default as InspectorSection, default as Section } from './molecules/Ins
|
|
|
67
67
|
export { default as SectionText, SECTION_TEXT_LEVELS } from './molecules/SectionText.jsx'
|
|
68
68
|
export { default as SectionLabel } from './atoms/SectionLabel.jsx'
|
|
69
69
|
export { default as SegmentedToggle } from './atoms/SegmentedToggle.jsx'
|
|
70
|
+
/* a page's variants — the pinned slider button and its panel of segmented rows (OneVariantsControl, 2026-10-29; catalog's) */
|
|
71
|
+
export { default as VariantsControl } from './molecules/VariantsControl.jsx'
|
|
70
72
|
export { default as Stepper } from './molecules/Stepper.jsx'
|
|
71
73
|
export { default as Kbd } from './atoms/Kbd.jsx'
|
|
72
74
|
export { default as OptionRow } from './molecules/OptionRow.jsx'
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from 'react'
|
|
2
|
+
import Button from '../atoms/Button.jsx'
|
|
3
|
+
import SegmentedToggle from '../atoms/SegmentedToggle.jsx'
|
|
4
|
+
import { usePopover, PopoverPanel } from '../utilities/Popover.jsx'
|
|
5
|
+
|
|
6
|
+
const cap = (v) => String(v).charAt(0).toUpperCase() + String(v).slice(1)
|
|
7
|
+
const KEY = 'kol-variants-control'
|
|
8
|
+
const read = () => { try { return JSON.parse(localStorage.getItem(KEY) ?? 'null') } catch { return null } }
|
|
9
|
+
const write = (p) => { try { p ? localStorage.setItem(KEY, JSON.stringify(p)) : localStorage.removeItem(KEY) } catch { /* private mode */ } }
|
|
10
|
+
const clamp = (p) => (typeof window === 'undefined' ? p : { x: Math.min(Math.max(8, p.x), window.innerWidth - 56), y: Math.min(Math.max(8, p.y), window.innerHeight - 56) })
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* VariantsControl — a page's variants: a round `slider-01` button pinned top-right at the shell's page pad that opens a
|
|
14
|
+
* panel of labelled segmented rows, one per axis. apps/catalog's `VariantsButton`, shipped (OneVariantsControl, kol-website
|
|
15
|
+
* 2026-10-29 — user: *"why are they reinventing the wheel? we already have a component for this"*); catalog, rails, shell
|
|
16
|
+
* and layouts each had it, by hand or reinvented. Each axis is a SEGMENTED row, not a dropdown (user 2026-10-22, on a
|
|
17
|
+
* dropdown list opening over the panel in the panel's own colour): every option visible, one click, nothing floats over the
|
|
18
|
+
* panel.
|
|
19
|
+
*
|
|
20
|
+
* DRAGGABLE (LayoutsAppPages 2026-10-29 — user: *"action button should be draggable if it's in the way of something"*):
|
|
21
|
+
* drag the button anywhere in the viewport and it stays there (remembered per app, in local storage); a double-click puts it
|
|
22
|
+
* back in its corner. A drag never opens the panel; a click still does.
|
|
23
|
+
*
|
|
24
|
+
* @param {Array} axes `[{ key, label, values }]` (each value labelled with its first letter capitalised) or
|
|
25
|
+
* `[{ key, label, options: [{ value, label }] }]`
|
|
26
|
+
* @param {Object} value the current value by axis key
|
|
27
|
+
* @param {Function} onChange `(key, value)`
|
|
28
|
+
* @param {node} [children] under the rows, in the panel — what the current variant is (a page's name, its purpose)
|
|
29
|
+
* @param {Object} [style] merged onto the pinned wrapper — a host whose own chrome holds that corner moves it
|
|
30
|
+
*/
|
|
31
|
+
export default function VariantsControl({ axes = [], value = {}, onChange, children, style, className = '' }) {
|
|
32
|
+
const [open, setOpen] = useState(false)
|
|
33
|
+
const [pos, setPos] = useState(read)
|
|
34
|
+
const drag = useRef(null)
|
|
35
|
+
const popover = usePopover({ open, onOpenChange: setOpen, placement: 'bottom-end', offset: 8 })
|
|
36
|
+
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
const onResize = () => setPos((p) => (p ? clamp(p) : p))
|
|
39
|
+
window.addEventListener('resize', onResize)
|
|
40
|
+
return () => window.removeEventListener('resize', onResize)
|
|
41
|
+
}, [])
|
|
42
|
+
|
|
43
|
+
const onPointerDown = (e) => {
|
|
44
|
+
if (e.button !== 0) return
|
|
45
|
+
const box = e.currentTarget.getBoundingClientRect()
|
|
46
|
+
drag.current = { sx: e.clientX, sy: e.clientY, ox: e.clientX - box.left, oy: e.clientY - box.top, moved: false }
|
|
47
|
+
const move = (ev) => {
|
|
48
|
+
const d = drag.current
|
|
49
|
+
if (!d) return
|
|
50
|
+
if (!d.moved && Math.hypot(ev.clientX - d.sx, ev.clientY - d.sy) < 4) return
|
|
51
|
+
d.moved = true
|
|
52
|
+
setOpen(false)
|
|
53
|
+
setPos(clamp({ x: ev.clientX - d.ox, y: ev.clientY - d.oy }))
|
|
54
|
+
}
|
|
55
|
+
const up = () => {
|
|
56
|
+
window.removeEventListener('pointermove', move)
|
|
57
|
+
window.removeEventListener('pointerup', up)
|
|
58
|
+
const d = drag.current
|
|
59
|
+
if (d?.moved) setPos((p) => { write(p); return p })
|
|
60
|
+
/* the click that ends a drag is swallowed below; clear the flag after it has passed */
|
|
61
|
+
setTimeout(() => { drag.current = null }, 0)
|
|
62
|
+
}
|
|
63
|
+
window.addEventListener('pointermove', move)
|
|
64
|
+
window.addEventListener('pointerup', up)
|
|
65
|
+
}
|
|
66
|
+
/* a drag's closing click never reaches the button (so it never opens the panel) */
|
|
67
|
+
const onClickCapture = (e) => { if (drag.current?.moved) { e.stopPropagation(); e.preventDefault() } }
|
|
68
|
+
const reset = () => { write(null); setPos(null) }
|
|
69
|
+
|
|
70
|
+
const where = pos
|
|
71
|
+
? { left: pos.x, top: pos.y }
|
|
72
|
+
: { top: 'var(--kol-shell-page-pad, 24px)', right: 'var(--kol-shell-page-pad, 24px)', ...style }
|
|
73
|
+
return (
|
|
74
|
+
<div className={`fixed z-10 ${className}`.trim()} style={pos ? { ...style, top: undefined, right: undefined, ...where } : where}>
|
|
75
|
+
<span ref={popover.refs.setReference} {...popover.getReferenceProps()} onPointerDown={onPointerDown} onClickCapture={onClickCapture} onDoubleClick={reset} className="inline-flex touch-none" title="Drag to move · double-click to put back">
|
|
76
|
+
<Button iconOnly="slider-01" size="lg" tone="inverted" radius="full" aria-label="Variants" style={{ boxShadow: 'var(--kol-shadow-lg)' }} pressed={open} />
|
|
77
|
+
</span>
|
|
78
|
+
<PopoverPanel popover={popover} className="grid grid-cols-[auto_minmax(0,1fr)] items-center gap-x-4 gap-y-2 p-3 max-w-[calc(100vw-16px)]">
|
|
79
|
+
{axes.map((a) => [
|
|
80
|
+
<span key={`${a.key}-l`} className="kol-mono-12 text-fg-64">{a.label}</span>,
|
|
81
|
+
/* a row wider than the panel (a phone, a long axis) scrolls sideways in its cell — every option still one tap away */
|
|
82
|
+
<div key={a.key} className="min-w-0 overflow-x-auto">
|
|
83
|
+
<SegmentedToggle size="sm" className="w-max min-w-full" ariaLabel={a.label} value={value[a.key]} onChange={(v) => onChange?.(a.key, v)}
|
|
84
|
+
options={a.options ?? (a.values ?? []).map((v) => ({ value: v, label: cap(v) }))} />
|
|
85
|
+
</div>,
|
|
86
|
+
])}
|
|
87
|
+
{children != null && <div className="col-span-2 border-t border-fg-08 pt-3 mt-1">{children}</div>}
|
|
88
|
+
</PopoverPanel>
|
|
89
|
+
</div>
|
|
90
|
+
)
|
|
91
|
+
}
|