sinwan-ui-core 1.5.1 → 2.0.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 (57) hide show
  1. package/CHANGELOG.md +52 -0
  2. package/README.md +29 -1
  3. package/agents/internal/llms.controlled.txt +39 -0
  4. package/agents/internal/llms.form.txt +43 -0
  5. package/agents/internal/llms.ids.txt +46 -0
  6. package/agents/internal/llms.modal.txt +51 -0
  7. package/agents/llms.controls.txt +82 -0
  8. package/agents/llms.core.txt +73 -0
  9. package/agents/llms.direction.txt +51 -0
  10. package/agents/llms.dismiss.txt +79 -0
  11. package/agents/llms.floating.txt +47 -0
  12. package/agents/llms.index.txt +46 -0
  13. package/agents/llms.menu-sub.txt +66 -0
  14. package/agents/llms.menu.txt +94 -0
  15. package/agents/llms.overlay.txt +122 -0
  16. package/agents/llms.sinwan-preload.txt +33 -0
  17. package/agents/llms.slot.txt +51 -0
  18. package/agents/llms.theme.txt +59 -0
  19. package/agents/llms.txt +45 -0
  20. package/agents/llms.types.txt +39 -0
  21. package/dist/chunks/chunk-7c8ct63g.js +126 -0
  22. package/dist/chunks/{chunk-16d43dkc.js → chunk-assqp8wg.js} +86 -13
  23. package/dist/chunks/{chunk-saen52f0.js → chunk-eczk2z48.js} +26 -14
  24. package/dist/chunks/{chunk-f6hw8ash.js → chunk-fe933s8d.js} +2 -2
  25. package/dist/chunks/chunk-rxbeqj7g.js +65 -0
  26. package/dist/chunks/{chunk-0dsacv44.js → chunk-s84my297.js} +124 -27
  27. package/dist/chunks/{chunk-6bm550r5.js → chunk-w6wwgdg6.js} +205 -46
  28. package/dist/chunks/{chunk-bw9vp0y9.js → chunk-wjzadrvr.js} +54 -0
  29. package/dist/chunks/{chunk-zmvg5gy1.js → chunk-y1ag4hk4.js} +57 -9
  30. package/dist/controls.d.ts +4 -0
  31. package/dist/controls.js +4 -4
  32. package/dist/core.d.ts +8 -0
  33. package/dist/core.js +4 -2
  34. package/dist/direction.d.ts +7 -1
  35. package/dist/direction.js +1 -1
  36. package/dist/dismiss.d.ts +18 -0
  37. package/dist/dismiss.js +9 -3
  38. package/dist/floating.d.ts +1 -0
  39. package/dist/index.d.ts +2 -0
  40. package/dist/index.js +54 -26
  41. package/dist/internal/controlled.d.ts +1 -0
  42. package/dist/internal/form.d.ts +6 -0
  43. package/dist/internal/ids.d.ts +8 -0
  44. package/dist/internal/modal.d.ts +8 -0
  45. package/dist/menu-sub.d.ts +10 -2
  46. package/dist/menu-sub.js +3 -2
  47. package/dist/menu.d.ts +66 -0
  48. package/dist/menu.js +16 -0
  49. package/dist/overlay.d.ts +27 -2
  50. package/dist/overlay.js +6 -4
  51. package/dist/slot.d.ts +1 -0
  52. package/dist/slot.js +1 -1
  53. package/dist/theme.d.ts +30 -2
  54. package/dist/theme.js +5 -1
  55. package/dist/types.d.ts +1 -0
  56. package/package.json +16 -7
  57. package/dist/chunks/chunk-9whagkb6.js +0 -31
@@ -0,0 +1,126 @@
1
+ // src/menu.ts
2
+ import { effect, untrack } from "sinwan/reactivity";
3
+ var MENU_ITEM_SELECTOR2 = '[role="menuitem"],[role="menuitemcheckbox"],[role="menuitemradio"]';
4
+ function isDisabledItem(item) {
5
+ if (item.hasAttribute("disabled"))
6
+ return true;
7
+ if (item.getAttribute("aria-disabled") === "true")
8
+ return true;
9
+ const flag = item.getAttribute("data-disabled");
10
+ return flag !== null && flag !== "false";
11
+ }
12
+ function getMenuItems2(menu) {
13
+ return Array.from(menu.querySelectorAll(MENU_ITEM_SELECTOR2)).filter((item) => item.closest('[role="menu"]') === menu && !isDisabledItem(item) && item.closest("[hidden]") === null);
14
+ }
15
+ function focusMenuItem2(menu, which) {
16
+ const items = getMenuItems2(menu);
17
+ const item = which === "first" ? items[0] : items[items.length - 1];
18
+ if (!item)
19
+ return false;
20
+ item.focus({ preventScroll: true });
21
+ return true;
22
+ }
23
+ function isKeyboardClick2(event) {
24
+ return event.detail === 0;
25
+ }
26
+ function itemText(item) {
27
+ return (item.textContent ?? "").trim().toLowerCase();
28
+ }
29
+ function handleMenuKeyDown2(event, menu, options = {}) {
30
+ const from = event.target instanceof Element ? event.target : document.activeElement;
31
+ if (!from || from.closest('[role="menu"]') !== menu)
32
+ return false;
33
+ if (event.key === "Tab") {
34
+ options.onTab?.();
35
+ return false;
36
+ }
37
+ if (event.ctrlKey || event.metaKey || event.altKey)
38
+ return false;
39
+ const items = getMenuItems2(menu);
40
+ if (items.length === 0)
41
+ return false;
42
+ const index = items.findIndex((item) => item === from);
43
+ let next;
44
+ switch (event.key) {
45
+ case "ArrowDown":
46
+ next = items[index < 0 ? 0 : (index + 1) % items.length];
47
+ break;
48
+ case "ArrowUp":
49
+ next = items[index <= 0 ? items.length - 1 : index - 1];
50
+ break;
51
+ case "Home":
52
+ next = items[0];
53
+ break;
54
+ case "End":
55
+ next = items[items.length - 1];
56
+ break;
57
+ default: {
58
+ if (event.key.length !== 1 || event.key === " ")
59
+ return false;
60
+ const letter = event.key.toLowerCase();
61
+ for (let step = 1;step <= items.length; step++) {
62
+ const candidate = items[(index + step + items.length) % items.length];
63
+ if (candidate && itemText(candidate).startsWith(letter)) {
64
+ next = candidate;
65
+ break;
66
+ }
67
+ }
68
+ }
69
+ }
70
+ if (!next)
71
+ return false;
72
+ event.preventDefault();
73
+ next.focus({ preventScroll: true });
74
+ return true;
75
+ }
76
+ function useMenuFocus2(options) {
77
+ let keyboard = false;
78
+ let focusedThisOpen = false;
79
+ let wasOpen = false;
80
+ const restoreFromBody = options.restoreFromBody ?? true;
81
+ effect(() => {
82
+ const isOpen = options.open();
83
+ const el = options.content.value;
84
+ if (isOpen) {
85
+ wasOpen = true;
86
+ if (!el || focusedThisOpen)
87
+ return;
88
+ focusedThisOpen = true;
89
+ const firstItem = keyboard || untrack(() => options.openedByKeyboard?.() ?? false);
90
+ queueMicrotask(() => {
91
+ if (!untrack(options.open))
92
+ return;
93
+ if (firstItem && focusMenuItem2(el, "first"))
94
+ return;
95
+ el.focus({ preventScroll: true });
96
+ });
97
+ return;
98
+ }
99
+ if (!wasOpen)
100
+ return;
101
+ wasOpen = false;
102
+ focusedThisOpen = false;
103
+ keyboard = false;
104
+ const trigger = untrack(() => options.trigger.value);
105
+ if (!trigger)
106
+ return;
107
+ const active = document.activeElement;
108
+ const inside = el !== null && active !== null && el.contains(active);
109
+ const lost = restoreFromBody && (active === null || active === document.body);
110
+ if (inside || lost)
111
+ trigger.focus({ preventScroll: true });
112
+ });
113
+ return {
114
+ markOpenedByKeyboard: (event) => {
115
+ keyboard = event instanceof KeyboardEvent || isKeyboardClick2(event);
116
+ },
117
+ onKeyDown: (event) => {
118
+ const el = options.content.value;
119
+ if (!el)
120
+ return;
121
+ handleMenuKeyDown2(event, el, { onTab: options.close });
122
+ }
123
+ };
124
+ }
125
+
126
+ export { MENU_ITEM_SELECTOR2, getMenuItems2, focusMenuItem2, isKeyboardClick2, handleMenuKeyDown2, useMenuFocus2 };
@@ -18,7 +18,56 @@ var _$tmpl_0 = {
18
18
  { path: [], type: "child" }
19
19
  ]
20
20
  };
21
- var DEFAULT_PALETTES = ["carzow", "jomlaty", "cazview", "voltzig"];
21
+ var BUILT_IN_PALETTES2 = ["carzow", "jomlaty", "cazview", "voltzig"];
22
+ var PALETTE_ATTRIBUTE = /\[\s*data-palette\s*=\s*(?:"([^"]+)"|'([^']+)'|([^\s\]]+))\s*(?:[is]\s*)?\]/g;
23
+ var NOT_PSEUDO = /:not\([^()]*\)/g;
24
+ function isRuleList(value) {
25
+ return typeof value === "object" && value !== null && "length" in value && typeof value.length === "number";
26
+ }
27
+ function isSheet(value) {
28
+ return typeof value === "object" && value !== null && "cssRules" in value;
29
+ }
30
+ function collectPalettes(rules, found) {
31
+ for (let i = 0;i < rules.length; i++) {
32
+ const rule = rules[i];
33
+ if (!rule)
34
+ continue;
35
+ if ("selectorText" in rule && typeof rule.selectorText === "string") {
36
+ const selector = rule.selectorText.replace(NOT_PSEUDO, "");
37
+ for (const match of selector.matchAll(PALETTE_ATTRIBUTE)) {
38
+ const name = match[1] ?? match[2] ?? match[3];
39
+ if (name)
40
+ found.add(name);
41
+ }
42
+ }
43
+ const children = Reflect.get(rule, "cssRules");
44
+ if (isRuleList(children))
45
+ collectPalettes(children, found);
46
+ const imported = Reflect.get(rule, "styleSheet");
47
+ if (isSheet(imported))
48
+ collectSheet(imported, found);
49
+ }
50
+ }
51
+ function collectSheet(sheet, found) {
52
+ let rules;
53
+ try {
54
+ rules = sheet.cssRules;
55
+ } catch {
56
+ return;
57
+ }
58
+ collectPalettes(rules, found);
59
+ }
60
+ function discoverPalettes2(doc) {
61
+ const target = doc ?? (typeof document === "undefined" ? undefined : document);
62
+ if (!target)
63
+ return [];
64
+ const found = new Set;
65
+ for (const sheet of Array.from(target.styleSheets))
66
+ collectSheet(sheet, found);
67
+ if (found.size === 0)
68
+ return [];
69
+ return Array.from(new Set(["carzow", ...found]));
70
+ }
22
71
  var ThemeKey2 = Symbol("sinwan-ui.theme");
23
72
  function isTheme(value) {
24
73
  return value === "light" || value === "dark" || value === "system";
@@ -36,15 +85,14 @@ function readStoredTheme(storageKey, fallback) {
36
85
  } catch {}
37
86
  return fallback;
38
87
  }
39
- function readStoredPalette(storageKey, fallback, allowed) {
88
+ function readStoredValue(storageKey) {
40
89
  if (typeof localStorage === "undefined")
41
- return fallback;
90
+ return null;
42
91
  try {
43
- const value = localStorage.getItem(storageKey);
44
- if (isPalette(value, allowed))
45
- return value;
46
- } catch {}
47
- return fallback;
92
+ return localStorage.getItem(storageKey);
93
+ } catch {
94
+ return null;
95
+ }
48
96
  }
49
97
  function getSystemTheme() {
50
98
  if (typeof window === "undefined" || !window.matchMedia)
@@ -78,11 +126,31 @@ function writeDomPalette(next) {
78
126
  document.documentElement.setAttribute("data-palette", next);
79
127
  }
80
128
  var ThemeProvider2 = cc((props) => {
81
- const allowedPalettes = () => (props.palettes === undefined ? DEFAULT_PALETTES : props.palettes).length > 0 ? props.palettes === undefined ? DEFAULT_PALETTES : props.palettes : DEFAULT_PALETTES;
129
+ const explicitPalettes = () => (props.palettes === undefined ? [] : props.palettes).length > 0 ? props.palettes === undefined ? [] : props.palettes : null;
130
+ const listPalettes = () => {
131
+ if (explicitPalettes())
132
+ return explicitPalettes();
133
+ const discovered = discoverPalettes2();
134
+ return Array.from(new Set([
135
+ ...discovered.length > 0 ? discovered : BUILT_IN_PALETTES2,
136
+ props.defaultPalette === undefined ? "carzow" : props.defaultPalette,
137
+ ...props.forcedPalette === undefined ? [] : [props.forcedPalette]
138
+ ]));
139
+ };
140
+ const allowedPalettes = signal(listPalettes());
82
141
  const initialTheme = props.forcedTheme ?? readStoredTheme(props.storageKey === undefined ? "sinwan-ui-theme" : props.storageKey, props.defaultTheme === undefined ? "system" : props.defaultTheme);
83
- const initialPalette = props.forcedPalette ?? readStoredPalette(props.paletteStorageKey === undefined ? "sinwan-ui-palette" : props.paletteStorageKey, props.defaultPalette === undefined ? "carzow" : props.defaultPalette, allowedPalettes());
142
+ const storedPalette = readStoredValue(props.paletteStorageKey === undefined ? "sinwan-ui-palette" : props.paletteStorageKey);
143
+ const initialPalette = props.forcedPalette ?? (isPalette(storedPalette, allowedPalettes.peek()) ? storedPalette : props.defaultPalette === undefined ? "carzow" : props.defaultPalette);
84
144
  const theme = signal(initialTheme);
85
145
  const palette = signal(initialPalette);
146
+ const refreshPalettes = () => {
147
+ const next = listPalettes();
148
+ const current = allowedPalettes.peek();
149
+ if (next.length !== current.length || next.some((name, index) => name !== current[index])) {
150
+ allowedPalettes.value = next;
151
+ }
152
+ return allowedPalettes.peek();
153
+ };
86
154
  const resolved = signal(initialTheme === "system" ? getSystemTheme() : initialTheme);
87
155
  const resolve = (value) => {
88
156
  if (props.forcedTheme === "light" || props.forcedTheme === "dark")
@@ -129,8 +197,9 @@ var ThemeProvider2 = cc((props) => {
129
197
  const setPalette = (value) => {
130
198
  if (props.forcedPalette)
131
199
  return;
132
- if (!allowedPalettes().includes(value))
200
+ if (!allowedPalettes.peek().includes(value) && !refreshPalettes().includes(value)) {
133
201
  return;
202
+ }
134
203
  try {
135
204
  localStorage.setItem(props.paletteStorageKey === undefined ? "sinwan-ui-palette" : props.paletteStorageKey, value);
136
205
  } catch {}
@@ -139,6 +208,9 @@ var ThemeProvider2 = cc((props) => {
139
208
  onMounted(() => {
140
209
  commit(theme.value);
141
210
  writeDomPalette(palette.value);
211
+ if (!props.forcedPalette && !explicitPalettes() && storedPalette !== null && storedPalette !== palette.peek() && isPalette(storedPalette, refreshPalettes())) {
212
+ commitPalette(storedPalette);
213
+ }
142
214
  if (!(props.enableSystem === undefined ? true : props.enableSystem))
143
215
  return;
144
216
  const mq = window.matchMedia("(prefers-color-scheme: dark)");
@@ -157,7 +229,8 @@ var ThemeProvider2 = cc((props) => {
157
229
  themes: (props.enableSystem === undefined ? true : props.enableSystem) ? ["light", "dark", "system"] : ["light", "dark"],
158
230
  palette,
159
231
  setPalette,
160
- palettes: allowedPalettes()
232
+ palettes: allowedPalettes,
233
+ refreshPalettes
161
234
  });
162
235
  computed(() => resolved.value);
163
236
  computed(() => palette.value);
@@ -184,4 +257,4 @@ var ThemeToggle2 = cc((props) => {
184
257
  ]);
185
258
  });
186
259
 
187
- export { ThemeKey2, ThemeProvider2, useTheme2, ThemeToggle2 };
260
+ export { BUILT_IN_PALETTES2, discoverPalettes2, ThemeKey2, ThemeProvider2, useTheme2, ThemeToggle2 };
@@ -13,9 +13,20 @@ function isControlledProp(props, key, component) {
13
13
  return true;
14
14
  }
15
15
 
16
+ // src/internal/ids.ts
17
+ import { getCurrentInstance } from "sinwan/component";
18
+ var mountCount = 0;
19
+ function treeId(prefix) {
20
+ const path = getCurrentInstance()?.idPath;
21
+ if (path !== undefined)
22
+ return `sinwan-ui-${prefix}-t${path.replace(/\./g, "_")}`;
23
+ mountCount += 1;
24
+ return `sinwan-ui-${prefix}-${mountCount}`;
25
+ }
26
+
16
27
  // src/core.tsx
17
28
  import { unwrap as _$unwrap } from "sinwan/reactivity";
18
- import { cc, onMounted, onUnmounted } from "sinwan/component";
29
+ import { cc, onUnmounted } from "sinwan/component";
19
30
  import { Portal as SinwanPortal2, Show as Show3 } from "sinwan/control-flow";
20
31
  import { effect, signal as signal2, untrack } from "sinwan/reactivity";
21
32
  import { jsx } from "sinwan/jsx-runtime";
@@ -55,7 +66,7 @@ var Presence2 = cc((props) => {
55
66
  return /* @__PURE__ */ jsx(Show3, {
56
67
  when: () => mounted.value,
57
68
  fallback: null,
58
- children: () => _$unwrap(props).children
69
+ children: props.children
59
70
  });
60
71
  });
61
72
  var UiPortal2 = cc((props) => {
@@ -85,18 +96,15 @@ function useControllableState2(options) {
85
96
  state.value = next;
86
97
  options.onChange?.(next);
87
98
  };
88
- onMounted(() => {
89
- const sync = () => {
90
- if (!isControlled)
91
- return;
99
+ if (isControlled) {
100
+ effect(() => {
92
101
  const v = read();
93
- if (v !== undefined && v !== state.value)
94
- state.value = v;
95
- };
96
- sync();
97
- const id = window.setInterval(sync, 16);
98
- onUnmounted(() => window.clearInterval(id));
99
- });
102
+ untrack(() => {
103
+ if (v !== undefined && v !== state.value)
104
+ state.value = v;
105
+ });
106
+ });
107
+ }
100
108
  return [state, setState];
101
109
  }
102
110
  function getFocusableElements2(root) {
@@ -119,4 +127,8 @@ function trapFocus2(event, root) {
119
127
  first.focus();
120
128
  }
121
129
  }
122
- export { isControlledProp, SinwanPortal2, Show3, signal2, Presence2, UiPortal2, useControllableState2, getFocusableElements2, trapFocus2 };
130
+ function usePartId2(prefix) {
131
+ return treeId(prefix);
132
+ }
133
+
134
+ export { isControlledProp, treeId, SinwanPortal2, Show3, signal2, Presence2, UiPortal2, useControllableState2, getFocusableElements2, trapFocus2, usePartId2 };
@@ -22,8 +22,8 @@ var DirectionProvider2 = cc((props) => {
22
22
  return _$createTemplate(_$tmpl_0, [() => dir.value, () => _$unwrap(props).children]);
23
23
  });
24
24
  function useDirection2() {
25
- const api = inject(DirectionKey2);
26
- return api?.dir.value ?? "ltr";
25
+ const api = inject(DirectionKey2, null);
26
+ return computed(() => api?.dir.value ?? "ltr");
27
27
  }
28
28
 
29
29
  export { DirectionKey2, DirectionProvider2, useDirection2 };
@@ -0,0 +1,65 @@
1
+ // src/dismiss.ts
2
+ import { onUnmounted } from "sinwan/component";
3
+ import { effect, untrack } from "sinwan/reactivity";
4
+ var DISMISS_EXEMPT_SELECTOR = [
5
+ "[data-slot$='-menu-content']",
6
+ "[data-slot='menubar-content']",
7
+ "[data-slot$='-sub-content']",
8
+ "[data-slot='select-content']",
9
+ "[data-slot='popover-content']",
10
+ "[data-slot='dialog-content']",
11
+ "[data-slot='alert-dialog-content']",
12
+ "[data-slot='sheet-content']",
13
+ "[data-slot='drawer-content']",
14
+ "[data-slot$='-overlay']",
15
+ "select",
16
+ "option",
17
+ "optgroup"
18
+ ].join(",");
19
+ function isDismissExemptPointerTarget2(target) {
20
+ if (target == null)
21
+ return false;
22
+ let el = null;
23
+ if (target instanceof Element) {
24
+ el = target;
25
+ } else if (target instanceof Node) {
26
+ el = target.parentElement;
27
+ }
28
+ if (el == null)
29
+ return false;
30
+ return el.closest(DISMISS_EXEMPT_SELECTOR) != null;
31
+ }
32
+ var layerStack = [];
33
+ function pushDismissLayer2(token) {
34
+ layerStack.push(token);
35
+ return () => {
36
+ const index = layerStack.lastIndexOf(token);
37
+ if (index !== -1)
38
+ layerStack.splice(index, 1);
39
+ };
40
+ }
41
+ function isTopDismissLayer2(token) {
42
+ return layerStack[layerStack.length - 1] === token;
43
+ }
44
+ function useDismissLayer2(open) {
45
+ const token = {};
46
+ let remove;
47
+ effect(() => {
48
+ const isOpen = open();
49
+ untrack(() => {
50
+ if (isOpen && !remove)
51
+ remove = pushDismissLayer2(token);
52
+ if (!isOpen && remove) {
53
+ remove();
54
+ remove = undefined;
55
+ }
56
+ });
57
+ });
58
+ onUnmounted(() => {
59
+ remove?.();
60
+ remove = undefined;
61
+ });
62
+ return { isTop: () => remove !== undefined && isTopDismissLayer2(token) };
63
+ }
64
+
65
+ export { isDismissExemptPointerTarget2, pushDismissLayer2, isTopDismissLayer2, useDismissLayer2 };