@snowui-design-system/components 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 (184) hide show
  1. package/README.md +82 -0
  2. package/package.json +47 -0
  3. package/src/App.svelte +103 -0
  4. package/src/components/Avatar.md +452 -0
  5. package/src/components/Avatar.svelte +593 -0
  6. package/src/components/Avatar.test.ts +231 -0
  7. package/src/components/AvatarGroup.md +246 -0
  8. package/src/components/AvatarGroup.svelte +691 -0
  9. package/src/components/AvatarGroup.test.ts +142 -0
  10. package/src/components/Badge.md +164 -0
  11. package/src/components/Badge.svelte +461 -0
  12. package/src/components/Badge.test.ts +293 -0
  13. package/src/components/Button.md +312 -0
  14. package/src/components/Button.svelte +796 -0
  15. package/src/components/Button.test.ts +631 -0
  16. package/src/components/Checkbox.md +313 -0
  17. package/src/components/Checkbox.svelte +466 -0
  18. package/src/components/Checkbox.test.ts +57 -0
  19. package/src/components/CodeBlock.md +240 -0
  20. package/src/components/CodeBlock.svelte +1287 -0
  21. package/src/components/CodeBlock.test.ts +192 -0
  22. package/src/components/Form.md +33 -0
  23. package/src/components/Form.test.ts +307 -0
  24. package/src/components/Frame.svelte +220 -0
  25. package/src/components/Icon.md +279 -0
  26. package/src/components/Icon.svelte +505 -0
  27. package/src/components/Icon.test.ts +82 -0
  28. package/src/components/Image.md +292 -0
  29. package/src/components/Image.svelte +634 -0
  30. package/src/components/Image.test.ts +42 -0
  31. package/src/components/ImageGroup.md +168 -0
  32. package/src/components/ImageGroup.svelte +151 -0
  33. package/src/components/ImageGroup.test.ts +46 -0
  34. package/src/components/InView.test.ts +55 -0
  35. package/src/components/Input.md +666 -0
  36. package/src/components/Input.svelte +2741 -0
  37. package/src/components/Input.test.ts +503 -0
  38. package/src/components/Kbd.md +100 -0
  39. package/src/components/Kbd.svelte +110 -0
  40. package/src/components/Kbd.test.ts +50 -0
  41. package/src/components/Listbox.md +245 -0
  42. package/src/components/Listbox.svelte +389 -0
  43. package/src/components/Listbox.test.ts +139 -0
  44. package/src/components/MenuItem.md +184 -0
  45. package/src/components/MenuItem.svelte +326 -0
  46. package/src/components/MenuItem.test.ts +167 -0
  47. package/src/components/Morph.test.ts +53 -0
  48. package/src/components/Popover.md +87 -0
  49. package/src/components/Popover.svelte +186 -0
  50. package/src/components/Popover.test.ts +60 -0
  51. package/src/components/QuickAction.md +146 -0
  52. package/src/components/QuickAction.svelte +161 -0
  53. package/src/components/QuickAction.test.ts +299 -0
  54. package/src/components/Radio.md +318 -0
  55. package/src/components/Radio.svelte +502 -0
  56. package/src/components/Radio.test.ts +76 -0
  57. package/src/components/Select.md +492 -0
  58. package/src/components/Select.svelte +535 -0
  59. package/src/components/Select.test.ts +146 -0
  60. package/src/components/Skeleton.md +323 -0
  61. package/src/components/Skeleton.svelte +791 -0
  62. package/src/components/Skeleton.test.ts +121 -0
  63. package/src/components/Spinner.md +136 -0
  64. package/src/components/Spinner.svelte +296 -0
  65. package/src/components/Spinner.test.ts +88 -0
  66. package/src/components/SplitText.test.ts +142 -0
  67. package/src/components/Stagger.test.ts +57 -0
  68. package/src/components/Test.md +72 -0
  69. package/src/components/Test.svelte +106 -0
  70. package/src/components/Test.test.ts +27 -0
  71. package/src/components/Text.md +450 -0
  72. package/src/components/Text.svelte +507 -0
  73. package/src/components/Text.test.ts +150 -0
  74. package/src/components/Textarea.md +240 -0
  75. package/src/components/Textarea.svelte +742 -0
  76. package/src/components/Textarea.test.ts +131 -0
  77. package/src/components/Toggle.md +196 -0
  78. package/src/components/Toggle.svelte +384 -0
  79. package/src/components/Toggle.test.ts +57 -0
  80. package/src/components/Tooltip.md +164 -0
  81. package/src/components/Tooltip.svelte +417 -0
  82. package/src/components/Tooltip.test.ts +64 -0
  83. package/src/context.ts +312 -0
  84. package/src/dev.ts +10 -0
  85. package/src/index.ts +29 -0
  86. package/src/keys.ts +26 -0
  87. package/src/lib/search-filter.md +82 -0
  88. package/src/lib/search-filter.ts +57 -0
  89. package/src/lib/truncate.md +25 -0
  90. package/src/lib/truncate.ts +234 -0
  91. package/src/logic/README.md +146 -0
  92. package/src/logic/collapse.svelte.ts +6 -0
  93. package/src/logic/frame-dom-runtime.md +19 -0
  94. package/src/logic/frame-dom-runtime.svelte.ts +286 -0
  95. package/src/logic/frame-field-control.md +38 -0
  96. package/src/logic/frame-field-control.ts +95 -0
  97. package/src/logic/frame-identity.md +68 -0
  98. package/src/logic/frame-identity.svelte.ts +25 -0
  99. package/src/logic/frame-layout.md +91 -0
  100. package/src/logic/frame-layout.svelte.ts +111 -0
  101. package/src/logic/frame-motion-defaults.md +180 -0
  102. package/src/logic/frame-motion-defaults.ts +711 -0
  103. package/src/logic/frame-profile.md +156 -0
  104. package/src/logic/frame-profile.ts +130 -0
  105. package/src/logic/frame-prop-filter.md +34 -0
  106. package/src/logic/frame-prop-filter.ts +254 -0
  107. package/src/logic/frame-responsive.md +91 -0
  108. package/src/logic/frame-responsive.svelte.ts +92 -0
  109. package/src/logic/frame-skeleton-runtime.md +20 -0
  110. package/src/logic/frame-skeleton-runtime.svelte.ts +160 -0
  111. package/src/logic/frame-truncation.md +127 -0
  112. package/src/logic/frame-truncation.svelte.ts +193 -0
  113. package/src/logic/frame-types.md +157 -0
  114. package/src/logic/frame-types.ts +434 -0
  115. package/src/logic/frame-utils.md +22 -0
  116. package/src/logic/frame-utils.ts +250 -0
  117. package/src/logic/geometry-cache.ts +120 -0
  118. package/src/logic/image-preview.md +24 -0
  119. package/src/logic/image-preview.ts +186 -0
  120. package/src/logic/layout-flip.ts +109 -0
  121. package/src/logic/motion-config.ts +102 -0
  122. package/src/logic/motion-types.md +47 -0
  123. package/src/logic/motion-types.ts +119 -0
  124. package/src/logic/observer.md +20 -0
  125. package/src/logic/observer.svelte.ts +53 -0
  126. package/src/logic/responsive.md +64 -0
  127. package/src/logic/responsive.svelte.ts +118 -0
  128. package/src/logic/text-collapse.md +22 -0
  129. package/src/logic/text-collapse.svelte.ts +507 -0
  130. package/src/logic/wc-adapter.ts +188 -0
  131. package/src/motion/NumberFlow.md +16 -0
  132. package/src/motion/README.md +87 -0
  133. package/src/motion/defaults/bridge.md +53 -0
  134. package/src/motion/defaults/bridge.ts +171 -0
  135. package/src/motion/defaults/content-crossfade.ts +22 -0
  136. package/src/motion/defaults/expand.md +66 -0
  137. package/src/motion/defaults/expand.ts +22 -0
  138. package/src/motion/defaults/fade-out.md +41 -0
  139. package/src/motion/defaults/fade-out.ts +14 -0
  140. package/src/motion/defaults/morph.md +42 -0
  141. package/src/motion/defaults/morph.ts +14 -0
  142. package/src/motion/defaults/move.md +81 -0
  143. package/src/motion/defaults/move.ts +31 -0
  144. package/src/motion/defaults/number.md +41 -0
  145. package/src/motion/defaults/number.ts +17 -0
  146. package/src/motion/defaults/replace.md +56 -0
  147. package/src/motion/defaults/replace.ts +22 -0
  148. package/src/motion/defaults/rotate.css +15 -0
  149. package/src/motion/defaults/rotate.md +60 -0
  150. package/src/motion/defaults/rotate.ts +20 -0
  151. package/src/motion/defaults/while-2xl.ts +9 -0
  152. package/src/motion/defaults/while-active.ts +12 -0
  153. package/src/motion/defaults/while-appear.ts +12 -0
  154. package/src/motion/defaults/while-click.ts +9 -0
  155. package/src/motion/defaults/while-collapsed.ts +12 -0
  156. package/src/motion/defaults/while-disabled.ts +13 -0
  157. package/src/motion/defaults/while-disappear.ts +12 -0
  158. package/src/motion/defaults/while-drag.ts +9 -0
  159. package/src/motion/defaults/while-editing.ts +9 -0
  160. package/src/motion/defaults/while-expanded.ts +12 -0
  161. package/src/motion/defaults/while-focus.ts +9 -0
  162. package/src/motion/defaults/while-hover.ts +13 -0
  163. package/src/motion/defaults/while-idle.ts +9 -0
  164. package/src/motion/defaults/while-key.ts +9 -0
  165. package/src/motion/defaults/while-lg.ts +9 -0
  166. package/src/motion/defaults/while-load.ts +11 -0
  167. package/src/motion/defaults/while-md.ts +9 -0
  168. package/src/motion/defaults/while-reload.ts +12 -0
  169. package/src/motion/defaults/while-scroll.ts +9 -0
  170. package/src/motion/defaults/while-sm.ts +9 -0
  171. package/src/motion/defaults/while-uploading.ts +9 -0
  172. package/src/motion/defaults/while-validating.ts +9 -0
  173. package/src/motion/defaults/while-xl.ts +9 -0
  174. package/src/motion/defaults/while-xs.ts +9 -0
  175. package/src/styles/README.md +95 -0
  176. package/src/styles/auto-layout.css +111 -0
  177. package/src/styles/index.css +7 -0
  178. package/src/styles/layout.css +319 -0
  179. package/src/styles/scale.css +106 -0
  180. package/src/styles/scale.md +218 -0
  181. package/src/styles/scrollbar.css +173 -0
  182. package/src/styles/tokens.css +192 -0
  183. package/src/vite-env.d.ts +9 -0
  184. package/src//344/273/243/347/240/201.txt +61 -0
@@ -0,0 +1,507 @@
1
+ /**
2
+ * Collapse Logic
3
+ * 提供通用的展开/收起动效逻辑。
4
+ */
5
+
6
+ import { resizeManager } from "./observer.svelte";
7
+
8
+ // 内联常量,避免依赖 PRO 包中的 morph.ts
9
+ const MORPH_STYLES = { transition: "all 300ms cubic-bezier(0.4, 0, 0.2, 1)" };
10
+
11
+ export type CollapseLines = 0 | 1 | 2 | 3;
12
+
13
+ export interface CollapseInfo {
14
+ isCollapsed: boolean;
15
+ styles: Record<string, string | null>;
16
+ }
17
+
18
+ export interface CollapseOptions {
19
+ lines?: CollapseLines;
20
+ isExpanded?: boolean;
21
+ onUpdate?: (info: CollapseInfo) => void;
22
+ _trigger?: any; // Used to trigger re-apply
23
+ }
24
+
25
+ export function measureLineHeight(cs: CSSStyleDeclaration) {
26
+ if (typeof document === "undefined" || !document.body) return NaN;
27
+ const probe = document.createElement("span");
28
+ probe.textContent = "Hg";
29
+ probe.style.position = "fixed";
30
+ probe.style.left = "-9999rem";
31
+ probe.style.top = "-9999rem";
32
+ probe.style.visibility = "hidden";
33
+ probe.style.whiteSpace = "nowrap";
34
+ probe.style.padding = "0";
35
+ probe.style.border = "0";
36
+ probe.style.margin = "0";
37
+ probe.style.fontFamily = cs.fontFamily;
38
+ probe.style.fontSize = cs.fontSize;
39
+ probe.style.fontWeight = cs.fontWeight;
40
+ probe.style.fontStyle = cs.fontStyle;
41
+ probe.style.letterSpacing = cs.letterSpacing;
42
+ probe.style.lineHeight = cs.lineHeight;
43
+ document.body.appendChild(probe);
44
+ const h = probe.getBoundingClientRect().height;
45
+ probe.remove();
46
+ return h;
47
+ }
48
+
49
+ export function findConstraintHost(
50
+ el: HTMLElement,
51
+ ignoreHeightValue: string | null = null,
52
+ ) {
53
+ const hasHeightUtilityClass = (node: HTMLElement) => {
54
+ const cls =
55
+ typeof node.className === "string" ? node.className : "";
56
+ return /\b(?:h|max-h|min-h)-(?:\d+|full|screen|fill|hug|\d+p)\b/.test(cls) ||
57
+ /\bsize-\d+\b/.test(cls);
58
+ };
59
+
60
+ const hasExplicitHeightConstraint = (value: string) => {
61
+ const v = (value || "").trim();
62
+ return (
63
+ v !== "" &&
64
+ v !== "auto" &&
65
+ v !== "none" &&
66
+ v !== "0" &&
67
+ v !== "0px"
68
+ );
69
+ };
70
+
71
+ const isIgnoredByValue = (
72
+ node: HTMLElement,
73
+ computedHeight: string,
74
+ computedMaxHeight: string,
75
+ ) => {
76
+ if (!ignoreHeightValue) return false;
77
+ const inlineHeight = (node.style.height || "").trim();
78
+ const inlineMaxHeight = (node.style.maxHeight || "").trim();
79
+ return (
80
+ computedHeight === ignoreHeightValue ||
81
+ computedMaxHeight === ignoreHeightValue ||
82
+ inlineHeight === ignoreHeightValue ||
83
+ inlineMaxHeight === ignoreHeightValue
84
+ );
85
+ };
86
+
87
+ // 1) 自身优先:识别 Text 本层由 class/style 声明的显式高度约束(不仅限内联 style)。
88
+ const selfCs = getComputedStyle(el);
89
+ const selfComputedHeight = (selfCs.height || "").trim();
90
+ const selfComputedMaxHeight = (selfCs.maxHeight || "").trim();
91
+ const selfOverflow = (selfCs.overflow || "").trim();
92
+ const selfOverflowY = (selfCs.overflowY || "").trim();
93
+ const selfHasOverflow =
94
+ (selfOverflow !== "" && selfOverflow !== "visible") ||
95
+ (selfOverflowY !== "" && selfOverflowY !== "visible");
96
+ const selfInlineHeight = (el.style.height || "").trim();
97
+ const selfInlineMaxHeight = (el.style.maxHeight || "").trim();
98
+ const selfIsInternalFillConstraint =
99
+ selfInlineHeight === "100%" || selfInlineMaxHeight === "100%";
100
+ const selfHasExplicitInlineConstraint =
101
+ hasExplicitHeightConstraint(selfInlineHeight) ||
102
+ hasExplicitHeightConstraint(selfInlineMaxHeight);
103
+ const selfHasExplicitConstraint =
104
+ selfHasExplicitInlineConstraint ||
105
+ hasExplicitHeightConstraint(selfComputedMaxHeight) ||
106
+ hasHeightUtilityClass(el);
107
+
108
+ if (
109
+ selfHasOverflow &&
110
+ selfHasExplicitConstraint &&
111
+ !selfIsInternalFillConstraint &&
112
+ !isIgnoredByValue(el, selfComputedHeight, selfComputedMaxHeight)
113
+ ) {
114
+ return el;
115
+ }
116
+
117
+ // 2) 对齐 Text2:向上找首个“max-height”或“overflow + fixed height”的父容器。
118
+ let p: HTMLElement | null = el.parentElement;
119
+ let depth = 0;
120
+ while (p && depth < 5) {
121
+ const cs = getComputedStyle(p);
122
+ const maxH = (cs.maxHeight || "").trim();
123
+ const h = (cs.height || "").trim();
124
+ const inlineH = (p.style.height || "").trim();
125
+ const inlineMaxH = (p.style.maxHeight || "").trim();
126
+ const hasUtilityHeightConstraint = hasHeightUtilityClass(p);
127
+ const parentHeightDiff = p.scrollHeight - p.clientHeight;
128
+ const parentWidthDiff = p.scrollWidth - p.clientWidth;
129
+ const parentActuallyClipping =
130
+ parentHeightDiff > 1 || parentWidthDiff > 1;
131
+
132
+ if (!isIgnoredByValue(p, h, maxH)) {
133
+ const overflow = (cs.overflow || "").trim();
134
+ const overflowY = (cs.overflowY || "").trim();
135
+ const hasMaxHeight =
136
+ hasExplicitHeightConstraint(maxH) ||
137
+ hasExplicitHeightConstraint(inlineMaxH);
138
+ const hasOverflow =
139
+ (overflow !== "" && overflow !== "visible") ||
140
+ (overflowY !== "" && overflowY !== "visible");
141
+ const hasFixedHeight =
142
+ hasExplicitHeightConstraint(inlineH) ||
143
+ hasUtilityHeightConstraint ||
144
+ (hasExplicitHeightConstraint(h) && parentActuallyClipping);
145
+
146
+ if (hasMaxHeight || (hasOverflow && hasFixedHeight)) {
147
+ return p;
148
+ }
149
+ }
150
+
151
+ p = p.parentElement;
152
+ depth += 1;
153
+ }
154
+
155
+ // 3) 兜底:多层动态布局场景,按真实裁剪差值识别,但跳过自身内部 100% 约束假宿主。
156
+ let current: HTMLElement | null = el;
157
+ depth = 0;
158
+ while (current && depth < 6) {
159
+ const cs = getComputedStyle(current);
160
+ const overflow = (cs.overflow || "").trim();
161
+ const overflowY = (cs.overflowY || "").trim();
162
+ const maxH = (cs.maxHeight || "").trim();
163
+ const hasOverflowConstraint =
164
+ (overflow !== "" && overflow !== "visible") ||
165
+ (overflowY !== "" && overflowY !== "visible");
166
+ const hasMaxHeight = hasExplicitHeightConstraint(maxH);
167
+ const inlineHeight = (current.style.height || "").trim();
168
+ const inlineMaxHeight = (current.style.maxHeight || "").trim();
169
+ const isInternalSelfControl =
170
+ current === el &&
171
+ (inlineHeight === "100%" || inlineMaxHeight === "100%");
172
+
173
+ if (
174
+ !isInternalSelfControl &&
175
+ !isIgnoredByValue(current, (cs.height || "").trim(), maxH) &&
176
+ (hasOverflowConstraint || hasMaxHeight)
177
+ ) {
178
+ const heightDiff = current.scrollHeight - current.clientHeight;
179
+ const widthDiff = current.scrollWidth - current.clientWidth;
180
+ if (heightDiff > 1 || widthDiff > 1) {
181
+ return current;
182
+ }
183
+ }
184
+
185
+ current = current.parentElement;
186
+ depth += 1;
187
+ }
188
+
189
+ return null;
190
+ }
191
+
192
+ export function computeLineMetrics(el: HTMLElement) {
193
+ const cs = getComputedStyle(el);
194
+ const fs = Number.parseFloat(cs.fontSize || "");
195
+ const fontSizePx = Number.isFinite(fs) && fs > 0 ? fs : NaN;
196
+ const lhRaw = cs.lineHeight;
197
+ let lh = Number.parseFloat(lhRaw || "");
198
+ if (!Number.isFinite(lh) || lh <= 0 || lhRaw === "normal") {
199
+ lh = measureLineHeight(cs);
200
+ }
201
+ return {
202
+ fontSizePx: Number.isFinite(fontSizePx) ? fontSizePx : null,
203
+ lineHeightPx: Number.isFinite(lh) && lh > 0 ? lh : null,
204
+ };
205
+ }
206
+
207
+ export function collapse(node: HTMLElement, options: CollapseOptions | undefined) {
208
+ if (typeof window === "undefined") {
209
+ return {
210
+ update() { },
211
+ destroy() { },
212
+ };
213
+ }
214
+
215
+ let opts = options || {};
216
+ let enabled = typeof opts.lines === "number";
217
+ let nodeUnwatch: (() => void) | null = null;
218
+ let hostUnwatch: (() => void) | null = null;
219
+ let lastHost: HTMLElement | null = null;
220
+ let host: HTMLElement | null = null;
221
+ let didEmit = false;
222
+
223
+ let last: CollapseInfo = {
224
+ isCollapsed: false,
225
+ styles: {},
226
+ };
227
+
228
+ const hasResizeObserver =
229
+ typeof (globalThis as any).ResizeObserver !== "undefined";
230
+
231
+ const state = {
232
+ pendingRaf: null as number | null,
233
+ pendingType: null as "raf" | "timeout" | null,
234
+ };
235
+
236
+ let currentStyles: Record<string, string | null> = {};
237
+
238
+ const setStyle = (
239
+ maxHeight: string | null,
240
+ overflow: string | null,
241
+ minHeight: string | null,
242
+ lineClamp: string | null,
243
+ height: string | null = null,
244
+ ) => {
245
+ currentStyles = {
246
+ "max-height": maxHeight,
247
+ overflow: overflow,
248
+ "min-height": minHeight,
249
+ "--text-line-clamp": lineClamp,
250
+ height: height,
251
+ transition: `var(--snowui-text-collapse-transition, ${MORPH_STYLES.transition})`,
252
+ };
253
+ };
254
+
255
+ const apply = () => {
256
+ if (!node.isConnected) return;
257
+ if (!enabled) return;
258
+
259
+ try {
260
+ const { lineHeightPx } = computeLineMetrics(node);
261
+ const isExpanded = opts.isExpanded ?? false;
262
+ const effectiveLines = opts.lines;
263
+ host = findConstraintHost(
264
+ node,
265
+ currentStyles.height || currentStyles["max-height"],
266
+ );
267
+
268
+ if (!host) {
269
+ let fallback: HTMLElement | null = node.parentElement;
270
+ let depth = 0;
271
+ while (fallback && depth < 3) {
272
+ const cs = getComputedStyle(fallback);
273
+ if (
274
+ cs.overflow !== "visible" ||
275
+ cs.height !== "auto" ||
276
+ cs.maxHeight !== "none"
277
+ ) {
278
+ host = fallback;
279
+ break;
280
+ }
281
+ fallback = fallback.parentElement;
282
+ depth += 1;
283
+ }
284
+ }
285
+
286
+ if (!host && effectiveLines === 0) {
287
+ host = node;
288
+ }
289
+
290
+ const constraintHeightPx = host
291
+ ? host.getBoundingClientRect().height || host.clientHeight
292
+ : null;
293
+
294
+ if (host !== lastHost) {
295
+ hostUnwatch?.();
296
+ hostUnwatch = null;
297
+ lastHost = host;
298
+ if (host) {
299
+ hostUnwatch = resizeManager.watch(host, requestApply);
300
+ }
301
+ }
302
+
303
+ if (typeof effectiveLines !== "number") {
304
+ setStyle(null, null, null, null);
305
+ } else if (effectiveLines === 0) {
306
+ if (isExpanded) {
307
+ setStyle(null, "visible", null, null, "auto");
308
+ } else if (constraintHeightPx) {
309
+ const applyMaxHeight = host !== node;
310
+ setStyle(
311
+ applyMaxHeight ? "100%" : null,
312
+ "hidden",
313
+ null,
314
+ null,
315
+ applyMaxHeight ? "100%" : null,
316
+ );
317
+
318
+ checkCollapse(
319
+ node,
320
+ opts,
321
+ last,
322
+ (next) => (last = next),
323
+ constraintHeightPx,
324
+ currentStyles,
325
+ );
326
+ return;
327
+ } else {
328
+ setStyle(null, null, null, null);
329
+ }
330
+ } else if (effectiveLines > 0) {
331
+ if (lineHeightPx) {
332
+ const desired = Math.max(1, effectiveLines) * lineHeightPx;
333
+ const desiredPx = `${desired / 16}rem`;
334
+ if (constraintHeightPx && constraintHeightPx < desired) {
335
+ const capPx = `${constraintHeightPx / 16}rem`;
336
+ setStyle(capPx, "hidden", null, String(effectiveLines));
337
+ } else {
338
+ // lines>1 场景始终以 max-height 约束可视行数,避免仅 min-height 时在无外层容器下出现“未截断”。
339
+ setStyle(desiredPx, "hidden", null, String(effectiveLines));
340
+ }
341
+ } else {
342
+ setStyle(null, null, null, String(effectiveLines));
343
+ }
344
+ } else {
345
+ setStyle(null, null, null, null);
346
+ }
347
+
348
+ checkCollapse(
349
+ node,
350
+ opts,
351
+ last,
352
+ (next) => (last = next),
353
+ constraintHeightPx,
354
+ currentStyles,
355
+ );
356
+ } catch (e) {
357
+ console.error("Collapse error:", e);
358
+ }
359
+ };
360
+
361
+ function requestApply() {
362
+ if (state.pendingRaf !== null) {
363
+ if (state.pendingType === "timeout") {
364
+ clearTimeout(state.pendingRaf as any);
365
+ } else {
366
+ const cancel = (globalThis as any).cancelAnimationFrame;
367
+ cancel?.(state.pendingRaf);
368
+ }
369
+ }
370
+ state.pendingRaf = requestAnimationFrame(() => {
371
+ state.pendingRaf = null;
372
+ state.pendingType = null;
373
+ apply();
374
+ });
375
+ state.pendingType = "raf";
376
+ }
377
+
378
+ function checkCollapse(
379
+ el: HTMLElement,
380
+ opts: CollapseOptions,
381
+ lastInfo: CollapseInfo,
382
+ setLast: (i: CollapseInfo) => void,
383
+ constraintHeightPx: number | null,
384
+ styles: Record<string, string | null>,
385
+ ) {
386
+ let measureEl: HTMLElement = el;
387
+ if (opts.lines === 0 && !(opts.isExpanded ?? false)) {
388
+ const direct = el.querySelector(":scope > .snowui-text-truncate-wrapper") as HTMLElement | null;
389
+ measureEl = direct ?? (el.querySelector(".snowui-text-truncate-wrapper") as HTMLElement | null) ?? el;
390
+ }
391
+ const cs = getComputedStyle(measureEl);
392
+ const isHorizontal = cs.whiteSpace === "nowrap";
393
+ const visibleHeightPx =
394
+ opts.lines === 0 && !(opts.isExpanded ?? false)
395
+ ? (() => {
396
+ const clientH = measureEl.clientHeight;
397
+ if (!constraintHeightPx) return clientH;
398
+ if (!clientH) return constraintHeightPx;
399
+ return Math.min(constraintHeightPx, clientH);
400
+ })()
401
+ : measureEl.clientHeight;
402
+ const diff =
403
+ opts.lines === 0 &&
404
+ !(opts.isExpanded ?? false) &&
405
+ !isHorizontal &&
406
+ visibleHeightPx
407
+ ? measureEl.scrollHeight - visibleHeightPx
408
+ : isHorizontal
409
+ ? measureEl.scrollWidth - measureEl.clientWidth
410
+ : measureEl.scrollHeight - measureEl.clientHeight;
411
+
412
+ const threshold = lastInfo.isCollapsed ? 0 : 1;
413
+ const isCollapsed = diff > threshold;
414
+
415
+ const info: CollapseInfo = {
416
+ isCollapsed,
417
+ styles,
418
+ };
419
+
420
+ if (!didEmit) {
421
+ didEmit = true;
422
+ setLast(info);
423
+ opts.onUpdate?.(info);
424
+ return;
425
+ }
426
+
427
+ if (
428
+ info.isCollapsed !== lastInfo.isCollapsed ||
429
+ JSON.stringify(info.styles) !== JSON.stringify(lastInfo.styles)
430
+ ) {
431
+ setLast(info);
432
+ opts.onUpdate?.(info);
433
+ }
434
+ }
435
+
436
+ function disable() {
437
+ enabled = false;
438
+ nodeUnwatch?.();
439
+ nodeUnwatch = null;
440
+ hostUnwatch?.();
441
+ hostUnwatch = null;
442
+ lastHost = null;
443
+ setStyle(null, null, null, null);
444
+ const nextInfo: CollapseInfo = {
445
+ isCollapsed: false,
446
+ styles: {},
447
+ };
448
+ last = nextInfo;
449
+ opts.onUpdate?.(nextInfo);
450
+ }
451
+
452
+ function enable() {
453
+ if (enabled) return;
454
+ if (!hasResizeObserver) {
455
+ disable();
456
+ return;
457
+ }
458
+ enabled = true;
459
+ nodeUnwatch = resizeManager.watch(node, requestApply);
460
+ apply();
461
+ requestApply();
462
+ }
463
+
464
+ if (!hasResizeObserver) {
465
+ disable();
466
+ return {
467
+ update(next: CollapseOptions) {
468
+ opts = next || {};
469
+ disable();
470
+ },
471
+ destroy() { },
472
+ };
473
+ }
474
+ if (enabled) {
475
+ nodeUnwatch = resizeManager.watch(node, requestApply);
476
+ apply();
477
+ requestApply();
478
+ } else {
479
+ disable();
480
+ }
481
+
482
+ return {
483
+ update(next: CollapseOptions) {
484
+ opts = next || {};
485
+ const nextEnabled = typeof opts.lines === "number";
486
+ if (!nextEnabled) {
487
+ disable();
488
+ return;
489
+ }
490
+ enable();
491
+ requestApply();
492
+ },
493
+ destroy() {
494
+ nodeUnwatch?.();
495
+ hostUnwatch?.();
496
+ if (state.pendingRaf !== null) {
497
+ if (state.pendingType === "timeout") {
498
+ clearTimeout(state.pendingRaf as any);
499
+ } else {
500
+ const cancel = (globalThis as any).cancelAnimationFrame;
501
+ cancel?.(state.pendingRaf);
502
+ }
503
+ }
504
+ setStyle(null, null, null, null);
505
+ },
506
+ };
507
+ }
@@ -0,0 +1,188 @@
1
+ /**
2
+ * Web Components 适配层
3
+ * v1.0.0
4
+ *
5
+ * 为 Frame 组件提供 Web Components 兼容接口,
6
+ * 使 Svelte 组件能够在原生 Web Component 环境中使用。
7
+ */
8
+
9
+ import type { FrameProps } from './frame-types';
10
+
11
+ /**
12
+ * Props 序列化/反序列化工具
13
+ * 用于将 FrameProps 转换为 HTML 属性格式
14
+ */
15
+ export interface WCPropsAdapter {
16
+ /**
17
+ * 将 FrameProps 序列化为 HTML 属性
18
+ * 复杂对象会被 JSON 序列化
19
+ */
20
+ serialize(props: Partial<FrameProps>): Record<string, string>;
21
+
22
+ /**
23
+ * 从 HTML 属性反序列化为 FrameProps
24
+ */
25
+ deserialize(attrs: NamedNodeMap): Partial<FrameProps>;
26
+ }
27
+
28
+ /**
29
+ * Snippet → Slot 映射配置
30
+ */
31
+ export interface SnippetToSlotMapping {
32
+ /** 默认 slot */
33
+ default: string;
34
+ /** 命名 snippets 到 named slots 的映射 */
35
+ named: Record<string, string>;
36
+ }
37
+
38
+ /**
39
+ * Context → 属性平坦化配置
40
+ */
41
+ export interface ContextFlattenConfig {
42
+ /** Context key → 属性前缀的映射 */
43
+ prefixes: Record<symbol, string>;
44
+ /** 需要暴露为属性的 Context 键 */
45
+ expose: string[];
46
+ }
47
+
48
+ /**
49
+ * Web Component 适配器接口
50
+ */
51
+ export interface WCAdapter {
52
+ /** Props 适配器 */
53
+ props: WCPropsAdapter;
54
+
55
+ /** Snippet → Slot 映射 */
56
+ snippetMapping: SnippetToSlotMapping;
57
+
58
+ /** Context 平坦化配置 */
59
+ contextConfig: ContextFlattenConfig;
60
+
61
+ /**
62
+ * 创建自定义元素类
63
+ * @param tagName - 自定义元素标签名 (如 "snowui-frame")
64
+ */
65
+ createCustomElement(tagName: string): CustomElementConstructor;
66
+ }
67
+
68
+ // === 默认实现 ===
69
+
70
+ /**
71
+ * 默认 Props 序列化器
72
+ */
73
+ export const defaultPropsAdapter: WCPropsAdapter = {
74
+ serialize(props: Partial<FrameProps>): Record<string, string> {
75
+ const result: Record<string, string> = {};
76
+
77
+ for (const [key, value] of Object.entries(props)) {
78
+ if (value === undefined) continue;
79
+
80
+ if (typeof value === 'object') {
81
+ result[`data-${key}`] = JSON.stringify(value);
82
+ } else if (typeof value === 'boolean') {
83
+ if (value) result[key] = '';
84
+ } else {
85
+ result[key] = String(value);
86
+ }
87
+ }
88
+
89
+ return result;
90
+ },
91
+
92
+ deserialize(attrs: NamedNodeMap): Partial<FrameProps> {
93
+ const result: Partial<FrameProps> = {};
94
+
95
+ for (let i = 0; i < attrs.length; i++) {
96
+ const attr = attrs[i];
97
+ const key = attr.name.replace(/^data-/, '');
98
+ const value = attr.value;
99
+
100
+ // 尝试 JSON 解析
101
+ if (value.startsWith('{') || value.startsWith('[')) {
102
+ try {
103
+ (result as any)[key] = JSON.parse(value);
104
+ } catch {
105
+ (result as any)[key] = value;
106
+ }
107
+ } else if (value === '' || value === 'true') {
108
+ (result as any)[key] = true;
109
+ } else if (value === 'false') {
110
+ (result as any)[key] = false;
111
+ } else if (!isNaN(Number(value))) {
112
+ (result as any)[key] = Number(value);
113
+ } else {
114
+ (result as any)[key] = value;
115
+ }
116
+ }
117
+
118
+ return result;
119
+ }
120
+ };
121
+
122
+ /**
123
+ * 默认 Snippet → Slot 映射
124
+ */
125
+ export const defaultSnippetMapping: SnippetToSlotMapping = {
126
+ default: '', // 默认 slot 无名称
127
+ named: {
128
+ // 预留:未来可扩展命名 snippets
129
+ }
130
+ };
131
+
132
+ /**
133
+ * 创建基础的 Web Component 包装器
134
+ * 注意:这是一个简化的实现,完整版需要与 Svelte 的 Custom Elements 编译选项配合使用
135
+ *
136
+ * @param SvelteComponent - Svelte 组件类
137
+ * @param tagName - 自定义元素标签名
138
+ */
139
+ export function createFrameElement(
140
+ SvelteComponent: any,
141
+ tagName: string = 'snowui-frame'
142
+ ): CustomElementConstructor {
143
+ class FrameElement extends HTMLElement {
144
+ private _component: any;
145
+
146
+ static get observedAttributes() {
147
+ return ['class', 'style', 'profile', 'disabled', 'loading'];
148
+ }
149
+
150
+ constructor() {
151
+ super();
152
+ this.attachShadow({ mode: 'open' });
153
+ }
154
+
155
+ connectedCallback() {
156
+ const props = defaultPropsAdapter.deserialize(this.attributes);
157
+
158
+ this._component = new SvelteComponent({
159
+ target: this.shadowRoot!,
160
+ props: {
161
+ ...props,
162
+ children: () => {
163
+ // 将 light DOM 内容移至 shadow DOM
164
+ const slot = document.createElement('slot');
165
+ return slot;
166
+ }
167
+ }
168
+ });
169
+ }
170
+
171
+ disconnectedCallback() {
172
+ this._component?.$destroy();
173
+ }
174
+
175
+ attributeChangedCallback(name: string, _oldValue: string, newValue: string) {
176
+ if (this._component) {
177
+ this._component.$set({ [name]: newValue });
178
+ }
179
+ }
180
+ }
181
+
182
+ // 注册自定义元素
183
+ if (!customElements.get(tagName)) {
184
+ customElements.define(tagName, FrameElement);
185
+ }
186
+
187
+ return FrameElement;
188
+ }
@@ -0,0 +1,16 @@
1
+ # NumberFlow.svelte(迁移说明)
2
+
3
+ > **版本**: v1.3.1
4
+ > **路径**: `src/components/NumberFlow.svelte`
5
+ > **职责**: `motion/NumberFlow.svelte` 兼容层已删除,统一使用组件目录主实现。
6
+
7
+ ## 说明
8
+
9
+ - 请直接使用 `src/components/NumberFlow.svelte`。
10
+ - 建议新代码直接从 `components` 目录导入:
11
+
12
+ ```ts
13
+ import NumberFlow from "../components/NumberFlow.svelte";
14
+ ```
15
+
16
+ - 旧路径 `src/motion/NumberFlow.svelte` 已移除。