@drawback/ui 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/README.md +22 -2
  2. package/dist/i18n-BZnCrD-C.d.mts +265 -0
  3. package/dist/index.d.mts +588 -0
  4. package/dist/index.mjs +4821 -0
  5. package/dist/locales/de.d.mts +3 -0
  6. package/dist/locales/de.mjs +231 -0
  7. package/package.json +69 -4
  8. package/src/app/default-editor.tsx +371 -0
  9. package/src/components/color-picker/color-picker.tsx +341 -0
  10. package/src/components/color-picker/colors.ts +135 -0
  11. package/src/components/font-picker/font-picker.tsx +298 -0
  12. package/src/components/icons.tsx +222 -0
  13. package/src/components/primitives.tsx +565 -0
  14. package/src/components/style-icons.tsx +172 -0
  15. package/src/features/canvas-commands/canvas-commands.tsx +173 -0
  16. package/src/features/context-menu/context-menu.tsx +354 -0
  17. package/src/features/controls/controls.tsx +281 -0
  18. package/src/features/export/export-dialog.tsx +164 -0
  19. package/src/features/layers/layers-tree.ts +335 -0
  20. package/src/features/layers/layers.tsx +858 -0
  21. package/src/features/links/links.tsx +268 -0
  22. package/src/features/main-menu/canvas-properties.tsx +121 -0
  23. package/src/features/main-menu/main-menu.tsx +215 -0
  24. package/src/features/minimap/minimap.tsx +69 -0
  25. package/src/features/properties/linear-properties.tsx +169 -0
  26. package/src/features/properties/properties.tsx +573 -0
  27. package/src/features/shortcuts/shortcuts-dialog.tsx +120 -0
  28. package/src/features/toolbar/toolbar.tsx +216 -0
  29. package/src/hooks/use-latest.ts +16 -0
  30. package/src/index.ts +138 -0
  31. package/src/lib/commands.ts +14 -0
  32. package/src/lib/download.ts +8 -0
  33. package/src/lib/i18n.ts +350 -0
  34. package/src/lib/locales/de.ts +231 -0
  35. package/src/lib/plugins.tsx +92 -0
  36. package/src/lib/registry.ts +140 -0
  37. package/src/stores/ui-context.tsx +253 -0
  38. package/src/styles.css +1366 -0
@@ -0,0 +1,565 @@
1
+ import {
2
+ useEffect,
3
+ useEffectEvent,
4
+ useId,
5
+ useLayoutEffect,
6
+ useRef,
7
+ useState,
8
+ type CSSProperties,
9
+ type KeyboardEvent,
10
+ type ReactNode,
11
+ type RefObject,
12
+ } from 'react';
13
+ import { createPortal } from 'react-dom';
14
+ import { useUi } from '../stores/ui-context.js';
15
+ import { Icon } from './icons.js';
16
+
17
+ export function cx(...names: readonly (string | false | undefined | null)[]): string {
18
+ return names.filter(Boolean).join(' ');
19
+ }
20
+
21
+ export interface IconButtonProps {
22
+ readonly icon: string;
23
+ readonly label: string;
24
+ /** Shortcut(s) for the tooltip; several are joined ("R or 2"). Formatted for the platform. */
25
+ readonly shortcut?: string | readonly string[] | undefined;
26
+ readonly onClick?: () => void;
27
+ readonly pressed?: boolean;
28
+ readonly disabled?: boolean;
29
+ readonly danger?: boolean;
30
+ readonly testId?: string;
31
+ readonly className?: string;
32
+ readonly tabIndex?: number;
33
+ readonly children?: ReactNode;
34
+ }
35
+
36
+ /** Square icon button with an accessible label and a tooltip that shows the shortcut. */
37
+ export function IconButton({
38
+ icon,
39
+ label,
40
+ shortcut,
41
+ onClick,
42
+ pressed,
43
+ disabled,
44
+ danger,
45
+ testId,
46
+ className,
47
+ tabIndex,
48
+ children,
49
+ }: IconButtonProps): ReactNode {
50
+ const { t } = useUi();
51
+ const keys = (typeof shortcut === 'string' ? [shortcut] : (shortcut ?? []))
52
+ .map((key) => formatShortcut(key))
53
+ .filter((key): key is string => key !== undefined && key !== '');
54
+ const title =
55
+ keys.length === 0
56
+ ? label
57
+ : t('shortcut.hint', { label, shortcut: keys.join(t('shortcut.or')) });
58
+ return (
59
+ <button
60
+ type="button"
61
+ className={cx('nx-icon-button', danger && 'nx-danger', className)}
62
+ aria-label={label}
63
+ title={title}
64
+ aria-pressed={pressed}
65
+ disabled={disabled}
66
+ onClick={onClick}
67
+ data-testid={testId}
68
+ tabIndex={tabIndex}
69
+ >
70
+ <Icon name={icon} />
71
+ {children}
72
+ </button>
73
+ );
74
+ }
75
+
76
+ export function Island({
77
+ children,
78
+ className,
79
+ label,
80
+ testId,
81
+ role,
82
+ }: {
83
+ readonly children?: ReactNode;
84
+ readonly className?: string;
85
+ readonly label?: string;
86
+ readonly testId?: string;
87
+ readonly role?: string;
88
+ }): ReactNode {
89
+ return (
90
+ <div
91
+ className={cx('nx-island', className)}
92
+ aria-label={label}
93
+ role={role}
94
+ data-testid={testId}
95
+ >
96
+ {children}
97
+ </div>
98
+ );
99
+ }
100
+
101
+ /** Formats `Mod+Shift+Z` for the platform (⌘⇧Z on macOS). */
102
+ export function formatShortcut(shortcut: string | undefined): string | undefined {
103
+ if (shortcut === undefined) {
104
+ return undefined;
105
+ }
106
+
107
+ const mac = typeof navigator !== 'undefined' && /Mac|iPhone|iPad/.test(navigator.platform);
108
+ return shortcut
109
+ .split('+')
110
+ .map((part) =>
111
+ part === 'Mod'
112
+ ? mac
113
+ ? '⌘'
114
+ : 'Ctrl'
115
+ : part === 'Shift' && mac
116
+ ? '⇧'
117
+ : part === 'Alt' && mac
118
+ ? '⌥'
119
+ : part,
120
+ )
121
+ .join(mac ? '' : '+');
122
+ }
123
+
124
+ function focusables(root: HTMLElement): HTMLElement[] {
125
+ return [
126
+ ...root.querySelectorAll<HTMLElement>(
127
+ 'button:not([disabled]), [href], input:not([disabled]), select:not([disabled]), textarea:not([disabled]), [tabindex]:not([tabindex="-1"])',
128
+ ),
129
+ ];
130
+ }
131
+
132
+ /** Moves focus among items with the arrow keys (roving focus for toolbars and menus). */
133
+ export function handleRovingKeys(
134
+ event: KeyboardEvent<HTMLElement>,
135
+ orientation: 'horizontal' | 'vertical',
136
+ ): void {
137
+ const root = event.currentTarget;
138
+ const field = event.target as HTMLElement;
139
+
140
+ // Arrow keys belong to text, number and select fields (menus can hold settings fields).
141
+ if (
142
+ field.tagName === 'SELECT' ||
143
+ field.tagName === 'TEXTAREA' ||
144
+ (field instanceof HTMLInputElement && !['checkbox', 'radio', 'button'].includes(field.type))
145
+ ) {
146
+ return;
147
+ }
148
+
149
+ const items = focusables(root);
150
+ const index = items.indexOf(document.activeElement as HTMLElement);
151
+ const next = orientation === 'horizontal' ? 'ArrowRight' : 'ArrowDown';
152
+ const previous = orientation === 'horizontal' ? 'ArrowLeft' : 'ArrowUp';
153
+ let target: number | undefined;
154
+
155
+ if (event.key === next) {
156
+ target = (index + 1) % items.length;
157
+ } else if (event.key === previous) {
158
+ // From the container itself (index -1), going back wraps to the last item.
159
+ target = index < 0 ? items.length - 1 : (index - 1 + items.length) % items.length;
160
+ } else if (event.key === 'Home') {
161
+ target = 0;
162
+ } else if (event.key === 'End') {
163
+ target = items.length - 1;
164
+ }
165
+
166
+ if (target !== undefined && items[target] !== undefined) {
167
+ event.preventDefault();
168
+ items[target]?.focus();
169
+ }
170
+ }
171
+
172
+ export function Portal({ children }: { readonly children: ReactNode }): ReactNode {
173
+ const { portalContainer } = useUi();
174
+ return portalContainer === null ? null : createPortal(children, portalContainer);
175
+ }
176
+
177
+ function useDismiss(
178
+ open: boolean,
179
+ refs: readonly RefObject<HTMLElement | null>[],
180
+ onClose: () => void,
181
+ ): void {
182
+ // Effect events read the latest refs and callback without re-subscribing on every render.
183
+ const onPointer = useEffectEvent((event: PointerEvent): void => {
184
+ const target = event.target as Node;
185
+ // Popovers opened from inside this one (a color picker in a menu) portal after it.
186
+ const nested = (target instanceof Element ? target : target.parentElement)?.closest(
187
+ '.nx-popover',
188
+ );
189
+ const own = refs[0]?.current;
190
+ const inChild =
191
+ nested != null &&
192
+ own != null &&
193
+ nested !== own &&
194
+ (own.compareDocumentPosition(nested) & Node.DOCUMENT_POSITION_FOLLOWING) !== 0;
195
+
196
+ if (!inChild && !refs.some((ref) => ref.current?.contains(target))) {
197
+ onClose();
198
+ }
199
+ });
200
+ const onKey = useEffectEvent((event: globalThis.KeyboardEvent): void => {
201
+ if (event.key === 'Escape') {
202
+ event.stopPropagation();
203
+ onClose();
204
+ }
205
+ });
206
+
207
+ useEffect(() => {
208
+ if (!open) {
209
+ return undefined;
210
+ }
211
+
212
+ const pointer = (event: PointerEvent): void => onPointer(event);
213
+ const key = (event: globalThis.KeyboardEvent): void => onKey(event);
214
+ document.addEventListener('pointerdown', pointer, true);
215
+ document.addEventListener('keydown', key, true);
216
+ return () => {
217
+ document.removeEventListener('pointerdown', pointer, true);
218
+ document.removeEventListener('keydown', key, true);
219
+ };
220
+ }, [open]);
221
+ }
222
+
223
+ export interface PopoverProps {
224
+ readonly open: boolean;
225
+ readonly onClose: () => void;
226
+ readonly anchor: RefObject<HTMLElement | null>;
227
+ readonly placement?: 'bottom-start' | 'bottom-end' | 'right-start' | 'top-start';
228
+ readonly label: string;
229
+ readonly role?: 'dialog' | 'menu';
230
+ readonly testId?: string;
231
+ readonly children: ReactNode;
232
+ }
233
+
234
+ /** Floating panel next to an anchor, portaled into the editor root. */
235
+ export function Popover({
236
+ open,
237
+ onClose,
238
+ anchor,
239
+ placement = 'bottom-start',
240
+ label,
241
+ role = 'dialog',
242
+ testId,
243
+ children,
244
+ }: PopoverProps): ReactNode {
245
+ const { portalContainer } = useUi();
246
+ const ref = useRef<HTMLDivElement>(null);
247
+ const [style, setStyle] = useState<CSSProperties>({ visibility: 'hidden' });
248
+ useDismiss(open, [ref, anchor], onClose);
249
+
250
+ useLayoutEffect(() => {
251
+ if (!open || anchor.current === null || portalContainer === null || ref.current === null) {
252
+ return;
253
+ }
254
+
255
+ const container = portalContainer.getBoundingClientRect();
256
+ const box = anchor.current.getBoundingClientRect();
257
+ const panel = ref.current.getBoundingClientRect();
258
+ let left =
259
+ placement === 'right-start'
260
+ ? box.right + 8
261
+ : placement === 'bottom-end'
262
+ ? box.right - panel.width
263
+ : box.left;
264
+ let top =
265
+ placement === 'right-start'
266
+ ? box.top
267
+ : placement === 'top-start'
268
+ ? box.top - panel.height - 8
269
+ : box.bottom + 8;
270
+ left = Math.max(container.left + 8, Math.min(left, container.right - panel.width - 8));
271
+ top = Math.max(container.top + 8, Math.min(top, container.bottom - panel.height - 8));
272
+ setStyle({ left: left - container.left, top: top - container.top });
273
+ }, [open, anchor, portalContainer, placement]);
274
+
275
+ // Focus once the panel is positioned: focusing it while still `visibility: hidden` is ignored.
276
+ const positioned = style.visibility !== 'hidden';
277
+
278
+ useEffect(() => {
279
+ if (open && positioned) {
280
+ // Content can mark its preferred first focus (e.g. the active color) with data-autofocus.
281
+ const preferred = ref.current?.querySelector<HTMLElement>('[data-autofocus]:not(:disabled)');
282
+ (preferred ?? focusables(ref.current ?? document.body)[0])?.focus();
283
+ }
284
+ }, [open, positioned]);
285
+
286
+ if (!open) {
287
+ return null;
288
+ }
289
+
290
+ return (
291
+ <Portal>
292
+ <div
293
+ ref={ref}
294
+ className="nx-popover nx-island"
295
+ role={role}
296
+ aria-label={label}
297
+ style={style}
298
+ data-testid={testId}
299
+ onKeyDown={role === 'menu' ? (event) => handleRovingKeys(event, 'vertical') : undefined}
300
+ >
301
+ {children}
302
+ </div>
303
+ </Portal>
304
+ );
305
+ }
306
+
307
+ export interface MenuEntry {
308
+ readonly id: string;
309
+ readonly label: string;
310
+ readonly icon?: string;
311
+ readonly shortcut?: string | undefined;
312
+ readonly disabled?: boolean;
313
+ readonly danger?: boolean;
314
+ readonly separatorBefore?: boolean;
315
+ /** Set for toggle items: shows a check mark while true. */
316
+ readonly checked?: boolean;
317
+ readonly onSelect: () => void;
318
+ }
319
+
320
+ export function MenuItems({
321
+ entries,
322
+ onDone,
323
+ }: {
324
+ readonly entries: readonly MenuEntry[];
325
+ readonly onDone: () => void;
326
+ }): ReactNode {
327
+ return entries.map((entry) => (
328
+ <div
329
+ key={entry.id}
330
+ className="nx-menu-group"
331
+ >
332
+ {entry.separatorBefore === true && (
333
+ <div
334
+ className="nx-separator"
335
+ role="separator"
336
+ />
337
+ )}
338
+ <button
339
+ type="button"
340
+ role={entry.checked === undefined ? 'menuitem' : 'menuitemcheckbox'}
341
+ {...(entry.checked === undefined ? {} : { 'aria-checked': entry.checked })}
342
+ className={cx('nx-menu-item', entry.danger && 'nx-danger')}
343
+ disabled={entry.disabled}
344
+ data-testid={`menu-item-${entry.id}`}
345
+ onClick={() => {
346
+ onDone();
347
+ entry.onSelect();
348
+ }}
349
+ >
350
+ {entry.checked === true ? (
351
+ <Icon name="check" />
352
+ ) : entry.icon === undefined ? (
353
+ <span className="nx-menu-icon" />
354
+ ) : (
355
+ <Icon name={entry.icon} />
356
+ )}
357
+ <span className="nx-menu-label">{entry.label}</span>
358
+ {entry.shortcut !== undefined && <kbd>{formatShortcut(entry.shortcut)}</kbd>}
359
+ </button>
360
+ </div>
361
+ ));
362
+ }
363
+
364
+ export interface DialogProps {
365
+ readonly open: boolean;
366
+ readonly onClose: () => void;
367
+ readonly title: string;
368
+ readonly testId?: string;
369
+ readonly wide?: boolean;
370
+ readonly children: ReactNode;
371
+ }
372
+
373
+ /** Modal dialog inside the editor: traps focus, closes on Escape, restores focus. */
374
+ export function Dialog({ open, onClose, title, testId, wide, children }: DialogProps): ReactNode {
375
+ const { t } = useUi();
376
+ const ref = useRef<HTMLDivElement>(null);
377
+ const titleId = useId();
378
+ const close = useEffectEvent(() => onClose());
379
+
380
+ useEffect(() => {
381
+ if (!open) {
382
+ return undefined;
383
+ }
384
+
385
+ const previous = document.activeElement as HTMLElement | null;
386
+ focusables(ref.current ?? document.body)[0]?.focus();
387
+ // Escape must close the dialog even when focus has left it: Safari does not focus buttons on
388
+ // click, and a download can blur the page. Keys inside the dialog are handled (and stopped)
389
+ // by `onKeyDown` below, so this only sees keys from outside.
390
+ const onDocumentKey = (event: globalThis.KeyboardEvent): void => {
391
+ if (
392
+ event.key === 'Escape' &&
393
+ !event.defaultPrevented &&
394
+ !(event.target instanceof Node && ref.current?.contains(event.target) === true)
395
+ ) {
396
+ close();
397
+ }
398
+ };
399
+ document.addEventListener('keydown', onDocumentKey);
400
+ return () => {
401
+ document.removeEventListener('keydown', onDocumentKey);
402
+ previous?.focus?.();
403
+ };
404
+ }, [open]);
405
+
406
+ if (!open) {
407
+ return null;
408
+ }
409
+
410
+ const onKeyDown = (event: KeyboardEvent<HTMLDivElement>): void => {
411
+ if (event.key === 'Escape') {
412
+ event.stopPropagation();
413
+ onClose();
414
+ return;
415
+ }
416
+
417
+ if (event.key !== 'Tab' || ref.current === null) {
418
+ return;
419
+ }
420
+
421
+ const items = focusables(ref.current);
422
+ const first = items[0];
423
+ const last = items.at(-1);
424
+
425
+ if (event.shiftKey && document.activeElement === first) {
426
+ event.preventDefault();
427
+ last?.focus();
428
+ } else if (!event.shiftKey && document.activeElement === last) {
429
+ event.preventDefault();
430
+ first?.focus();
431
+ }
432
+ };
433
+
434
+ return (
435
+ <Portal>
436
+ <div
437
+ className="nx-dialog-backdrop"
438
+ onPointerDown={(event) => {
439
+ if (event.target === event.currentTarget) {
440
+ onClose();
441
+ }
442
+ }}
443
+ >
444
+ <div
445
+ ref={ref}
446
+ className={cx('nx-dialog nx-island', wide && 'nx-dialog-wide')}
447
+ role="dialog"
448
+ aria-modal="true"
449
+ aria-labelledby={titleId}
450
+ data-testid={testId}
451
+ onKeyDown={onKeyDown}
452
+ >
453
+ <header className="nx-dialog-header">
454
+ <h2 id={titleId}>{title}</h2>
455
+ <IconButton
456
+ icon="close"
457
+ label={t('dialog.close')}
458
+ onClick={onClose}
459
+ testId="dialog-close"
460
+ />
461
+ </header>
462
+ <div className="nx-dialog-body">{children}</div>
463
+ </div>
464
+ </div>
465
+ </Portal>
466
+ );
467
+ }
468
+
469
+ export interface ChoiceOption<Value extends string | number> {
470
+ readonly value: Value;
471
+ /** Accessible name and tooltip. */
472
+ readonly label: string;
473
+ readonly icon?: string;
474
+ /** Short text shown in a square button instead of an icon (e.g. "S", "XL"). */
475
+ readonly glyph?: string;
476
+ readonly swatch?: string;
477
+ }
478
+
479
+ /** Row of toggle buttons for one value; `undefined` value shows no selection (mixed). */
480
+ export function Choice<Value extends string | number>({
481
+ label,
482
+ value,
483
+ options,
484
+ onChange,
485
+ testId,
486
+ disabled,
487
+ }: {
488
+ readonly label: string;
489
+ readonly value: Value | undefined;
490
+ readonly options: readonly ChoiceOption<Value>[];
491
+ readonly onChange: (value: Value) => void;
492
+ readonly testId?: string;
493
+ readonly disabled?: boolean | undefined;
494
+ }): ReactNode {
495
+ return (
496
+ <div
497
+ className="nx-choice"
498
+ role="radiogroup"
499
+ aria-label={label}
500
+ data-testid={testId}
501
+ onKeyDown={(event) => handleRovingKeys(event, 'horizontal')}
502
+ >
503
+ {options.map((option) => (
504
+ <button
505
+ key={String(option.value)}
506
+ type="button"
507
+ role="radio"
508
+ aria-checked={value === option.value}
509
+ aria-label={option.label}
510
+ title={option.label}
511
+ disabled={disabled}
512
+ className={cx(
513
+ 'nx-choice-option',
514
+ option.swatch !== undefined && 'nx-swatch-option',
515
+ (option.icon !== undefined || option.glyph !== undefined) && 'nx-choice-square',
516
+ )}
517
+ data-value={String(option.value)}
518
+ onClick={() => onChange(option.value)}
519
+ >
520
+ {option.swatch !== undefined ? (
521
+ <span
522
+ className={cx('nx-swatch', option.swatch === 'transparent' && 'nx-swatch-none')}
523
+ style={option.swatch === 'transparent' ? undefined : { background: option.swatch }}
524
+ />
525
+ ) : option.icon !== undefined ? (
526
+ <Icon name={option.icon} />
527
+ ) : option.glyph !== undefined ? (
528
+ <span
529
+ className="nx-choice-glyph"
530
+ aria-hidden
531
+ >
532
+ {option.glyph}
533
+ </span>
534
+ ) : (
535
+ <span className="nx-choice-text">{option.label}</span>
536
+ )}
537
+ </button>
538
+ ))}
539
+ </div>
540
+ );
541
+ }
542
+
543
+ export function Section({
544
+ title,
545
+ children,
546
+ }: {
547
+ readonly title: string;
548
+ readonly children: ReactNode;
549
+ }): ReactNode {
550
+ const id = useId();
551
+ return (
552
+ <section
553
+ className="nx-section"
554
+ aria-labelledby={id}
555
+ >
556
+ <h3
557
+ id={id}
558
+ className="nx-section-title"
559
+ >
560
+ {title}
561
+ </h3>
562
+ {children}
563
+ </section>
564
+ );
565
+ }