Skip to content

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

TypeScript

fix(List): a swizzled copy reads a package reorderable object (#10741)

Christopher Chedeauf2a6332eb6cc
diff --git a/.changeset/core-shared-reorder-key.md b/.changeset/core-shared-reorder-key.md
new file mode 100644
index 0000000000..92bda2ae84
--- /dev/null
+++ b/.changeset/core-shared-reorder-key.md
@@ -0,0 +1,6 @@
+---
+'@astryxdesign/core': patch
+---
+
+[fix] A swizzled `List`, `TransferList` or `ListInput` and the package now agree on a `createReorderable` object: its reorder machinery is read through a registered, versioned key. Before, a swizzled `List` found no machinery on an object from the package and showed no reorder handles. No API changes.
+@vjeux
diff --git a/docs/specs/AST-103-reorderable/spec.md b/docs/specs/AST-103-reorderable/spec.md
index 1ef0f3dcf8..0ba798efcf 100644
--- a/docs/specs/AST-103-reorderable/spec.md
+++ b/docs/specs/AST-103-reorderable/spec.md
@@ -144,7 +144,10 @@ const {detach} = reorderable.attach(listElement, {
during a drag does not cancel the drag. One object passed to two
collections, or attached to two containers, reorders each on its own and
moves nothing between them; its `start` targets the one mounted or attached
- last, with a development warning.
+ last, with a development warning. The object carries its machinery under a
+ registered key, so the package and a swizzled copy agree on it
+ (`spec:AST-110` FR3): a swizzled `List`, `TransferList` or `ListInput`
+ reads an object the package made, and the package reads one a copy adapted.
`options` also takes `labels?: ReorderableLabels`, which
holds every string the object announces or shows
(`architecture:internationalization` INV3a):
@@ -307,6 +310,7 @@ const {detach} = reorderable.attach(listElement, {
| Contract | Verification | Representative states | Mutation or failure expectation |
| ------------------- | ------------------------------------------------------------------------------------------------------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| FR1, FR6–FR8 | Public-API review, type tests, and descriptor checks | the factory's options, `ReorderableLabels`' keys included; a collection's `reorderable` type; the factory's and List's descriptors | an extra gesture, timing, or animation option, an experimental name or subpath, a missing descriptor, a second public reorder API, a string the object announces or shows that no `labels` key replaces, or a translator option, fails |
+| FR1 copies | Unit tests that load a second copy of List's reorder context and of the internal reorder entry | a copied List reading an object the package made; the package List reading an object a copied entry adapted | a copy that reads `undefined` where the package put the machinery, in either direction, fails |
| FR2 | Bundle and runtime check of a collection with and without the prop | without the prop; with the prop | reorder code in the bundle, a handle, or a reorder listener without the prop fails; the measured cost is recorded in the pull request |
| FR3 | Browser tests with real touch input and a mouse (`spec:AST-013` FR10) | a hold and drag; a flick; a drop; a cancelled drag; Reduce Motion; a fanned hand with custom `positions`; a list without them; a `positions` array of the wrong length; a tree with `positions`; custom `positions` beside a rejected end row; a pen drag on a fine and on a coarse primary pointer | a drag that starts off the handle, a commit before the drop, two callbacks for one drag, a callback on cancel, travel under Reduce Motion, a drop that ignores `positions` of the right length, a wrong-length array or a tree whose drops do not come from the measured layout, a drop outside the drop range, a mouse drag, or a pen drag on a fine primary pointer that waits for a hold, or a pen drag on a coarse primary pointer that starts without one, or a call without `positions` whose behavior changes fails |
| FR3 autoscroll | Browser tests with a mouse of a drag held near a scroll edge, and unit tests of the autoscroll step and its timer | a row held near the page's bottom edge, and held there until the list's end is in view; a row held near the bottom of a box the list scrolls in; the step at each depth past the edge; the timer at an edge | a held drag near an edge that scrolls nothing, scrolls the page while a box holds the list, scrolls away from the edge, keeps one speed nearer the edge, or scrolls past the end fails |
diff --git a/packages/core/src/List/listReorderContext.ts b/packages/core/src/List/listReorderContext.ts
index ed37a33d82..c4515e5c2f 100644
--- a/packages/core/src/List/listReorderContext.ts
+++ b/packages/core/src/List/listReorderContext.ts
@@ -4,7 +4,7 @@
/**
* @file listReorderContext.ts
- * @input React's hooks and a symbol
+ * @input React's hooks and a registered key (utils/sharedRealm.ts)
* @output `REORDER_ENGINE`, the key a reorderable object carries its
* machinery under. Also the internal types between List, ListItem and that
* machinery: what a List hands its rows through ListContext's `reorder`.
@@ -31,15 +31,17 @@ import {
type RefObject,
} from 'react';
import {useIsomorphicLayoutEffect} from '../hooks/useIsomorphicLayoutEffect';
+import {sharedKey} from '../utils/sharedRealm';
import type {Reorderable} from '../reorderable/createReorderable';
import type {StyleXStyles} from '@stylexjs/stylex';
import type {reorderListStyle} from './ListReorderHandle';
/**
- * The key a reorderable object carries its machinery under. No description:
- * it would ship in every List.
+ * The key a reorderable object carries its machinery under. `sharedKey`
+ * registers it, so a swizzled List reads a package object, and the package
+ * List reads an object a copy adapted.
*/
-export const REORDER_ENGINE: unique symbol = Symbol();
+export const REORDER_ENGINE = sharedKey('list/reorder-engine', 1);
/** What a List's reorder reports and asks, as createReorderable took them. */
export interface ListReorderOptions {
diff --git a/packages/core/src/utils/sharedBrands.test.ts b/packages/core/src/utils/sharedBrands.test.ts
index 4639265fe6..abed42267b 100644
--- a/packages/core/src/utils/sharedBrands.test.ts
+++ b/packages/core/src/utils/sharedBrands.test.ts
@@ -40,6 +40,41 @@ describe('brands a swizzled copy shares with the package', () => {
expect(copy.isSnapPaging(value)).toBe(true);
});
+ it("a copy of List's reorder context reads a package reorderable object", async () => {
+ const {createReorderable} =
+ await import('../reorderable/createReorderable');
+ const pkg = await import('../List/listReorderContext');
+ vi.resetModules();
+ const copy = await import('../List/listReorderContext');
+ expect(copy).not.toBe(pkg);
+ // A swizzled List carries its own listReorderContext and reads the
+ // object the package's createReorderable made.
+ const reorderable = createReorderable({onReorder: () => {}});
+ const internals = reorderable as unknown as Record<symbol, unknown>;
+ expect(internals[copy.REORDER_ENGINE]).toBeDefined();
+ expect(internals[copy.REORDER_ENGINE]).toBe(internals[pkg.REORDER_ENGINE]);
+ });
+
+ it('the package List reads an object a copy of the Lab reorder entry adapted', async () => {
+ const {createReorderable} =
+ await import('../reorderable/createReorderable');
+ const pkg = await import('../List/listReorderContext');
+ vi.resetModules();
+ const copy = await import('../internal/reorder');
+ // A swizzled TransferList or ListInput carries Core's internal reorder
+ // entry and adapts the owner's object with it, for the package List.
+ const reorderable = createReorderable({onReorder: () => {}});
+ const adapted = copy.adaptReorderable(reorderable, {onReorder: () => {}});
+ expect(
+ (adapted as unknown as Record<symbol, unknown>)[pkg.REORDER_ENGINE],
+ ).toBe(
+ (reorderable as unknown as Record<symbol, unknown>)[pkg.REORDER_ENGINE],
+ );
+ expect(
+ (adapted as unknown as Record<symbol, unknown>)[pkg.REORDER_ENGINE],
+ ).toBeDefined();
+ });
+
it('the package and a copy give the branded types one shape', () => {
// Two paths to Core's source that TypeScript reads as two modules each,
// as it reads a package module and the copy of it.