Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions r/sortable-grid.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
"path": "src/components/cipher/ui/sortable-grid.tsx",
"type": "registry:file",
"target": "src/components/cipher/ui/sortable-grid.tsx",
"content": "import * as React from \"react\";\nimport { motion, useReducedMotion } from \"motion/react\";\n\nimport { snap, spring } from \"../lib/motion\";\nimport { useSortable } from \"../lib/sortable\";\nimport { cn } from \"../lib/utils\";\nimport { SortableAnnouncer, SortableHandle } from \"./sortable-list\";\n\nimport styles from \"./sortable-grid.module.css\";\n\nexport interface SortableGridItemState {\n index: number;\n /** True while the item is held, by pointer or keyboard. */\n dragging: boolean;\n disabled: boolean;\n /** The item's grip. Place it in the item, such as in a Card's action slot. */\n handle: React.ReactNode;\n}\nexport interface SortableGridProps<T> {\n items: readonly T[];\n getKey: (item: T) => string;\n /** Called once per committed move: on drop, on a keyboard drop, or on an Alt+Arrow step. Never during a drag. */\n onReorder: (nextItems: T[]) => void;\n renderItem: (item: T, state: SortableGridItemState) => React.ReactNode;\n /** The item's name, used for its handle label and the announcements. */\n getLabel: (item: T) => string;\n /** Accessible name of the grid. */\n label: string;\n /** The narrowest a column gets before the grid drops a column, in pixels. */\n minItemWidth?: number;\n disabled?: boolean;\n /** A disabled item keeps its handle inert, so it cannot be picked up; other items can still move past it. */\n isItemDisabled?: (item: T) => boolean;\n className?: string;\n}\n\nconst columnsOf = (container: HTMLElement | null) => {\n const items = Array.from(container?.children ?? []) as HTMLElement[];\n const top = items[0]?.offsetTop;\n return Math.max(1, items.filter((item) => item.offsetTop === top).length);\n};\n\n/** Items in responsive columns that a person puts in order, by dragging an item's grip or from the keyboard in two dimensions. */\nexport function SortableGrid<T>({\n items,\n getKey,\n onReorder,\n renderItem,\n getLabel,\n label,\n minItemWidth = 240,\n disabled = false,\n isItemDisabled,\n className\n}: SortableGridProps<T>) {\n const reduced = useReducedMotion();\n const sort = useSortable({ items, getKey, getLabel, onReorder });\n const list = React.useRef<HTMLUListElement>(null);\n\n const delta = (key: string) => {\n if (key === \"ArrowLeft\") return -1;\n if (key === \"ArrowRight\") return 1;\n const columns = columnsOf(list.current);\n if (key === \"ArrowUp\") return -columns;\n if (key === \"ArrowDown\") return columns;\n return 0;\n };\n const follow = (event: PointerEvent) => {\n const target = Array.from(list.current?.children ?? []).findIndex((element) => {\n const box = element.getBoundingClientRect();\n return (\n event.clientX >= box.left &&\n event.clientX <= box.right &&\n event.clientY >= box.top &&\n event.clientY <= box.bottom\n );\n });\n if (target >= 0) sort.moveTo(target);\n };\n const startDrag = (item: T) => {\n sort.pick(item, true);\n const stop = () => {\n window.removeEventListener(\"pointermove\", follow);\n window.removeEventListener(\"pointerup\", stop);\n window.removeEventListener(\"pointercancel\", stop);\n };\n window.addEventListener(\"pointermove\", follow);\n window.addEventListener(\"pointerup\", stop);\n window.addEventListener(\"pointercancel\", stop);\n };\n\n return (\n <div\n className={cn(styles.shell, className)}\n data-slot=\"sortable-grid\"\n data-disabled={disabled || undefined}\n >\n <ul\n ref={(node) => {\n list.current = node;\n sort.container.current = node;\n }}\n className={styles.grid}\n aria-label={label}\n style={\n {\n \"--cipher-sortable-grid-min\": `${minItemWidth}px`\n } as React.CSSProperties\n }\n >\n {sort.preview.map((item, index) => {\n const key = getKey(item);\n const itemDisabled = disabled || Boolean(isItemDisabled?.(item));\n const lifted = sort.lift?.key === key;\n return (\n <motion.li\n key={key}\n layout=\"position\"\n transition={reduced ? snap : spring.moderate}\n className={styles.item}\n data-slot=\"sortable-grid-item\"\n data-dragging={lifted || undefined}\n data-disabled={itemDisabled || undefined}\n >\n {renderItem(item, {\n index,\n dragging: lifted,\n disabled: itemDisabled,\n handle: (\n <SortableHandle\n itemKey={key}\n label={getLabel(item)}\n instructions={sort.instructions}\n lifted={lifted && !sort.lift?.pointer}\n disabled={itemDisabled}\n onPointerDown={() => {\n if (!sort.lift) startDrag(item);\n }}\n onKeyDown={(event) => sort.keyDown(event, item, delta(event.key))}\n onBlur={(event) => sort.blur(event, item)}\n />\n )\n })}\n </motion.li>\n );\n })}\n </ul>\n <SortableAnnouncer instructions={sort.instructions} announcement={sort.announcement}>\n Press Space or Enter to pick up, then use the arrow keys to move across and between rows.\n Alt with an arrow key moves one step.\n </SortableAnnouncer>\n </div>\n );\n}\n"
"content": "import * as React from \"react\";\nimport { animate, motion, useMotionValue, useReducedMotion } from \"motion/react\";\n\nimport { snap, spring } from \"../lib/motion\";\nimport { useSortable } from \"../lib/sortable\";\nimport { cn } from \"../lib/utils\";\nimport { SortableAnnouncer, SortableHandle } from \"./sortable-list\";\n\nimport styles from \"./sortable-grid.module.css\";\n\nexport interface SortableGridItemState {\n index: number;\n /** True while the item is held, by pointer or keyboard. */\n dragging: boolean;\n disabled: boolean;\n /** The item's grip. Place it in the item, such as in a Card's action slot. */\n handle: React.ReactNode;\n}\nexport interface SortableGridProps<T> {\n items: readonly T[];\n getKey: (item: T) => string;\n /** Called once per committed move: on drop, on a keyboard drop, or on an Alt+Arrow step. Never during a drag. */\n onReorder: (nextItems: T[]) => void;\n renderItem: (item: T, state: SortableGridItemState) => React.ReactNode;\n /** The item's name, used for its handle label and the announcements. */\n getLabel: (item: T) => string;\n /** Accessible name of the grid. */\n label: string;\n /** The narrowest a column gets before the grid drops a column, in pixels. */\n minItemWidth?: number;\n disabled?: boolean;\n /** A disabled item keeps its handle inert, so it cannot be picked up; other items can still move past it. */\n isItemDisabled?: (item: T) => boolean;\n className?: string;\n}\n\ntype Box = { left: number; top: number; width: number; height: number };\ntype Drag = {\n slots: Box[];\n grab: { x: number; y: number };\n pointer: { x: number; y: number };\n};\n\nconst center = (box: Box) => ({ x: box.left + box.width / 2, y: box.top + box.height / 2 });\n\nconst columnsOf = (container: HTMLElement | null) => {\n const items = Array.from(container?.children ?? []) as HTMLElement[];\n const top = items[0]?.offsetTop;\n return Math.max(1, items.filter((item) => item.offsetTop === top).length);\n};\n\n/** Items in responsive columns that a person puts in order, by dragging an item's grip or from the keyboard in two dimensions. */\nexport function SortableGrid<T>({\n items,\n getKey,\n onReorder,\n renderItem,\n getLabel,\n label,\n minItemWidth = 240,\n disabled = false,\n isItemDisabled,\n className\n}: SortableGridProps<T>) {\n const reduced = useReducedMotion();\n const sort = useSortable({ items, getKey, getLabel, onReorder });\n const list = React.useRef<HTMLUListElement>(null);\n\n const delta = (key: string) => {\n if (key === \"ArrowLeft\") return -1;\n if (key === \"ArrowRight\") return 1;\n const columns = columnsOf(list.current);\n if (key === \"ArrowUp\") return -columns;\n if (key === \"ArrowDown\") return columns;\n return 0;\n };\n const x = useMotionValue(0);\n const y = useMotionValue(0);\n const drag = React.useRef<Drag | null>(null);\n const [held, setHeld] = React.useState<string | null>(null);\n\n const origin = () => list.current?.getBoundingClientRect() ?? { left: 0, top: 0 };\n const place = () => {\n const current = drag.current;\n const element = list.current?.querySelector<HTMLElement>(\"[data-following]\");\n if (!current || !element) return;\n const box = element.getBoundingClientRect();\n x.set(current.pointer.x - current.grab.x - box.left);\n y.set(current.pointer.y - current.grab.y - box.top);\n };\n const follow = (event: PointerEvent) => {\n const current = drag.current;\n const lift = sort.liftRef.current;\n if (!current || !lift) return;\n current.pointer = { x: event.clientX, y: event.clientY };\n const { left, top } = origin();\n const from = current.slots[lift.from]!;\n const card = {\n x: event.clientX - left - current.grab.x + from.width / 2,\n y: event.clientY - top - current.grab.y + from.height / 2\n };\n const distance = (box: Box) => Math.hypot(center(box).x - card.x, center(box).y - card.y);\n let best = lift.to;\n current.slots.forEach((slot, index) => {\n if (distance(slot) < distance(current.slots[best]!)) best = index;\n });\n const slot = current.slots[lift.to]!;\n const hysteresis = Math.min(slot.width, slot.height) * 0.25;\n if (best !== lift.to && distance(current.slots[best]!) < distance(slot) - hysteresis)\n sort.moveTo(best);\n else place();\n };\n React.useLayoutEffect(place, [sort.lift?.to]);\n const startDrag = (item: T, event: React.PointerEvent) => {\n const container = list.current;\n if (!container) return;\n const { left, top } = container.getBoundingClientRect();\n const slots = Array.from(container.children).map((element) => {\n const box = element.getBoundingClientRect();\n return { left: box.left - left, top: box.top - top, width: box.width, height: box.height };\n });\n const from = slots[items.indexOf(item)];\n if (!from) return;\n const key = getKey(item);\n drag.current = {\n slots,\n grab: { x: event.clientX - left - from.left, y: event.clientY - top - from.top },\n pointer: { x: event.clientX, y: event.clientY }\n };\n x.set(0);\n y.set(0);\n setHeld(key);\n sort.pick(item, true);\n const stop = () => {\n window.removeEventListener(\"pointermove\", follow);\n window.removeEventListener(\"pointerup\", stop);\n window.removeEventListener(\"pointercancel\", stop);\n drag.current = null;\n const settle = reduced ? snap : spring.moderate;\n Promise.all([animate(x, 0, settle), animate(y, 0, settle)]).then(() =>\n setHeld((current) => (current === key ? null : current))\n );\n };\n window.addEventListener(\"pointermove\", follow);\n window.addEventListener(\"pointerup\", stop);\n window.addEventListener(\"pointercancel\", stop);\n };\n\n return (\n <div\n className={cn(styles.shell, className)}\n data-slot=\"sortable-grid\"\n data-disabled={disabled || undefined}\n >\n <ul\n ref={(node) => {\n list.current = node;\n sort.container.current = node;\n }}\n className={styles.grid}\n aria-label={label}\n style={\n {\n \"--cipher-sortable-grid-min\": `${minItemWidth}px`\n } as React.CSSProperties\n }\n >\n {sort.preview.map((item, index) => {\n const key = getKey(item);\n const itemDisabled = disabled || Boolean(isItemDisabled?.(item));\n const lifted = sort.lift?.key === key;\n const following = held === key;\n return (\n <motion.li\n key={key}\n layout={following ? false : \"position\"}\n transition={reduced ? snap : spring.moderate}\n className={styles.item}\n data-slot=\"sortable-grid-item\"\n data-dragging={lifted || following || undefined}\n data-following={following || undefined}\n data-disabled={itemDisabled || undefined}\n >\n <motion.div className={styles.card} style={following ? { x, y } : undefined}>\n {renderItem(item, {\n index,\n dragging: lifted,\n disabled: itemDisabled,\n handle: (\n <SortableHandle\n itemKey={key}\n label={getLabel(item)}\n instructions={sort.instructions}\n lifted={lifted && !sort.lift?.pointer}\n disabled={itemDisabled}\n onPointerDown={(event) => {\n if (!sort.lift && !held) startDrag(item, event);\n }}\n onKeyDown={(event) => sort.keyDown(event, item, delta(event.key))}\n onBlur={(event) => sort.blur(event, item)}\n />\n )\n })}\n </motion.div>\n </motion.li>\n );\n })}\n </ul>\n <SortableAnnouncer instructions={sort.instructions} announcement={sort.announcement}>\n Press Space or Enter to pick up, then use the arrow keys to move across and between rows.\n Alt with an arrow key moves one step.\n </SortableAnnouncer>\n </div>\n );\n}\n"
},
{
"path": "src/components/cipher/ui/sortable-grid.module.css",
"type": "registry:file",
"target": "src/components/cipher/ui/sortable-grid.module.css",
"content": ".shell {\n min-width: 0;\n}\n.grid {\n display: grid;\n grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--cipher-sortable-grid-min)), 1fr));\n gap: var(--cipher-space-16);\n margin: 0;\n padding: 0;\n list-style: none;\n}\n.item {\n display: flex;\n min-width: 0;\n flex-direction: column;\n}\n.item > * {\n flex: 1;\n}\n.item[data-dragging] {\n position: relative;\n z-index: 1;\n box-shadow: var(--cipher-shadow-pop);\n}\n"
"content": ".shell {\n min-width: 0;\n}\n.grid {\n display: grid;\n grid-template-columns: repeat(auto-fill, minmax(min(100%, var(--cipher-sortable-grid-min)), 1fr));\n gap: var(--cipher-space-16);\n margin: 0;\n padding: 0;\n list-style: none;\n}\n.item {\n position: relative;\n display: flex;\n min-width: 0;\n flex-direction: column;\n}\n.card {\n display: flex;\n flex: 1;\n min-width: 0;\n flex-direction: column;\n}\n.card > * {\n flex: 1;\n}\n.item[data-dragging] {\n z-index: 1;\n}\n.item[data-dragging]:not([data-following]) > .card,\n.item[data-following] > .card {\n box-shadow: var(--cipher-shadow-pop);\n}\n.item[data-following] {\n z-index: 2;\n}\n.item[data-following]::before {\n content: \"\";\n position: absolute;\n inset: 0;\n border: var(--cipher-size-control-border) dashed var(--cipher-line);\n background: var(--cipher-tint);\n}\n.item[data-following] > .card {\n position: relative;\n cursor: grabbing;\n}\n"
}
]
}
2 changes: 1 addition & 1 deletion site/src/examples/empty-state/ghost.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export default function Example() {
</figcaption>
</figure>
<figure className="m-0 flex w-full min-w-0 flex-col gap-3">
<Card variant="framed">
<Card variant="framed" flush>
<CardHeader>
<CardTitle>App connections</CardTitle>
</CardHeader>
Expand Down
1 change: 1 addition & 0 deletions site/src/examples/sortable-list/card.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export default function Example() {
<div className="flex min-h-8 w-full min-w-0 flex-col items-stretch gap-3">
<Card
variant="framed"
flush
title="Evaluation order"
action={
<Button variant="outline" icon="plus">
Expand Down
27 changes: 25 additions & 2 deletions src/ui/sortable-grid.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,38 @@
list-style: none;
}
.item {
position: relative;
display: flex;
min-width: 0;
flex-direction: column;
}
.card {
display: flex;
flex: 1;
min-width: 0;
flex-direction: column;
}
.item > * {
.card > * {
flex: 1;
}
.item[data-dragging] {
position: relative;
z-index: 1;
}
.item[data-dragging]:not([data-following]) > .card,
.item[data-following] > .card {
box-shadow: var(--cipher-shadow-pop);
}
.item[data-following] {
z-index: 2;
}
.item[data-following]::before {
content: "";
position: absolute;
inset: 0;
border: var(--cipher-size-control-border) dashed var(--cipher-line);
background: var(--cipher-tint);
}
.item[data-following] > .card {
position: relative;
cursor: grabbing;
}
Loading
Loading