@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.
- package/README.md +82 -0
- package/package.json +47 -0
- package/src/App.svelte +103 -0
- package/src/components/Avatar.md +452 -0
- package/src/components/Avatar.svelte +593 -0
- package/src/components/Avatar.test.ts +231 -0
- package/src/components/AvatarGroup.md +246 -0
- package/src/components/AvatarGroup.svelte +691 -0
- package/src/components/AvatarGroup.test.ts +142 -0
- package/src/components/Badge.md +164 -0
- package/src/components/Badge.svelte +461 -0
- package/src/components/Badge.test.ts +293 -0
- package/src/components/Button.md +312 -0
- package/src/components/Button.svelte +796 -0
- package/src/components/Button.test.ts +631 -0
- package/src/components/Checkbox.md +313 -0
- package/src/components/Checkbox.svelte +466 -0
- package/src/components/Checkbox.test.ts +57 -0
- package/src/components/CodeBlock.md +240 -0
- package/src/components/CodeBlock.svelte +1287 -0
- package/src/components/CodeBlock.test.ts +192 -0
- package/src/components/Form.md +33 -0
- package/src/components/Form.test.ts +307 -0
- package/src/components/Frame.svelte +220 -0
- package/src/components/Icon.md +279 -0
- package/src/components/Icon.svelte +505 -0
- package/src/components/Icon.test.ts +82 -0
- package/src/components/Image.md +292 -0
- package/src/components/Image.svelte +634 -0
- package/src/components/Image.test.ts +42 -0
- package/src/components/ImageGroup.md +168 -0
- package/src/components/ImageGroup.svelte +151 -0
- package/src/components/ImageGroup.test.ts +46 -0
- package/src/components/InView.test.ts +55 -0
- package/src/components/Input.md +666 -0
- package/src/components/Input.svelte +2741 -0
- package/src/components/Input.test.ts +503 -0
- package/src/components/Kbd.md +100 -0
- package/src/components/Kbd.svelte +110 -0
- package/src/components/Kbd.test.ts +50 -0
- package/src/components/Listbox.md +245 -0
- package/src/components/Listbox.svelte +389 -0
- package/src/components/Listbox.test.ts +139 -0
- package/src/components/MenuItem.md +184 -0
- package/src/components/MenuItem.svelte +326 -0
- package/src/components/MenuItem.test.ts +167 -0
- package/src/components/Morph.test.ts +53 -0
- package/src/components/Popover.md +87 -0
- package/src/components/Popover.svelte +186 -0
- package/src/components/Popover.test.ts +60 -0
- package/src/components/QuickAction.md +146 -0
- package/src/components/QuickAction.svelte +161 -0
- package/src/components/QuickAction.test.ts +299 -0
- package/src/components/Radio.md +318 -0
- package/src/components/Radio.svelte +502 -0
- package/src/components/Radio.test.ts +76 -0
- package/src/components/Select.md +492 -0
- package/src/components/Select.svelte +535 -0
- package/src/components/Select.test.ts +146 -0
- package/src/components/Skeleton.md +323 -0
- package/src/components/Skeleton.svelte +791 -0
- package/src/components/Skeleton.test.ts +121 -0
- package/src/components/Spinner.md +136 -0
- package/src/components/Spinner.svelte +296 -0
- package/src/components/Spinner.test.ts +88 -0
- package/src/components/SplitText.test.ts +142 -0
- package/src/components/Stagger.test.ts +57 -0
- package/src/components/Test.md +72 -0
- package/src/components/Test.svelte +106 -0
- package/src/components/Test.test.ts +27 -0
- package/src/components/Text.md +450 -0
- package/src/components/Text.svelte +507 -0
- package/src/components/Text.test.ts +150 -0
- package/src/components/Textarea.md +240 -0
- package/src/components/Textarea.svelte +742 -0
- package/src/components/Textarea.test.ts +131 -0
- package/src/components/Toggle.md +196 -0
- package/src/components/Toggle.svelte +384 -0
- package/src/components/Toggle.test.ts +57 -0
- package/src/components/Tooltip.md +164 -0
- package/src/components/Tooltip.svelte +417 -0
- package/src/components/Tooltip.test.ts +64 -0
- package/src/context.ts +312 -0
- package/src/dev.ts +10 -0
- package/src/index.ts +29 -0
- package/src/keys.ts +26 -0
- package/src/lib/search-filter.md +82 -0
- package/src/lib/search-filter.ts +57 -0
- package/src/lib/truncate.md +25 -0
- package/src/lib/truncate.ts +234 -0
- package/src/logic/README.md +146 -0
- package/src/logic/collapse.svelte.ts +6 -0
- package/src/logic/frame-dom-runtime.md +19 -0
- package/src/logic/frame-dom-runtime.svelte.ts +286 -0
- package/src/logic/frame-field-control.md +38 -0
- package/src/logic/frame-field-control.ts +95 -0
- package/src/logic/frame-identity.md +68 -0
- package/src/logic/frame-identity.svelte.ts +25 -0
- package/src/logic/frame-layout.md +91 -0
- package/src/logic/frame-layout.svelte.ts +111 -0
- package/src/logic/frame-motion-defaults.md +180 -0
- package/src/logic/frame-motion-defaults.ts +711 -0
- package/src/logic/frame-profile.md +156 -0
- package/src/logic/frame-profile.ts +130 -0
- package/src/logic/frame-prop-filter.md +34 -0
- package/src/logic/frame-prop-filter.ts +254 -0
- package/src/logic/frame-responsive.md +91 -0
- package/src/logic/frame-responsive.svelte.ts +92 -0
- package/src/logic/frame-skeleton-runtime.md +20 -0
- package/src/logic/frame-skeleton-runtime.svelte.ts +160 -0
- package/src/logic/frame-truncation.md +127 -0
- package/src/logic/frame-truncation.svelte.ts +193 -0
- package/src/logic/frame-types.md +157 -0
- package/src/logic/frame-types.ts +434 -0
- package/src/logic/frame-utils.md +22 -0
- package/src/logic/frame-utils.ts +250 -0
- package/src/logic/geometry-cache.ts +120 -0
- package/src/logic/image-preview.md +24 -0
- package/src/logic/image-preview.ts +186 -0
- package/src/logic/layout-flip.ts +109 -0
- package/src/logic/motion-config.ts +102 -0
- package/src/logic/motion-types.md +47 -0
- package/src/logic/motion-types.ts +119 -0
- package/src/logic/observer.md +20 -0
- package/src/logic/observer.svelte.ts +53 -0
- package/src/logic/responsive.md +64 -0
- package/src/logic/responsive.svelte.ts +118 -0
- package/src/logic/text-collapse.md +22 -0
- package/src/logic/text-collapse.svelte.ts +507 -0
- package/src/logic/wc-adapter.ts +188 -0
- package/src/motion/NumberFlow.md +16 -0
- package/src/motion/README.md +87 -0
- package/src/motion/defaults/bridge.md +53 -0
- package/src/motion/defaults/bridge.ts +171 -0
- package/src/motion/defaults/content-crossfade.ts +22 -0
- package/src/motion/defaults/expand.md +66 -0
- package/src/motion/defaults/expand.ts +22 -0
- package/src/motion/defaults/fade-out.md +41 -0
- package/src/motion/defaults/fade-out.ts +14 -0
- package/src/motion/defaults/morph.md +42 -0
- package/src/motion/defaults/morph.ts +14 -0
- package/src/motion/defaults/move.md +81 -0
- package/src/motion/defaults/move.ts +31 -0
- package/src/motion/defaults/number.md +41 -0
- package/src/motion/defaults/number.ts +17 -0
- package/src/motion/defaults/replace.md +56 -0
- package/src/motion/defaults/replace.ts +22 -0
- package/src/motion/defaults/rotate.css +15 -0
- package/src/motion/defaults/rotate.md +60 -0
- package/src/motion/defaults/rotate.ts +20 -0
- package/src/motion/defaults/while-2xl.ts +9 -0
- package/src/motion/defaults/while-active.ts +12 -0
- package/src/motion/defaults/while-appear.ts +12 -0
- package/src/motion/defaults/while-click.ts +9 -0
- package/src/motion/defaults/while-collapsed.ts +12 -0
- package/src/motion/defaults/while-disabled.ts +13 -0
- package/src/motion/defaults/while-disappear.ts +12 -0
- package/src/motion/defaults/while-drag.ts +9 -0
- package/src/motion/defaults/while-editing.ts +9 -0
- package/src/motion/defaults/while-expanded.ts +12 -0
- package/src/motion/defaults/while-focus.ts +9 -0
- package/src/motion/defaults/while-hover.ts +13 -0
- package/src/motion/defaults/while-idle.ts +9 -0
- package/src/motion/defaults/while-key.ts +9 -0
- package/src/motion/defaults/while-lg.ts +9 -0
- package/src/motion/defaults/while-load.ts +11 -0
- package/src/motion/defaults/while-md.ts +9 -0
- package/src/motion/defaults/while-reload.ts +12 -0
- package/src/motion/defaults/while-scroll.ts +9 -0
- package/src/motion/defaults/while-sm.ts +9 -0
- package/src/motion/defaults/while-uploading.ts +9 -0
- package/src/motion/defaults/while-validating.ts +9 -0
- package/src/motion/defaults/while-xl.ts +9 -0
- package/src/motion/defaults/while-xs.ts +9 -0
- package/src/styles/README.md +95 -0
- package/src/styles/auto-layout.css +111 -0
- package/src/styles/index.css +7 -0
- package/src/styles/layout.css +319 -0
- package/src/styles/scale.css +106 -0
- package/src/styles/scale.md +218 -0
- package/src/styles/scrollbar.css +173 -0
- package/src/styles/tokens.css +192 -0
- package/src/vite-env.d.ts +9 -0
- package/src//344/273/243/347/240/201.txt +61 -0
|
@@ -0,0 +1,691 @@
|
|
|
1
|
+
<!--
|
|
2
|
+
AvatarGroup 组件:头像组组件
|
|
3
|
+
v1.8.6
|
|
4
|
+
|
|
5
|
+
变更记录 (2026-04-25):
|
|
6
|
+
- 修复 stack 模式 hover 错乱:stack 定位与 mask 改由实例级 CSS 规则承接,避免子 Avatar hover 时重写 style 属性清掉父组件写入的几何样式
|
|
7
|
+
|
|
8
|
+
变更记录 (2026-03-03):
|
|
9
|
+
- 修复切割半径测量受 transform 影响:改用布局尺寸(offsetWidth/offsetHeight)计算,确保边框与头像同比放大
|
|
10
|
+
- 修复动效场景下切割半径取值:mask 计算优先使用实时测量尺寸,不再被 size prop 固定值锁住
|
|
11
|
+
- 修复尺寸动效期间切割边框不同步:新增基于 ResizeObserver 的实时重算,确保 mask 半径随头像尺寸变化同步更新
|
|
12
|
+
|
|
13
|
+
变更记录 (2026-02-03):
|
|
14
|
+
- 新增 max 溢出计数展示与 +N 头像
|
|
15
|
+
- 叠放规则应用于所有直接子元素,边框与间距按 size 等比缩放
|
|
16
|
+
- 叠放使用边框切割方式(参考 Avatar 徽标切割思路)
|
|
17
|
+
- +N 头像使用固定白色文字与随机次要色背景
|
|
18
|
+
- 修复 AvatarGroup 选择器与测试资源导致的运行时错误
|
|
19
|
+
- 修复 max 隐藏规则被子组件样式覆盖的问题
|
|
20
|
+
- 新增 size 属性用于统一控制组内头像大小
|
|
21
|
+
- 修复溢出头像与普通头像的间隔不一致问题
|
|
22
|
+
- 内部变量命名改为 snowui 规范
|
|
23
|
+
- 补齐 Frame 能力开关显式传参
|
|
24
|
+
|
|
25
|
+
变更记录 (2026-02-04):
|
|
26
|
+
- 能力开关统一写法,显式传参
|
|
27
|
+
|
|
28
|
+
变更记录 (2026-02-05):
|
|
29
|
+
- 溢出头像背景色改为固定 #EDEDED,计数文字改为固定黑色
|
|
30
|
+
- 叠放改为真实边框切割(CSS Mask)
|
|
31
|
+
- 移除叠放头像的边框,避免暗色模式出现黑边
|
|
32
|
+
- 叠放层级调整:后面的头像在前面之上
|
|
33
|
+
- 叠放层级改为动态 z-index,避免数量限制
|
|
34
|
+
- 叠放层级修复:确保后面的头像在上层
|
|
35
|
+
- 边框切割改为切除前一项右侧,保证后面的头像在上层
|
|
36
|
+
- 添加 SIZE_CONTEXT 提供尺寸给子组件(通过 Frame 的 size prop)
|
|
37
|
+
- 新增 layout/groupSize/avatarSize,支持线性叠放与圆形堆叠
|
|
38
|
+
- stack 模式固定最多 3 个头像 + +N,且支持 avatarSize 撑大 groupSize
|
|
39
|
+
- stack 模式未传 avatarSize 时按 groupSize * 0.6 推导默认头像大小
|
|
40
|
+
- stack 模式圆形布局主动制造重叠,以确保边框切割可见
|
|
41
|
+
- stack 模式默认头像尺寸改为基于 max 的分段比例
|
|
42
|
+
-->
|
|
43
|
+
|
|
44
|
+
<script module lang="ts">
|
|
45
|
+
let avatarGroupInstanceSeed = 0;
|
|
46
|
+
</script>
|
|
47
|
+
|
|
48
|
+
<script lang="ts">
|
|
49
|
+
import { type Snippet } from "svelte";
|
|
50
|
+
import Frame, { type FrameProps } from "./Frame.svelte";
|
|
51
|
+
import Avatar from "./Avatar.svelte";
|
|
52
|
+
import { getSizeContext, getSize, type ResourceSize } from "../context";
|
|
53
|
+
import { resizeManager } from "../logic/observer.svelte";
|
|
54
|
+
|
|
55
|
+
type AvatarGroupLayout = "row" | "stack";
|
|
56
|
+
type StackPosition = {
|
|
57
|
+
item: HTMLElement;
|
|
58
|
+
x: number;
|
|
59
|
+
y: number;
|
|
60
|
+
zIndex: number;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
interface Props extends FrameProps {
|
|
64
|
+
max?: number;
|
|
65
|
+
layout?: AvatarGroupLayout;
|
|
66
|
+
size?: ResourceSize;
|
|
67
|
+
enableResponsive?: boolean;
|
|
68
|
+
enableContext?: boolean;
|
|
69
|
+
children?: Snippet;
|
|
70
|
+
[key: string]: any;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
let {
|
|
74
|
+
class: className = "",
|
|
75
|
+
style: userStyle = "",
|
|
76
|
+
children,
|
|
77
|
+
max = undefined,
|
|
78
|
+
layout = "row",
|
|
79
|
+
size = undefined,
|
|
80
|
+
enableResponsive = true,
|
|
81
|
+
enableContext = true,
|
|
82
|
+
renderHints: userRenderHints = undefined,
|
|
83
|
+
...rest
|
|
84
|
+
}: Props = $props();
|
|
85
|
+
|
|
86
|
+
getSize();
|
|
87
|
+
const sizeCtx = getSizeContext();
|
|
88
|
+
const contextSize = $derived(
|
|
89
|
+
sizeCtx?.avatarSize !== undefined ? sizeCtx.avatarSize : sizeCtx?.size,
|
|
90
|
+
);
|
|
91
|
+
const resolvedLayout = $derived(layout ?? "row");
|
|
92
|
+
const DEFAULT_AVATAR_SIZE_VAL: ResourceSize = 16;
|
|
93
|
+
// 尺寸处理逻辑
|
|
94
|
+
const effectiveSize = $derived(size ?? contextSize);
|
|
95
|
+
|
|
96
|
+
const finalGroupSize = $derived.by(() => {
|
|
97
|
+
if (effectiveSize === undefined) return undefined;
|
|
98
|
+
return effectiveSize;
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
const finalAvatarSize = $derived.by(() => {
|
|
102
|
+
if (effectiveSize === undefined) return undefined;
|
|
103
|
+
|
|
104
|
+
if (resolvedLayout === "stack") {
|
|
105
|
+
const baseMax =
|
|
106
|
+
typeof max === "number" ? Math.max(0, Math.floor(max)) : 3;
|
|
107
|
+
const stackRatio = baseMax <= 2 ? 1.8 : baseMax === 3 ? 2 : 2.2;
|
|
108
|
+
return effectiveSize / stackRatio;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return effectiveSize;
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const resolvedSizeContext = $derived(finalAvatarSize);
|
|
115
|
+
|
|
116
|
+
// 尺寸上下文由 Frame 的 size prop 提供(通过 enableContext)
|
|
117
|
+
|
|
118
|
+
let element = $state<HTMLElement>();
|
|
119
|
+
let overflowCount = $state(0);
|
|
120
|
+
const avatarGroupInstanceId = `snowui-avatar-group-${++avatarGroupInstanceSeed}`;
|
|
121
|
+
let stackStyleElement: HTMLStyleElement | null = null;
|
|
122
|
+
|
|
123
|
+
const sizeMap: Record<ResourceSize, string> = {
|
|
124
|
+
12: "calc(var(--size-4) * 3 / 4)",
|
|
125
|
+
16: "calc(var(--size-4) * 4 / 4)",
|
|
126
|
+
20: "calc(var(--size-4) * 5 / 4)",
|
|
127
|
+
24: "calc(var(--size-4) * 6 / 4)",
|
|
128
|
+
28: "calc(var(--size-4) * 7 / 4)",
|
|
129
|
+
32: "calc(var(--size-4) * 8 / 4)",
|
|
130
|
+
40: "calc(var(--size-4) * 10 / 4)",
|
|
131
|
+
48: "calc(var(--size-4) * 12 / 4)",
|
|
132
|
+
80: "calc(var(--size-4) * 20 / 4)",
|
|
133
|
+
};
|
|
134
|
+
|
|
135
|
+
const sizeRemMap: Record<ResourceSize, number> = {
|
|
136
|
+
12: 0.75,
|
|
137
|
+
16: 1,
|
|
138
|
+
20: 1.25,
|
|
139
|
+
24: 1.5,
|
|
140
|
+
28: 1.75,
|
|
141
|
+
32: 2,
|
|
142
|
+
40: 2.5,
|
|
143
|
+
48: 3,
|
|
144
|
+
80: 5,
|
|
145
|
+
};
|
|
146
|
+
|
|
147
|
+
const stackAngles: Record<number, number[]> = {
|
|
148
|
+
1: [-90],
|
|
149
|
+
2: [-45, 135],
|
|
150
|
+
3: [-90, 150, 30],
|
|
151
|
+
4: [-45, -135, 135, 45],
|
|
152
|
+
};
|
|
153
|
+
const STACK_CUTOUT_BORDER_RATIO = 0.04;
|
|
154
|
+
const STACK_CUTOUT_FEATHER_PX = 0.5;
|
|
155
|
+
|
|
156
|
+
function getSizeCssValue(value?: number) {
|
|
157
|
+
if (value === undefined) return "";
|
|
158
|
+
const mapped = sizeMap[value as ResourceSize];
|
|
159
|
+
if (mapped) return mapped;
|
|
160
|
+
return `${value / 16}em`;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
function getSizeRemValue(value?: number) {
|
|
164
|
+
if (value === undefined) return undefined;
|
|
165
|
+
const mapped = sizeRemMap[value as ResourceSize];
|
|
166
|
+
if (mapped) return mapped;
|
|
167
|
+
return value / 16;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
// 取不受 transform 影响的布局尺寸(优先 offsetWidth)
|
|
171
|
+
function getLayoutWidth(node?: HTMLElement | null) {
|
|
172
|
+
if (!node) return 0;
|
|
173
|
+
if (node.offsetWidth > 0) return node.offsetWidth;
|
|
174
|
+
const cssWidth = parseFloat(getComputedStyle(node).width || "0");
|
|
175
|
+
if (cssWidth > 0) return cssWidth;
|
|
176
|
+
return node.getBoundingClientRect().width || 0;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function getLayoutHeight(node?: HTMLElement | null) {
|
|
180
|
+
if (!node) return 0;
|
|
181
|
+
if (node.offsetHeight > 0) return node.offsetHeight;
|
|
182
|
+
const cssHeight = parseFloat(getComputedStyle(node).height || "0");
|
|
183
|
+
if (cssHeight > 0) return cssHeight;
|
|
184
|
+
return node.getBoundingClientRect().height || 0;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const overflowText = $derived(
|
|
188
|
+
overflowCount > 99
|
|
189
|
+
? "+99"
|
|
190
|
+
: overflowCount > 0
|
|
191
|
+
? `+${overflowCount}`
|
|
192
|
+
: "",
|
|
193
|
+
);
|
|
194
|
+
|
|
195
|
+
const overflowBg = $derived(overflowCount > 0 ? "#EDEDED" : "");
|
|
196
|
+
|
|
197
|
+
const itemSizeStyle = $derived.by(() => {
|
|
198
|
+
if (finalAvatarSize === undefined) return "";
|
|
199
|
+
const value = getSizeCssValue(finalAvatarSize);
|
|
200
|
+
return value ? `--snowui-avatar-group-item-size: ${value};` : "";
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
const groupSizeStyle = $derived.by(() => {
|
|
204
|
+
if (finalGroupSize === undefined) return "";
|
|
205
|
+
const value = getSizeCssValue(finalGroupSize);
|
|
206
|
+
return value ? `--snowui-avatar-group-size: ${value};` : "";
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
const combinedStyle = $derived.by(() => {
|
|
210
|
+
const countBgStyle = overflowBg
|
|
211
|
+
? `--snowui-avatar-group-count-bg: ${overflowBg};`
|
|
212
|
+
: "";
|
|
213
|
+
const styles = [userStyle, itemSizeStyle, groupSizeStyle, countBgStyle]
|
|
214
|
+
.filter(Boolean)
|
|
215
|
+
.join("; ");
|
|
216
|
+
return styles || undefined;
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
function ensureStackStyleElement() {
|
|
220
|
+
if (typeof document === "undefined") return null;
|
|
221
|
+
if (stackStyleElement?.isConnected) return stackStyleElement;
|
|
222
|
+
|
|
223
|
+
stackStyleElement = document.createElement("style");
|
|
224
|
+
stackStyleElement.dataset.snowuiAvatarGroupStyle =
|
|
225
|
+
avatarGroupInstanceId;
|
|
226
|
+
document.head.appendChild(stackStyleElement);
|
|
227
|
+
return stackStyleElement;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function clearStackStyleRules() {
|
|
231
|
+
if (stackStyleElement) {
|
|
232
|
+
stackStyleElement.textContent = "";
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function removeStackAttributes(item: HTMLElement) {
|
|
237
|
+
item.removeAttribute("data-snowui-avatar-group-id");
|
|
238
|
+
item.removeAttribute("data-snowui-avatar-group-stack-index");
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
function clearLegacyStackInlineStyles(item: HTMLElement) {
|
|
242
|
+
item.style.removeProperty("left");
|
|
243
|
+
item.style.removeProperty("top");
|
|
244
|
+
item.style.removeProperty("position");
|
|
245
|
+
item.style.removeProperty("margin-left");
|
|
246
|
+
item.style.removeProperty("mask-image");
|
|
247
|
+
item.style.removeProperty("-webkit-mask-image");
|
|
248
|
+
item.style.removeProperty("mask-composite");
|
|
249
|
+
item.style.removeProperty("-webkit-mask-composite");
|
|
250
|
+
item.style.removeProperty("z-index");
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
function applyStackLayout(
|
|
254
|
+
visibleItems: HTMLElement[],
|
|
255
|
+
countElement: HTMLElement | null,
|
|
256
|
+
itemSizePx: number,
|
|
257
|
+
groupSizePx: number,
|
|
258
|
+
): StackPosition[] {
|
|
259
|
+
const avatarCount = visibleItems.length;
|
|
260
|
+
const hasCount = Boolean(countElement);
|
|
261
|
+
const totalItems = avatarCount + (hasCount ? 1 : 0);
|
|
262
|
+
|
|
263
|
+
const angles =
|
|
264
|
+
stackAngles[Math.min(totalItems, 4)] || stackAngles[3] || [];
|
|
265
|
+
|
|
266
|
+
const radius = Math.max(0, (groupSizePx - itemSizePx) / 2);
|
|
267
|
+
const center = groupSizePx / 2;
|
|
268
|
+
const allItems: HTMLElement[] = [];
|
|
269
|
+
if (countElement) {
|
|
270
|
+
allItems.push(countElement);
|
|
271
|
+
}
|
|
272
|
+
allItems.push(...visibleItems);
|
|
273
|
+
|
|
274
|
+
return allItems.map((item, index) => {
|
|
275
|
+
const angle = angles[index % angles.length];
|
|
276
|
+
const rad = (Math.PI / 180) * angle;
|
|
277
|
+
const x = center + radius * Math.cos(rad) - itemSizePx / 2;
|
|
278
|
+
const y = center - radius * Math.sin(rad) - itemSizePx / 2;
|
|
279
|
+
return { item, x, y, zIndex: index + 1 };
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
function syncStackStyleRules(
|
|
284
|
+
positions: StackPosition[],
|
|
285
|
+
itemSizePx: number,
|
|
286
|
+
) {
|
|
287
|
+
const styleElement = ensureStackStyleElement();
|
|
288
|
+
if (!styleElement || !element) return;
|
|
289
|
+
|
|
290
|
+
element.dataset.snowuiAvatarGroupId = avatarGroupInstanceId;
|
|
291
|
+
|
|
292
|
+
// 与文档保持一致:stack 切割边框厚度固定为头像尺寸的 0.04 倍。
|
|
293
|
+
const borderPx = itemSizePx * STACK_CUTOUT_BORDER_RATIO;
|
|
294
|
+
const cutoutRadiusPx = itemSizePx / 2 + borderPx;
|
|
295
|
+
const rules: string[] = [];
|
|
296
|
+
|
|
297
|
+
positions.forEach((position, index) => {
|
|
298
|
+
const item = position.item;
|
|
299
|
+
item.dataset.snowuiAvatarGroupId = avatarGroupInstanceId;
|
|
300
|
+
item.dataset.snowuiAvatarGroupStackIndex = String(index);
|
|
301
|
+
|
|
302
|
+
const masks: string[] = [];
|
|
303
|
+
|
|
304
|
+
positions.forEach((other) => {
|
|
305
|
+
if (other.item === item || other.zIndex <= position.zIndex) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
const dx = other.x - position.x;
|
|
310
|
+
const dy = other.y - position.y;
|
|
311
|
+
const dist = Math.sqrt(dx * dx + dy * dy);
|
|
312
|
+
|
|
313
|
+
if (dist < itemSizePx + borderPx) {
|
|
314
|
+
const cx = dx + itemSizePx / 2;
|
|
315
|
+
const cy = dy + itemSizePx / 2;
|
|
316
|
+
masks.push(
|
|
317
|
+
`radial-gradient(circle at ${cx}px ${cy}px, transparent 0, transparent ${cutoutRadiusPx}px, black ${cutoutRadiusPx + STACK_CUTOUT_FEATHER_PX}px)`,
|
|
318
|
+
);
|
|
319
|
+
}
|
|
320
|
+
});
|
|
321
|
+
|
|
322
|
+
const maskRule =
|
|
323
|
+
masks.length > 0
|
|
324
|
+
? `
|
|
325
|
+
mask-image: ${masks.join(", ")};
|
|
326
|
+
-webkit-mask-image: ${masks.join(", ")};
|
|
327
|
+
mask-composite: intersect;
|
|
328
|
+
-webkit-mask-composite: source-in;`
|
|
329
|
+
: "";
|
|
330
|
+
|
|
331
|
+
rules.push(`
|
|
332
|
+
[data-snowui-avatar-group-id="${avatarGroupInstanceId}"] > [data-snowui-avatar-group-stack-index="${index}"] {
|
|
333
|
+
position: absolute;
|
|
334
|
+
margin-left: 0;
|
|
335
|
+
left: ${position.x}px;
|
|
336
|
+
top: ${position.y}px;
|
|
337
|
+
z-index: ${position.zIndex};
|
|
338
|
+
${maskRule}
|
|
339
|
+
}
|
|
340
|
+
`);
|
|
341
|
+
});
|
|
342
|
+
|
|
343
|
+
styleElement.textContent = rules.join("\n");
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
function updateItems() {
|
|
347
|
+
if (!element) return;
|
|
348
|
+
|
|
349
|
+
const items = Array.from(element.children).filter(
|
|
350
|
+
(child) =>
|
|
351
|
+
!(child as HTMLElement).classList.contains(
|
|
352
|
+
"snowui-avatar-group-count",
|
|
353
|
+
),
|
|
354
|
+
) as HTMLElement[];
|
|
355
|
+
|
|
356
|
+
const maxValue =
|
|
357
|
+
typeof max === "number" ? Math.max(0, Math.floor(max)) : undefined;
|
|
358
|
+
const totalCount = items.length;
|
|
359
|
+
|
|
360
|
+
let visibleCount = totalCount;
|
|
361
|
+
if (resolvedLayout === "stack") {
|
|
362
|
+
const capacity = Math.min(maxValue ?? 4, 4);
|
|
363
|
+
if (totalCount > capacity) {
|
|
364
|
+
visibleCount = Math.max(0, capacity - 1);
|
|
365
|
+
} else {
|
|
366
|
+
visibleCount = Math.min(totalCount, capacity);
|
|
367
|
+
}
|
|
368
|
+
} else if (maxValue !== undefined) {
|
|
369
|
+
visibleCount = Math.max(0, Math.min(totalCount, maxValue));
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
overflowCount = Math.max(0, totalCount - visibleCount);
|
|
373
|
+
|
|
374
|
+
const rootFontSize = parseFloat(
|
|
375
|
+
getComputedStyle(document.documentElement).fontSize || "16",
|
|
376
|
+
);
|
|
377
|
+
const visibleItems = items.filter((_, index) => index < visibleCount);
|
|
378
|
+
const baseItem = visibleItems[0] || items[0];
|
|
379
|
+
const measuredItemSize = getLayoutWidth(baseItem);
|
|
380
|
+
const itemRemValue = getSizeRemValue(finalAvatarSize);
|
|
381
|
+
const fallbackItemSizePx =
|
|
382
|
+
itemRemValue !== undefined
|
|
383
|
+
? itemRemValue * rootFontSize
|
|
384
|
+
: rootFontSize * 1.5; // Default 1.5rem
|
|
385
|
+
const itemSizePx =
|
|
386
|
+
measuredItemSize > 0 ? measuredItemSize : fallbackItemSizePx;
|
|
387
|
+
|
|
388
|
+
if (finalAvatarSize === undefined) {
|
|
389
|
+
const baseItemSize = getLayoutWidth(baseItem);
|
|
390
|
+
|
|
391
|
+
if (baseItemSize > 0) {
|
|
392
|
+
element.style.setProperty(
|
|
393
|
+
"--snowui-avatar-group-item-size",
|
|
394
|
+
`${baseItemSize}px`,
|
|
395
|
+
);
|
|
396
|
+
} else {
|
|
397
|
+
element.style.removeProperty("--snowui-avatar-group-item-size");
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
const countEl = element.querySelector<HTMLElement>(
|
|
402
|
+
".snowui-avatar-group-count",
|
|
403
|
+
);
|
|
404
|
+
const countElement = overflowCount > 0 ? countEl : null;
|
|
405
|
+
|
|
406
|
+
items.forEach((item, index) => {
|
|
407
|
+
const isHidden = index >= visibleCount;
|
|
408
|
+
|
|
409
|
+
// row 模式使用 index + 1,stack 模式在 applyStackLayout 中设置
|
|
410
|
+
if (resolvedLayout === "row") {
|
|
411
|
+
item.style.zIndex = String(index + 1);
|
|
412
|
+
}
|
|
413
|
+
item.hidden = isHidden;
|
|
414
|
+
if (isHidden) {
|
|
415
|
+
item.setAttribute("aria-hidden", "true");
|
|
416
|
+
} else {
|
|
417
|
+
item.removeAttribute("aria-hidden");
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
if (resolvedLayout === "stack") {
|
|
421
|
+
clearLegacyStackInlineStyles(item);
|
|
422
|
+
} else {
|
|
423
|
+
removeStackAttributes(item);
|
|
424
|
+
item.style.removeProperty("left");
|
|
425
|
+
item.style.removeProperty("top");
|
|
426
|
+
item.style.removeProperty("position");
|
|
427
|
+
item.style.removeProperty("margin-left");
|
|
428
|
+
}
|
|
429
|
+
});
|
|
430
|
+
|
|
431
|
+
if (countEl) {
|
|
432
|
+
if (resolvedLayout === "stack") {
|
|
433
|
+
clearLegacyStackInlineStyles(countEl);
|
|
434
|
+
} else {
|
|
435
|
+
removeStackAttributes(countEl);
|
|
436
|
+
countEl.style.removeProperty("position");
|
|
437
|
+
countEl.style.removeProperty("margin-left");
|
|
438
|
+
countEl.style.removeProperty("left");
|
|
439
|
+
countEl.style.removeProperty("top");
|
|
440
|
+
countEl.style.zIndex = String(items.length + 1);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
|
|
444
|
+
// Apply Layout and Masking
|
|
445
|
+
if (resolvedLayout === "stack") {
|
|
446
|
+
const measuredGroupWidth = getLayoutWidth(element);
|
|
447
|
+
const measuredGroupHeight = getLayoutHeight(element);
|
|
448
|
+
const measuredGroupSize = Math.max(
|
|
449
|
+
measuredGroupWidth,
|
|
450
|
+
measuredGroupHeight,
|
|
451
|
+
);
|
|
452
|
+
const groupRemValue = getSizeRemValue(finalGroupSize);
|
|
453
|
+
const fallbackGroupSizePx =
|
|
454
|
+
groupRemValue !== undefined
|
|
455
|
+
? groupRemValue * rootFontSize
|
|
456
|
+
: itemSizePx;
|
|
457
|
+
const groupSizePx =
|
|
458
|
+
measuredGroupSize > 0 ? measuredGroupSize : fallbackGroupSizePx;
|
|
459
|
+
const effectiveGroupSize = Math.max(groupSizePx, itemSizePx);
|
|
460
|
+
|
|
461
|
+
if (effectiveGroupSize) {
|
|
462
|
+
element.style.setProperty(
|
|
463
|
+
"--snowui-avatar-group-size",
|
|
464
|
+
`${effectiveGroupSize}px`,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const positions = applyStackLayout(
|
|
469
|
+
visibleItems,
|
|
470
|
+
countElement,
|
|
471
|
+
itemSizePx,
|
|
472
|
+
effectiveGroupSize,
|
|
473
|
+
);
|
|
474
|
+
syncStackStyleRules(positions, itemSizePx);
|
|
475
|
+
} else {
|
|
476
|
+
element.removeAttribute("data-snowui-avatar-group-id");
|
|
477
|
+
element.style.removeProperty("--snowui-avatar-group-size");
|
|
478
|
+
clearStackStyleRules();
|
|
479
|
+
|
|
480
|
+
// Clear any lingering absolute positioning or masks from stack mode
|
|
481
|
+
items.forEach((item) => {
|
|
482
|
+
removeStackAttributes(item);
|
|
483
|
+
item.style.removeProperty("left");
|
|
484
|
+
item.style.removeProperty("top");
|
|
485
|
+
item.style.removeProperty("position");
|
|
486
|
+
item.style.removeProperty("margin-left");
|
|
487
|
+
item.style.removeProperty("mask-image");
|
|
488
|
+
item.style.removeProperty("-webkit-mask-image");
|
|
489
|
+
item.style.removeProperty("mask-composite");
|
|
490
|
+
item.style.removeProperty("-webkit-mask-composite");
|
|
491
|
+
item.style.removeProperty("z-index");
|
|
492
|
+
});
|
|
493
|
+
|
|
494
|
+
// Row Masking 由 CSS 规则承接(见组件 style 块中的 data-layout="row" 规则)
|
|
495
|
+
// 无需 JS 内联样式,避免被 Frame 响应式 style 更新清除
|
|
496
|
+
}
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
$effect(() => {
|
|
500
|
+
if (!element) return;
|
|
501
|
+
const host = element;
|
|
502
|
+
updateItems();
|
|
503
|
+
|
|
504
|
+
let rafId: number | null = null;
|
|
505
|
+
const scheduleUpdate = () => {
|
|
506
|
+
if (rafId !== null) return;
|
|
507
|
+
rafId = requestAnimationFrame(() => {
|
|
508
|
+
rafId = null;
|
|
509
|
+
updateItems();
|
|
510
|
+
});
|
|
511
|
+
};
|
|
512
|
+
|
|
513
|
+
const resizeCleanup = new Set<() => void>();
|
|
514
|
+
const bindResizeObservers = () => {
|
|
515
|
+
resizeCleanup.forEach((off) => off());
|
|
516
|
+
resizeCleanup.clear();
|
|
517
|
+
|
|
518
|
+
resizeCleanup.add(resizeManager.watch(host, scheduleUpdate));
|
|
519
|
+
Array.from(host.children).forEach((child) => {
|
|
520
|
+
resizeCleanup.add(resizeManager.watch(child, scheduleUpdate));
|
|
521
|
+
});
|
|
522
|
+
};
|
|
523
|
+
|
|
524
|
+
bindResizeObservers();
|
|
525
|
+
|
|
526
|
+
const observer = new MutationObserver(() => {
|
|
527
|
+
scheduleUpdate();
|
|
528
|
+
bindResizeObservers();
|
|
529
|
+
});
|
|
530
|
+
observer.observe(host, { childList: true });
|
|
531
|
+
|
|
532
|
+
return () => {
|
|
533
|
+
observer.disconnect();
|
|
534
|
+
resizeCleanup.forEach((off) => off());
|
|
535
|
+
resizeCleanup.clear();
|
|
536
|
+
clearStackStyleRules();
|
|
537
|
+
stackStyleElement?.remove();
|
|
538
|
+
stackStyleElement = null;
|
|
539
|
+
if (rafId !== null) {
|
|
540
|
+
cancelAnimationFrame(rafId);
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
});
|
|
544
|
+
|
|
545
|
+
$effect(() => {
|
|
546
|
+
max;
|
|
547
|
+
layout;
|
|
548
|
+
size;
|
|
549
|
+
updateItems();
|
|
550
|
+
});
|
|
551
|
+
|
|
552
|
+
const avatarGroupRenderHints = $derived.by(() => {
|
|
553
|
+
return {
|
|
554
|
+
variant: userRenderHints?.variant ?? "circle",
|
|
555
|
+
width:
|
|
556
|
+
userRenderHints?.width ??
|
|
557
|
+
finalAvatarSize ??
|
|
558
|
+
DEFAULT_AVATAR_SIZE_VAL,
|
|
559
|
+
height:
|
|
560
|
+
userRenderHints?.height ??
|
|
561
|
+
finalAvatarSize ??
|
|
562
|
+
DEFAULT_AVATAR_SIZE_VAL,
|
|
563
|
+
};
|
|
564
|
+
});
|
|
565
|
+
</script>
|
|
566
|
+
|
|
567
|
+
<Frame
|
|
568
|
+
profile="Container"
|
|
569
|
+
enableResponsive={enableResponsive}
|
|
570
|
+
enableContext={enableContext}
|
|
571
|
+
enableScaleObserver={false}
|
|
572
|
+
size={resolvedSizeContext}
|
|
573
|
+
class={["snowui-avatar-group", className].filter(Boolean).join(" ")}
|
|
574
|
+
style={combinedStyle}
|
|
575
|
+
renderHints={avatarGroupRenderHints}
|
|
576
|
+
data-layout={resolvedLayout}
|
|
577
|
+
role="group"
|
|
578
|
+
bind:element
|
|
579
|
+
{...rest}
|
|
580
|
+
>
|
|
581
|
+
{#snippet children()}
|
|
582
|
+
{#if children}
|
|
583
|
+
{@render children({})}
|
|
584
|
+
{/if}
|
|
585
|
+
{#if overflowCount > 0}
|
|
586
|
+
<Avatar
|
|
587
|
+
class="snowui-avatar-group-count"
|
|
588
|
+
fallbackText={overflowText}
|
|
589
|
+
aria-label={overflowText}
|
|
590
|
+
/>
|
|
591
|
+
{/if}
|
|
592
|
+
{/snippet}
|
|
593
|
+
</Frame>
|
|
594
|
+
|
|
595
|
+
<style>
|
|
596
|
+
:global(:where(.snowui-avatar-group)) {
|
|
597
|
+
display: inline-flex;
|
|
598
|
+
align-items: center;
|
|
599
|
+
flex-direction: row;
|
|
600
|
+
vertical-align: top;
|
|
601
|
+
/* 统一尺寸基准,确保 +N 与 Avatar 子项的尺寸梯度 token 计算一致 */
|
|
602
|
+
font-size: var(--text-16);
|
|
603
|
+
line-height: 1;
|
|
604
|
+
--snowui-avatar-group-item-size-resolved: var(
|
|
605
|
+
--snowui-avatar-group-item-size-override,
|
|
606
|
+
var(--snowui-avatar-group-item-size, calc(var(--size-4) * 6 / 4))
|
|
607
|
+
);
|
|
608
|
+
--snowui-avatar-group-size-resolved: var(
|
|
609
|
+
--snowui-avatar-group-size-override,
|
|
610
|
+
var(
|
|
611
|
+
--snowui-avatar-group-size,
|
|
612
|
+
var(--snowui-avatar-group-item-size-resolved)
|
|
613
|
+
)
|
|
614
|
+
);
|
|
615
|
+
--snowui-avatar-group-overlap: calc(
|
|
616
|
+
var(--snowui-avatar-group-item-size-resolved) * 0.25
|
|
617
|
+
);
|
|
618
|
+
}
|
|
619
|
+
|
|
620
|
+
:global(:where(.snowui-avatar-group)[data-layout="stack"]) {
|
|
621
|
+
display: inline-block;
|
|
622
|
+
position: relative;
|
|
623
|
+
width: var(
|
|
624
|
+
--snowui-avatar-group-size-resolved,
|
|
625
|
+
var(--snowui-avatar-group-item-size-resolved)
|
|
626
|
+
);
|
|
627
|
+
height: var(
|
|
628
|
+
--snowui-avatar-group-size-resolved,
|
|
629
|
+
var(--snowui-avatar-group-item-size-resolved)
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
|
|
633
|
+
:global(:where(.snowui-avatar-group) > *) {
|
|
634
|
+
margin-left: calc(var(--snowui-avatar-group-overlap) * -1);
|
|
635
|
+
box-sizing: border-box;
|
|
636
|
+
position: relative;
|
|
637
|
+
flex-shrink: 0;
|
|
638
|
+
}
|
|
639
|
+
|
|
640
|
+
:global(:where(.snowui-avatar-group)[data-layout="stack"] > *) {
|
|
641
|
+
margin-left: 0;
|
|
642
|
+
}
|
|
643
|
+
|
|
644
|
+
:global(:where(.snowui-avatar-group) > :first-child) {
|
|
645
|
+
margin-left: 0;
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
:global(:where(.snowui-avatar-group) > [hidden]) {
|
|
649
|
+
display: none;
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
:global(:where(.snowui-avatar-group-count)) {
|
|
653
|
+
position: relative;
|
|
654
|
+
flex-shrink: 0;
|
|
655
|
+
color: var(--static-black-100, #000000);
|
|
656
|
+
}
|
|
657
|
+
|
|
658
|
+
:global(:where(.snowui-avatar-group-count) .snowui-avatar-background) {
|
|
659
|
+
background-color: var(--snowui-avatar-group-count-bg, #ededed);
|
|
660
|
+
}
|
|
661
|
+
|
|
662
|
+
/*
|
|
663
|
+
* Row 模式切割遮罩(CSS 实现)
|
|
664
|
+
* 选择所有可见且后方还有可见兄弟的子项,为它们切掉右侧与下一项重叠的圆弧。
|
|
665
|
+
* 使用 :has(~ *:not([hidden])) 精确定位"最后可见项之前的所有可见项",
|
|
666
|
+
* 避免对最后可见项施加多余切割。
|
|
667
|
+
* 此规则替代原 JS 内联 mask-image 注入,防止被 Frame 响应式 style 更新清除。
|
|
668
|
+
*/
|
|
669
|
+
:global(
|
|
670
|
+
:where(.snowui-avatar-group)[data-layout="row"]
|
|
671
|
+
> *:not([hidden]):has(~ *:not([hidden]))
|
|
672
|
+
) {
|
|
673
|
+
--_sg-s: var(--snowui-avatar-group-item-size-resolved);
|
|
674
|
+
--_sg-border: calc(var(--_sg-s) * 0.04);
|
|
675
|
+
--_sg-r: calc(var(--_sg-s) / 2 + var(--_sg-border));
|
|
676
|
+
/* center-x = size * 0.75 + size * 0.5 = size * 1.25 */
|
|
677
|
+
--_sg-cx: calc(var(--_sg-s) * 1.25);
|
|
678
|
+
mask-image: radial-gradient(
|
|
679
|
+
circle at var(--_sg-cx) 50%,
|
|
680
|
+
transparent 0,
|
|
681
|
+
transparent var(--_sg-r),
|
|
682
|
+
black calc(var(--_sg-r) + 0.5px)
|
|
683
|
+
);
|
|
684
|
+
-webkit-mask-image: radial-gradient(
|
|
685
|
+
circle at var(--_sg-cx) 50%,
|
|
686
|
+
transparent 0,
|
|
687
|
+
transparent var(--_sg-r),
|
|
688
|
+
black calc(var(--_sg-r) + 0.5px)
|
|
689
|
+
);
|
|
690
|
+
}
|
|
691
|
+
</style>
|