@editora/ui-sortable 0.1.0 → 0.1.2

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/README.md ADDED
@@ -0,0 +1,334 @@
1
+ # @editora/ui-sortable
2
+
3
+ [![Version](https://img.shields.io/npm/v/@editora/ui-sortable)](https://www.npmjs.com/package/@editora/ui-sortable)
4
+ [![License](https://img.shields.io/npm/l/@editora/ui-sortable)](https://github.com/ajaykr089/Editora/blob/main/LICENSE)
5
+ [![TypeScript](https://img.shields.io/badge/TypeScript-007ACC?logo=typescript&logoColor=white)](https://www.typescriptlang.org/)
6
+ [![Size](https://img.shields.io/bundlephobia/minzip/@editora/ui-sortable)](https://bundlephobia.com/package/@editora/ui-sortable)
7
+
8
+ > [!IMPORTANT]
9
+ > **Broader docs and examples:** https://editora-ecosystem.netlify.app/docs/ui-react/interactive/sortable
10
+
11
+ Standalone package for the Editora sortable web component.
12
+
13
+ `@editora/ui-sortable` is the smallest way to ship `<ui-sortable>` without importing the full `@editora/ui-core` catalog. It supports:
14
+ - single-list reordering
15
+ - multi-list transfer
16
+ - nesting and hierarchy
17
+ - multi-selection
18
+ - keyboard-accessible drag and drop
19
+ - indicator or container-style dropzones
20
+ - an optional React wrapper via `@editora/ui-sortable/react`
21
+
22
+ ## Installation
23
+
24
+ ```bash
25
+ npm install @editora/ui-sortable
26
+ ```
27
+
28
+ If you want the React wrapper entry, also install React peer dependencies:
29
+
30
+ ```bash
31
+ npm install react react-dom
32
+ ```
33
+
34
+ Or install the standalone React-ready setup in one step:
35
+
36
+ ```bash
37
+ npm install @editora/ui-sortable react react-dom
38
+ ```
39
+
40
+ ## Quick Start
41
+
42
+ Import the package once to register the custom element:
43
+
44
+ ```ts
45
+ import '@editora/ui-sortable';
46
+ ```
47
+
48
+ Then bind lists and items:
49
+
50
+ ```ts
51
+ import '@editora/ui-sortable';
52
+
53
+ const sortable = document.querySelector('ui-sortable');
54
+
55
+ sortable.lists = [
56
+ { id: 'backlog', label: 'Backlog' },
57
+ { id: 'in-progress', label: 'In Progress' },
58
+ { id: 'done', label: 'Done' }
59
+ ];
60
+
61
+ sortable.items = [
62
+ { id: 'story-1', label: 'Audit onboarding', listId: 'backlog' },
63
+ { id: 'story-2', label: 'Ship release notes', listId: 'in-progress' },
64
+ { id: 'story-3', label: 'Review handoff', listId: 'done' }
65
+ ];
66
+ ```
67
+
68
+ ```html
69
+ <ui-sortable></ui-sortable>
70
+ ```
71
+
72
+ ## React Wrapper
73
+
74
+ The package also exposes a standalone React wrapper:
75
+
76
+ ```tsx
77
+ import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
78
+
79
+ const lists: SortableList[] = [
80
+ { id: 'backlog', label: 'Backlog' },
81
+ { id: 'done', label: 'Done' }
82
+ ];
83
+
84
+ const items: SortableItem[] = [
85
+ { id: 'story-1', label: 'Audit onboarding', listId: 'backlog' },
86
+ { id: 'story-2', label: 'Ship release notes', listId: 'done' }
87
+ ];
88
+
89
+ export function Example() {
90
+ return (
91
+ <Sortable
92
+ lists={lists}
93
+ items={items}
94
+ dropzoneStyle="container"
95
+ onChange={(detail) => {
96
+ console.log(detail.items);
97
+ }}
98
+ />
99
+ );
100
+ }
101
+ ```
102
+
103
+ Use `@editora/ui-sortable/react` when you want the sortable wrapper only, without taking the rest of `@editora/ui-react`.
104
+
105
+ If you are using Next.js, App Router, or another SSR/RSC framework, import `@editora/ui-sortable/react` from a client component because it registers the custom element in the browser runtime.
106
+
107
+ ```tsx
108
+ 'use client';
109
+
110
+ import { Sortable } from '@editora/ui-sortable/react';
111
+ ```
112
+
113
+ ## React Examples
114
+
115
+ ### Controlled Board
116
+
117
+ ```tsx
118
+ import * as React from 'react';
119
+ import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
120
+
121
+ const lists: SortableList[] = [
122
+ { id: 'templates', label: 'Templates', cloneOnDrag: true },
123
+ { id: 'backlog', label: 'Backlog' },
124
+ { id: 'active', label: 'In Progress' },
125
+ { id: 'done', label: 'Done', orientation: 'horizontal' }
126
+ ];
127
+
128
+ const initialItems: SortableItem[] = [
129
+ { id: 'template-brief', label: 'Launch brief template', listId: 'templates', cloneOnDrag: true },
130
+ { id: 'epic', label: 'Release epic', listId: 'backlog' },
131
+ { id: 'brief', label: 'Draft launch brief', listId: 'backlog', parentId: 'epic' },
132
+ { id: 'review', label: 'Design review', listId: 'active' }
133
+ ];
134
+
135
+ export function ReleaseBoard() {
136
+ const [items, setItems] = React.useState(initialItems);
137
+ const [selection, setSelection] = React.useState<string[]>(['epic']);
138
+
139
+ return (
140
+ <Sortable
141
+ lists={lists}
142
+ items={items}
143
+ selection={selection}
144
+ persistKey="release-board"
145
+ onItemsChange={setItems}
146
+ onSelectionChange={(detail) => setSelection(detail.selection)}
147
+ onPersistRequest={(detail) => {
148
+ console.log(detail.persistence.records);
149
+ }}
150
+ />
151
+ );
152
+ }
153
+ ```
154
+
155
+ ### Flat List Sorting
156
+
157
+ ```tsx
158
+ import * as React from 'react';
159
+ import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
160
+
161
+ const lists: SortableList[] = [
162
+ { id: 'queue', label: 'Editorial Queue' }
163
+ ];
164
+
165
+ const initialItems: SortableItem[] = [
166
+ { id: 'headline', label: 'Headline polish', listId: 'queue' },
167
+ { id: 'legal', label: 'Legal review', listId: 'queue' },
168
+ { id: 'seo', label: 'SEO metadata', listId: 'queue' }
169
+ ];
170
+
171
+ export function FlatSortableQueue() {
172
+ const [items, setItems] = React.useState(initialItems);
173
+
174
+ return (
175
+ <Sortable
176
+ lists={lists}
177
+ items={items}
178
+ allowNesting={false}
179
+ onItemsChange={setItems}
180
+ />
181
+ );
182
+ }
183
+ ```
184
+
185
+ ### Clone Library
186
+
187
+ ```tsx
188
+ import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
189
+
190
+ const lists: SortableList[] = [
191
+ { id: 'library', label: 'Template Library', cloneOnDrag: true },
192
+ { id: 'campaign', label: 'Campaign Plan' }
193
+ ];
194
+
195
+ const items: SortableItem[] = [
196
+ { id: 'press-kit', label: 'Press kit template', listId: 'library', cloneOnDrag: true },
197
+ { id: 'email-series', label: 'Lifecycle email series', listId: 'library', cloneOnDrag: true },
198
+ { id: 'owner-sync', label: 'Owner sync', listId: 'campaign' }
199
+ ];
200
+
201
+ export function CloneLibraryExample() {
202
+ return <Sortable lists={lists} items={items} />;
203
+ }
204
+ ```
205
+
206
+ ### Container Dropzones
207
+
208
+ ```tsx
209
+ <Sortable
210
+ lists={lists}
211
+ items={items}
212
+ dropzoneStyle="container"
213
+ />
214
+ ```
215
+
216
+ ### Custom Item Rendering
217
+
218
+ ```tsx
219
+ <Sortable
220
+ lists={lists}
221
+ items={items}
222
+ selection={selection}
223
+ onItemsChange={setItems}
224
+ renderItem={(item, context) => (
225
+ <div style={{ display: 'grid', gap: 8 }}>
226
+ <div style={{ display: 'flex', justifyContent: 'space-between', gap: 12 }}>
227
+ <strong>{item.label}</strong>
228
+ <span>{context.selected ? 'Selected' : 'Ready'}</span>
229
+ </div>
230
+ {item.description && <div>{item.description}</div>}
231
+ <small>{context.list?.label}</small>
232
+ </div>
233
+ )}
234
+ />
235
+ ```
236
+
237
+ ### Custom List Headers And Empty States
238
+
239
+ ```tsx
240
+ <Sortable
241
+ lists={lists}
242
+ items={items}
243
+ renderListHeader={(list, context) => (
244
+ <div style={{ display: 'flex', justifyContent: 'space-between', gap: 12 }}>
245
+ <strong>{list.label}</strong>
246
+ <span>{context.itemCount} items</span>
247
+ </div>
248
+ )}
249
+ renderEmptyState={(list) => (
250
+ <div>
251
+ No items in {list.label} yet. Drag something here to get started.
252
+ </div>
253
+ )}
254
+ />
255
+ ```
256
+
257
+ ### Nested Hierarchy
258
+
259
+ ```tsx
260
+ import { Sortable, type SortableItem, type SortableList } from '@editora/ui-sortable/react';
261
+
262
+ const lists: SortableList[] = [
263
+ { id: 'roadmap', label: 'Roadmap' }
264
+ ];
265
+
266
+ const items: SortableItem[] = [
267
+ { id: 'initiative', label: 'Reader retention initiative', listId: 'roadmap' },
268
+ { id: 'survey', label: 'Audience survey', listId: 'roadmap', parentId: 'initiative' },
269
+ { id: 'experiment', label: 'Paywall experiment', listId: 'roadmap' }
270
+ ];
271
+
272
+ export function NestedHierarchyExample() {
273
+ return <Sortable lists={lists} items={items} />;
274
+ }
275
+ ```
276
+
277
+ For broader usage patterns, Storybook-style variations, and the full prop reference, use the Editora docs:
278
+
279
+ - https://editora-ecosystem.netlify.app/docs/ui-react/interactive/sortable
280
+
281
+ ## Dropzone Styles
282
+
283
+ Use the default insertion rails:
284
+
285
+ ```html
286
+ <ui-sortable></ui-sortable>
287
+ ```
288
+
289
+ Or switch to container-style dropzones:
290
+
291
+ ```html
292
+ <ui-sortable dropzone-style="container"></ui-sortable>
293
+ ```
294
+
295
+ ## Common Attributes
296
+
297
+ - `dropzone-style="indicator|container"`
298
+ - `drop-indicator-visibility="active|always"`
299
+ - `drag-preview-size="match-item|compact"`
300
+ - `drag-handle-mode="handle|item"`
301
+ - `allow-nesting`
302
+ - `persist-key`
303
+
304
+ ## Events
305
+
306
+ The component emits `CustomEvent`s such as:
307
+ - `change`
308
+ - `selection-change`
309
+
310
+ Each event provides structured detail payloads for the latest ordering or selection state.
311
+
312
+ ## Type Exports
313
+
314
+ The package also re-exports sortable types from `@editora/ui-core/sortable`, including:
315
+ - `UISortable`
316
+ - `UISortableList`
317
+ - `UISortableItem`
318
+ - `UISortableChangeDetail`
319
+ - `UISortableDropzoneStyle`
320
+
321
+ The React subpath exports:
322
+ - `Sortable`
323
+ - `SortableList`
324
+ - `SortableItem`
325
+ - `SortableChangeDetail`
326
+ - `SortableDropzoneStyle`
327
+
328
+ ## Development
329
+
330
+ ```bash
331
+ cd packages/ui-sortable
332
+ npm run build
333
+ npm run test
334
+ ```
@@ -0,0 +1 @@
1
+ "use strict";Object.defineProperties(exports,{__esModule:{value:!0},[Symbol.toStringTag]:{value:"Module"}});const E=require("react/jsx-runtime"),bt=require("react"),z=require("react-dom");require("@editora/ui-core/sortable");const gt=e=>e&&e.__esModule?e:{default:e},p=gt(bt),L=typeof window<"u"?p.default.useLayoutEffect:p.default.useEffect;function mt(e){const i=p.default.useRef(null);return p.default.useImperativeHandle(e,()=>i.current),i}function pt(e,i,u){p.default.useEffect(()=>{const a=e.current;if(!a)return;const f=i.filter(n=>typeof n.listener=="function");return f.forEach(({type:n,listener:b})=>{a.addEventListener(n,b)}),()=>{f.forEach(({type:n,listener:b})=>{a.removeEventListener(n,b)})}},[e,...u])}function yt(e,i,u){L(()=>{const a=e.current;a&&i(a)},[e,...u])}function H(e){return e.detail}function y(e,i,u){const a=e.getAttribute(i);if(u==null||u===""){a!=null&&e.removeAttribute(i);return}a!==u&&e.setAttribute(i,u)}function it(e,i,u){if(u){e.hasAttribute(i)||e.setAttribute(i,"");return}e.hasAttribute(i)&&e.removeAttribute(i)}function B(e,i,u){if(u==null){y(e,i,null);return}try{y(e,i,JSON.stringify(u))}catch{y(e,i,null)}}function F(e){return typeof CSS<"u"&&typeof CSS.escape=="function"?CSS.escape(e):e.replace(/"/g,'\\"')}function ot(e){if(e==null||e==="")return null;if(typeof e=="number")return`${e}px`;switch(e){case"none":return"0px";case"sm":return"10px";case"md":return"16px";case"lg":return"20px";case"xl":return"28px";case"full":return"999px";default:return e}}function ht(e){if(e==null||e==="")return null;if(typeof e=="number")return`${e}px`;switch(e){case"xs":return"6px";case"sm":return"8px";case"md":return"10px";case"lg":return"14px";case"xl":return"18px";default:return e}}const T=p.default.forwardRef(function(i,u){const{lists:a,items:f,selection:n,filterQuery:b,sort:g,persistKey:C,allowFilteredDrag:D,allowNesting:M,dropIndicatorVisibility:j,dropzoneStyle:k,dragPreviewSize:O,dragHandleMode:S,dragHandleSelector:h,itemRadius:N,handleRadius:Q,listGap:G,itemSpacing:J,showSelectionBadge:V,disabled:A,renderItem:d,renderListHeader:w,renderEmptyState:R,onChange:q,onItemsChange:P,onSelectionChange:_,onPersistRequest:$,children:at,...nt}=i,l=mt(u),[x,ut]=p.default.useState(0),K=!!(d||w||R);pt(l,[{type:"change",listener:q||P?t=>{const r=H(t);r&&(q==null||q(r),P==null||P(r.items))}:void 0},{type:"selection-change",listener:_?t=>{const r=H(t);r&&_(r)}:void 0},{type:"persist-request",listener:$?t=>{const r=H(t);r&&$(r)}:void 0}],[q,P,_,$]),yt(l,t=>{B(t,"lists",a),B(t,"items",f),B(t,"selection",n??null),y(t,"filter-query",b??null),y(t,"sort",g&&g!=="manual"?g:null),y(t,"persist-key",C??null),it(t,"allow-filtered-drag",D),M===!1?t.setAttribute("allow-nesting","false"):t.removeAttribute("allow-nesting"),j==="always"?t.setAttribute("drop-indicator-visibility","always"):t.removeAttribute("drop-indicator-visibility"),k==="container"?t.setAttribute("dropzone-style","container"):t.removeAttribute("dropzone-style"),O==="compact"?t.setAttribute("drag-preview-size","compact"):t.removeAttribute("drag-preview-size"),S==="item"?t.setAttribute("drag-handle-mode","item"):t.removeAttribute("drag-handle-mode"),y(t,"drag-handle-selector",h??null);const r=ot(N);r?t.style.setProperty("--ui-sortable-item-radius",r):t.style.removeProperty("--ui-sortable-item-radius");const s=ot(Q);s?t.style.setProperty("--ui-sortable-handle-radius",s):t.style.removeProperty("--ui-sortable-handle-radius");const o=ht(G??J);o?t.style.setProperty("--ui-sortable-list-gap",o):t.style.removeProperty("--ui-sortable-list-gap"),d?t.setAttribute("custom-item-rendering","true"):t.removeAttribute("custom-item-rendering"),V===!1?t.setAttribute("show-selection-badge","false"):t.removeAttribute("show-selection-badge"),it(t,"disabled",A)},[a,f,n,b,g,C,D,M,j,k,O,S,h,N,Q,G,J,d,V,A]),L(()=>{var rt;if(!K)return;const t=l.current,r=(rt=t==null?void 0:t.ownerDocument)==null?void 0:rt.defaultView;if(!t||!r)return;let s=!1,o=!1,c=null,v=null;const Z=[d&&f.length>0?".item-custom-content[data-item-content-target]":null,w&&a.length>0?".list-custom-header[data-list-header-target]":null,R&&a.length>0?".list-custom-empty[data-list-empty-target]":null].filter(Boolean).join(", "),tt=()=>{s||o||(o=!0,queueMicrotask(()=>{o=!1,!s&&ut(m=>m+1)}))},ft=()=>{var I;const m=(I=l.current)==null?void 0:I.shadowRoot;return m?(v||(v=new MutationObserver(()=>tt()),v.observe(m,{childList:!0,subtree:!0})),!0):!1},et=()=>{var st;if(s)return;const m=(st=l.current)==null?void 0:st.shadowRoot,I=!Z||!!(m!=null&&m.querySelector(Z));if(ft()&&I){tt();return}c=r.requestAnimationFrame(et)};return et(),()=>{s=!0,c!==null&&r.cancelAnimationFrame(c),v==null||v.disconnect()}},[l,K,f,a,n,b,g,C,D,M,j,O,S,h,V,A,d,w,R]),L(()=>{const t=l.current,r=t==null?void 0:t.shadowRoot;if(!t)return;if(!d||!r){t.removeAttribute("custom-item-rendering-ready"),t.removeAttribute("custom-item-rendering-pending");return}const s=r.querySelectorAll(".item-custom-content[data-item-content-target]"),o=r.querySelectorAll("[data-sortable-custom-item]");s.length>0&&o.length>=s.length?(t.setAttribute("custom-item-rendering-ready","true"),t.removeAttribute("custom-item-rendering-pending")):(t.removeAttribute("custom-item-rendering-ready"),t.setAttribute("custom-item-rendering-pending","true"))},[l,d,x,f,n,b,g,A]),L(()=>{var r;const t=(r=l.current)==null?void 0:r.shadowRoot;t&&(t.querySelectorAll("[data-sortable-custom-handle]").forEach(s=>{s.removeAttribute("data-sortable-custom-handle"),s.removeAttribute("data-drag-trigger"),s.removeAttribute("data-id"),s.removeAttribute("draggable")}),!(!d||!h||S==="item")&&t.querySelectorAll("[data-sortable-custom-item][data-sortable-item-id]").forEach(s=>{const o=s.getAttribute("data-sortable-item-id");o&&s.querySelectorAll(h).forEach(c=>{c.setAttribute("data-sortable-custom-handle",""),c.setAttribute("data-drag-trigger","custom"),c.setAttribute("data-id",o),c.setAttribute("draggable","true")})}))},[l,d,h,S,x,f,n,b,g,A]);const U=new Map(a.map(t=>[t.id,t])),W=new Map(a.map(t=>[t.id,f.filter(r=>r.listId===t.id).length])),X=!!A||g==="label"||!!b&&!D,lt=d?f.map(t=>{var s,o,c;const r=(o=(s=l.current)==null?void 0:s.shadowRoot)==null?void 0:o.querySelector(`.item-custom-content[data-item-content-target="${F(t.id)}"]`);return r?z.createPortal(E.jsx("div",{"data-sortable-custom-item":"","data-sortable-item-id":t.id,"data-selected":n!=null&&n.includes(t.id)?"true":"false","data-disabled":t.disabled?"true":"false","data-drag-disabled":t.dragDisabled?"true":"false",children:d(t,{selected:(n==null?void 0:n.includes(t.id))??!1,disabled:!!t.disabled,dragDisabled:!!t.dragDisabled,cloneOnDrag:!!t.cloneOnDrag||!!((c=U.get(t.listId))!=null&&c.cloneOnDrag),list:U.get(t.listId)})}),r,t.id):null}):null,dt=w?a.map(t=>{var s,o;const r=(o=(s=l.current)==null?void 0:s.shadowRoot)==null?void 0:o.querySelector(`.list-custom-header[data-list-header-target="${F(t.id)}"]`);return r?z.createPortal(E.jsx("div",{"data-sortable-custom-list-header":"","data-sortable-list-id":t.id,children:w(t,{itemCount:W.get(t.id)??0,dragLocked:X})}),r,`header-${t.id}`):null}):null,ct=R?a.map(t=>{var s,o;const r=(o=(s=l.current)==null?void 0:s.shadowRoot)==null?void 0:o.querySelector(`.list-custom-empty[data-list-empty-target="${F(t.id)}"]`);return r?z.createPortal(E.jsx("div",{"data-sortable-custom-empty":"","data-sortable-list-id":t.id,children:R(t,{itemCount:W.get(t.id)??0,dragLocked:X,filterQuery:b??""})}),r,`empty-${t.id}`):null}):null,Y={ref:l,...nt};return d&&(Y["custom-item-rendering"]=""),E.jsxs(E.Fragment,{children:[p.default.createElement("ui-sortable",Y,at),x>=0?lt:null,x>=0?dt:null,x>=0?ct:null]})});T.displayName="Sortable";exports.Sortable=T;exports.default=T;
@@ -0,0 +1,338 @@
1
+ import { jsx as B, jsxs as ft, Fragment as bt } from "react/jsx-runtime";
2
+ import p from "react";
3
+ import { createPortal as F } from "react-dom";
4
+ import "@editora/ui-core/sortable";
5
+ const L = typeof window < "u" ? p.useLayoutEffect : p.useEffect;
6
+ function gt(e) {
7
+ const s = p.useRef(null);
8
+ return p.useImperativeHandle(e, () => s.current), s;
9
+ }
10
+ function mt(e, s, u) {
11
+ p.useEffect(() => {
12
+ const a = e.current;
13
+ if (!a)
14
+ return;
15
+ const f = s.filter(
16
+ (n) => typeof n.listener == "function"
17
+ );
18
+ return f.forEach(({ type: n, listener: b }) => {
19
+ a.addEventListener(n, b);
20
+ }), () => {
21
+ f.forEach(({ type: n, listener: b }) => {
22
+ a.removeEventListener(n, b);
23
+ });
24
+ };
25
+ }, [e, ...u]);
26
+ }
27
+ function pt(e, s, u) {
28
+ L(() => {
29
+ const a = e.current;
30
+ a && s(a);
31
+ }, [e, ...u]);
32
+ }
33
+ function M(e) {
34
+ return e.detail;
35
+ }
36
+ function y(e, s, u) {
37
+ const a = e.getAttribute(s);
38
+ if (u == null || u === "") {
39
+ a != null && e.removeAttribute(s);
40
+ return;
41
+ }
42
+ a !== u && e.setAttribute(s, u);
43
+ }
44
+ function it(e, s, u) {
45
+ if (u) {
46
+ e.hasAttribute(s) || e.setAttribute(s, "");
47
+ return;
48
+ }
49
+ e.hasAttribute(s) && e.removeAttribute(s);
50
+ }
51
+ function j(e, s, u) {
52
+ if (u == null) {
53
+ y(e, s, null);
54
+ return;
55
+ }
56
+ try {
57
+ y(e, s, JSON.stringify(u));
58
+ } catch {
59
+ y(e, s, null);
60
+ }
61
+ }
62
+ function k(e) {
63
+ return typeof CSS < "u" && typeof CSS.escape == "function" ? CSS.escape(e) : e.replace(/"/g, '\\"');
64
+ }
65
+ function st(e) {
66
+ if (e == null || e === "")
67
+ return null;
68
+ if (typeof e == "number")
69
+ return `${e}px`;
70
+ switch (e) {
71
+ case "none":
72
+ return "0px";
73
+ case "sm":
74
+ return "10px";
75
+ case "md":
76
+ return "16px";
77
+ case "lg":
78
+ return "20px";
79
+ case "xl":
80
+ return "28px";
81
+ case "full":
82
+ return "999px";
83
+ default:
84
+ return e;
85
+ }
86
+ }
87
+ function yt(e) {
88
+ if (e == null || e === "")
89
+ return null;
90
+ if (typeof e == "number")
91
+ return `${e}px`;
92
+ switch (e) {
93
+ case "xs":
94
+ return "6px";
95
+ case "sm":
96
+ return "8px";
97
+ case "md":
98
+ return "10px";
99
+ case "lg":
100
+ return "14px";
101
+ case "xl":
102
+ return "18px";
103
+ default:
104
+ return e;
105
+ }
106
+ }
107
+ const ht = p.forwardRef(function(s, u) {
108
+ const {
109
+ lists: a,
110
+ items: f,
111
+ selection: n,
112
+ filterQuery: b,
113
+ sort: g,
114
+ persistKey: D,
115
+ allowFilteredDrag: q,
116
+ allowNesting: C,
117
+ dropIndicatorVisibility: V,
118
+ dropzoneStyle: N,
119
+ dragPreviewSize: $,
120
+ dragHandleMode: w,
121
+ dragHandleSelector: h,
122
+ itemRadius: Q,
123
+ handleRadius: T,
124
+ listGap: G,
125
+ itemSpacing: J,
126
+ showSelectionBadge: z,
127
+ disabled: A,
128
+ renderItem: d,
129
+ renderListHeader: S,
130
+ renderEmptyState: R,
131
+ onChange: E,
132
+ onItemsChange: x,
133
+ onSelectionChange: H,
134
+ onPersistRequest: O,
135
+ children: ot,
136
+ ...at
137
+ } = s, l = gt(u), [P, nt] = p.useState(0), K = !!(d || S || R);
138
+ mt(
139
+ l,
140
+ [
141
+ {
142
+ type: "change",
143
+ listener: E || x ? (t) => {
144
+ const r = M(t);
145
+ r && (E == null || E(r), x == null || x(r.items));
146
+ } : void 0
147
+ },
148
+ {
149
+ type: "selection-change",
150
+ listener: H ? (t) => {
151
+ const r = M(t);
152
+ r && H(r);
153
+ } : void 0
154
+ },
155
+ {
156
+ type: "persist-request",
157
+ listener: O ? (t) => {
158
+ const r = M(t);
159
+ r && O(r);
160
+ } : void 0
161
+ }
162
+ ],
163
+ [E, x, H, O]
164
+ ), pt(
165
+ l,
166
+ (t) => {
167
+ j(t, "lists", a), j(t, "items", f), j(t, "selection", n ?? null), y(t, "filter-query", b ?? null), y(t, "sort", g && g !== "manual" ? g : null), y(t, "persist-key", D ?? null), it(t, "allow-filtered-drag", q), C === !1 ? t.setAttribute("allow-nesting", "false") : t.removeAttribute("allow-nesting"), V === "always" ? t.setAttribute("drop-indicator-visibility", "always") : t.removeAttribute("drop-indicator-visibility"), N === "container" ? t.setAttribute("dropzone-style", "container") : t.removeAttribute("dropzone-style"), $ === "compact" ? t.setAttribute("drag-preview-size", "compact") : t.removeAttribute("drag-preview-size"), w === "item" ? t.setAttribute("drag-handle-mode", "item") : t.removeAttribute("drag-handle-mode"), y(t, "drag-handle-selector", h ?? null);
168
+ const r = st(Q);
169
+ r ? t.style.setProperty("--ui-sortable-item-radius", r) : t.style.removeProperty("--ui-sortable-item-radius");
170
+ const i = st(T);
171
+ i ? t.style.setProperty("--ui-sortable-handle-radius", i) : t.style.removeProperty("--ui-sortable-handle-radius");
172
+ const o = yt(G ?? J);
173
+ o ? t.style.setProperty("--ui-sortable-list-gap", o) : t.style.removeProperty("--ui-sortable-list-gap"), d ? t.setAttribute("custom-item-rendering", "true") : t.removeAttribute("custom-item-rendering"), z === !1 ? t.setAttribute("show-selection-badge", "false") : t.removeAttribute("show-selection-badge"), it(t, "disabled", A);
174
+ },
175
+ [
176
+ a,
177
+ f,
178
+ n,
179
+ b,
180
+ g,
181
+ D,
182
+ q,
183
+ C,
184
+ V,
185
+ N,
186
+ $,
187
+ w,
188
+ h,
189
+ Q,
190
+ T,
191
+ G,
192
+ J,
193
+ d,
194
+ z,
195
+ A
196
+ ]
197
+ ), L(() => {
198
+ var et;
199
+ if (!K)
200
+ return;
201
+ const t = l.current, r = (et = t == null ? void 0 : t.ownerDocument) == null ? void 0 : et.defaultView;
202
+ if (!t || !r)
203
+ return;
204
+ let i = !1, o = !1, c = null, v = null;
205
+ const Z = [
206
+ d && f.length > 0 ? ".item-custom-content[data-item-content-target]" : null,
207
+ S && a.length > 0 ? ".list-custom-header[data-list-header-target]" : null,
208
+ R && a.length > 0 ? ".list-custom-empty[data-list-empty-target]" : null
209
+ ].filter(Boolean).join(", "), _ = () => {
210
+ i || o || (o = !0, queueMicrotask(() => {
211
+ o = !1, !i && nt((m) => m + 1);
212
+ }));
213
+ }, ct = () => {
214
+ var I;
215
+ const m = (I = l.current) == null ? void 0 : I.shadowRoot;
216
+ return m ? (v || (v = new MutationObserver(() => _()), v.observe(m, { childList: !0, subtree: !0 })), !0) : !1;
217
+ }, tt = () => {
218
+ var rt;
219
+ if (i)
220
+ return;
221
+ const m = (rt = l.current) == null ? void 0 : rt.shadowRoot, I = !Z || !!(m != null && m.querySelector(Z));
222
+ if (ct() && I) {
223
+ _();
224
+ return;
225
+ }
226
+ c = r.requestAnimationFrame(tt);
227
+ };
228
+ return tt(), () => {
229
+ i = !0, c !== null && r.cancelAnimationFrame(c), v == null || v.disconnect();
230
+ };
231
+ }, [
232
+ l,
233
+ K,
234
+ f,
235
+ a,
236
+ n,
237
+ b,
238
+ g,
239
+ D,
240
+ q,
241
+ C,
242
+ V,
243
+ $,
244
+ w,
245
+ h,
246
+ z,
247
+ A,
248
+ d,
249
+ S,
250
+ R
251
+ ]), L(() => {
252
+ const t = l.current, r = t == null ? void 0 : t.shadowRoot;
253
+ if (!t)
254
+ return;
255
+ if (!d || !r) {
256
+ t.removeAttribute("custom-item-rendering-ready"), t.removeAttribute("custom-item-rendering-pending");
257
+ return;
258
+ }
259
+ const i = r.querySelectorAll(".item-custom-content[data-item-content-target]"), o = r.querySelectorAll("[data-sortable-custom-item]");
260
+ i.length > 0 && o.length >= i.length ? (t.setAttribute("custom-item-rendering-ready", "true"), t.removeAttribute("custom-item-rendering-pending")) : (t.removeAttribute("custom-item-rendering-ready"), t.setAttribute("custom-item-rendering-pending", "true"));
261
+ }, [l, d, P, f, n, b, g, A]), L(() => {
262
+ var r;
263
+ const t = (r = l.current) == null ? void 0 : r.shadowRoot;
264
+ t && (t.querySelectorAll("[data-sortable-custom-handle]").forEach((i) => {
265
+ i.removeAttribute("data-sortable-custom-handle"), i.removeAttribute("data-drag-trigger"), i.removeAttribute("data-id"), i.removeAttribute("draggable");
266
+ }), !(!d || !h || w === "item") && t.querySelectorAll("[data-sortable-custom-item][data-sortable-item-id]").forEach((i) => {
267
+ const o = i.getAttribute("data-sortable-item-id");
268
+ o && i.querySelectorAll(h).forEach((c) => {
269
+ c.setAttribute("data-sortable-custom-handle", ""), c.setAttribute("data-drag-trigger", "custom"), c.setAttribute("data-id", o), c.setAttribute("draggable", "true");
270
+ });
271
+ }));
272
+ }, [l, d, h, w, P, f, n, b, g, A]);
273
+ const U = new Map(a.map((t) => [t.id, t])), W = new Map(a.map((t) => [t.id, f.filter((r) => r.listId === t.id).length])), X = !!A || g === "label" || !!b && !q, ut = d ? f.map((t) => {
274
+ var i, o, c;
275
+ const r = (o = (i = l.current) == null ? void 0 : i.shadowRoot) == null ? void 0 : o.querySelector(
276
+ `.item-custom-content[data-item-content-target="${k(t.id)}"]`
277
+ );
278
+ return r ? F(
279
+ /* @__PURE__ */ B(
280
+ "div",
281
+ {
282
+ "data-sortable-custom-item": "",
283
+ "data-sortable-item-id": t.id,
284
+ "data-selected": n != null && n.includes(t.id) ? "true" : "false",
285
+ "data-disabled": t.disabled ? "true" : "false",
286
+ "data-drag-disabled": t.dragDisabled ? "true" : "false",
287
+ children: d(t, {
288
+ selected: (n == null ? void 0 : n.includes(t.id)) ?? !1,
289
+ disabled: !!t.disabled,
290
+ dragDisabled: !!t.dragDisabled,
291
+ cloneOnDrag: !!t.cloneOnDrag || !!((c = U.get(t.listId)) != null && c.cloneOnDrag),
292
+ list: U.get(t.listId)
293
+ })
294
+ }
295
+ ),
296
+ r,
297
+ t.id
298
+ ) : null;
299
+ }) : null, lt = S ? a.map((t) => {
300
+ var i, o;
301
+ const r = (o = (i = l.current) == null ? void 0 : i.shadowRoot) == null ? void 0 : o.querySelector(
302
+ `.list-custom-header[data-list-header-target="${k(t.id)}"]`
303
+ );
304
+ return r ? F(
305
+ /* @__PURE__ */ B("div", { "data-sortable-custom-list-header": "", "data-sortable-list-id": t.id, children: S(t, {
306
+ itemCount: W.get(t.id) ?? 0,
307
+ dragLocked: X
308
+ }) }),
309
+ r,
310
+ `header-${t.id}`
311
+ ) : null;
312
+ }) : null, dt = R ? a.map((t) => {
313
+ var i, o;
314
+ const r = (o = (i = l.current) == null ? void 0 : i.shadowRoot) == null ? void 0 : o.querySelector(
315
+ `.list-custom-empty[data-list-empty-target="${k(t.id)}"]`
316
+ );
317
+ return r ? F(
318
+ /* @__PURE__ */ B("div", { "data-sortable-custom-empty": "", "data-sortable-list-id": t.id, children: R(t, {
319
+ itemCount: W.get(t.id) ?? 0,
320
+ dragLocked: X,
321
+ filterQuery: b ?? ""
322
+ }) }),
323
+ r,
324
+ `empty-${t.id}`
325
+ ) : null;
326
+ }) : null, Y = { ref: l, ...at };
327
+ return d && (Y["custom-item-rendering"] = ""), /* @__PURE__ */ ft(bt, { children: [
328
+ p.createElement("ui-sortable", Y, ot),
329
+ P >= 0 ? ut : null,
330
+ P >= 0 ? lt : null,
331
+ P >= 0 ? dt : null
332
+ ] });
333
+ });
334
+ ht.displayName = "Sortable";
335
+ export {
336
+ ht as Sortable,
337
+ ht as default
338
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@editora/ui-sortable",
3
- "version": "0.1.0",
3
+ "version": "0.1.2",
4
4
  "description": "Standalone install for the Editora sortable web component, with reordering, transfer, hierarchy, cloning, and keyboard-accessible drag and drop.",
5
5
  "homepage": "https://editora-ecosystem.netlify.app/docs/ui-react/interactive/sortable",
6
6
  "bugs": {
@@ -20,25 +20,51 @@
20
20
  "import": "./dist/index.esm.js",
21
21
  "require": "./dist/index.cjs.js",
22
22
  "default": "./dist/index.esm.js"
23
+ },
24
+ "./react": {
25
+ "types": "./react.d.ts",
26
+ "import": "./dist/react.esm.js",
27
+ "require": "./dist/react.cjs.js",
28
+ "default": "./dist/react.esm.js"
23
29
  }
24
30
  },
25
31
  "files": [
26
32
  "dist",
27
- "index.d.ts"
33
+ "index.d.ts",
34
+ "react.d.ts"
28
35
  ],
29
36
  "scripts": {
30
37
  "build": "vite build",
31
38
  "dev": "vite build --watch",
39
+ "test": "vitest run",
40
+ "test:watch": "vitest",
32
41
  "clean": "rimraf dist",
33
42
  "prepare": "npm run build"
34
43
  },
35
44
  "dependencies": {
36
- "@editora/ui-core": "^0.1.9"
45
+ "@editora/ui-core": "^0.1.16"
46
+ },
47
+ "peerDependencies": {
48
+ "react": ">=16.8 <21",
49
+ "react-dom": ">=16.8 <21"
50
+ },
51
+ "peerDependenciesMeta": {
52
+ "react": {
53
+ "optional": true
54
+ },
55
+ "react-dom": {
56
+ "optional": true
57
+ }
37
58
  },
38
59
  "devDependencies": {
60
+ "@testing-library/react": "^16.3.2",
61
+ "jsdom": "^22.1.0",
62
+ "react": "^18.3.1",
63
+ "react-dom": "^18.3.1",
39
64
  "rimraf": "^5.0.0",
40
65
  "typescript": "^5.0.0",
41
- "vite": "^4.5.14"
66
+ "vite": "^4.5.14",
67
+ "vitest": "^1.6.1"
42
68
  },
43
69
  "keywords": [
44
70
  "editora",
package/react.d.ts ADDED
@@ -0,0 +1,96 @@
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
+ } from '@editora/ui-core/sortable';
20
+
21
+ export type SortableListOrientation = UISortableListOrientation;
22
+ export type SortableSortMode = UISortableSortMode;
23
+ export type SortableDropMode = UISortableDropMode;
24
+ export type SortableChangeSource = UISortableChangeSource;
25
+ export type SortableOperation = UISortableOperation;
26
+ export type SortableDropIndicatorVisibility = UISortableDropIndicatorVisibility;
27
+ export type SortableDropzoneStyle = UISortableDropzoneStyle;
28
+ export type SortableDragPreviewSize = 'match-item' | 'compact';
29
+ export type SortableDragHandleMode = UISortableDragHandleMode;
30
+ export type SortableRadiusPreset = 'none' | 'sm' | 'md' | 'lg' | 'xl' | 'full';
31
+ export type SortableRadius = SortableRadiusPreset | number | string;
32
+ export type SortableSpacePreset = 'xs' | 'sm' | 'md' | 'lg' | 'xl';
33
+ export type SortableSpace = SortableSpacePreset | number | string;
34
+
35
+ export type SortableList = UISortableList;
36
+ export type SortableItem = UISortableItem;
37
+ export type SortableSelectionChangeDetail = UISortableSelectionChangeDetail;
38
+ export type SortablePersistenceRecord = UISortablePersistenceRecord;
39
+ export type SortablePersistenceSnapshot = UISortablePersistenceSnapshot;
40
+ export type SortableChangeDetail = UISortableChangeDetail;
41
+ export type SortableMoveOptions = UISortableMoveOptions;
42
+ export type SortableElement = UISortable;
43
+
44
+ export type SortableRenderItemContext = {
45
+ selected: boolean;
46
+ disabled: boolean;
47
+ dragDisabled: boolean;
48
+ cloneOnDrag: boolean;
49
+ list?: SortableList;
50
+ };
51
+
52
+ export type SortableRenderListHeaderContext = {
53
+ itemCount: number;
54
+ dragLocked: boolean;
55
+ };
56
+
57
+ export type SortableRenderEmptyStateContext = {
58
+ itemCount: number;
59
+ dragLocked: boolean;
60
+ filterQuery: string;
61
+ };
62
+
63
+ export type SortableProps = Omit<React.HTMLAttributes<SortableElement>, 'onChange'> & {
64
+ lists: SortableList[];
65
+ items: SortableItem[];
66
+ selection?: string[];
67
+ filterQuery?: string;
68
+ sort?: SortableSortMode;
69
+ persistKey?: string;
70
+ allowFilteredDrag?: boolean;
71
+ allowNesting?: boolean;
72
+ dropIndicatorVisibility?: SortableDropIndicatorVisibility;
73
+ dropzoneStyle?: SortableDropzoneStyle;
74
+ dragPreviewSize?: SortableDragPreviewSize;
75
+ dragHandleMode?: SortableDragHandleMode;
76
+ dragHandleSelector?: string;
77
+ itemRadius?: SortableRadius;
78
+ handleRadius?: SortableRadius;
79
+ listGap?: SortableSpace;
80
+ itemSpacing?: SortableSpace;
81
+ showSelectionBadge?: boolean;
82
+ disabled?: boolean;
83
+ renderItem?: (item: SortableItem, context: SortableRenderItemContext) => React.ReactNode;
84
+ renderListHeader?: (list: SortableList, context: SortableRenderListHeaderContext) => React.ReactNode;
85
+ renderEmptyState?: (list: SortableList, context: SortableRenderEmptyStateContext) => React.ReactNode;
86
+ onChange?: (detail: SortableChangeDetail) => void;
87
+ onItemsChange?: (items: SortableItem[]) => void;
88
+ onSelectionChange?: (detail: SortableSelectionChangeDetail) => void;
89
+ onPersistRequest?: (detail: SortableChangeDetail) => void;
90
+ };
91
+
92
+ export declare const Sortable: React.ForwardRefExoticComponent<
93
+ SortableProps & React.RefAttributes<SortableElement>
94
+ >;
95
+
96
+ export default Sortable;