diff --git a/r/sortable-grid.json b/r/sortable-grid.json index f5f3aae5..32d34ece 100644 --- a/r/sortable-grid.json +++ b/r/sortable-grid.json @@ -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 {\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({\n items,\n getKey,\n onReorder,\n renderItem,\n getLabel,\n label,\n minItemWidth = 240,\n disabled = false,\n isItemDisabled,\n className\n}: SortableGridProps) {\n const reduced = useReducedMotion();\n const sort = useSortable({ items, getKey, getLabel, onReorder });\n const list = React.useRef(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 \n {\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 \n {renderItem(item, {\n index,\n dragging: lifted,\n disabled: itemDisabled,\n handle: (\n {\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 \n );\n })}\n \n \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 \n \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 {\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({\n items,\n getKey,\n onReorder,\n renderItem,\n getLabel,\n label,\n minItemWidth = 240,\n disabled = false,\n isItemDisabled,\n className\n}: SortableGridProps) {\n const reduced = useReducedMotion();\n const sort = useSortable({ items, getKey, getLabel, onReorder });\n const list = React.useRef(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(null);\n const [held, setHeld] = React.useState(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(\"[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 \n {\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 \n \n {renderItem(item, {\n index,\n dragging: lifted,\n disabled: itemDisabled,\n handle: (\n {\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 \n \n );\n })}\n \n \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 \n \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" } ] } diff --git a/site/src/examples/empty-state/ghost.tsx b/site/src/examples/empty-state/ghost.tsx index ac6e3b1d..2c089a22 100644 --- a/site/src/examples/empty-state/ghost.tsx +++ b/site/src/examples/empty-state/ghost.tsx @@ -56,7 +56,7 @@ export default function Example() {
- + App connections diff --git a/site/src/examples/sortable-list/card.tsx b/site/src/examples/sortable-list/card.tsx index 83374da6..f9928b18 100644 --- a/site/src/examples/sortable-list/card.tsx +++ b/site/src/examples/sortable-list/card.tsx @@ -34,6 +34,7 @@ export default function Example() {
diff --git a/src/ui/sortable-grid.module.css b/src/ui/sortable-grid.module.css index c13eba53..22db8c3d 100644 --- a/src/ui/sortable-grid.module.css +++ b/src/ui/sortable-grid.module.css @@ -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; +} diff --git a/src/ui/sortable-grid.tsx b/src/ui/sortable-grid.tsx index 07c8b44e..82183579 100644 --- a/src/ui/sortable-grid.tsx +++ b/src/ui/sortable-grid.tsx @@ -1,5 +1,5 @@ import * as React from "react"; -import { motion, useReducedMotion } from "motion/react"; +import { animate, motion, useMotionValue, useReducedMotion } from "motion/react"; import { snap, spring } from "../lib/motion"; import { useSortable } from "../lib/sortable"; @@ -34,6 +34,15 @@ export interface SortableGridProps { className?: string; } +type Box = { left: number; top: number; width: number; height: number }; +type Drag = { + slots: Box[]; + grab: { x: number; y: number }; + pointer: { x: number; y: number }; +}; + +const center = (box: Box) => ({ x: box.left + box.width / 2, y: box.top + box.height / 2 }); + const columnsOf = (container: HTMLElement | null) => { const items = Array.from(container?.children ?? []) as HTMLElement[]; const top = items[0]?.offsetTop; @@ -65,24 +74,72 @@ export function SortableGrid({ if (key === "ArrowDown") return columns; return 0; }; + const x = useMotionValue(0); + const y = useMotionValue(0); + const drag = React.useRef(null); + const [held, setHeld] = React.useState(null); + + const origin = () => list.current?.getBoundingClientRect() ?? { left: 0, top: 0 }; + const place = () => { + const current = drag.current; + const element = list.current?.querySelector("[data-following]"); + if (!current || !element) return; + const box = element.getBoundingClientRect(); + x.set(current.pointer.x - current.grab.x - box.left); + y.set(current.pointer.y - current.grab.y - box.top); + }; const follow = (event: PointerEvent) => { - const target = Array.from(list.current?.children ?? []).findIndex((element) => { - const box = element.getBoundingClientRect(); - return ( - event.clientX >= box.left && - event.clientX <= box.right && - event.clientY >= box.top && - event.clientY <= box.bottom - ); + const current = drag.current; + const lift = sort.liftRef.current; + if (!current || !lift) return; + current.pointer = { x: event.clientX, y: event.clientY }; + const { left, top } = origin(); + const from = current.slots[lift.from]!; + const card = { + x: event.clientX - left - current.grab.x + from.width / 2, + y: event.clientY - top - current.grab.y + from.height / 2 + }; + const distance = (box: Box) => Math.hypot(center(box).x - card.x, center(box).y - card.y); + let best = lift.to; + current.slots.forEach((slot, index) => { + if (distance(slot) < distance(current.slots[best]!)) best = index; }); - if (target >= 0) sort.moveTo(target); + const slot = current.slots[lift.to]!; + const hysteresis = Math.min(slot.width, slot.height) * 0.25; + if (best !== lift.to && distance(current.slots[best]!) < distance(slot) - hysteresis) + sort.moveTo(best); + else place(); }; - const startDrag = (item: T) => { + React.useLayoutEffect(place, [sort.lift?.to]); + const startDrag = (item: T, event: React.PointerEvent) => { + const container = list.current; + if (!container) return; + const { left, top } = container.getBoundingClientRect(); + const slots = Array.from(container.children).map((element) => { + const box = element.getBoundingClientRect(); + return { left: box.left - left, top: box.top - top, width: box.width, height: box.height }; + }); + const from = slots[items.indexOf(item)]; + if (!from) return; + const key = getKey(item); + drag.current = { + slots, + grab: { x: event.clientX - left - from.left, y: event.clientY - top - from.top }, + pointer: { x: event.clientX, y: event.clientY } + }; + x.set(0); + y.set(0); + setHeld(key); sort.pick(item, true); const stop = () => { window.removeEventListener("pointermove", follow); window.removeEventListener("pointerup", stop); window.removeEventListener("pointercancel", stop); + drag.current = null; + const settle = reduced ? snap : spring.moderate; + Promise.all([animate(x, 0, settle), animate(y, 0, settle)]).then(() => + setHeld((current) => (current === key ? null : current)) + ); }; window.addEventListener("pointermove", follow); window.addEventListener("pointerup", stop); @@ -112,35 +169,39 @@ export function SortableGrid({ const key = getKey(item); const itemDisabled = disabled || Boolean(isItemDisabled?.(item)); const lifted = sort.lift?.key === key; + const following = held === key; return ( - {renderItem(item, { - index, - dragging: lifted, - disabled: itemDisabled, - handle: ( - { - if (!sort.lift) startDrag(item); - }} - onKeyDown={(event) => sort.keyDown(event, item, delta(event.key))} - onBlur={(event) => sort.blur(event, item)} - /> - ) - })} + + {renderItem(item, { + index, + dragging: lifted, + disabled: itemDisabled, + handle: ( + { + if (!sort.lift && !held) startDrag(item, event); + }} + onKeyDown={(event) => sort.keyDown(event, item, delta(event.key))} + onBlur={(event) => sort.blur(event, item)} + /> + ) + })} + ); })} diff --git a/test-results-r1-mid.png b/test-results-r1-mid.png new file mode 100644 index 00000000..c033af46 Binary files /dev/null and b/test-results-r1-mid.png differ diff --git a/visual/baselines/desktop/empty-state-ghost--dark.png b/visual/baselines/desktop/empty-state-ghost--dark.png index 23cefe4c..6568b96e 100644 Binary files a/visual/baselines/desktop/empty-state-ghost--dark.png and b/visual/baselines/desktop/empty-state-ghost--dark.png differ diff --git a/visual/baselines/desktop/empty-state-ghost--light.png b/visual/baselines/desktop/empty-state-ghost--light.png index 1510ef72..14a57963 100644 Binary files a/visual/baselines/desktop/empty-state-ghost--light.png and b/visual/baselines/desktop/empty-state-ghost--light.png differ diff --git a/visual/baselines/desktop/sortable-list-card--dark.png b/visual/baselines/desktop/sortable-list-card--dark.png index 73b47f33..67771eb9 100644 Binary files a/visual/baselines/desktop/sortable-list-card--dark.png and b/visual/baselines/desktop/sortable-list-card--dark.png differ diff --git a/visual/baselines/desktop/sortable-list-card--light.png b/visual/baselines/desktop/sortable-list-card--light.png index b950310f..1015751c 100644 Binary files a/visual/baselines/desktop/sortable-list-card--light.png and b/visual/baselines/desktop/sortable-list-card--light.png differ diff --git a/visual/baselines/mobile/empty-state-ghost--dark.png b/visual/baselines/mobile/empty-state-ghost--dark.png index e1a19159..48c2618d 100644 Binary files a/visual/baselines/mobile/empty-state-ghost--dark.png and b/visual/baselines/mobile/empty-state-ghost--dark.png differ diff --git a/visual/baselines/mobile/empty-state-ghost--light.png b/visual/baselines/mobile/empty-state-ghost--light.png index fabef33c..306deeff 100644 Binary files a/visual/baselines/mobile/empty-state-ghost--light.png and b/visual/baselines/mobile/empty-state-ghost--light.png differ diff --git a/visual/baselines/mobile/sortable-list-card--dark.png b/visual/baselines/mobile/sortable-list-card--dark.png index e4661918..7a43be23 100644 Binary files a/visual/baselines/mobile/sortable-list-card--dark.png and b/visual/baselines/mobile/sortable-list-card--dark.png differ diff --git a/visual/baselines/mobile/sortable-list-card--light.png b/visual/baselines/mobile/sortable-list-card--light.png index 80d0d556..5aadec5d 100644 Binary files a/visual/baselines/mobile/sortable-list-card--light.png and b/visual/baselines/mobile/sortable-list-card--light.png differ