@cloudscape-design/components 3.0.850 → 3.0.852
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.
- package/app-layout/visual-refresh-toolbar/contexts.d.ts +1 -0
- package/app-layout/visual-refresh-toolbar/contexts.d.ts.map +1 -1
- package/app-layout/visual-refresh-toolbar/contexts.js +1 -0
- package/app-layout/visual-refresh-toolbar/contexts.js.map +1 -1
- package/app-layout/visual-refresh-toolbar/index.d.ts.map +1 -1
- package/app-layout/visual-refresh-toolbar/index.js +2 -1
- package/app-layout/visual-refresh-toolbar/index.js.map +1 -1
- package/autosuggest/internal.js +2 -2
- package/autosuggest/internal.js.map +1 -1
- package/calendar/internal.js +1 -1
- package/calendar/internal.js.map +1 -1
- package/collection-preferences/content-display/content-display-option.d.ts +2 -3
- package/collection-preferences/content-display/content-display-option.d.ts.map +1 -1
- package/collection-preferences/content-display/content-display-option.js +2 -5
- package/collection-preferences/content-display/content-display-option.js.map +1 -1
- package/collection-preferences/content-display/index.d.ts.map +1 -1
- package/collection-preferences/content-display/index.js +7 -2
- package/collection-preferences/content-display/index.js.map +1 -1
- package/internal/components/dnd-container/index.d.ts +3 -27
- package/internal/components/dnd-container/index.d.ts.map +1 -1
- package/internal/components/dnd-container/index.js +68 -38
- package/internal/components/dnd-container/index.js.map +1 -1
- package/internal/components/dnd-container/interfaces.d.ts +31 -4
- package/internal/components/dnd-container/interfaces.d.ts.map +1 -1
- package/internal/components/dnd-container/interfaces.js.map +1 -1
- package/internal/components/dnd-container/use-drag-and-drop-reorder.d.ts +6 -4
- package/internal/components/dnd-container/use-drag-and-drop-reorder.d.ts.map +1 -1
- package/internal/components/dnd-container/use-drag-and-drop-reorder.js +8 -8
- package/internal/components/dnd-container/use-drag-and-drop-reorder.js.map +1 -1
- package/internal/components/dnd-container/use-live-announcements.d.ts +4 -3
- package/internal/components/dnd-container/use-live-announcements.d.ts.map +1 -1
- package/internal/components/dnd-container/use-live-announcements.js +9 -9
- package/internal/components/dnd-container/use-live-announcements.js.map +1 -1
- package/internal/components/drag-handle/index.d.ts +1 -2
- package/internal/components/drag-handle/index.d.ts.map +1 -1
- package/internal/components/drag-handle/index.js +9 -4
- package/internal/components/drag-handle/index.js.map +1 -1
- package/internal/components/drag-handle/styles.css.js +3 -2
- package/internal/components/drag-handle/styles.scoped.css +180 -3
- package/internal/components/drag-handle/styles.selectors.js +3 -2
- package/internal/environment.js +1 -1
- package/internal/environment.json +1 -1
- package/internal/manifest.json +1 -1
- package/internal/plugins/helpers/use-global-breadcrumbs.d.ts.map +1 -1
- package/internal/plugins/helpers/use-global-breadcrumbs.js +5 -4
- package/internal/plugins/helpers/use-global-breadcrumbs.js.map +1 -1
- package/package.json +1 -1
- package/internal/components/handle/index.d.ts +0 -3
- package/internal/components/handle/index.d.ts.map +0 -1
- package/internal/components/handle/index.js +0 -14
- package/internal/components/handle/index.js.map +0 -1
- package/internal/components/handle/styles.css.js +0 -7
- package/internal/components/handle/styles.scoped.css +0 -183
- package/internal/components/handle/styles.selectors.js +0 -8
|
@@ -1,28 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
import { ReorderAnnouncements, ReorderOptions } from './interfaces';
|
|
5
|
-
export interface DndContainerProps<Option> extends ReorderOptions<Option> {
|
|
6
|
-
onChange: (sortedOption: readonly Option[]) => void;
|
|
7
|
-
disabled?: boolean;
|
|
8
|
-
renderOption: (props: RenderOptionProps<Option>) => React.ReactNode;
|
|
9
|
-
renderActiveOption: (props: RenderActiveOptionProps<Option>) => React.ReactNode;
|
|
10
|
-
i18nStrings: ReorderAnnouncements;
|
|
11
|
-
dragOverlayClassName?: string;
|
|
12
|
-
children?: (optionsContent: React.ReactNode) => React.ReactNode;
|
|
13
|
-
}
|
|
14
|
-
interface RenderOptionProps<Option> extends RenderActiveOptionProps<Option> {
|
|
15
|
-
ref: React.RefCallback<HTMLElement>;
|
|
16
|
-
style: React.CSSProperties;
|
|
17
|
-
isDragging: boolean;
|
|
18
|
-
isSorting: boolean;
|
|
19
|
-
attributes: DraggableAttributes;
|
|
20
|
-
}
|
|
21
|
-
interface RenderActiveOptionProps<Option> {
|
|
22
|
-
option: Option;
|
|
23
|
-
dragHandleAriaLabel?: string;
|
|
24
|
-
listeners?: SyntheticListenerMap;
|
|
25
|
-
}
|
|
26
|
-
export declare function DndContainer<Option>({ getId, sortedOptions, onChange, disabled, renderOption, renderActiveOption, i18nStrings, dragOverlayClassName, children, }: DndContainerProps<Option>): JSX.Element;
|
|
27
|
-
export {};
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import { DndContainerProps } from './interfaces';
|
|
3
|
+
export declare function DndContainer<Data>({ items, renderItem, onItemsChange, disableReorder, i18nStrings, dragOverlayClassName, }: DndContainerProps<Data>): JSX.Element;
|
|
28
4
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/index.tsx"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/index.tsx"],"names":[],"mappings":";AAUA,OAAO,EAAoB,iBAAiB,EAAmB,MAAM,cAAc,CAAC;AAMpF,wBAAgB,YAAY,CAAC,IAAI,EAAE,EACjC,KAAK,EACL,UAAU,EACV,aAAa,EACb,cAAc,EACd,WAAW,EACX,oBAA6C,GAC9C,EAAE,iBAAiB,CAAC,IAAI,CAAC,eAuEzB"}
|
|
@@ -1,67 +1,97 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import React from 'react';
|
|
3
|
+
import React, { useEffect, useRef } from 'react';
|
|
4
4
|
import { DndContext, DragOverlay } from '@dnd-kit/core';
|
|
5
5
|
import { arrayMove, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable';
|
|
6
6
|
import { CSS } from '@dnd-kit/utilities';
|
|
7
|
+
import { joinStrings } from '../../utils/strings';
|
|
7
8
|
import Portal from '../portal';
|
|
8
9
|
import useDragAndDropReorder from './use-drag-and-drop-reorder';
|
|
9
10
|
import useLiveAnnouncements from './use-live-announcements';
|
|
10
11
|
import styles from './styles.css.js';
|
|
11
|
-
export function DndContainer({
|
|
12
|
-
const {
|
|
13
|
-
|
|
14
|
-
getId,
|
|
12
|
+
export function DndContainer({ items, renderItem, onItemsChange, disableReorder, i18nStrings, dragOverlayClassName = styles['drag-overlay'], }) {
|
|
13
|
+
const { activeItemId, setActiveItemId, collisionDetection, handleKeyDown, sensors } = useDragAndDropReorder({
|
|
14
|
+
items,
|
|
15
15
|
});
|
|
16
|
-
const
|
|
17
|
-
const isDragging =
|
|
18
|
-
const announcements = useLiveAnnouncements(Object.assign({
|
|
16
|
+
const activeItem = activeItemId ? items.find(item => item.id === activeItemId) : null;
|
|
17
|
+
const isDragging = activeItemId !== null;
|
|
18
|
+
const announcements = useLiveAnnouncements(Object.assign({ items, isDragging }, i18nStrings));
|
|
19
|
+
const portalContainer = usePortalContainer();
|
|
19
20
|
return (React.createElement(DndContext, { sensors: sensors, collisionDetection: collisionDetection, accessibility: {
|
|
20
21
|
announcements,
|
|
21
22
|
restoreFocus: false,
|
|
22
23
|
screenReaderInstructions: i18nStrings.dragHandleAriaDescription
|
|
23
24
|
? { draggable: i18nStrings.dragHandleAriaDescription }
|
|
24
25
|
: undefined,
|
|
25
|
-
|
|
26
|
-
|
|
26
|
+
container: portalContainer,
|
|
27
|
+
}, onDragStart: ({ active }) => setActiveItemId(active.id), onDragEnd: event => {
|
|
28
|
+
setActiveItemId(null);
|
|
27
29
|
const { active, over } = event;
|
|
28
30
|
if (over && active.id !== over.id) {
|
|
29
|
-
const oldIndex =
|
|
30
|
-
const newIndex =
|
|
31
|
-
|
|
31
|
+
const oldIndex = items.findIndex(item => item.id === active.id);
|
|
32
|
+
const newIndex = items.findIndex(item => item.id === over.id);
|
|
33
|
+
onItemsChange(arrayMove([...items], oldIndex, newIndex));
|
|
32
34
|
}
|
|
33
|
-
}, onDragCancel: () =>
|
|
34
|
-
|
|
35
|
-
React.createElement(Portal,
|
|
36
|
-
React.createElement(DragOverlay, { className: dragOverlayClassName, dropAnimation: null, style: { zIndex: 5000 } },
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
35
|
+
}, onDragCancel: () => setActiveItemId(null) },
|
|
36
|
+
React.createElement(SortableContext, { disabled: disableReorder, items: items.map(item => item.id), strategy: verticalListSortingStrategy }, items.map(item => (React.createElement(DraggableItem, { key: item.id, item: item, renderItem: renderItem, onKeyDown: handleKeyDown, dragHandleAriaLabel: i18nStrings.dragHandleAriaLabel })))),
|
|
37
|
+
React.createElement(Portal, { container: portalContainer },
|
|
38
|
+
React.createElement(DragOverlay, { className: dragOverlayClassName, dropAnimation: null, style: { zIndex: 5000 } }, activeItem &&
|
|
39
|
+
renderItem({
|
|
40
|
+
item: activeItem,
|
|
41
|
+
style: {},
|
|
42
|
+
isDragging: true,
|
|
43
|
+
isSorting: false,
|
|
44
|
+
isActive: true,
|
|
45
|
+
dragHandleAttributes: {
|
|
46
|
+
['aria-label']: joinStrings(i18nStrings.dragHandleAriaLabel, activeItem.label),
|
|
47
|
+
},
|
|
48
|
+
dragHandleListeners: {
|
|
49
|
+
onKeyDown: handleKeyDown,
|
|
50
|
+
},
|
|
41
51
|
})))));
|
|
42
52
|
}
|
|
43
|
-
function
|
|
44
|
-
const
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
listeners.onKeyDown(event);
|
|
53
|
+
function usePortalContainer() {
|
|
54
|
+
const portalContainerRef = useRef(document.createElement('div'));
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
const container = portalContainerRef.current;
|
|
57
|
+
if (!container.isConnected) {
|
|
58
|
+
document.body.appendChild(container);
|
|
59
|
+
}
|
|
60
|
+
return () => {
|
|
61
|
+
if (container.isConnected) {
|
|
62
|
+
document.body.removeChild(container);
|
|
54
63
|
}
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
64
|
+
};
|
|
65
|
+
}, []);
|
|
66
|
+
return portalContainerRef.current;
|
|
67
|
+
}
|
|
68
|
+
function DraggableItem({ item, dragHandleAriaLabel, onKeyDown, renderItem, }) {
|
|
69
|
+
const { isDragging, isSorting, listeners, setNodeRef, transform, attributes } = useSortable({ id: item.id });
|
|
70
|
+
const style = { transform: CSS.Translate.toString(transform) };
|
|
71
|
+
const dragHandleListeners = attributes['aria-disabled']
|
|
72
|
+
? {}
|
|
73
|
+
: Object.assign(Object.assign({}, listeners), { onKeyDown: (event) => {
|
|
74
|
+
if (onKeyDown) {
|
|
75
|
+
onKeyDown(event);
|
|
76
|
+
}
|
|
77
|
+
if (listeners === null || listeners === void 0 ? void 0 : listeners.onKeyDown) {
|
|
78
|
+
listeners.onKeyDown(event);
|
|
79
|
+
}
|
|
80
|
+
} });
|
|
81
|
+
const dragHandleAttributes = {
|
|
82
|
+
['aria-label']: joinStrings(dragHandleAriaLabel, item.label),
|
|
83
|
+
['aria-describedby']: attributes['aria-describedby'],
|
|
84
|
+
['aria-disabled']: attributes['aria-disabled'],
|
|
85
|
+
};
|
|
86
|
+
return (React.createElement(React.Fragment, null, renderItem({
|
|
87
|
+
item,
|
|
59
88
|
ref: setNodeRef,
|
|
60
89
|
style,
|
|
61
90
|
isDragging,
|
|
62
91
|
isSorting,
|
|
63
|
-
|
|
64
|
-
|
|
92
|
+
isActive: false,
|
|
93
|
+
dragHandleListeners,
|
|
94
|
+
dragHandleAttributes,
|
|
65
95
|
})));
|
|
66
96
|
}
|
|
67
97
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,KAAK,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,KAAK,EAAE,EAAE,SAAS,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AACxD,OAAO,EAAE,SAAS,EAAE,eAAe,EAAE,WAAW,EAAE,2BAA2B,EAAE,MAAM,mBAAmB,CAAC;AACzG,OAAO,EAAE,GAAG,EAAE,MAAM,oBAAoB,CAAC;AAEzC,OAAO,EAAE,WAAW,EAAE,MAAM,qBAAqB,CAAC;AAClD,OAAO,MAAM,MAAM,WAAW,CAAC;AAE/B,OAAO,qBAAqB,MAAM,6BAA6B,CAAC;AAChE,OAAO,oBAAoB,MAAM,0BAA0B,CAAC;AAE5D,OAAO,MAAM,MAAM,iBAAiB,CAAC;AAErC,MAAM,UAAU,YAAY,CAAO,EACjC,KAAK,EACL,UAAU,EACV,aAAa,EACb,cAAc,EACd,WAAW,EACX,oBAAoB,GAAG,MAAM,CAAC,cAAc,CAAC,GACrB;IACxB,MAAM,EAAE,YAAY,EAAE,eAAe,EAAE,kBAAkB,EAAE,aAAa,EAAE,OAAO,EAAE,GAAG,qBAAqB,CAAC;QAC1G,KAAK;KACN,CAAC,CAAC;IACH,MAAM,UAAU,GAAG,YAAY,CAAC,CAAC,CAAC,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,YAAY,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC;IACtF,MAAM,UAAU,GAAG,YAAY,KAAK,IAAI,CAAC;IACzC,MAAM,aAAa,GAAG,oBAAoB,iBAAG,KAAK,EAA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AAC;AACJ,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport React, { useEffect, useRef } from 'react';\nimport { DndContext, DragOverlay } from '@dnd-kit/core';\nimport { arrayMove, SortableContext, useSortable, verticalListSortingStrategy } from '@dnd-kit/sortable';\nimport { CSS } from '@dnd-kit/utilities';\n\nimport { joinStrings } from '../../utils/strings';\nimport Portal from '../portal';\nimport { DndContainerItem, DndContainerProps, RenderItemProps } from './interfaces';\nimport useDragAndDropReorder from './use-drag-and-drop-reorder';\nimport useLiveAnnouncements from './use-live-announcements';\n\nimport styles from './styles.css.js';\n\nexport function DndContainer<Data>({\n items,\n renderItem,\n onItemsChange,\n disableReorder,\n i18nStrings,\n dragOverlayClassName = styles['drag-overlay'],\n}: DndContainerProps<Data>) {\n const { activeItemId, setActiveItemId, collisionDetection, handleKeyDown, sensors } = useDragAndDropReorder({\n items,\n });\n const activeItem = activeItemId ? items.find(item => item.id === activeItemId) : null;\n const isDragging = activeItemId !== null;\n const announcements = useLiveAnnouncements({ items, isDragging, ...i18nStrings });\n const portalContainer = usePortalContainer();\n return (\n <DndContext\n sensors={sensors}\n collisionDetection={collisionDetection}\n accessibility={{\n announcements,\n restoreFocus: false,\n screenReaderInstructions: i18nStrings.dragHandleAriaDescription\n ? { draggable: i18nStrings.dragHandleAriaDescription }\n : undefined,\n container: portalContainer,\n }}\n onDragStart={({ active }) => setActiveItemId(active.id)}\n onDragEnd={event => {\n setActiveItemId(null);\n const { active, over } = event;\n\n if (over && active.id !== over.id) {\n const oldIndex = items.findIndex(item => item.id === active.id);\n const newIndex = items.findIndex(item => item.id === over.id);\n onItemsChange(arrayMove([...items], oldIndex, newIndex));\n }\n }}\n onDragCancel={() => setActiveItemId(null)}\n >\n <SortableContext\n disabled={disableReorder}\n items={items.map(item => item.id)}\n strategy={verticalListSortingStrategy}\n >\n {items.map(item => (\n <DraggableItem\n key={item.id}\n item={item}\n renderItem={renderItem}\n onKeyDown={handleKeyDown}\n dragHandleAriaLabel={i18nStrings.dragHandleAriaLabel}\n />\n ))}\n </SortableContext>\n\n <Portal container={portalContainer}>\n {/* Make sure that the drag overlay is above the modal by assigning the z-index as inline style\n so that it prevails over dnd-kit's inline z-index of 999 */}\n <DragOverlay className={dragOverlayClassName} dropAnimation={null} style={{ zIndex: 5000 }}>\n {activeItem &&\n renderItem({\n item: activeItem,\n style: {},\n isDragging: true,\n isSorting: false,\n isActive: true,\n dragHandleAttributes: {\n ['aria-label']: joinStrings(i18nStrings.dragHandleAriaLabel, activeItem.label),\n },\n dragHandleListeners: {\n onKeyDown: handleKeyDown,\n },\n })}\n </DragOverlay>\n </Portal>\n </DndContext>\n );\n}\n\nfunction usePortalContainer() {\n const portalContainerRef = useRef(document.createElement('div'));\n useEffect(() => {\n const container = portalContainerRef.current;\n if (!container.isConnected) {\n document.body.appendChild(container);\n }\n return () => {\n if (container.isConnected) {\n document.body.removeChild(container);\n }\n };\n }, []);\n return portalContainerRef.current;\n}\n\nfunction DraggableItem<Data>({\n item,\n dragHandleAriaLabel,\n onKeyDown,\n renderItem,\n}: {\n item: DndContainerItem<Data>;\n dragHandleAriaLabel?: string;\n onKeyDown: (event: React.KeyboardEvent) => void;\n renderItem: (props: RenderItemProps<Data>) => React.ReactNode;\n}) {\n const { isDragging, isSorting, listeners, setNodeRef, transform, attributes } = useSortable({ id: item.id });\n const style = { transform: CSS.Translate.toString(transform) };\n const dragHandleListeners = attributes['aria-disabled']\n ? {}\n : {\n ...listeners,\n onKeyDown: (event: React.KeyboardEvent) => {\n if (onKeyDown) {\n onKeyDown(event);\n }\n if (listeners?.onKeyDown) {\n listeners.onKeyDown(event);\n }\n },\n };\n const dragHandleAttributes = {\n ['aria-label']: joinStrings(dragHandleAriaLabel, item.label),\n ['aria-describedby']: attributes['aria-describedby'],\n ['aria-disabled']: attributes['aria-disabled'],\n };\n return (\n <>\n {renderItem({\n item,\n ref: setNodeRef,\n style,\n isDragging,\n isSorting,\n isActive: false,\n dragHandleListeners,\n dragHandleAttributes,\n })}\n </>\n );\n}\n"]}
|
|
@@ -1,8 +1,35 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
export interface DndContainerProps<Data> {
|
|
3
|
+
items: readonly DndContainerItem<Data>[];
|
|
4
|
+
renderItem: (props: RenderItemProps<Data>) => React.ReactNode;
|
|
5
|
+
onItemsChange: (sortedOptions: readonly DndContainerItem<Data>[]) => void;
|
|
6
|
+
i18nStrings: DndContainerI18nStrings;
|
|
7
|
+
disableReorder?: boolean;
|
|
8
|
+
dragOverlayClassName?: string;
|
|
4
9
|
}
|
|
5
|
-
export interface
|
|
10
|
+
export interface DndContainerItem<Data> {
|
|
11
|
+
id: string;
|
|
12
|
+
label: string;
|
|
13
|
+
data: Data;
|
|
14
|
+
}
|
|
15
|
+
export interface RenderItemProps<Data> {
|
|
16
|
+
ref?: React.RefCallback<HTMLElement>;
|
|
17
|
+
item: DndContainerItem<Data>;
|
|
18
|
+
style: React.CSSProperties;
|
|
19
|
+
isDragging: boolean;
|
|
20
|
+
isSorting: boolean;
|
|
21
|
+
isActive: boolean;
|
|
22
|
+
dragHandleAttributes: {
|
|
23
|
+
'aria-label'?: string;
|
|
24
|
+
'aria-describedby'?: string;
|
|
25
|
+
'aria-disabled'?: boolean;
|
|
26
|
+
};
|
|
27
|
+
dragHandleListeners?: {
|
|
28
|
+
onPointerDown?: React.PointerEventHandler;
|
|
29
|
+
onKeyDown?: React.KeyboardEventHandler;
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
export interface DndContainerI18nStrings {
|
|
6
33
|
liveAnnouncementDndStarted?: (position: number, total: number) => string;
|
|
7
34
|
liveAnnouncementDndItemReordered?: (initialPosition: number, currentPosition: number, total: number) => string;
|
|
8
35
|
liveAnnouncementDndItemCommitted?: (initialPosition: number, finalPosition: number, total: number) => string;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/interfaces.ts"],"names":[],"mappings":"AAGA,MAAM,WAAW,
|
|
1
|
+
{"version":3,"file":"interfaces.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/interfaces.ts"],"names":[],"mappings":";AAGA,MAAM,WAAW,iBAAiB,CAAC,IAAI;IACrC,KAAK,EAAE,SAAS,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;IACzC,UAAU,EAAE,CAAC,KAAK,EAAE,eAAe,CAAC,IAAI,CAAC,KAAK,KAAK,CAAC,SAAS,CAAC;IAC9D,aAAa,EAAE,CAAC,aAAa,EAAE,SAAS,gBAAgB,CAAC,IAAI,CAAC,EAAE,KAAK,IAAI,CAAC;IAC1E,WAAW,EAAE,uBAAuB,CAAC;IACrC,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,oBAAoB,CAAC,EAAE,MAAM,CAAC;CAC/B;AAED,MAAM,WAAW,gBAAgB,CAAC,IAAI;IACpC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,IAAI,EAAE,IAAI,CAAC;CACZ;AAED,MAAM,WAAW,eAAe,CAAC,IAAI;IACnC,GAAG,CAAC,EAAE,KAAK,CAAC,WAAW,CAAC,WAAW,CAAC,CAAC;IACrC,IAAI,EAAE,gBAAgB,CAAC,IAAI,CAAC,CAAC;IAC7B,KAAK,EAAE,KAAK,CAAC,aAAa,CAAC;IAC3B,UAAU,EAAE,OAAO,CAAC;IACpB,SAAS,EAAE,OAAO,CAAC;IACnB,QAAQ,EAAE,OAAO,CAAC;IAClB,oBAAoB,EAAE;QACpB,YAAY,CAAC,EAAE,MAAM,CAAC;QACtB,kBAAkB,CAAC,EAAE,MAAM,CAAC;QAC5B,eAAe,CAAC,EAAE,OAAO,CAAC;KAC3B,CAAC;IACF,mBAAmB,CAAC,EAAE;QACpB,aAAa,CAAC,EAAE,KAAK,CAAC,mBAAmB,CAAC;QAC1C,SAAS,CAAC,EAAE,KAAK,CAAC,oBAAoB,CAAC;KACxC,CAAC;CACH;AAED,MAAM,WAAW,uBAAuB;IACtC,0BAA0B,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IACzE,gCAAgC,CAAC,EAAE,CAAC,eAAe,EAAE,MAAM,EAAE,eAAe,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAC/G,gCAAgC,CAAC,EAAE,CAAC,eAAe,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,KAAK,MAAM,CAAC;IAC7G,4BAA4B,CAAC,EAAE,MAAM,CAAC;IACtC,mBAAmB,CAAC,EAAE,MAAM,CAAC;IAC7B,yBAAyB,CAAC,EAAE,MAAM,CAAC;CACpC"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/interfaces.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport interface
|
|
1
|
+
{"version":3,"file":"interfaces.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/interfaces.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nexport interface DndContainerProps<Data> {\n items: readonly DndContainerItem<Data>[];\n renderItem: (props: RenderItemProps<Data>) => React.ReactNode;\n onItemsChange: (sortedOptions: readonly DndContainerItem<Data>[]) => void;\n i18nStrings: DndContainerI18nStrings;\n disableReorder?: boolean;\n dragOverlayClassName?: string;\n}\n\nexport interface DndContainerItem<Data> {\n id: string;\n label: string;\n data: Data;\n}\n\nexport interface RenderItemProps<Data> {\n ref?: React.RefCallback<HTMLElement>;\n item: DndContainerItem<Data>;\n style: React.CSSProperties;\n isDragging: boolean;\n isSorting: boolean;\n isActive: boolean;\n dragHandleAttributes: {\n 'aria-label'?: string;\n 'aria-describedby'?: string;\n 'aria-disabled'?: boolean;\n };\n dragHandleListeners?: {\n onPointerDown?: React.PointerEventHandler;\n onKeyDown?: React.KeyboardEventHandler;\n };\n}\n\nexport interface DndContainerI18nStrings {\n liveAnnouncementDndStarted?: (position: number, total: number) => string;\n liveAnnouncementDndItemReordered?: (initialPosition: number, currentPosition: number, total: number) => string;\n liveAnnouncementDndItemCommitted?: (initialPosition: number, finalPosition: number, total: number) => string;\n liveAnnouncementDndDiscarded?: string;\n dragHandleAriaLabel?: string;\n dragHandleAriaDescription?: string;\n}\n"]}
|
|
@@ -1,12 +1,14 @@
|
|
|
1
1
|
import React from 'react';
|
|
2
2
|
import { CollisionDetection, KeyboardCoordinateGetter, UniqueIdentifier } from '@dnd-kit/core';
|
|
3
|
-
import {
|
|
4
|
-
export default function useDragAndDropReorder<
|
|
5
|
-
|
|
3
|
+
import { DndContainerItem } from './interfaces';
|
|
4
|
+
export default function useDragAndDropReorder<Data>({ items }: {
|
|
5
|
+
items: readonly DndContainerItem<Data>[];
|
|
6
|
+
}): {
|
|
7
|
+
activeItemId: UniqueIdentifier | null;
|
|
8
|
+
setActiveItemId: (id: UniqueIdentifier | null) => void;
|
|
6
9
|
collisionDetection: CollisionDetection;
|
|
7
10
|
coordinateGetter: KeyboardCoordinateGetter;
|
|
8
11
|
handleKeyDown: (event: React.KeyboardEvent) => void;
|
|
9
12
|
sensors: import("@dnd-kit/core").SensorDescriptor<import("@dnd-kit/core").SensorOptions>[];
|
|
10
|
-
setActiveItem: (id: UniqueIdentifier | null) => void;
|
|
11
13
|
};
|
|
12
14
|
//# sourceMappingURL=use-drag-and-drop-reorder.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-drag-and-drop-reorder.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-drag-and-drop-reorder.ts"],"names":[],"mappings":"AAEA,OAAO,KAA2B,MAAM,OAAO,CAAC;AAChD,OAAO,EAGL,kBAAkB,EAElB,wBAAwB,EAExB,gBAAgB,EAGjB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"use-drag-and-drop-reorder.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-drag-and-drop-reorder.ts"],"names":[],"mappings":"AAEA,OAAO,KAA2B,MAAM,OAAO,CAAC;AAChD,OAAO,EAGL,kBAAkB,EAElB,wBAAwB,EAExB,gBAAgB,EAGjB,MAAM,eAAe,CAAC;AAGvB,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AA8BhD,MAAM,CAAC,OAAO,UAAU,qBAAqB,CAAC,IAAI,EAAE,EAAE,KAAK,EAAE,EAAE;IAAE,KAAK,EAAE,SAAS,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAA;CAAE;;0BAK9E,gBAAgB,GAAG,IAAI;;;2BAQpB,mBAAmB;;EAuGlD"}
|
|
@@ -27,7 +27,7 @@ var KeyboardCode;
|
|
|
27
27
|
// We let our collisionDetection and customCoordinateGetter use the same
|
|
28
28
|
// getClosestId function which takes its value from the current component
|
|
29
29
|
// state, to make sure they are always in sync.
|
|
30
|
-
export default function useDragAndDropReorder({
|
|
30
|
+
export default function useDragAndDropReorder({ items }) {
|
|
31
31
|
const isKeyboard = useRef(false);
|
|
32
32
|
const positionDelta = useRef(0);
|
|
33
33
|
const [activeItemId, setActiveItemId] = useState(null);
|
|
@@ -40,8 +40,8 @@ export default function useDragAndDropReorder({ sortedOptions, getId }) {
|
|
|
40
40
|
};
|
|
41
41
|
const handleKeyDown = (event) => {
|
|
42
42
|
if (isKeyboard.current && activeItemId) {
|
|
43
|
-
const currentTargetIndex =
|
|
44
|
-
if (event.key === 'ArrowDown' && currentTargetIndex <
|
|
43
|
+
const currentTargetIndex = items.findIndex(item => item.id === activeItemId) + positionDelta.current;
|
|
44
|
+
if (event.key === 'ArrowDown' && currentTargetIndex < items.length - 1) {
|
|
45
45
|
positionDelta.current += 1;
|
|
46
46
|
}
|
|
47
47
|
else if (event.key === 'ArrowUp' && currentTargetIndex > 0) {
|
|
@@ -57,9 +57,9 @@ export default function useDragAndDropReorder({ sortedOptions, getId }) {
|
|
|
57
57
|
if (positionDelta.current === 0) {
|
|
58
58
|
return active.id;
|
|
59
59
|
}
|
|
60
|
-
const currentIndex =
|
|
61
|
-
const newIndex = Math.max(0, Math.min(
|
|
62
|
-
return
|
|
60
|
+
const currentIndex = items.findIndex(item => item.id === active.id);
|
|
61
|
+
const newIndex = Math.max(0, Math.min(items.length - 1, currentIndex + positionDelta.current));
|
|
62
|
+
return items[newIndex].id;
|
|
63
63
|
};
|
|
64
64
|
const collisionDetection = ({ active, collisionRect, droppableContainers, droppableRects, pointerCoordinates, }) => {
|
|
65
65
|
if (isKeyboard.current) {
|
|
@@ -114,12 +114,12 @@ export default function useDragAndDropReorder({ sortedOptions, getId }) {
|
|
|
114
114
|
},
|
|
115
115
|
}));
|
|
116
116
|
return {
|
|
117
|
-
|
|
117
|
+
activeItemId,
|
|
118
|
+
setActiveItemId: setActiveItem,
|
|
118
119
|
collisionDetection,
|
|
119
120
|
coordinateGetter,
|
|
120
121
|
handleKeyDown,
|
|
121
122
|
sensors,
|
|
122
|
-
setActiveItem,
|
|
123
123
|
};
|
|
124
124
|
}
|
|
125
125
|
function isAfter(a, b) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-drag-and-drop-reorder.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-drag-and-drop-reorder.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAc,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAChD,OAAO,EAEL,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BAAqB,CAAC,EAC7B,QAAQ,EACR,SAAS,EACT,mBAAmB,GAKpB;IACC,IAAI,SAAS,KAAK,QAAQ,EAAE;QAC1B,OAAO;KACR;IACD,MAAM,kBAAkB,GAAG,mBAAmB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC;IAClF,IAAI,kBAAkB,EAAE;QACtB,OAAO;YACL,EAAE,EAAE,kBAAkB,CAAC,EAAE;YACzB,IAAI,EAAE;gBACJ,kBAAkB,EAAE,kBAAkB;gBACtC,KAAK,EAAE,CAAC;aACT;SACF,CAAC;KACH;AACH,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,KAAK,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport React, { useRef, useState } from 'react';\nimport {\n Active,\n closestCenter,\n CollisionDetection,\n DroppableContainer,\n KeyboardCoordinateGetter,\n PointerSensor,\n UniqueIdentifier,\n useSensor,\n useSensors,\n} from '@dnd-kit/core';\nimport { hasSortableData } from '@dnd-kit/sortable';\n\nimport { ReorderOptions } from './interfaces';\nimport { KeyboardSensor } from './keyboard-sensor';\n\nenum KeyboardCode {\n Space = 'Space',\n Down = 'ArrowDown',\n Right = 'ArrowRight',\n Left = 'ArrowLeft',\n Up = 'ArrowUp',\n Esc = 'Escape',\n Enter = 'Enter',\n}\n\n// A custom collision detection algorithm is used when using a keyboard to\n// work around an unexpected behavior when reordering items of variable height\n// with the keyboard.\n\n// Neither closestCenter nor closestCorners work really well for this case,\n// because the center (or corners) of a tall rectangle might be so low that it\n// is detected as being closest to the rectangle below of the one it should\n// actually swap with.\n\n// Instead of relying on coordinates, the expected results are achieved by\n// moving X positions up or down in the initially sorted array, depending on\n// the desired direction.\n\n// We let our collisionDetection and customCoordinateGetter use the same\n// getClosestId function which takes its value from the current component\n// state, to make sure they are always in sync.\n\nexport default function useDragAndDropReorder<Option>({ sortedOptions, getId }: ReorderOptions<Option>) {\n const isKeyboard = useRef(false);\n const positionDelta = useRef(0);\n const [activeItemId, setActiveItemId] = useState<UniqueIdentifier | null>(null);\n\n const setActiveItem = (id: UniqueIdentifier | null) => {\n setActiveItemId(id);\n if (!id) {\n isKeyboard.current = false;\n positionDelta.current = 0;\n }\n };\n\n const handleKeyDown = (event: React.KeyboardEvent) => {\n if (isKeyboard.current && activeItemId) {\n const currentTargetIndex =\n sortedOptions.findIndex(option => getId(option) === activeItemId) + positionDelta.current;\n if (event.key === 'ArrowDown' && currentTargetIndex < sortedOptions.length - 1) {\n positionDelta.current += 1;\n } else if (event.key === 'ArrowUp' && currentTargetIndex > 0) {\n positionDelta.current -= 1;\n }\n }\n if (activeItemId && isEscape(event.key)) {\n // Prevent modal from closing when pressing Esc to cancel the dragging action\n event.stopPropagation();\n }\n };\n\n const getClosestId = (active: Active) => {\n if (positionDelta.current === 0) {\n return active.id;\n }\n const currentIndex = sortedOptions.findIndex(option => getId(option) === active.id);\n const newIndex = Math.max(0, Math.min(sortedOptions.length - 1, currentIndex + positionDelta.current));\n return getId(sortedOptions[newIndex]);\n };\n\n const collisionDetection: CollisionDetection = ({\n active,\n collisionRect,\n droppableContainers,\n droppableRects,\n pointerCoordinates,\n }) => {\n if (isKeyboard.current) {\n // For keyboard interaction, determine the colliding container based on the movements made by the arrow keys,\n // via getClosestId\n const collidingContainer = getCollidingContainer({\n activeId: active.id,\n closestId: getClosestId(active),\n droppableContainers,\n });\n return collidingContainer ? [collidingContainer] : [];\n } else {\n // For mouse interaction, use the closest center algorithm\n return closestCenter({ active, collisionRect, droppableRects, droppableContainers, pointerCoordinates });\n }\n };\n\n const coordinateGetter: KeyboardCoordinateGetter = (\n event,\n { context: { active, collisionRect, droppableRects, droppableContainers } }\n ) => {\n if (event.code === KeyboardCode.Up || event.code === KeyboardCode.Down) {\n event.preventDefault();\n\n if (!active || !collisionRect) {\n return;\n }\n\n const closestId = getClosestId(active);\n\n if (closestId !== null) {\n const activeDroppable = droppableContainers.get(active.id);\n const newDroppable = droppableContainers.get(closestId);\n const newRect = newDroppable ? droppableRects.get(newDroppable.id) : null;\n const newNode = newDroppable?.node.current;\n\n if (newNode && newRect && activeDroppable && newDroppable) {\n const isAfterActive = isAfter(activeDroppable, newDroppable);\n const offset = {\n x: isAfterActive ? collisionRect.width - newRect.width : 0,\n y: isAfterActive ? collisionRect.height - newRect.height : 0,\n };\n const rectCoordinates = {\n x: newRect.left,\n y: newRect.top,\n };\n\n return {\n x: rectCoordinates.x - offset.x,\n y: rectCoordinates.y - offset.y,\n };\n }\n }\n }\n };\n\n const sensors = useSensors(\n useSensor(PointerSensor),\n useSensor(KeyboardSensor, {\n coordinateGetter,\n onActivation: () => {\n isKeyboard.current = true;\n },\n })\n );\n\n return {\n activeItem: activeItemId,\n collisionDetection,\n coordinateGetter,\n handleKeyDown,\n sensors,\n setActiveItem,\n };\n}\n\nfunction isAfter(a: DroppableContainer, b: DroppableContainer) {\n return hasSortableData(a) && hasSortableData(b) && a.data.current.sortable.index < b.data.current.sortable.index;\n}\n\nfunction getCollidingContainer({\n activeId,\n closestId,\n droppableContainers,\n}: {\n activeId: UniqueIdentifier;\n closestId: UniqueIdentifier;\n droppableContainers: DroppableContainer[];\n}) {\n if (closestId === activeId) {\n return;\n }\n const collidingContainer = droppableContainers.find(({ id }) => id === closestId);\n if (collidingContainer) {\n return {\n id: collidingContainer.id,\n data: {\n droppableContainer: collidingContainer,\n value: 0,\n },\n };\n }\n}\n\nconst isEscape = (key: string) => key === 'Escape' || key === 'Esc';\n"]}
|
|
1
|
+
{"version":3,"file":"use-drag-and-drop-reorder.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-drag-and-drop-reorder.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAc,EAAE,MAAM,EAAE,QAAQ,E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;AAED,SAAS,qBAAqB,CAAC,EAC7B,QAAQ,EACR,SAAS,EACT,mBAAmB,GAKpB;IACC,IAAI,SAAS,KAAK,QAAQ,EAAE;QAC1B,OAAO;KACR;IACD,MAAM,kBAAkB,GAAG,mBAAmB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,EAAE,EAAE,EAAE,CAAC,EAAE,KAAK,SAAS,CAAC,CAAC;IAClF,IAAI,kBAAkB,EAAE;QACtB,OAAO;YACL,EAAE,EAAE,kBAAkB,CAAC,EAAE;YACzB,IAAI,EAAE;gBACJ,kBAAkB,EAAE,kBAAkB;gBACtC,KAAK,EAAE,CAAC;aACT;SACF,CAAC;KACH;AACH,CAAC;AAED,MAAM,QAAQ,GAAG,CAAC,GAAW,EAAE,EAAE,CAAC,GAAG,KAAK,QAAQ,IAAI,GAAG,KAAK,KAAK,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport React, { useRef, useState } from 'react';\nimport {\n Active,\n closestCenter,\n CollisionDetection,\n DroppableContainer,\n KeyboardCoordinateGetter,\n PointerSensor,\n UniqueIdentifier,\n useSensor,\n useSensors,\n} from '@dnd-kit/core';\nimport { hasSortableData } from '@dnd-kit/sortable';\n\nimport { DndContainerItem } from './interfaces';\nimport { KeyboardSensor } from './keyboard-sensor';\n\nenum KeyboardCode {\n Space = 'Space',\n Down = 'ArrowDown',\n Right = 'ArrowRight',\n Left = 'ArrowLeft',\n Up = 'ArrowUp',\n Esc = 'Escape',\n Enter = 'Enter',\n}\n\n// A custom collision detection algorithm is used when using a keyboard to\n// work around an unexpected behavior when reordering items of variable height\n// with the keyboard.\n\n// Neither closestCenter nor closestCorners work really well for this case,\n// because the center (or corners) of a tall rectangle might be so low that it\n// is detected as being closest to the rectangle below of the one it should\n// actually swap with.\n\n// Instead of relying on coordinates, the expected results are achieved by\n// moving X positions up or down in the initially sorted array, depending on\n// the desired direction.\n\n// We let our collisionDetection and customCoordinateGetter use the same\n// getClosestId function which takes its value from the current component\n// state, to make sure they are always in sync.\n\nexport default function useDragAndDropReorder<Data>({ items }: { items: readonly DndContainerItem<Data>[] }) {\n const isKeyboard = useRef(false);\n const positionDelta = useRef(0);\n const [activeItemId, setActiveItemId] = useState<UniqueIdentifier | null>(null);\n\n const setActiveItem = (id: UniqueIdentifier | null) => {\n setActiveItemId(id);\n if (!id) {\n isKeyboard.current = false;\n positionDelta.current = 0;\n }\n };\n\n const handleKeyDown = (event: React.KeyboardEvent) => {\n if (isKeyboard.current && activeItemId) {\n const currentTargetIndex = items.findIndex(item => item.id === activeItemId) + positionDelta.current;\n if (event.key === 'ArrowDown' && currentTargetIndex < items.length - 1) {\n positionDelta.current += 1;\n } else if (event.key === 'ArrowUp' && currentTargetIndex > 0) {\n positionDelta.current -= 1;\n }\n }\n if (activeItemId && isEscape(event.key)) {\n // Prevent modal from closing when pressing Esc to cancel the dragging action\n event.stopPropagation();\n }\n };\n\n const getClosestId = (active: Active) => {\n if (positionDelta.current === 0) {\n return active.id;\n }\n const currentIndex = items.findIndex(item => item.id === active.id);\n const newIndex = Math.max(0, Math.min(items.length - 1, currentIndex + positionDelta.current));\n return items[newIndex].id;\n };\n\n const collisionDetection: CollisionDetection = ({\n active,\n collisionRect,\n droppableContainers,\n droppableRects,\n pointerCoordinates,\n }) => {\n if (isKeyboard.current) {\n // For keyboard interaction, determine the colliding container based on the movements made by the arrow keys,\n // via getClosestId\n const collidingContainer = getCollidingContainer({\n activeId: active.id,\n closestId: getClosestId(active),\n droppableContainers,\n });\n return collidingContainer ? [collidingContainer] : [];\n } else {\n // For mouse interaction, use the closest center algorithm\n return closestCenter({ active, collisionRect, droppableRects, droppableContainers, pointerCoordinates });\n }\n };\n\n const coordinateGetter: KeyboardCoordinateGetter = (\n event,\n { context: { active, collisionRect, droppableRects, droppableContainers } }\n ) => {\n if (event.code === KeyboardCode.Up || event.code === KeyboardCode.Down) {\n event.preventDefault();\n\n if (!active || !collisionRect) {\n return;\n }\n\n const closestId = getClosestId(active);\n\n if (closestId !== null) {\n const activeDroppable = droppableContainers.get(active.id);\n const newDroppable = droppableContainers.get(closestId);\n const newRect = newDroppable ? droppableRects.get(newDroppable.id) : null;\n const newNode = newDroppable?.node.current;\n\n if (newNode && newRect && activeDroppable && newDroppable) {\n const isAfterActive = isAfter(activeDroppable, newDroppable);\n const offset = {\n x: isAfterActive ? collisionRect.width - newRect.width : 0,\n y: isAfterActive ? collisionRect.height - newRect.height : 0,\n };\n const rectCoordinates = {\n x: newRect.left,\n y: newRect.top,\n };\n\n return {\n x: rectCoordinates.x - offset.x,\n y: rectCoordinates.y - offset.y,\n };\n }\n }\n }\n };\n\n const sensors = useSensors(\n useSensor(PointerSensor),\n useSensor(KeyboardSensor, {\n coordinateGetter,\n onActivation: () => {\n isKeyboard.current = true;\n },\n })\n );\n\n return {\n activeItemId,\n setActiveItemId: setActiveItem,\n collisionDetection,\n coordinateGetter,\n handleKeyDown,\n sensors,\n };\n}\n\nfunction isAfter(a: DroppableContainer, b: DroppableContainer) {\n return hasSortableData(a) && hasSortableData(b) && a.data.current.sortable.index < b.data.current.sortable.index;\n}\n\nfunction getCollidingContainer({\n activeId,\n closestId,\n droppableContainers,\n}: {\n activeId: UniqueIdentifier;\n closestId: UniqueIdentifier;\n droppableContainers: DroppableContainer[];\n}) {\n if (closestId === activeId) {\n return;\n }\n const collidingContainer = droppableContainers.find(({ id }) => id === closestId);\n if (collidingContainer) {\n return {\n id: collidingContainer.id,\n data: {\n droppableContainer: collidingContainer,\n value: 0,\n },\n };\n }\n}\n\nconst isEscape = (key: string) => key === 'Escape' || key === 'Esc';\n"]}
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
import { DragEndEvent, DragOverEvent, DragStartEvent } from '@dnd-kit/core';
|
|
2
|
-
import {
|
|
3
|
-
export default function useLiveAnnouncements<
|
|
2
|
+
import { DndContainerI18nStrings, DndContainerItem } from './interfaces';
|
|
3
|
+
export default function useLiveAnnouncements<Data>({ items, isDragging, liveAnnouncementDndStarted, liveAnnouncementDndItemReordered, liveAnnouncementDndItemCommitted, liveAnnouncementDndDiscarded, }: {
|
|
4
|
+
items: readonly DndContainerItem<Data>[];
|
|
4
5
|
isDragging: boolean;
|
|
5
|
-
}): {
|
|
6
|
+
} & DndContainerI18nStrings): {
|
|
6
7
|
onDragStart({ active }: DragStartEvent): string | undefined;
|
|
7
8
|
onDragOver({ active, over }: DragOverEvent): string | undefined;
|
|
8
9
|
onDragEnd({ active, over }: DragEndEvent): string | undefined;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-live-announcements.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-live-announcements.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE5E,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"use-live-announcements.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-live-announcements.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,YAAY,EAAE,aAAa,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAE5E,OAAO,EAAE,uBAAuB,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAEzE,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAAC,IAAI,EAAE,EACjD,KAAK,EACL,UAAU,EACV,0BAA0B,EAC1B,gCAAgC,EAChC,gCAAgC,EAChC,4BAA4B,GAC7B,EAAE;IAAE,KAAK,EAAE,SAAS,gBAAgB,CAAC,IAAI,CAAC,EAAE,CAAC;IAAC,UAAU,EAAE,OAAO,CAAA;CAAE,GAAG,uBAAuB;4BAOlE,cAAc;iCAMT,aAAa;gCAcd,YAAY;;EAW3C"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { useRef } from 'react';
|
|
4
|
-
export default function useLiveAnnouncements({ isDragging, liveAnnouncementDndStarted, liveAnnouncementDndItemReordered, liveAnnouncementDndItemCommitted, liveAnnouncementDndDiscarded,
|
|
4
|
+
export default function useLiveAnnouncements({ items, isDragging, liveAnnouncementDndStarted, liveAnnouncementDndItemReordered, liveAnnouncementDndItemCommitted, liveAnnouncementDndDiscarded, }) {
|
|
5
5
|
const isFirstAnnouncement = useRef(true);
|
|
6
6
|
if (!isDragging) {
|
|
7
7
|
isFirstAnnouncement.current = true;
|
|
@@ -9,8 +9,8 @@ export default function useLiveAnnouncements({ isDragging, liveAnnouncementDndSt
|
|
|
9
9
|
return {
|
|
10
10
|
onDragStart({ active }) {
|
|
11
11
|
if (active && liveAnnouncementDndStarted) {
|
|
12
|
-
const index =
|
|
13
|
-
return liveAnnouncementDndStarted(index + 1,
|
|
12
|
+
const index = items.findIndex(item => item.id === active.id);
|
|
13
|
+
return liveAnnouncementDndStarted(index + 1, items.length);
|
|
14
14
|
}
|
|
15
15
|
},
|
|
16
16
|
onDragOver({ active, over }) {
|
|
@@ -22,16 +22,16 @@ export default function useLiveAnnouncements({ isDragging, liveAnnouncementDndSt
|
|
|
22
22
|
return;
|
|
23
23
|
}
|
|
24
24
|
}
|
|
25
|
-
const initialIndex =
|
|
26
|
-
const currentIdex = over ?
|
|
27
|
-
return liveAnnouncementDndItemReordered(initialIndex + 1, currentIdex + 1,
|
|
25
|
+
const initialIndex = items.findIndex(item => item.id === active.id);
|
|
26
|
+
const currentIdex = over ? items.findIndex(item => item.id === over.id) : initialIndex;
|
|
27
|
+
return liveAnnouncementDndItemReordered(initialIndex + 1, currentIdex + 1, items.length);
|
|
28
28
|
}
|
|
29
29
|
},
|
|
30
30
|
onDragEnd({ active, over }) {
|
|
31
31
|
if (liveAnnouncementDndItemCommitted) {
|
|
32
|
-
const initialIndex =
|
|
33
|
-
const finalIndex = over ?
|
|
34
|
-
return liveAnnouncementDndItemCommitted(initialIndex + 1, finalIndex + 1,
|
|
32
|
+
const initialIndex = items.findIndex(item => item.id === active.id);
|
|
33
|
+
const finalIndex = over ? items.findIndex(item => item.id === over.id) : initialIndex;
|
|
34
|
+
return liveAnnouncementDndItemCommitted(initialIndex + 1, finalIndex + 1, items.length);
|
|
35
35
|
}
|
|
36
36
|
},
|
|
37
37
|
onDragCancel() {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"use-live-announcements.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-live-announcements.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAK/B,MAAM,CAAC,OAAO,UAAU,oBAAoB,
|
|
1
|
+
{"version":3,"file":"use-live-announcements.js","sourceRoot":"","sources":["../../../../../src/internal/components/dnd-container/use-live-announcements.ts"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AAEtC,OAAO,EAAE,MAAM,EAAE,MAAM,OAAO,CAAC;AAK/B,MAAM,CAAC,OAAO,UAAU,oBAAoB,CAAO,EACjD,KAAK,EACL,UAAU,EACV,0BAA0B,EAC1B,gCAAgC,EAChC,gCAAgC,EAChC,4BAA4B,GACgE;IAC5F,MAAM,mBAAmB,GAAG,MAAM,CAAC,IAAI,CAAC,CAAC;IACzC,IAAI,CAAC,UAAU,EAAE;QACf,mBAAmB,CAAC,OAAO,GAAG,IAAI,CAAC;KACpC;IAED,OAAO;QACL,WAAW,CAAC,EAAE,MAAM,EAAkB;YACpC,IAAI,MAAM,IAAI,0BAA0B,EAAE;gBACxC,MAAM,KAAK,GAAG,KAAK,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,EAAE,KAAK,MAAM,CAAC,EAAE,CAAC,CAAC;gBAC7D,OAAO,0BAA0B,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,CAAC,MAAM,CAAC,CAAC;aAC5D;QACH,CA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,4BAA4B,CAAC;QACtC,CAAC;KACF,CAAC;AACJ,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { useRef } from 'react';\nimport { DragEndEvent, DragOverEvent, DragStartEvent } from '@dnd-kit/core';\n\nimport { DndContainerI18nStrings, DndContainerItem } from './interfaces';\n\nexport default function useLiveAnnouncements<Data>({\n items,\n isDragging,\n liveAnnouncementDndStarted,\n liveAnnouncementDndItemReordered,\n liveAnnouncementDndItemCommitted,\n liveAnnouncementDndDiscarded,\n}: { items: readonly DndContainerItem<Data>[]; isDragging: boolean } & DndContainerI18nStrings) {\n const isFirstAnnouncement = useRef(true);\n if (!isDragging) {\n isFirstAnnouncement.current = true;\n }\n\n return {\n onDragStart({ active }: DragStartEvent) {\n if (active && liveAnnouncementDndStarted) {\n const index = items.findIndex(item => item.id === active.id);\n return liveAnnouncementDndStarted(index + 1, items.length);\n }\n },\n onDragOver({ active, over }: DragOverEvent) {\n if (liveAnnouncementDndItemReordered) {\n // Don't announce on the first dragOver because it's redundant with onDragStart.\n if (isFirstAnnouncement.current) {\n isFirstAnnouncement.current = false;\n if (!over || over.id === active.id) {\n return;\n }\n }\n const initialIndex = items.findIndex(item => item.id === active.id);\n const currentIdex = over ? items.findIndex(item => item.id === over.id) : initialIndex;\n return liveAnnouncementDndItemReordered(initialIndex + 1, currentIdex + 1, items.length);\n }\n },\n onDragEnd({ active, over }: DragEndEvent) {\n if (liveAnnouncementDndItemCommitted) {\n const initialIndex = items.findIndex(item => item.id === active.id);\n const finalIndex = over ? items.findIndex(item => item.id === over.id) : initialIndex;\n return liveAnnouncementDndItemCommitted(initialIndex + 1, finalIndex + 1, items.length);\n }\n },\n onDragCancel() {\n return liveAnnouncementDndDiscarded;\n },\n };\n}\n"]}
|
|
@@ -3,7 +3,6 @@ export interface DragHandleProps {
|
|
|
3
3
|
attributes: ButtonHTMLAttributes<HTMLDivElement>;
|
|
4
4
|
hideFocus?: boolean;
|
|
5
5
|
listeners: Record<string, Function> | undefined;
|
|
6
|
-
disabled?: boolean;
|
|
7
6
|
}
|
|
8
|
-
export default function DragHandle({ attributes, hideFocus, listeners
|
|
7
|
+
export default function DragHandle({ attributes, hideFocus, listeners }: DragHandleProps): JSX.Element;
|
|
9
8
|
//# sourceMappingURL=index.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/drag-handle/index.tsx"],"names":[],"mappings":"AAEA,OAAc,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../../src/internal/components/drag-handle/index.tsx"],"names":[],"mappings":"AAEA,OAAc,EAAE,oBAAoB,EAAE,MAAM,OAAO,CAAC;AAOpD,MAAM,WAAW,eAAe;IAC9B,UAAU,EAAE,oBAAoB,CAAC,cAAc,CAAC,CAAC;IACjD,SAAS,CAAC,EAAE,OAAO,CAAC;IAGpB,SAAS,EAAE,MAAM,CAAC,MAAM,EAAE,QAAQ,CAAC,GAAG,SAAS,CAAC;CACjD;AAED,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAE,EAAE,eAAe,eAkBvF"}
|
|
@@ -3,11 +3,16 @@
|
|
|
3
3
|
import React from 'react';
|
|
4
4
|
import clsx from 'clsx';
|
|
5
5
|
import InternalIcon from '../../../icon/internal';
|
|
6
|
-
import Handle from '../handle';
|
|
7
|
-
import handleStyles from '../handle/styles.css.js';
|
|
8
6
|
import styles from './styles.css.js';
|
|
9
|
-
export default function DragHandle({ attributes, hideFocus, listeners
|
|
10
|
-
|
|
7
|
+
export default function DragHandle({ attributes, hideFocus, listeners }) {
|
|
8
|
+
const disabled = attributes['aria-disabled'];
|
|
9
|
+
return (
|
|
10
|
+
// We need to use a div with button role instead of a button
|
|
11
|
+
// so that Safari will focus on it when clicking it.
|
|
12
|
+
// (See https://bugs.webkit.org/show_bug.cgi?id=22261)
|
|
13
|
+
// Otherwise, we can't reliably catch keyboard events coming from the handle
|
|
14
|
+
// when it is being dragged.
|
|
15
|
+
React.createElement("div", Object.assign({ role: "button", tabIndex: 0, className: clsx(styles.handle, hideFocus && styles['hide-focus'], disabled && styles['handle-disabled']) }, attributes, listeners),
|
|
11
16
|
React.createElement(InternalIcon, { variant: disabled ? 'disabled' : undefined, name: "drag-indicator" })));
|
|
12
17
|
}
|
|
13
18
|
//# sourceMappingURL=index.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/internal/components/drag-handle/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAO,KAA+B,MAAM,OAAO,CAAC;AACpD,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,YAAY,MAAM,wBAAwB,CAAC;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../../../../src/internal/components/drag-handle/index.tsx"],"names":[],"mappings":"AAAA,qEAAqE;AACrE,sCAAsC;AACtC,OAAO,KAA+B,MAAM,OAAO,CAAC;AACpD,OAAO,IAAI,MAAM,MAAM,CAAC;AAExB,OAAO,YAAY,MAAM,wBAAwB,CAAC;AAElD,OAAO,MAAM,MAAM,iBAAiB,CAAC;AAUrC,MAAM,CAAC,OAAO,UAAU,UAAU,CAAC,EAAE,UAAU,EAAE,SAAS,EAAE,SAAS,EAAmB;IACtF,MAAM,QAAQ,GAAG,UAAU,CAAC,eAAe,CAAC,CAAC;IAC7C,OAAO;IACL,4DAA4D;IAC5D,oDAAoD;IACpD,sDAAsD;IACtD,4EAA4E;IAC5E,4BAA4B;IAC5B,2CACE,IAAI,EAAC,QAAQ,EACb,QAAQ,EAAE,CAAC,EACX,SAAS,EAAE,IAAI,CAAC,MAAM,CAAC,MAAM,EAAE,SAAS,IAAI,MAAM,CAAC,YAAY,CAAC,EAAE,QAAQ,IAAI,MAAM,CAAC,iBAAiB,CAAC,CAAC,IACpG,UAAU,EACV,SAAS;QAEb,oBAAC,YAAY,IAAC,OAAO,EAAE,QAAQ,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,SAAS,EAAE,IAAI,EAAC,gBAAgB,GAAG,CAC9E,CACP,CAAC;AACJ,CAAC","sourcesContent":["// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.\n// SPDX-License-Identifier: Apache-2.0\nimport React, { ButtonHTMLAttributes } from 'react';\nimport clsx from 'clsx';\n\nimport InternalIcon from '../../../icon/internal';\n\nimport styles from './styles.css.js';\n\nexport interface DragHandleProps {\n attributes: ButtonHTMLAttributes<HTMLDivElement>;\n hideFocus?: boolean;\n // @dnd-kit uses this type\n // eslint-disable-next-line @typescript-eslint/ban-types\n listeners: Record<string, Function> | undefined;\n}\n\nexport default function DragHandle({ attributes, hideFocus, listeners }: DragHandleProps) {\n const disabled = attributes['aria-disabled'];\n return (\n // We need to use a div with button role instead of a button\n // so that Safari will focus on it when clicking it.\n // (See https://bugs.webkit.org/show_bug.cgi?id=22261)\n // Otherwise, we can't reliably catch keyboard events coming from the handle\n // when it is being dragged.\n <div\n role=\"button\"\n tabIndex={0}\n className={clsx(styles.handle, hideFocus && styles['hide-focus'], disabled && styles['handle-disabled'])}\n {...attributes}\n {...listeners}\n >\n <InternalIcon variant={disabled ? 'disabled' : undefined} name=\"drag-indicator\" />\n </div>\n );\n}\n"]}
|
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
|
|
2
2
|
import './styles.scoped.css';
|
|
3
3
|
export default {
|
|
4
|
-
"handle": "
|
|
5
|
-
"
|
|
4
|
+
"handle": "awsui_handle_sdha6_yy78y_145",
|
|
5
|
+
"hide-focus": "awsui_hide-focus_sdha6_yy78y_162",
|
|
6
|
+
"handle-disabled": "awsui_handle-disabled_sdha6_yy78y_191"
|
|
6
7
|
};
|
|
7
8
|
|