@editora/ui-sortable 0.1.12 → 0.2.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.
@@ -0,0 +1,106 @@
1
+ export type SortableDirection = 'vertical' | 'horizontal' | 'grid';
2
+ export type SortableSource = 'pointer' | 'keyboard';
3
+ export interface SortableMove {
4
+ item: HTMLElement;
5
+ from: HTMLElement;
6
+ to: HTMLElement;
7
+ /** The index the item would have in `to`. */
8
+ newIndex: number;
9
+ }
10
+ export interface SortableEvent extends SortableMove {
11
+ /** `update`: moved within its list. `remove` (on the list it left) and `add` (on the list it joined): moved between lists. */
12
+ type: 'update' | 'add' | 'remove';
13
+ itemId: string | null;
14
+ fromId: string | null;
15
+ toId: string | null;
16
+ oldIndex: number;
17
+ source: SortableSource;
18
+ }
19
+ export interface SortableEndEvent extends SortableMove {
20
+ itemId: string | null;
21
+ oldIndex: number;
22
+ /** The item ended somewhere else than it started. */
23
+ changed: boolean;
24
+ /** Escape, a cancelled pointer, or a drop that canMove refused. */
25
+ cancelled: boolean;
26
+ source: SortableSource;
27
+ }
28
+ export interface SortableAnnouncementContext {
29
+ item: HTMLElement;
30
+ itemLabel: string;
31
+ listLabel: string;
32
+ position: number;
33
+ total: number;
34
+ }
35
+ export interface SortableAnnouncements {
36
+ instructions: string;
37
+ lifted: (context: SortableAnnouncementContext) => string;
38
+ moved: (context: SortableAnnouncementContext) => string;
39
+ movedToList: (context: SortableAnnouncementContext) => string;
40
+ dropped: (context: SortableAnnouncementContext) => string;
41
+ cancelled: (context: SortableAnnouncementContext) => string;
42
+ }
43
+ export interface SortableOptions {
44
+ /** Which children are items (a selector). Left out: every child element. */
45
+ itemSelector?: string;
46
+ /** Start a pointer drag only from this part of an item (a selector), such as a grip icon. The whole item otherwise. */
47
+ handle?: string;
48
+ /** Lists with the same group exchange items. */
49
+ group?: string;
50
+ /** This list's id in events. Left out: the element's data-sortable-id or id. */
51
+ id?: string;
52
+ /** How the items are laid out, for the arrow keys. Left out: worked out from where the items are. */
53
+ direction?: SortableDirection;
54
+ disabled?: boolean;
55
+ /** How long the other items take to slide aside, in ms (0: no animation; none with prefers-reduced-motion). */
56
+ animation?: number;
57
+ /** For touch: how long to press before an item lifts, in ms, so that a swipe still scrolls the page. */
58
+ touchDelay?: number;
59
+ /** How far a mouse or pen must move, in px, before a press becomes a drag (a shorter move is a click). */
60
+ threshold?: number;
61
+ /** Space, arrows and Escape move the items, which become focusable. */
62
+ keyboard?: boolean;
63
+ /** Put the elements back after a drop and leave the reordering to you (for frameworks that own the DOM). */
64
+ revert?: boolean;
65
+ /** Each item's id, for events and toArray(). Left out: its data-id, or its id. */
66
+ getId?: (item: HTMLElement) => string | null;
67
+ /** Return false to refuse a move (a full list, a locked item...). */
68
+ canMove?: (move: SortableMove) => boolean;
69
+ onStart?: (event: {
70
+ item: HTMLElement;
71
+ from: HTMLElement;
72
+ oldIndex: number;
73
+ source: SortableSource;
74
+ }) => void;
75
+ /** The order changed. Called on the list that changed, or on both lists for a move between them (remove, then add). */
76
+ onSort?: (event: SortableEvent) => void;
77
+ /** Every drag or keyboard move ends here, also when nothing moved or it was cancelled. Called on the list it started in. */
78
+ onEnd?: (event: SortableEndEvent) => void;
79
+ /** Where the copy that follows the pointer goes. Left out: into the list, so the list's CSS styles it as it styles
80
+ * the items. Pass document.body for a list that clips what overflows it. */
81
+ ghostParent?: HTMLElement;
82
+ /** A class for the copy that follows the pointer. Given, it replaces the built-in look (shadow, slight lift). */
83
+ ghostClass?: string;
84
+ /** A class for the item while it is dragged. Given, it replaces the built-in look (faded). */
85
+ placeholderClass?: string;
86
+ /** What screen readers are told; any of them can be replaced, for another language. */
87
+ announcements?: Partial<SortableAnnouncements>;
88
+ }
89
+ export interface Sortable {
90
+ readonly element: HTMLElement;
91
+ /** The ids of the items, in order. */
92
+ toArray(): string[];
93
+ /** Reorder the items to these ids (items without an id keep their place at the end). */
94
+ sort(ids: string[]): void;
95
+ /** Change options; the others are kept. */
96
+ update(options: Partial<SortableOptions>): void;
97
+ destroy(): void;
98
+ }
99
+ /** Moves an item of an array from one index to another, in a new array. */
100
+ export declare function moveItem<T>(list: readonly T[], from: number, to: number): T[];
101
+ /** Moves an item from one array to another, in new arrays: [from without it, to with it]. */
102
+ export declare function moveBetween<T>(from: readonly T[], to: readonly T[], fromIndex: number, toIndex: number): [T[], T[]];
103
+ /** Applies an onSort event to lists kept in one object, keyed by list id: returns a new object. */
104
+ export declare function applySortEvent<T>(lists: Record<string, readonly T[]>, event: SortableEvent): Record<string, T[]>;
105
+ /** Makes the children of `element` sortable. Returns the list: toArray(), sort(ids), update(options), destroy(). */
106
+ export declare function createSortable(element: HTMLElement, options?: SortableOptions): Sortable;
@@ -0,0 +1,2 @@
1
+ export { UISortable } from '@editora/ui-core/sortable';
2
+ export type { UISortableList, UISortableItem, UISortableMoveOptions, UISortableDropMode, UISortableSortMode, UISortableChangeSource, UISortableOperation, UISortableSelectionChangeDetail, UISortablePersistenceRecord, UISortablePersistenceSnapshot, UISortableChangeDetail, UISortableDropIndicatorVisibility, UISortableDropzoneStyle, UISortableDragHandleMode, UISortableListOrientation, UISortableLabels, UISortableNestTrigger, UISortableDropInfo, UISortableBeforeChangeDetail, UISortableRenderItemContext, UISortableRenderListHeaderContext, UISortableRenderEmptyStateContext, UISortableRenderResult, UISortableExternalMove } from '@editora/ui-core/sortable';
@@ -0,0 +1,22 @@
1
+ import React from 'react';
2
+ export declare const useIsomorphicLayoutEffect: typeof React.useEffect;
3
+ export type ElementEventListener = {
4
+ type: string;
5
+ listener?: EventListener | null;
6
+ };
7
+ /**
8
+ * Builds the `<ui-sortable>` host. React 16-18 only map `className` to `class` on built-in
9
+ * elements and write a literal `classname` attribute on custom elements, which silently drops the
10
+ * consumer's class (React 19 maps it, and `class` works on all of them).
11
+ */
12
+ export declare function createUIElement(tag: string, props?: (Record<string, unknown> & {
13
+ className?: unknown;
14
+ class?: unknown;
15
+ }) | null, ...children: React.ReactNode[]): React.ReactElement;
16
+ export declare function useForwardedHostRef<T extends HTMLElement>(forwardedRef: React.ForwardedRef<T>): React.MutableRefObject<T | null>;
17
+ export declare function useElementEventListeners<T extends HTMLElement>(ref: React.RefObject<T | null>, listeners: ElementEventListener[], dependencies: React.DependencyList): void;
18
+ export declare function useElementAttributes<T extends HTMLElement>(ref: React.RefObject<T | null>, sync: (element: T) => void, dependencies: React.DependencyList): void;
19
+ export declare function getCustomEventDetail<T>(event: Event): T | undefined;
20
+ export declare function syncStringAttribute(el: HTMLElement, name: string, next: string | null): void;
21
+ export declare function syncBooleanAttribute(el: HTMLElement, name: string, next: boolean | undefined): void;
22
+ export declare function syncJsonAttribute(el: HTMLElement, name: string, next: unknown): void;
@@ -0,0 +1,134 @@
1
+ import React from 'react';
2
+ import './index';
3
+ import type { UISortable, UISortableList, UISortableItem, UISortableMoveOptions, UISortableDropMode, UISortableSortMode, UISortableChangeSource, UISortableOperation, UISortableSelectionChangeDetail, UISortablePersistenceRecord, UISortablePersistenceSnapshot, UISortableChangeDetail, UISortableDropIndicatorVisibility, UISortableDropzoneStyle, UISortableDragHandleMode, UISortableListOrientation, UISortableLabels, UISortableNestTrigger, UISortableDropInfo, UISortableBeforeChangeDetail, UISortableExternalMove } from '@editora/ui-core/sortable';
4
+ export type SortableListOrientation = UISortableListOrientation;
5
+ export type SortableSortMode = UISortableSortMode;
6
+ export type SortableDropMode = UISortableDropMode;
7
+ export type SortableChangeSource = UISortableChangeSource;
8
+ export type SortableOperation = UISortableOperation;
9
+ export type SortableDropIndicatorVisibility = UISortableDropIndicatorVisibility;
10
+ export type SortableDropzoneStyle = UISortableDropzoneStyle;
11
+ export type SortableDragPreviewSize = 'match-item' | 'compact';
12
+ export type SortableDragHandleMode = UISortableDragHandleMode;
13
+ export type SortableLabels = Partial<UISortableLabels>;
14
+ export type SortableNestTrigger = UISortableNestTrigger;
15
+ export type SortableRadiusPreset = 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full';
16
+ export type SortableRadius = SortableRadiusPreset | number | string;
17
+ export type SortableSpacePreset = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
18
+ export type SortableSpace = SortableSpacePreset | number | string;
19
+ export type SortableList = UISortableList;
20
+ export type SortableItem = UISortableItem;
21
+ export type SortableSelectionChangeDetail = UISortableSelectionChangeDetail;
22
+ export type SortablePersistenceRecord = UISortablePersistenceRecord;
23
+ export type SortablePersistenceSnapshot = UISortablePersistenceSnapshot;
24
+ export type SortableChangeDetail = UISortableChangeDetail;
25
+ export type SortableBeforeChangeDetail = UISortableBeforeChangeDetail;
26
+ export type SortableDropInfo = UISortableDropInfo;
27
+ export type SortableExternalMove = UISortableExternalMove;
28
+ export type SortableMoveOptions = UISortableMoveOptions;
29
+ export type SortableElement = UISortable;
30
+ export type SortableRenderItemContext = {
31
+ selected: boolean;
32
+ disabled: boolean;
33
+ dragDisabled: boolean;
34
+ cloneOnDrag: boolean;
35
+ list?: SortableList;
36
+ };
37
+ export type SortableRenderListHeaderContext = {
38
+ itemCount: number;
39
+ dragLocked: boolean;
40
+ };
41
+ export type SortableRenderEmptyStateContext = {
42
+ itemCount: number;
43
+ dragLocked: boolean;
44
+ filterQuery: string;
45
+ };
46
+ export type SortableProps = Omit<React.HTMLAttributes<SortableElement>, 'onChange'> & {
47
+ lists: SortableList[];
48
+ items: SortableItem[];
49
+ selection?: string[];
50
+ filterQuery?: string;
51
+ sort?: SortableSortMode;
52
+ persistKey?: string;
53
+ allowFilteredDrag?: boolean;
54
+ allowNesting?: boolean;
55
+ nestTrigger?: SortableNestTrigger;
56
+ /** Sortables with the same group can exchange items: drop a card on another one and both report it (`change`'s `external`). Ids must be unique across the group. */
57
+ group?: string;
58
+ /** Milliseconds a finger must rest on a card before it lifts (touch, dragging by the card). 0 lifts at once. Default 350. */
59
+ touchHoldDelay?: number;
60
+ /** The tallest the cards of a list can be before the list scrolls on its own: a number of px or a CSS length such as "20rem". A list's own `maxHeight` overrides it. */
61
+ listMaxHeight?: number | string;
62
+ dropIndicatorVisibility?: SortableDropIndicatorVisibility;
63
+ dropzoneStyle?: SortableDropzoneStyle;
64
+ dragPreviewSize?: SortableDragPreviewSize;
65
+ dragHandleMode?: SortableDragHandleMode;
66
+ dragHandleSelector?: string;
67
+ itemRadius?: SortableRadius;
68
+ handleRadius?: SortableRadius;
69
+ listGap?: SortableSpace;
70
+ itemSpacing?: SortableSpace;
71
+ /** Decides whether items may go somewhere; return false to refuse. Asked on every pointer move, so keep it quick. */
72
+ canDrop?: (info: SortableDropInfo) => boolean;
73
+ showSelectionBadge?: boolean;
74
+ labels?: SortableLabels;
75
+ hideToolbar?: boolean;
76
+ disabled?: boolean;
77
+ renderItem?: (item: SortableItem, context: SortableRenderItemContext) => React.ReactNode;
78
+ renderListHeader?: (list: SortableList, context: SortableRenderListHeaderContext) => React.ReactNode;
79
+ renderEmptyState?: (list: SortableList, context: SortableRenderEmptyStateContext) => React.ReactNode;
80
+ /**
81
+ * Called just before a move is applied, with the result it would have. Return `false` (or call
82
+ * `event.preventDefault()`) to cancel it: nothing changes and `onChange` does not fire.
83
+ */
84
+ onBeforeChange?: (detail: SortableBeforeChangeDetail, event: Event) => boolean | void;
85
+ onChange?: (detail: SortableChangeDetail) => void;
86
+ onItemsChange?: (items: SortableItem[]) => void;
87
+ onSelectionChange?: (detail: SortableSelectionChangeDetail) => void;
88
+ onPersistRequest?: (detail: SortableChangeDetail) => void;
89
+ };
90
+ export declare const Sortable: React.ForwardRefExoticComponent<Omit<React.HTMLAttributes<UISortable>, "onChange"> & {
91
+ lists: SortableList[];
92
+ items: SortableItem[];
93
+ selection?: string[];
94
+ filterQuery?: string;
95
+ sort?: SortableSortMode;
96
+ persistKey?: string;
97
+ allowFilteredDrag?: boolean;
98
+ allowNesting?: boolean;
99
+ nestTrigger?: SortableNestTrigger;
100
+ /** Sortables with the same group can exchange items: drop a card on another one and both report it (`change`'s `external`). Ids must be unique across the group. */
101
+ group?: string;
102
+ /** Milliseconds a finger must rest on a card before it lifts (touch, dragging by the card). 0 lifts at once. Default 350. */
103
+ touchHoldDelay?: number;
104
+ /** The tallest the cards of a list can be before the list scrolls on its own: a number of px or a CSS length such as "20rem". A list's own `maxHeight` overrides it. */
105
+ listMaxHeight?: number | string;
106
+ dropIndicatorVisibility?: SortableDropIndicatorVisibility;
107
+ dropzoneStyle?: SortableDropzoneStyle;
108
+ dragPreviewSize?: SortableDragPreviewSize;
109
+ dragHandleMode?: SortableDragHandleMode;
110
+ dragHandleSelector?: string;
111
+ itemRadius?: SortableRadius;
112
+ handleRadius?: SortableRadius;
113
+ listGap?: SortableSpace;
114
+ itemSpacing?: SortableSpace;
115
+ /** Decides whether items may go somewhere; return false to refuse. Asked on every pointer move, so keep it quick. */
116
+ canDrop?: (info: SortableDropInfo) => boolean;
117
+ showSelectionBadge?: boolean;
118
+ labels?: SortableLabels;
119
+ hideToolbar?: boolean;
120
+ disabled?: boolean;
121
+ renderItem?: (item: SortableItem, context: SortableRenderItemContext) => React.ReactNode;
122
+ renderListHeader?: (list: SortableList, context: SortableRenderListHeaderContext) => React.ReactNode;
123
+ renderEmptyState?: (list: SortableList, context: SortableRenderEmptyStateContext) => React.ReactNode;
124
+ /**
125
+ * Called just before a move is applied, with the result it would have. Return `false` (or call
126
+ * `event.preventDefault()`) to cancel it: nothing changes and `onChange` does not fire.
127
+ */
128
+ onBeforeChange?: (detail: SortableBeforeChangeDetail, event: Event) => boolean | void;
129
+ onChange?: (detail: SortableChangeDetail) => void;
130
+ onItemsChange?: (items: SortableItem[]) => void;
131
+ onSelectionChange?: (detail: SortableSelectionChangeDetail) => void;
132
+ onPersistRequest?: (detail: SortableChangeDetail) => void;
133
+ } & React.RefAttributes<UISortable>>;
134
+ export default Sortable;
package/index.d.ts DELETED
@@ -1,27 +0,0 @@
1
- export { UISortable } from '@editora/ui-core/sortable';
2
- export type {
3
- UISortableList,
4
- UISortableItem,
5
- UISortableMoveOptions,
6
- UISortableDropMode,
7
- UISortableSortMode,
8
- UISortableChangeSource,
9
- UISortableOperation,
10
- UISortableSelectionChangeDetail,
11
- UISortablePersistenceRecord,
12
- UISortablePersistenceSnapshot,
13
- UISortableChangeDetail,
14
- UISortableDropIndicatorVisibility,
15
- UISortableDropzoneStyle,
16
- UISortableDragHandleMode,
17
- UISortableListOrientation,
18
- UISortableLabels,
19
- UISortableNestTrigger,
20
- UISortableDropInfo,
21
- UISortableBeforeChangeDetail,
22
- UISortableRenderItemContext,
23
- UISortableRenderListHeaderContext,
24
- UISortableRenderEmptyStateContext,
25
- UISortableRenderResult,
26
- UISortableExternalMove
27
- } from '@editora/ui-core/sortable';
package/react.d.ts DELETED
@@ -1,122 +0,0 @@
1
- import * as React from 'react';
2
- import type {
3
- UISortable,
4
- UISortableList,
5
- UISortableItem,
6
- UISortableMoveOptions,
7
- UISortableDropMode,
8
- UISortableSortMode,
9
- UISortableChangeSource,
10
- UISortableOperation,
11
- UISortableSelectionChangeDetail,
12
- UISortablePersistenceRecord,
13
- UISortablePersistenceSnapshot,
14
- UISortableChangeDetail,
15
- UISortableDropIndicatorVisibility,
16
- UISortableDropzoneStyle,
17
- UISortableDragHandleMode,
18
- UISortableListOrientation,
19
- UISortableLabels,
20
- UISortableNestTrigger,
21
- UISortableDropInfo,
22
- UISortableBeforeChangeDetail,
23
- UISortableExternalMove
24
- } from '@editora/ui-core/sortable';
25
-
26
- export type SortableListOrientation = UISortableListOrientation;
27
- export type SortableSortMode = UISortableSortMode;
28
- export type SortableDropMode = UISortableDropMode;
29
- export type SortableChangeSource = UISortableChangeSource;
30
- export type SortableOperation = UISortableOperation;
31
- export type SortableDropIndicatorVisibility = UISortableDropIndicatorVisibility;
32
- export type SortableDropzoneStyle = UISortableDropzoneStyle;
33
- export type SortableDragPreviewSize = 'match-item' | 'compact';
34
- export type SortableDragHandleMode = UISortableDragHandleMode;
35
- export type SortableLabels = Partial<UISortableLabels>;
36
- export type SortableNestTrigger = UISortableNestTrigger;
37
- export type SortableRadiusPreset = 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full';
38
- export type SortableRadius = SortableRadiusPreset | number | string;
39
- export type SortableSpacePreset = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
40
- export type SortableSpace = SortableSpacePreset | number | string;
41
-
42
- export type SortableList = UISortableList;
43
- export type SortableItem = UISortableItem;
44
- export type SortableSelectionChangeDetail = UISortableSelectionChangeDetail;
45
- export type SortablePersistenceRecord = UISortablePersistenceRecord;
46
- export type SortablePersistenceSnapshot = UISortablePersistenceSnapshot;
47
- export type SortableChangeDetail = UISortableChangeDetail;
48
- export type SortableBeforeChangeDetail = UISortableBeforeChangeDetail;
49
- export type SortableDropInfo = UISortableDropInfo;
50
- export type SortableExternalMove = UISortableExternalMove;
51
- export type SortableMoveOptions = UISortableMoveOptions;
52
- export type SortableElement = UISortable;
53
-
54
- export type SortableRenderItemContext = {
55
- selected: boolean;
56
- disabled: boolean;
57
- dragDisabled: boolean;
58
- cloneOnDrag: boolean;
59
- list?: SortableList;
60
- };
61
-
62
- export type SortableRenderListHeaderContext = {
63
- itemCount: number;
64
- dragLocked: boolean;
65
- };
66
-
67
- export type SortableRenderEmptyStateContext = {
68
- itemCount: number;
69
- dragLocked: boolean;
70
- filterQuery: string;
71
- };
72
-
73
- export type SortableProps = Omit<React.HTMLAttributes<SortableElement>, 'onChange'> & {
74
- lists: SortableList[];
75
- items: SortableItem[];
76
- selection?: string[];
77
- filterQuery?: string;
78
- sort?: SortableSortMode;
79
- persistKey?: string;
80
- allowFilteredDrag?: boolean;
81
- allowNesting?: boolean;
82
- nestTrigger?: SortableNestTrigger;
83
- /** Sortables with the same group can exchange items: drop a card on another one and both report it (`change`'s `external`). Ids must be unique across the group. */
84
- group?: string;
85
- /** Milliseconds a finger must rest on a card before it lifts (touch, dragging by the card). 0 lifts at once. Default 350. */
86
- touchHoldDelay?: number;
87
- /** The tallest the cards of a list can be before the list scrolls on its own: a number of px or a CSS length such as "20rem". A list's own `maxHeight` overrides it. */
88
- listMaxHeight?: number | string;
89
- dropIndicatorVisibility?: SortableDropIndicatorVisibility;
90
- dropzoneStyle?: SortableDropzoneStyle;
91
- dragPreviewSize?: SortableDragPreviewSize;
92
- dragHandleMode?: SortableDragHandleMode;
93
- dragHandleSelector?: string;
94
- itemRadius?: SortableRadius;
95
- handleRadius?: SortableRadius;
96
- listGap?: SortableSpace;
97
- itemSpacing?: SortableSpace;
98
- /** Decides whether items may go somewhere; return false to refuse. Asked on every pointer move, so keep it quick. */
99
- canDrop?: (info: SortableDropInfo) => boolean;
100
- showSelectionBadge?: boolean;
101
- labels?: SortableLabels;
102
- hideToolbar?: boolean;
103
- disabled?: boolean;
104
- renderItem?: (item: SortableItem, context: SortableRenderItemContext) => React.ReactNode;
105
- renderListHeader?: (list: SortableList, context: SortableRenderListHeaderContext) => React.ReactNode;
106
- renderEmptyState?: (list: SortableList, context: SortableRenderEmptyStateContext) => React.ReactNode;
107
- /**
108
- * Called just before a move is applied, with the result it would have. Return `false` (or call
109
- * `event.preventDefault()`) to cancel it: nothing changes and `onChange` does not fire.
110
- */
111
- onBeforeChange?: (detail: SortableBeforeChangeDetail, event: Event) => boolean | void;
112
- onChange?: (detail: SortableChangeDetail) => void;
113
- onItemsChange?: (items: SortableItem[]) => void;
114
- onSelectionChange?: (detail: SortableSelectionChangeDetail) => void;
115
- onPersistRequest?: (detail: SortableChangeDetail) => void;
116
- };
117
-
118
- export declare const Sortable: React.ForwardRefExoticComponent<
119
- SortableProps & React.RefAttributes<SortableElement>
120
- >;
121
-
122
- export default Sortable;