|
1 | | -import { splitProps, Show, type ParentComponent, createMemo } from "solid-js"; |
2 | | -import { type DividerProps } from "./setting"; |
3 | | -import { cn } from "@solid-element-ui/utils/cn"; |
| 1 | +import { splitProps, Show, createMemo } from "solid-js"; |
| 2 | +import { dividerVariants, type DividerProps } from "./setting"; |
4 | 3 |
|
5 | | -// 1. 定义预设颜色映射(对应你 Tailwind v4 的主题配置) |
6 | | -const COLOR_MAP: Record<string, string> = { |
7 | | - primary: "border-primary text-primary", |
8 | | - success: "border-green-500 text-green-500", |
9 | | - warning: "border-yellow-500 text-yellow-500", |
10 | | - danger: "border-red-500 text-red-500", |
11 | | - info: "border-gray-400 text-gray-400", |
12 | | -}; |
13 | | - |
14 | | -const customAttributes = [ |
15 | | - "direction", |
16 | | - "contentPosition", |
17 | | - "dashed", |
18 | | - "color", |
19 | | - "class", |
20 | | - "children", |
21 | | -] as const; |
| 4 | +// 对应 Tailwind 主题配置的预设名 |
| 5 | +const PRESET_COLORS = ["primary", "success", "warning", "danger", "info"]; |
22 | 6 |
|
23 | | -export const SeDivider: ParentComponent<DividerProps> = ( |
24 | | - props: DividerProps |
25 | | -) => { |
26 | | - const [local, others] = splitProps(props, customAttributes); |
| 7 | +export const Divider = (props: DividerProps) => { |
| 8 | + // 1. 拆分参数:style 必须分到 local 中以修复 TS2339 |
| 9 | + const [local, variantProps, others] = splitProps( |
| 10 | + props, |
| 11 | + ["class", "style", "children", "color"], |
| 12 | + ["direction", "contentPosition", "dashed"] |
| 13 | + ); |
27 | 14 |
|
28 | | - const isVertical = () => local.direction === "vertical"; |
29 | | - const contentPos = () => local.contentPosition || "center"; |
| 15 | + const isVertical = () => variantProps.direction === "vertical"; |
30 | 16 |
|
31 | | - // 2. 核心判断逻辑 |
| 17 | + // 2. 判断是否为自定义颜色(如 #fff, rgb...) |
32 | 18 | const isCustomColor = createMemo( |
33 | | - () => local.color && !COLOR_MAP[local.color] |
| 19 | + () => local.color && !PRESET_COLORS.includes(local.color) |
34 | 20 | ); |
35 | | - const presetColorClass = createMemo(() => |
36 | | - local.color ? COLOR_MAP[local.color] : "" |
| 21 | + |
| 22 | + // 3. 生成 TV 样式类 |
| 23 | + const styles = createMemo(() => |
| 24 | + dividerVariants({ |
| 25 | + ...variantProps, |
| 26 | + // 如果是预设色,传给 TV;如果是自定义色,TV 不处理 |
| 27 | + color: !isCustomColor() ? (local.color as any) : undefined, |
| 28 | + hasChildren: !!local.children && !isVertical(), |
| 29 | + }) |
37 | 30 | ); |
38 | 31 |
|
39 | | - // 3. 动态样式处理 |
| 32 | + // 4. 处理 Style 逻辑:合并自定义颜色 |
40 | 33 | const resolveStyles = createMemo(() => { |
41 | | - if (!isCustomColor()) return others.style || {}; |
42 | | - return { |
43 | | - "border-color": local.color, |
44 | | - color: local.color, |
45 | | - ...(others.style as any), |
46 | | - }; |
| 34 | + const baseStyle = typeof local.style === "object" ? local.style : {}; |
| 35 | + |
| 36 | + if (isCustomColor()) { |
| 37 | + return { |
| 38 | + "border-color": local.color, |
| 39 | + color: local.color, // 让 text-inherit 和 border-inherit 生效 |
| 40 | + ...baseStyle, |
| 41 | + }; |
| 42 | + } |
| 43 | + |
| 44 | + // 无颜色且无文字时的默认兜底色(Tailwind v4 默认透明,需指定) |
| 45 | + if (!local.color && !local.children) { |
| 46 | + return { |
| 47 | + "border-color": "var(--color-gray-200, #e5e7eb)", |
| 48 | + ...baseStyle, |
| 49 | + }; |
| 50 | + } |
| 51 | + |
| 52 | + return baseStyle; |
47 | 53 | }); |
48 | 54 |
|
49 | 55 | return ( |
50 | 56 | <div |
51 | 57 | {...others} |
52 | 58 | role="separator" |
53 | | - style={resolveStyles()} |
54 | | - class={cn( |
55 | | - "relative transition-colors duration-200", |
56 | | - // 如果没有颜色且没有文字,给一个默认边框色 |
57 | | - !local.color && !local.children && "border-gray-200", |
58 | | - // 应用预设类名 |
59 | | - presetColorClass(), |
60 | | - |
61 | | - !isVertical() |
62 | | - ? [ |
63 | | - "flex items-center w-full my-6", |
64 | | - // 关键:带文字时父容器不能有 border,否则会穿透文字 |
65 | | - local.children ? "border-none" : "border-t", |
66 | | - ] |
67 | | - : "inline-block mx-2 h-[0.9em] align-middle border-l", |
68 | | - |
69 | | - local.dashed && "border-dashed", |
70 | | - local.class |
71 | | - )} |
| 59 | + style={resolveStyles() as any} |
| 60 | + class={styles().base({ class: local.class })} |
72 | 61 | > |
73 | 62 | <Show when={!isVertical() && local.children}> |
74 | | - <div |
75 | | - class={cn( |
76 | | - "flex items-center w-full text-sm font-medium", |
77 | | - // 必须包含 border-inherit,让 before/after 线条继承父级的预设类名颜色或内联样式颜色 |
78 | | - "before:content-[''] before:border-t before:border-inherit", |
79 | | - "after:content-[''] after:border-t after:border-inherit", |
80 | | - local.dashed && |
81 | | - "before:border-dashed after:border-dashed", |
82 | | - // 位置逻辑 |
83 | | - contentPos() === "center" && |
84 | | - "before:flex-1 after:flex-1", |
85 | | - contentPos() === "left" && "before:w-6 after:flex-1", |
86 | | - contentPos() === "right" && "before:flex-1 after:w-6" |
87 | | - )} |
88 | | - > |
89 | | - <span class="px-4 whitespace-nowrap text-inherit"> |
90 | | - {local.children} |
91 | | - </span> |
| 63 | + <div class={styles().container()}> |
| 64 | + <span class={styles().text()}>{local.children}</span> |
92 | 65 | </div> |
93 | 66 | </Show> |
94 | 67 | </div> |
|
0 commit comments