newspack-components 4.2.0 → 4.3.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.
Files changed (131) hide show
  1. package/dist/cjs/action-card/index.js +21 -12
  2. package/dist/cjs/action-card/style.scss +25 -2
  3. package/dist/cjs/autocomplete-tokenfield/index.js +2 -2
  4. package/dist/cjs/autocomplete-with-latest-posts/index.js +289 -0
  5. package/dist/cjs/autocomplete-with-latest-posts/style.scss +150 -0
  6. package/dist/cjs/badge/index.js +32 -0
  7. package/dist/cjs/badge/style.scss +36 -0
  8. package/dist/cjs/button/index.js +2 -0
  9. package/dist/cjs/card/core-card.js +141 -0
  10. package/dist/cjs/card/index.js +33 -0
  11. package/dist/cjs/card/style-core.scss +227 -0
  12. package/dist/cjs/card/style.scss +2 -2
  13. package/dist/cjs/card-settings-group/index.js +49 -0
  14. package/dist/cjs/card-settings-group/style.scss +33 -0
  15. package/dist/cjs/card-sortable-list/index.js +390 -0
  16. package/dist/cjs/card-sortable-list/style.scss +48 -0
  17. package/dist/cjs/confirm-dialog/index.js +59 -0
  18. package/dist/cjs/divider/index.js +42 -0
  19. package/dist/cjs/divider/style.scss +51 -0
  20. package/dist/cjs/form-token-field/style.scss +0 -5
  21. package/dist/cjs/grid/index.js +2 -1
  22. package/dist/cjs/grid/style.scss +63 -9
  23. package/dist/cjs/index.js +42 -0
  24. package/dist/cjs/modal/index.js +11 -4
  25. package/dist/cjs/modal/style.scss +43 -13
  26. package/dist/cjs/newspack-icon/style.scss +5 -3
  27. package/dist/cjs/section-header/index.js +76 -17
  28. package/dist/cjs/section-header/style.scss +109 -4
  29. package/dist/cjs/style.scss +2 -0
  30. package/dist/cjs/waiting/index.js +3 -1
  31. package/dist/cjs/waiting/style.scss +4 -0
  32. package/dist/cjs/web-preview/index.js +98 -39
  33. package/dist/cjs/web-preview/style.scss +20 -53
  34. package/dist/cjs/with-wizard/index.js +1 -1
  35. package/dist/cjs/with-wizard/style.scss +24 -6
  36. package/dist/cjs/with-wizard-screen/style.scss +39 -10
  37. package/dist/cjs/wizard/components/WizardSnackbar.js +70 -0
  38. package/dist/cjs/wizard/components/style.scss +48 -0
  39. package/dist/cjs/wizard/index.js +97 -16
  40. package/dist/cjs/wizard/store/index.js +46 -0
  41. package/dist/esm/action-card/index.js +21 -12
  42. package/dist/esm/action-card/style.scss +25 -2
  43. package/dist/esm/autocomplete-tokenfield/index.js +2 -2
  44. package/dist/esm/autocomplete-with-latest-posts/index.js +281 -0
  45. package/dist/esm/autocomplete-with-latest-posts/style.scss +150 -0
  46. package/dist/esm/badge/index.js +24 -0
  47. package/dist/esm/badge/style.scss +36 -0
  48. package/dist/esm/button/index.js +2 -0
  49. package/dist/esm/card/core-card.js +133 -0
  50. package/dist/esm/card/index.js +33 -0
  51. package/dist/esm/card/style-core.scss +227 -0
  52. package/dist/esm/card/style.scss +2 -2
  53. package/dist/esm/card-settings-group/index.js +42 -0
  54. package/dist/esm/card-settings-group/style.scss +33 -0
  55. package/dist/esm/card-sortable-list/index.js +381 -0
  56. package/dist/esm/card-sortable-list/style.scss +48 -0
  57. package/dist/esm/confirm-dialog/index.js +49 -0
  58. package/dist/esm/divider/index.js +34 -0
  59. package/dist/esm/divider/style.scss +51 -0
  60. package/dist/esm/form-token-field/style.scss +0 -5
  61. package/dist/esm/grid/index.js +2 -1
  62. package/dist/esm/grid/style.scss +63 -9
  63. package/dist/esm/index.js +6 -0
  64. package/dist/esm/modal/index.js +11 -4
  65. package/dist/esm/modal/style.scss +43 -13
  66. package/dist/esm/newspack-icon/style.scss +5 -3
  67. package/dist/esm/section-header/index.js +75 -18
  68. package/dist/esm/section-header/style.scss +109 -4
  69. package/dist/esm/style.scss +2 -0
  70. package/dist/esm/waiting/index.js +3 -1
  71. package/dist/esm/waiting/style.scss +4 -0
  72. package/dist/esm/web-preview/index.js +101 -42
  73. package/dist/esm/web-preview/style.scss +20 -53
  74. package/dist/esm/with-wizard/index.js +1 -1
  75. package/dist/esm/with-wizard/style.scss +24 -6
  76. package/dist/esm/with-wizard-screen/style.scss +39 -10
  77. package/dist/esm/wizard/components/WizardSnackbar.js +64 -0
  78. package/dist/esm/wizard/components/style.scss +48 -0
  79. package/dist/esm/wizard/index.js +101 -20
  80. package/dist/esm/wizard/store/index.js +46 -0
  81. package/package.json +5 -6
  82. package/shared/author-context.js +26 -0
  83. package/shared/hooks/use-coauthors.js +220 -0
  84. package/shared/hooks/use-coauthors.test.js +624 -0
  85. package/shared/hooks/use-custom-byline.js +47 -0
  86. package/shared/hooks/use-custom-byline.test.js +133 -0
  87. package/shared/scss/_mixins.scss +46 -0
  88. package/src/action-card/action-card.d.ts +7 -3
  89. package/src/action-card/index.js +24 -11
  90. package/src/action-card/style.scss +25 -2
  91. package/src/autocomplete-tokenfield/index.js +2 -2
  92. package/src/autocomplete-with-latest-posts/index.js +290 -0
  93. package/src/autocomplete-with-latest-posts/style.scss +150 -0
  94. package/src/badge/index.tsx +24 -0
  95. package/src/badge/style.scss +36 -0
  96. package/src/button/index.tsx +3 -2
  97. package/src/card/core-card.js +147 -0
  98. package/src/card/index.js +39 -1
  99. package/src/card/style-core.scss +227 -0
  100. package/src/card/style.scss +2 -2
  101. package/src/card-settings-group/README.md +43 -0
  102. package/src/card-settings-group/index.tsx +53 -0
  103. package/src/card-settings-group/style.scss +33 -0
  104. package/src/card-sortable-list/README.md +51 -0
  105. package/src/card-sortable-list/index.tsx +412 -0
  106. package/src/card-sortable-list/style.scss +48 -0
  107. package/src/confirm-dialog/index.tsx +64 -0
  108. package/src/divider/README.md +25 -0
  109. package/src/divider/index.js +31 -0
  110. package/src/divider/style.scss +51 -0
  111. package/src/form-token-field/style.scss +0 -5
  112. package/src/grid/index.tsx +2 -1
  113. package/src/grid/style.scss +63 -9
  114. package/src/index.js +6 -0
  115. package/src/modal/index.js +12 -3
  116. package/src/modal/style.scss +43 -13
  117. package/src/newspack-icon/style.scss +5 -3
  118. package/src/section-header/index.js +72 -13
  119. package/src/section-header/style.scss +109 -4
  120. package/src/style.scss +2 -0
  121. package/src/waiting/index.js +2 -1
  122. package/src/waiting/style.scss +4 -0
  123. package/src/web-preview/index.js +90 -35
  124. package/src/web-preview/style.scss +20 -53
  125. package/src/with-wizard/index.js +1 -1
  126. package/src/with-wizard/style.scss +24 -6
  127. package/src/with-wizard-screen/style.scss +39 -10
  128. package/src/wizard/components/WizardSnackbar.js +58 -0
  129. package/src/wizard/components/style.scss +48 -0
  130. package/src/wizard/index.js +108 -14
  131. package/src/wizard/store/index.js +26 -0
@@ -0,0 +1,43 @@
1
+ # CardSettingsGroup
2
+
3
+ A collapsible settings card that renders a title, optional description, and optional icon in the header. Children are rendered only when the card is active.
4
+
5
+ ## Props
6
+
7
+ | Prop | Type | Default | Description |
8
+ |------|------|---------|-------------|
9
+ | `actionType` | `'chevron'` \| `'toggle'` \| `'button'` \| `'link'` \| `'none'` | `'chevron'` | Controls the interactive element rendered in the card header. |
10
+ | `children` | `React.ReactNode` | — | Content displayed inside the card body. Only rendered when `isActive` is `true`. |
11
+ | `description` | `string` | `''` | Optional description rendered below the title in the card header. |
12
+ | `icon` | `React.ReactNode` | `null` | Optional icon rendered in the card header with a colored background. |
13
+ | `isActive` | `boolean` | `false` | When `true`, the card is expanded and children are visible. |
14
+ | `onEnable` | `() => void` | `() => {}` | Callback fired when the card header is clicked. Typically used to toggle `isActive`. |
15
+ | `title` | `string` | — | (**Required**) Title rendered in the card header. |
16
+
17
+ ## Usage
18
+
19
+ ```jsx
20
+ import { CardSettingsGroup } from 'newspack-components';
21
+ import { megaphone } from '@wordpress/icons';
22
+
23
+ // Basic collapsible settings group
24
+ <CardSettingsGroup
25
+ title="Email Notifications"
26
+ description="Configure how readers are notified."
27
+ isActive={ isEnabled }
28
+ onEnable={ () => setIsEnabled( ! isEnabled ) }
29
+ >
30
+ <p>Settings content shown when active.</p>
31
+ </CardSettingsGroup>
32
+
33
+ // With an icon and toggle action type
34
+ <CardSettingsGroup
35
+ actionType="toggle"
36
+ icon={ megaphone }
37
+ title="Push Notifications"
38
+ isActive={ isEnabled }
39
+ onEnable={ () => setIsEnabled( ! isEnabled ) }
40
+ >
41
+ <p>Settings content shown when active.</p>
42
+ </CardSettingsGroup>
43
+ ```
@@ -0,0 +1,53 @@
1
+ /**
2
+ * Card - Settings group component.
3
+ */
4
+
5
+ /**
6
+ * Internal dependencies
7
+ */
8
+ import { Card } from '../';
9
+ import './style.scss';
10
+
11
+ const CardSettingsGroup = ( {
12
+ actionType = 'chevron',
13
+ children,
14
+ icon = null,
15
+ title = '',
16
+ description = '',
17
+ isActive = false,
18
+ onEnable = () => {},
19
+ }: {
20
+ actionType?: 'chevron' | 'toggle' | 'button' | 'link' | 'none';
21
+ children?: React.ReactNode;
22
+ icon?: React.ReactNode;
23
+ title: string;
24
+ description?: string;
25
+ isActive?: boolean;
26
+ onEnable?: () => void;
27
+ } ) => {
28
+ return (
29
+ <Card
30
+ className="newspack-card--core--settings-group"
31
+ actionType={ actionType }
32
+ isSmall
33
+ __experimentalCoreCard
34
+ __experimentalCoreProps={ {
35
+ header: (
36
+ <>
37
+ <h3>{ title }</h3>
38
+ { description && <p>{ description }</p> }
39
+ </>
40
+ ),
41
+ onHeaderClick: onEnable,
42
+ icon,
43
+ iconBackgroundColor: true,
44
+ isActive,
45
+ title,
46
+ } }
47
+ >
48
+ { isActive && children }
49
+ </Card>
50
+ );
51
+ };
52
+
53
+ export default CardSettingsGroup;
@@ -0,0 +1,33 @@
1
+ .newspack-card--core--settings-group {
2
+ .components-card__body {
3
+ > .newspack-notice {
4
+ margin: 16px 0;
5
+ }
6
+ > .components-base-control {
7
+ &:first-child {
8
+ margin-top: 0;
9
+ }
10
+ + .components-base-control,
11
+ + .newspack-form-token-field {
12
+ margin-top: 16px;
13
+ }
14
+ }
15
+
16
+ /** ToggleControl switch should appear after the label. */
17
+ > .components-toggle-control {
18
+ .components-h-stack {
19
+ .components-form-toggle {
20
+ order: 2;
21
+ }
22
+ }
23
+ .components-toggle-control__help {
24
+ margin-inline-start: auto;
25
+ }
26
+ }
27
+ .newspack-grid:last-child,
28
+ .components-base-control:last-child,
29
+ p:last-child {
30
+ margin-bottom: 0;
31
+ }
32
+ }
33
+ }
@@ -0,0 +1,51 @@
1
+ # CardSortableList
2
+
3
+ A drag-and-drop sortable list of cards. Each item displays a title and a status badge. Items can be reordered by dragging with a pointer or by using the up/down chevron buttons (keyboard accessible). The list locks its height during a drag to prevent layout shifts.
4
+
5
+ ## Props
6
+
7
+ | Prop | Type | Default | Description |
8
+ |------|------|---------|-------------|
9
+ | `disabled` | `boolean` | `false` | When `true`, all drag and button controls are disabled via `Disabled`. |
10
+ | `items` | `DraggableItem[]` | `[]` | Array of items to render. See the `DraggableItem` shape below. |
11
+ | `onDragCallback` | `(index: number, targetIndex: number) => void` | `() => {}` | Callback fired after a reorder completes (drag-and-drop or button move). Receives the original index and the new index. |
12
+
13
+ ### `DraggableItem`
14
+
15
+ | Field | Type | Description |
16
+ |-------|------|-------------|
17
+ | `id` | `string` \| `number` | Unique identifier for the item. Used as the React `key`. |
18
+ | `title` | `string` | Label rendered in the card header. |
19
+ | `badgeLevel` | `'default'` \| `'success'` \| `'info'` \| `'warning'` \| `'error'` | Visual style of the status badge. |
20
+ | `badgeText` | `string` | Text displayed inside the badge. |
21
+
22
+ ## Usage
23
+
24
+ ```jsx
25
+ import { CardSortableList } from 'newspack-components';
26
+
27
+ const [ items, setItems ] = useState( [
28
+ { id: 1, title: 'Homepage', badgeLevel: 'success', badgeText: 'Active' },
29
+ { id: 2, title: 'About', badgeLevel: 'default', badgeText: 'Draft' },
30
+ { id: 3, title: 'Contact', badgeLevel: 'warning', badgeText: 'Review' },
31
+ ] );
32
+
33
+ const handleReorder = ( fromIndex, toIndex ) => {
34
+ const reordered = [ ...items ];
35
+ const [ moved ] = reordered.splice( fromIndex, 1 );
36
+ reordered.splice( toIndex, 0, moved );
37
+ setItems( reordered );
38
+ };
39
+
40
+ <CardSortableList
41
+ items={ items }
42
+ onDragCallback={ handleReorder }
43
+ />
44
+
45
+ // Disabled state (controls are rendered but non-interactive)
46
+ <CardSortableList
47
+ items={ items }
48
+ disabled={ true }
49
+ onDragCallback={ handleReorder }
50
+ />
51
+ ```
@@ -0,0 +1,412 @@
1
+ /**
2
+ * Card - Sortable list component.
3
+ */
4
+
5
+ /**
6
+ * WordPress dependencies.
7
+ */
8
+ import { Disabled, Draggable, __experimentalVStack as VStack } from '@wordpress/components'; // eslint-disable-line @wordpress/no-unsafe-wp-apis
9
+ import { useEffect, useLayoutEffect, useRef, useState } from '@wordpress/element';
10
+
11
+ /**
12
+ * Internal dependencies
13
+ */
14
+ import { Badge, Card } from '../';
15
+ import './style.scss';
16
+
17
+ /**
18
+ * External dependencies
19
+ */
20
+ import classNames from 'classnames';
21
+
22
+ const DROP_ANIMATION_DURATION = 400; // ms — must match $drop-duration in style.scss
23
+ const BUTTON_MOVE_DURATION = 200; // ms — must match $shift-duration in style.scss
24
+
25
+ type DraggableItem = {
26
+ id: string | number;
27
+ title: string;
28
+ badgeLevel: 'default' | 'success' | 'info' | 'warning' | 'error';
29
+ badgeText: string;
30
+ };
31
+
32
+ type DragMeasurements = {
33
+ lockedHeight: number;
34
+ sourceHeight: number;
35
+ // Height of each item (including the VStack gap after it, except the last).
36
+ itemStrides: number[];
37
+ // Top edge of each item, used for drop position hit-testing in onDragEnd.
38
+ itemTops: number[];
39
+ };
40
+
41
+ const CardSortableList = ( {
42
+ disabled = false,
43
+ items = [],
44
+ onDragCallback = () => {},
45
+ }: {
46
+ disabled?: boolean;
47
+ items?: DraggableItem[];
48
+ onDragCallback?: ( index: number, targetIndex: number ) => void;
49
+ } ) => {
50
+ const [ sortedItems, setSortedItems ] = useState( items );
51
+ const [ draggingIndex, setDraggingIndex ] = useState< number | null >( null );
52
+ const [ hoverIndex, setHoverIndex ] = useState< number | null >( null );
53
+ const [ droppedIndex, setDroppedIndex ] = useState< number | null >( null );
54
+ const [ measurements, setMeasurements ] = useState< DragMeasurements | null >( null );
55
+ const listRef = useRef< HTMLDivElement | null >( null );
56
+ const itemRefs = useRef< ( HTMLDivElement | null )[] >( [] );
57
+ const dropAnimationTimer = useRef< ReturnType< typeof setTimeout > | null >( null );
58
+ const buttonMoveTimer = useRef< ReturnType< typeof setTimeout > | null >( null );
59
+ const buttonFlipRef = useRef< {
60
+ fromIndex: number;
61
+ toIndex: number;
62
+ stride: number;
63
+ direction: number;
64
+ } | null >( null );
65
+ const [ buttonMoveId, setButtonMoveId ] = useState( 0 );
66
+ const documentDragOverRef = useRef< ( ( e: Event ) => void ) | null >( null );
67
+
68
+ // Keep sortedItems in sync when the items prop changes externally (e.g. after a save).
69
+ useEffect( () => {
70
+ setSortedItems( items );
71
+ }, [ items ] );
72
+
73
+ // Clean up any pending animation timer on unmount.
74
+ useEffect( () => {
75
+ return () => {
76
+ if ( dropAnimationTimer.current ) {
77
+ clearTimeout( dropAnimationTimer.current );
78
+ }
79
+ if ( buttonMoveTimer.current ) {
80
+ clearTimeout( buttonMoveTimer.current );
81
+ }
82
+ if ( documentDragOverRef.current ) {
83
+ document.removeEventListener( 'dragover', documentDragOverRef.current );
84
+ }
85
+ };
86
+ }, [] );
87
+
88
+ /**
89
+ * Handle a chevron-button move. Records item positions before the reorder so
90
+ * the FLIP animation can play after React commits the new DOM order.
91
+ */
92
+ const handleButtonMove = ( fromIndex: number, toIndex: number ) => {
93
+ if ( toIndex < 0 || toIndex >= sortedItems.length ) {
94
+ return;
95
+ }
96
+
97
+ const fromEl = itemRefs.current[ fromIndex ];
98
+ const toEl = itemRefs.current[ toIndex ];
99
+ if ( ! fromEl || ! toEl ) {
100
+ return;
101
+ }
102
+
103
+ const stride = Math.abs( toEl.getBoundingClientRect().top - fromEl.getBoundingClientRect().top );
104
+ const direction = toIndex > fromIndex ? 1 : -1;
105
+ buttonFlipRef.current = { fromIndex, toIndex, stride, direction };
106
+
107
+ const reordered = [ ...sortedItems ];
108
+ const [ moved ] = reordered.splice( fromIndex, 1 );
109
+ reordered.splice( toIndex, 0, moved );
110
+ setSortedItems( reordered );
111
+ setButtonMoveId( id => id + 1 );
112
+ };
113
+
114
+ /**
115
+ * FLIP Invert+Play step: runs synchronously after React commits the reordered
116
+ * DOM. Animate each swapped item from its previous position to its new one
117
+ * using inline CSS transitions (no CSS classes, to avoid creating a new
118
+ * containing block that would break position:fixed on the Draggable clone).
119
+ */
120
+ useLayoutEffect( () => {
121
+ const flipData = buttonFlipRef.current;
122
+ if ( ! flipData ) {
123
+ return;
124
+ }
125
+ buttonFlipRef.current = null;
126
+
127
+ const { fromIndex, toIndex, stride, direction } = flipData;
128
+
129
+ const startAnimation = ( el: HTMLDivElement | null, startY: number ) => {
130
+ if ( ! el ) {
131
+ return;
132
+ }
133
+ // Snap to the starting position with no transition.
134
+ el.style.transition = 'none';
135
+ el.style.transform = `translateY(${ startY }px)`;
136
+ // Force a reflow so the browser registers the starting position
137
+ // before we switch to the animated transition.
138
+ el.getBoundingClientRect();
139
+ // Animate from startY back to 0 (its new natural position).
140
+ el.style.transition = `transform ${ BUTTON_MOVE_DURATION }ms ease-out`;
141
+ el.style.transform = '';
142
+ };
143
+
144
+ // Moved item is now at toIndex: animate it in from its old position.
145
+ startAnimation( itemRefs.current[ toIndex ], -direction * stride );
146
+ // Displaced item is now at fromIndex: animate it in from its old position.
147
+ startAnimation( itemRefs.current[ fromIndex ], direction * stride );
148
+
149
+ if ( buttonMoveTimer.current ) {
150
+ clearTimeout( buttonMoveTimer.current );
151
+ }
152
+ buttonMoveTimer.current = setTimeout( () => {
153
+ // Clear inline animation styles before calling the external callback so
154
+ // the subsequent React re-render doesn't fight a live animation.
155
+ [ fromIndex, toIndex ].forEach( i => {
156
+ const el = itemRefs.current[ i ];
157
+ if ( el ) {
158
+ el.style.transition = '';
159
+ el.style.transform = '';
160
+ }
161
+ } );
162
+
163
+ // Move focus to the matching chevron button on the card at toIndex.
164
+ // Focus before onDragCallback so a parent rerender can't steal focus.
165
+ const targetEl = itemRefs.current[ toIndex ];
166
+ if ( targetEl ) {
167
+ const moveButtons = targetEl.querySelectorAll< HTMLButtonElement >(
168
+ '.newspack-card--core__header__draggable-controls__move-buttons button'
169
+ );
170
+ // direction > 0 = moved down → prefer the down button (index 1).
171
+ // direction < 0 = moved up → prefer the up button (index 0).
172
+ const preferred = direction > 0 ? moveButtons[ 1 ] : moveButtons[ 0 ];
173
+ const fallback = direction > 0 ? moveButtons[ 0 ] : moveButtons[ 1 ];
174
+ if ( preferred && ! preferred.disabled ) {
175
+ preferred.focus();
176
+ } else if ( fallback && ! fallback.disabled ) {
177
+ fallback.focus();
178
+ }
179
+ }
180
+
181
+ onDragCallback( fromIndex, toIndex );
182
+ buttonMoveTimer.current = null;
183
+ }, BUTTON_MOVE_DURATION );
184
+ }, [ buttonMoveId ] ); // eslint-disable-line react-hooks/exhaustive-deps
185
+
186
+ const handleDragStart = ( index: number ) => {
187
+ // Cancel any in-progress button-move animation so no item wrapper keeps
188
+ // an inline transform. A transformed ancestor breaks position:fixed on
189
+ // the Draggable clone (CSS spec: fixed positioning is relative to the
190
+ // nearest ancestor with a transform/filter/perspective).
191
+ if ( buttonMoveTimer.current ) {
192
+ clearTimeout( buttonMoveTimer.current );
193
+ buttonMoveTimer.current = null;
194
+ itemRefs.current.forEach( el => {
195
+ if ( el ) {
196
+ el.style.transition = '';
197
+ el.style.transform = '';
198
+ }
199
+ } );
200
+ }
201
+
202
+ const listEl = listRef.current;
203
+ if ( listEl ) {
204
+ const itemEls = itemRefs.current;
205
+ const rects = itemEls.map( el => el?.getBoundingClientRect() );
206
+
207
+ // Stride = item height + gap to next item (difference between consecutive tops).
208
+ const itemStrides = rects.map( ( rect, i ) => {
209
+ if ( ! rect ) {
210
+ return 0;
211
+ }
212
+ const nextRect = rects[ i + 1 ];
213
+ return nextRect ? nextRect.top - rect.top : rect.height;
214
+ } );
215
+
216
+ setMeasurements( {
217
+ lockedHeight: listEl.getBoundingClientRect().height,
218
+ sourceHeight: rects[ index ]?.height ?? 0,
219
+ itemStrides,
220
+ itemTops: rects.map( rect => rect?.top ?? 0 ),
221
+ } );
222
+ }
223
+ // Make the entire document a valid drop target so the browser skips
224
+ // its snap-back animation when the cursor is released outside the list.
225
+ const preventSnapback = ( e: Event ) => e.preventDefault();
226
+ document.addEventListener( 'dragover', preventSnapback );
227
+ documentDragOverRef.current = preventSnapback;
228
+
229
+ setDraggingIndex( index );
230
+ setDroppedIndex( null );
231
+ };
232
+
233
+ const clearDragState = () => {
234
+ if ( documentDragOverRef.current ) {
235
+ document.removeEventListener( 'dragover', documentDragOverRef.current );
236
+ documentDragOverRef.current = null;
237
+ }
238
+ setDraggingIndex( null );
239
+ setHoverIndex( null );
240
+ setMeasurements( null );
241
+ };
242
+
243
+ /**
244
+ * Determine the drop target index from cursor coordinates and the item
245
+ * top positions snapshotted at drag start. Mirrors the midpoint logic
246
+ * previously used in getDropIndex but works without a live event target.
247
+ */
248
+ const getDropIndexFromCursor = ( clientY: number, m: DragMeasurements, sourceIndex: number ): number => {
249
+ const { itemTops, itemStrides, sourceHeight } = m;
250
+ for ( let i = 0; i < itemTops.length; i++ ) {
251
+ const movingDown = sourceIndex > i;
252
+ const midpoint = itemTops[ i ] + ( movingDown ? itemStrides[ i ] : itemStrides[ i ] - sourceHeight );
253
+ if ( clientY < midpoint ) {
254
+ return i;
255
+ }
256
+ }
257
+ return itemTops.length; // below all items
258
+ };
259
+
260
+ const handleDragEnd = ( event: DragEvent ) => {
261
+ // Take a local copy of measurements before clearDragState nulls it.
262
+ const m = measurements;
263
+ const sourceIndex = draggingIndex;
264
+
265
+ clearDragState();
266
+
267
+ if ( m === null || sourceIndex === null ) {
268
+ return;
269
+ }
270
+
271
+ const dropIndex = getDropIndexFromCursor( event.clientY, m, sourceIndex );
272
+
273
+ // Compute the destination index in the post-removal array.
274
+ // After splicing out the dragged item, indices above it shift down by one.
275
+ const insertIndex = dropIndex > sourceIndex ? dropIndex - 1 : dropIndex;
276
+ if ( insertIndex === sourceIndex ) {
277
+ return;
278
+ }
279
+
280
+ const reordered = [ ...sortedItems ];
281
+ const [ moved ] = reordered.splice( sourceIndex, 1 );
282
+ reordered.splice( insertIndex, 0, moved );
283
+
284
+ // First render: show items in their new order with no animation.
285
+ // The dragging CSS classes are already cleared by clearDragState() above,
286
+ // so no transitions will fire on this paint.
287
+ setSortedItems( reordered );
288
+
289
+ // Next frame: trigger the drop animation on the newly-positioned item,
290
+ // then call onDragCallback after the animation completes.
291
+ requestAnimationFrame( () => {
292
+ setDroppedIndex( insertIndex );
293
+ dropAnimationTimer.current = setTimeout( () => {
294
+ setDroppedIndex( null );
295
+ onDragCallback( sourceIndex, insertIndex );
296
+ }, DROP_ANIMATION_DURATION );
297
+ } );
298
+ };
299
+
300
+ const handleDragOver = ( event: React.DragEvent< HTMLDivElement >, index: number ) => {
301
+ event.preventDefault();
302
+ if ( ! measurements ) {
303
+ return;
304
+ }
305
+ const rect = event.currentTarget.getBoundingClientRect();
306
+ const midpoint = rect.top + rect.height / 2;
307
+ setHoverIndex( event.clientY < midpoint ? index : index + 1 );
308
+ };
309
+
310
+ /**
311
+ * Compute the translateY for a non-source item at `index` given the current
312
+ * draggingIndex and hoverIndex, so that items visually shift to show the gap
313
+ * while the container's layout height stays fixed.
314
+ *
315
+ * The source item has been visually hidden but still occupies its layout slot.
316
+ * Items between the source and the hover position need to slide by the source
317
+ * item's stride (height + gap) to either fill the vacated source slot or make
318
+ * room for the incoming gap at the hover position.
319
+ */
320
+ const getTranslateY = ( index: number ): number => {
321
+ if ( draggingIndex === null || hoverIndex === null || ! measurements ) {
322
+ return 0;
323
+ }
324
+ if ( index === draggingIndex ) {
325
+ return 0;
326
+ }
327
+
328
+ const { itemStrides, sourceHeight } = measurements;
329
+ const sourceStride = itemStrides[ draggingIndex ] ?? sourceHeight;
330
+
331
+ if ( hoverIndex > draggingIndex ) {
332
+ // Dragging downward: items strictly between source and hover position slide up.
333
+ if ( index > draggingIndex && index < hoverIndex ) {
334
+ return -sourceStride;
335
+ }
336
+ } else if ( index >= hoverIndex && index < draggingIndex ) {
337
+ // Dragging upward: items between hover position and source slide down.
338
+ return sourceStride;
339
+ }
340
+
341
+ return 0;
342
+ };
343
+
344
+ const isDragging = draggingIndex !== null;
345
+
346
+ return (
347
+ <VStack
348
+ ref={ listRef }
349
+ className={ classNames(
350
+ 'newspack-card--core--sortable-list',
351
+ disabled && 'newspack-card--core--sortable-list__is-disabled',
352
+ isDragging && 'newspack-card--core--sortable-list__is-dragging'
353
+ ) }
354
+ style={ measurements ? { height: measurements.lockedHeight } : undefined }
355
+ spacing={ 4 }
356
+ >
357
+ { sortedItems.map( ( item, index ) => {
358
+ const translateY = getTranslateY( index );
359
+ return (
360
+ <Disabled key={ item.id } isDisabled={ disabled }>
361
+ <div
362
+ ref={ el => {
363
+ itemRefs.current[ index ] = el;
364
+ } }
365
+ className={ classNames( 'newspack-card--core--sortable-list__item', {
366
+ 'is-source': draggingIndex === index,
367
+ 'is-dropped': droppedIndex === index,
368
+ } ) }
369
+ style={ translateY ? { transform: `translateY(${ translateY }px)` } : { transition: ! isDragging ? 'none' : undefined } }
370
+ id={ `draggable-card-${ index }` }
371
+ onDragOver={ e => handleDragOver( e, index ) }
372
+ >
373
+ <Draggable
374
+ transferData={ {} }
375
+ cloneClassname="newspack-card--core--sortable-list__item__clone"
376
+ elementId={ `draggable-card-${ index }` }
377
+ onDragStart={ () => handleDragStart( index ) }
378
+ onDragEnd={ handleDragEnd }
379
+ appendToOwnerDocument
380
+ >
381
+ { ( { onDraggableStart, onDraggableEnd } ) => (
382
+ <Card
383
+ isSmall
384
+ draggable
385
+ onDragStart={ onDraggableStart }
386
+ onDragEnd={ onDraggableEnd }
387
+ __experimentalCoreCard
388
+ __experimentalCoreProps={ {
389
+ header: (
390
+ <h3>
391
+ { item.title }
392
+ <Badge level={ item.badgeLevel } text={ item.badgeText } />
393
+ </h3>
394
+ ),
395
+ isDraggable: true,
396
+ isFirstTarget: index === 0,
397
+ isLastTarget: index === sortedItems.length - 1,
398
+ dragIndex: index,
399
+ onDragCallback: handleButtonMove,
400
+ } }
401
+ />
402
+ ) }
403
+ </Draggable>
404
+ </div>
405
+ </Disabled>
406
+ );
407
+ } ) }
408
+ </VStack>
409
+ );
410
+ };
411
+
412
+ export default CardSortableList;
@@ -0,0 +1,48 @@
1
+ $collapse-duration: 200ms;
2
+ $shift-duration: 150ms;
3
+ $drop-duration: 400ms; /* must match DROP_ANIMATION_DURATION in index.tsx */
4
+
5
+ .newspack-card--core--sortable-list {
6
+ &__item {
7
+ cursor: grab;
8
+
9
+ /*
10
+ * Animate items sliding to fill/make space around the dragged item.
11
+ */
12
+ transition: transform $shift-duration ease;
13
+
14
+ /*
15
+ * Collapse the source item's content — the wrapper stays in layout as the
16
+ * anchor point, and the container height is locked via inline style in JS.
17
+ */
18
+ &.is-source {
19
+ cursor: grabbing;
20
+ opacity: 0;
21
+ transition: opacity $collapse-duration ease;
22
+ }
23
+
24
+ /*
25
+ * Drop landing animation: brief fade-in and scale on the newly placed item.
26
+ */
27
+ &.is-dropped {
28
+ animation: newspack-sortable-drop $drop-duration ease forwards;
29
+ opacity: 0;
30
+ }
31
+
32
+ }
33
+ }
34
+
35
+ @keyframes newspack-sortable-drop {
36
+ 0% {
37
+ opacity: 0;
38
+ transform: scale(0.98);
39
+ }
40
+ 60% {
41
+ opacity: 1;
42
+ transform: scale(1.01);
43
+ }
44
+ 100% {
45
+ opacity: 1;
46
+ transform: scale(1);
47
+ }
48
+ }