Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

chore(sandbox): List reorder cases on a page the document scrolls (#10739)

Christopher Chedeau86a32152f4ed
diff --git a/apps/sandbox/scripts/next-export-routes.json b/apps/sandbox/scripts/next-export-routes.json
index 278c39b588..90fd10e0c3 100644
--- a/apps/sandbox/scripts/next-export-routes.json
+++ b/apps/sandbox/scripts/next-export-routes.json
@@ -22,6 +22,7 @@
"/pages/example/",
"/pages/gothic-palette/",
"/pages/layout-dsl/",
+ "/pages/list-reorder-cases/",
"/pages/markdown-perf/",
"/pages/media-mode/",
"/pages/mega-menu/",
diff --git a/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/caseIds.ts b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/caseIds.ts
new file mode 100644
index 0000000000..75b904bdeb
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/caseIds.ts
@@ -0,0 +1,34 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+/**
+ * @file caseIds.ts
+ * @input None
+ * @output The List reorder case ids, their labels, and the parser for `?case=`
+ * @position Shared by the List reorder cases page and its cases.
+ *
+ * SYNC: A new case needs a component in cases.tsx.
+ */
+
+export const LIST_REORDER_CASES = [
+ 'list',
+ 'transfer-list',
+ 'list-in-a-card',
+] as const;
+
+export type ListReorderCaseId = (typeof LIST_REORDER_CASES)[number];
+
+export const LIST_REORDER_CASE_LABELS: Readonly<
+ Record<ListReorderCaseId, string>
+> = {
+ list: "A reorderable List: hold a row's handle, then drop the row in a new place",
+ 'transfer-list':
+ 'A TransferList: reorder the shown fields inside their panel, which clips them',
+ 'list-in-a-card':
+ 'A reorderable List in a Card, which clips it to its rounded corners. Each row also swipes to Archive',
+};
+
+export function parseListReorderCase(value: string | null): ListReorderCaseId {
+ return (LIST_REORDER_CASES as readonly string[]).includes(value ?? '')
+ ? (value as ListReorderCaseId)
+ : LIST_REORDER_CASES[0];
+}
diff --git a/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/cases.tsx b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/cases.tsx
new file mode 100644
index 0000000000..a5f0163174
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/cases.tsx
@@ -0,0 +1,129 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file cases.tsx
+ * @input A case id from caseIds.ts
+ * @output The case: a reorderable List, a TransferList, or a reorderable List
+ * in a Card
+ * @position The List reorder cases page's content: a List's reorder alone and
+ * inside the containers that clip it.
+ */
+
+import {useMemo, useState} from 'react';
+import {createReorderable} from '@astryxdesign/core';
+import {Card} from '@astryxdesign/core/Card';
+import {List, ListItem} from '@astryxdesign/core/List';
+import {TransferList} from '@astryxdesign/lab';
+import type {TransferListOption} from '@astryxdesign/lab';
+import type {ListReorderCaseId} from './caseIds';
+
+const MAILBOXES = [
+ 'Inbox',
+ 'Sent',
+ 'Drafts',
+ 'Archive',
+ 'Spam',
+ 'Trash',
+ 'Receipts',
+ 'Travel',
+];
+
+const FIELDS: ReadonlyArray<TransferListOption<string>> = Array.from(
+ {length: 12},
+ (_, index) => {
+ const label = `Field ${String(index + 1).padStart(3, '0')}`;
+ return {value: label, label};
+ },
+);
+
+/** `items` with `key` moved to index `to`. */
+function moveTo(
+ items: ReadonlyArray<string>,
+ key: string,
+ to: number,
+): string[] {
+ const next = items.filter(item => item !== key);
+ next.splice(to, 0, key);
+ return next;
+}
+
+function ReorderableList({hasSwipeActions}: {hasSwipeActions: boolean}) {
+ const [rows, setRows] = useState<ReadonlyArray<string>>(MAILBOXES);
+ const [archived, setArchived] = useState(0);
+ const reorderable = useMemo(
+ () =>
+ createReorderable({
+ onReorder: ({key, to}) =>
+ setRows(current => moveTo(current, String(key), to)),
+ }),
+ [],
+ );
+ return (
+ <List hasDividers reorderable={reorderable}>
+ {rows.map(label => (
+ <ListItem
+ key={label}
+ itemKey={label}
+ label={label}
+ description={
+ hasSwipeActions ? `Archive swipes: ${archived}` : undefined
+ }
+ swipeActions={
+ hasSwipeActions
+ ? {
+ trailing: [
+ {
+ label: 'Archive',
+ onActivate: () => setArchived(count => count + 1),
+ },
+ ],
+ }
+ : undefined
+ }
+ />
+ ))}
+ </List>
+ );
+}
+
+function TransferListCase() {
+ const [value, setValue] = useState<readonly string[]>(
+ FIELDS.map(field => field.value),
+ );
+ const reorderable = useMemo(
+ () =>
+ createReorderable<string>({
+ onReorder: ({key, to}) => setValue(current => moveTo(current, key, to)),
+ }),
+ [],
+ );
+ return (
+ <TransferList
+ label="Fields"
+ description="Reorder the shown fields."
+ options={FIELDS}
+ value={value}
+ onChange={setValue}
+ reorderable={reorderable}
+ selectedLabel="Shown fields"
+ availableLabel="Hidden fields"
+ />
+ );
+}
+
+export function ListReorderCase({id}: {id: ListReorderCaseId}) {
+ switch (id) {
+ case 'list':
+ return <ReorderableList hasSwipeActions={false} />;
+ case 'transfer-list':
+ return <TransferListCase />;
+ case 'list-in-a-card':
+ return (
+ <Card>
+ <ReorderableList hasSwipeActions />
+ </Card>
+ );
+ }
+}
diff --git a/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/page.tsx b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/page.tsx
new file mode 100644
index 0000000000..4d24708bd8
--- /dev/null
+++ b/apps/sandbox/src/app/(raw)/pages/list-reorder-cases/page.tsx
@@ -0,0 +1,62 @@
+// Copyright (c) Meta Platforms, Inc. and affiliates.
+
+'use client';
+
+/**
+ * @file page.tsx
+ * @input `?case=` (one of the ids in caseIds.ts; the first by default)
+ * @output A bare page with one List reorder case above rows that make the
+ * document scroll
+ * @position A raw page, with no sandbox chrome, for checking the cases on a
+ * phone. The page scrolls with the document, as an app's page does.
+ */
+
+import {Suspense} from 'react';
+import {useSearchParams} from 'react-router-dom';
+import {HStack, VStack} from '@astryxdesign/core/Layout';
+import {Link} from '@astryxdesign/core/Link';
+import {Heading, Text} from '@astryxdesign/core/Text';
+import {ListReorderCase} from './cases';
+import {
+ LIST_REORDER_CASE_LABELS,
+ LIST_REORDER_CASES,
+ parseListReorderCase,
+} from './caseIds';
+
+const ROWS = Array.from({length: 40}, (_, index) => `Row ${index + 1}`);
+
+function ListReorderCases() {
+ const [searchParams] = useSearchParams();
+ const id = parseListReorderCase(searchParams.get('case'));
+ return (
+ <VStack gap={4} padding={4}>
+ <VStack gap={1}>
+ <Heading level={1}>List reorder cases</Heading>
+ <Text color="secondary">{LIST_REORDER_CASE_LABELS[id]}</Text>
+ <HStack gap={3}>
+ {LIST_REORDER_CASES.map(caseId => (
+ <Link key={caseId} href={`?case=${caseId}`}>
+ {caseId}
+ </Link>
+ ))}
+ </HStack>
+ </VStack>
+ <ListReorderCase key={id} id={id} />
+ <VStack gap={2}>
+ {ROWS.map(row => (
+ <Text key={row} color="secondary">
+ {row}
+ </Text>
+ ))}
+ </VStack>
+ </VStack>
+ );
+}
+
+export default function ListReorderCasesPage() {
+ return (
+ <Suspense>
+ <ListReorderCases />
+ </Suspense>
+ );
+}
diff --git a/apps/sandbox/src/app/sandboxPages.ts b/apps/sandbox/src/app/sandboxPages.ts
index 2d63fd5896..367c73c932 100644
--- a/apps/sandbox/src/app/sandboxPages.ts
+++ b/apps/sandbox/src/app/sandboxPages.ts
@@ -105,6 +105,12 @@ export const categories: SandboxCategory[] = [
description:
'An alert over a toast and a dialog closed after a save, on a page that scrolls with the document, to check on a phone',
},
+ {
+ name: 'List Reorder Cases',
+ href: '/pages/list-reorder-cases/',
+ description:
+ 'A reorderable List, a TransferList and a reorderable List in a Card, on a page that scrolls with the document, to check on a phone',
+ },
{
name: 'TextArea Counter',
href: '/pages/textarea-counter/',
diff --git a/apps/sandbox/src/hydration-policy.ts b/apps/sandbox/src/hydration-policy.ts
index 6b4e1e2fe8..91c4df3b5c 100644
--- a/apps/sandbox/src/hydration-policy.ts
+++ b/apps/sandbox/src/hydration-policy.ts
@@ -6,7 +6,7 @@
* @output Whether React can safely hydrate instead of client-rendering.
* @position Prevents mismatches for iframe themes and for pages whose first
* render reads URL controls (Shell Lab, Perf Scenes, Chat Layout,
- * AlertDialog Cases).
+ * AlertDialog Cases, List Reorder Cases).
*/
/** Pages whose first render reads the query string. */
@@ -15,6 +15,7 @@ const QUERY_RENDERED_PAGES = new Set([
'/pages/perf-scenes/',
'/pages/chat-layout/',
'/pages/alert-dialog-cases/',
+ '/pages/list-reorder-cases/',
]);
export function canHydrate(route: string | undefined, search: string): boolean {
diff --git a/apps/sandbox/src/route-manifest.test.ts b/apps/sandbox/src/route-manifest.test.ts
index aec001ca2e..f0203c6d29 100644
--- a/apps/sandbox/src/route-manifest.test.ts
+++ b/apps/sandbox/src/route-manifest.test.ts
@@ -118,6 +118,8 @@ describe('Sandbox static export contract', () => {
canHydrate('/pages/alert-dialog-cases/', '?case=alert-over-a-toast'),
).toBe(false);
expect(canHydrate('/pages/alert-dialog-cases/', '')).toBe(true);
+ expect(canHydrate('/pages/list-reorder-cases/', '?case=list')).toBe(false);
+ expect(canHydrate('/pages/list-reorder-cases/', '')).toBe(true);
});
it('does not turn unknown URLs, dynamic segments or traversal into a 200 fallback', () => {