devnonla-ui 0.11.0 → 0.13.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 +6 -245
- package/package.json +4 -3
- package/src/app/App.tsx +6 -24
- package/src/app/context.ts +3 -7
- package/src/avatar/AgentAvatar.tsx +17 -6
- package/src/button/Button.tsx +28 -21
- package/src/card/Card.tsx +1 -4
- package/src/chat/AgentChatbox.tsx +1 -1
- package/src/chat/AgentPanel.tsx +1 -1
- package/src/chat/chatMessageList.tsx +1 -4
- package/src/chat/message-ui/ChatAgentMessage.tsx +6 -4
- package/src/chat/message-ui/ChatMarkdown.tsx +2 -2
- package/src/chat/tool-ui/CallAgentToolUI.tsx +1 -1
- package/src/codeblock/CodeBlock.tsx +1 -1
- package/src/desktop/DesktopWindow.tsx +41 -17
- package/src/index.ts +7 -5
- package/src/logo/Logo.tsx +17 -197
- package/src/markdown-editor/MarkdownPreview.tsx +3 -3
- package/src/markdown-editor/MarkdownTable.tsx +2 -2
- package/src/markdown-editor/MarkdownViewer.tsx +53 -55
- package/src/markdown-editor/MermaidBlock.tsx +1 -1
- package/src/markdown-editor/ReactCode.tsx +6 -7
- package/src/markdown-editor/ReactCodeFrame.tsx +41 -15
- package/src/markdown-editor/ReactCodeRunner.tsx +9 -0
- package/src/markdown-editor/ReactCodeSandbox.tsx +75 -30
- package/src/markdown-editor/markdownScale.ts +144 -0
- package/src/sidebar/Sidebar.tsx +7 -7
- package/src/styles.css +86 -52
- package/src/tabs/Tabs.tsx +261 -0
- package/src/tag/Tag.tsx +13 -13
- package/src/theme.ts +2 -87
- package/src/typography/Typography.tsx +41 -35
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import { type CSSProperties, type KeyboardEvent, type ReactNode, useCallback, useEffect, useId, useLayoutEffect, useRef, useState } from "react";
|
|
2
|
+
import { SolarIcon } from "../icon/SolarIcon";
|
|
3
|
+
import { solarIconName } from "../icon/solar";
|
|
4
|
+
import { cn } from "../lib/cn";
|
|
5
|
+
|
|
6
|
+
export type TabsItem = {
|
|
7
|
+
key: string;
|
|
8
|
+
label: ReactNode;
|
|
9
|
+
children?: ReactNode;
|
|
10
|
+
disabled?: boolean;
|
|
11
|
+
/** Solar icon name (`settings`), a legacy Fluent Color id (`settings-24`), or a node. */
|
|
12
|
+
icon?: ReactNode;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
export type TabsProps = {
|
|
16
|
+
items: TabsItem[];
|
|
17
|
+
activeKey?: string;
|
|
18
|
+
defaultActiveKey?: string;
|
|
19
|
+
onChange?: (key: string) => void;
|
|
20
|
+
className?: string;
|
|
21
|
+
style?: CSSProperties;
|
|
22
|
+
"aria-label"?: string;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/** Chevron sits in the clear zone. Tabs scroll in past the fade. */
|
|
26
|
+
const FADE = 32;
|
|
27
|
+
const EDGE = 64;
|
|
28
|
+
const TAB_HEIGHT = 40;
|
|
29
|
+
|
|
30
|
+
type Ink = { left: number; width: number };
|
|
31
|
+
type Overflow = { left: boolean; right: boolean };
|
|
32
|
+
|
|
33
|
+
function firstEnabled(items: TabsItem[], preferred?: string) {
|
|
34
|
+
if (preferred && items.some((item) => item.key === preferred)) return preferred;
|
|
35
|
+
return items.find((item) => !item.disabled)?.key ?? items[0]?.key ?? "";
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function fadeOf(overflow: Overflow): "left" | "right" | "both" | "none" {
|
|
39
|
+
if (overflow.left && overflow.right) return "both";
|
|
40
|
+
if (overflow.left) return "left";
|
|
41
|
+
if (overflow.right) return "right";
|
|
42
|
+
return "none";
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function maskImage(fade: ReturnType<typeof fadeOf>) {
|
|
46
|
+
if (fade === "left") return `linear-gradient(to right, transparent ${FADE}px, #000 ${EDGE}px)`;
|
|
47
|
+
if (fade === "right") return `linear-gradient(to left, transparent ${FADE}px, #000 ${EDGE}px)`;
|
|
48
|
+
if (fade === "both") return `linear-gradient(to right, transparent ${FADE}px, #000 ${EDGE}px, #000 calc(100% - ${EDGE}px), transparent calc(100% - ${FADE}px))`;
|
|
49
|
+
return undefined;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
function scrollToX(el: HTMLElement, target: number) {
|
|
53
|
+
const max = Math.max(0, el.scrollWidth - el.clientWidth);
|
|
54
|
+
el.scrollLeft = Math.min(max, Math.max(0, target));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function TabIcon({ icon, size }: { icon?: ReactNode; size: number }) {
|
|
58
|
+
if (icon == null || icon === false) return null;
|
|
59
|
+
if (typeof icon === "string") return <SolarIcon name={solarIconName(icon)} size={size} />;
|
|
60
|
+
return <span className="inline-flex shrink-0 items-center justify-center">{icon}</span>;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function ScrollButton({ dir, onClick }: { dir: "left" | "right"; onClick: () => void }) {
|
|
64
|
+
return (
|
|
65
|
+
<button
|
|
66
|
+
type="button"
|
|
67
|
+
aria-label={dir === "left" ? "Scroll tabs left" : "Scroll tabs right"}
|
|
68
|
+
className={cn(
|
|
69
|
+
"absolute top-0 z-10 flex h-full w-8 cursor-pointer items-center justify-center border-0 bg-transparent text-tertiary-foreground hover:text-foreground",
|
|
70
|
+
dir === "left" ? "left-0" : "right-0",
|
|
71
|
+
)}
|
|
72
|
+
onClick={onClick}
|
|
73
|
+
>
|
|
74
|
+
<SolarIcon name={dir === "left" ? "alt-arrow-left-linear" : "alt-arrow-right-linear"} size={16} />
|
|
75
|
+
</button>
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
export function Tabs({ items, activeKey, defaultActiveKey, onChange, className, style, "aria-label": ariaLabel = "Tabs" }: TabsProps) {
|
|
80
|
+
const uid = useId();
|
|
81
|
+
|
|
82
|
+
const [inner, setInner] = useState(() => firstEnabled(items, defaultActiveKey));
|
|
83
|
+
const active = firstEnabled(items, activeKey ?? inner);
|
|
84
|
+
const itemsKey = items.map((item) => item.key).join("\0");
|
|
85
|
+
|
|
86
|
+
const scrollerRef = useRef<HTMLDivElement>(null);
|
|
87
|
+
const rowRef = useRef<HTMLDivElement>(null);
|
|
88
|
+
const tabRefs = useRef(new Map<string, HTMLButtonElement>());
|
|
89
|
+
const focusKey = useRef<string | null>(null);
|
|
90
|
+
|
|
91
|
+
const [ink, setInk] = useState<Ink | null>(null);
|
|
92
|
+
const [overflow, setOverflow] = useState<Overflow>({ left: false, right: false });
|
|
93
|
+
|
|
94
|
+
const updateOverflow = useCallback(() => {
|
|
95
|
+
const el = scrollerRef.current;
|
|
96
|
+
if (!el) return;
|
|
97
|
+
const max = el.scrollWidth - el.clientWidth;
|
|
98
|
+
const left = el.scrollLeft > 1;
|
|
99
|
+
const right = max > 1 && el.scrollLeft < max - 1;
|
|
100
|
+
setOverflow((prev) => (prev.left === left && prev.right === right ? prev : { left, right }));
|
|
101
|
+
}, []);
|
|
102
|
+
|
|
103
|
+
const reveal = useCallback(() => {
|
|
104
|
+
const scroller = scrollerRef.current;
|
|
105
|
+
const tab = tabRefs.current.get(active);
|
|
106
|
+
if (!scroller || !tab) return;
|
|
107
|
+
const max = scroller.scrollWidth - scroller.clientWidth;
|
|
108
|
+
if (max <= 1) return;
|
|
109
|
+
const left = tab.offsetLeft;
|
|
110
|
+
const right = left + tab.offsetWidth;
|
|
111
|
+
const viewLeft = scroller.scrollLeft + EDGE;
|
|
112
|
+
const viewRight = scroller.scrollLeft + scroller.clientWidth - EDGE;
|
|
113
|
+
if (left < viewLeft) scrollToX(scroller, left - EDGE);
|
|
114
|
+
else if (right > viewRight) scrollToX(scroller, right - scroller.clientWidth + EDGE);
|
|
115
|
+
updateOverflow();
|
|
116
|
+
}, [active, updateOverflow]);
|
|
117
|
+
|
|
118
|
+
useLayoutEffect(() => {
|
|
119
|
+
const row = rowRef.current;
|
|
120
|
+
const measure = () => {
|
|
121
|
+
const el = tabRefs.current.get(active);
|
|
122
|
+
if (!el) {
|
|
123
|
+
setInk(null);
|
|
124
|
+
return;
|
|
125
|
+
}
|
|
126
|
+
const next = { left: el.offsetLeft, width: el.offsetWidth };
|
|
127
|
+
setInk((prev) => (prev && prev.left === next.left && prev.width === next.width ? prev : next));
|
|
128
|
+
updateOverflow();
|
|
129
|
+
};
|
|
130
|
+
measure();
|
|
131
|
+
const ro = new ResizeObserver(measure);
|
|
132
|
+
if (row) ro.observe(row);
|
|
133
|
+
if (scrollerRef.current) ro.observe(scrollerRef.current);
|
|
134
|
+
return () => ro.disconnect();
|
|
135
|
+
}, [active, itemsKey, updateOverflow]);
|
|
136
|
+
|
|
137
|
+
useLayoutEffect(() => {
|
|
138
|
+
reveal();
|
|
139
|
+
}, [active, itemsKey, reveal]);
|
|
140
|
+
|
|
141
|
+
useLayoutEffect(() => {
|
|
142
|
+
const key = focusKey.current;
|
|
143
|
+
if (!key || key !== active) return;
|
|
144
|
+
focusKey.current = null;
|
|
145
|
+
tabRefs.current.get(key)?.focus({ preventScroll: true });
|
|
146
|
+
}, [active]);
|
|
147
|
+
|
|
148
|
+
useEffect(() => {
|
|
149
|
+
const el = scrollerRef.current;
|
|
150
|
+
if (!el) return;
|
|
151
|
+
const onWheel = (event: WheelEvent) => {
|
|
152
|
+
if (el.scrollWidth <= el.clientWidth + 1) return;
|
|
153
|
+
if (Math.abs(event.deltaX) >= Math.abs(event.deltaY)) return;
|
|
154
|
+
if (event.deltaY === 0) return;
|
|
155
|
+
const max = el.scrollWidth - el.clientWidth;
|
|
156
|
+
const next = Math.min(max, Math.max(0, el.scrollLeft + event.deltaY));
|
|
157
|
+
if (next === el.scrollLeft) return;
|
|
158
|
+
el.scrollLeft = next;
|
|
159
|
+
updateOverflow();
|
|
160
|
+
event.preventDefault();
|
|
161
|
+
};
|
|
162
|
+
el.addEventListener("wheel", onWheel, { passive: false });
|
|
163
|
+
return () => el.removeEventListener("wheel", onWheel);
|
|
164
|
+
}, [itemsKey, updateOverflow]);
|
|
165
|
+
|
|
166
|
+
function select(key: string, fromKeyboard = false) {
|
|
167
|
+
if (fromKeyboard) focusKey.current = key;
|
|
168
|
+
if (activeKey === undefined) setInner(key);
|
|
169
|
+
if (key !== active) onChange?.(key);
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
function nudge(dir: -1 | 1) {
|
|
173
|
+
const el = scrollerRef.current;
|
|
174
|
+
if (!el) return;
|
|
175
|
+
const step = dir * Math.max(96, Math.round(el.clientWidth * 0.72));
|
|
176
|
+
scrollToX(el, el.scrollLeft + step);
|
|
177
|
+
updateOverflow();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
function onKeyDown(event: KeyboardEvent<HTMLDivElement>) {
|
|
181
|
+
const enabled = items.filter((item) => !item.disabled);
|
|
182
|
+
if (!enabled.length) return;
|
|
183
|
+
const index = enabled.findIndex((item) => item.key === active);
|
|
184
|
+
let next = -1;
|
|
185
|
+
if (event.key === "ArrowRight") next = index < 0 ? 0 : (index + 1) % enabled.length;
|
|
186
|
+
else if (event.key === "ArrowLeft") next = index < 0 ? enabled.length - 1 : (index - 1 + enabled.length) % enabled.length;
|
|
187
|
+
else if (event.key === "Home") next = 0;
|
|
188
|
+
else if (event.key === "End") next = enabled.length - 1;
|
|
189
|
+
else return;
|
|
190
|
+
event.preventDefault();
|
|
191
|
+
const item = enabled[next];
|
|
192
|
+
if (item) select(item.key, true);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
const fade = fadeOf(overflow);
|
|
196
|
+
const mask = maskImage(fade);
|
|
197
|
+
const panel = items.find((item) => item.key === active);
|
|
198
|
+
const showPanel = items.some((item) => item.children != null);
|
|
199
|
+
|
|
200
|
+
return (
|
|
201
|
+
<div className={cn("w-full min-w-0", className)} style={style}>
|
|
202
|
+
<div className="relative">
|
|
203
|
+
<div aria-hidden className="pointer-events-none absolute inset-x-0 bottom-0 h-px bg-border" />
|
|
204
|
+
<div
|
|
205
|
+
ref={scrollerRef}
|
|
206
|
+
className="nonla-scroll-hidden relative overflow-x-auto overflow-y-hidden overscroll-x-contain"
|
|
207
|
+
style={mask ? { maskImage: mask, WebkitMaskImage: mask } : undefined}
|
|
208
|
+
onScroll={updateOverflow}
|
|
209
|
+
>
|
|
210
|
+
<div ref={rowRef} role="tablist" aria-label={ariaLabel} aria-orientation="horizontal" className="relative flex w-max gap-3" onKeyDown={onKeyDown}>
|
|
211
|
+
{ink ? <span aria-hidden className="nonla-tabs-ink" style={{ width: ink.width, transform: `translateX(${ink.left}px)` }} /> : null}
|
|
212
|
+
{items.map((item) => {
|
|
213
|
+
const selected = item.key === active;
|
|
214
|
+
return (
|
|
215
|
+
<button
|
|
216
|
+
key={item.key}
|
|
217
|
+
ref={(node) => {
|
|
218
|
+
if (node) tabRefs.current.set(item.key, node);
|
|
219
|
+
else tabRefs.current.delete(item.key);
|
|
220
|
+
}}
|
|
221
|
+
type="button"
|
|
222
|
+
role="tab"
|
|
223
|
+
id={`${uid}-tab-${item.key}`}
|
|
224
|
+
aria-selected={selected}
|
|
225
|
+
aria-controls={showPanel ? `${uid}-panel` : undefined}
|
|
226
|
+
tabIndex={selected ? 0 : -1}
|
|
227
|
+
disabled={item.disabled}
|
|
228
|
+
className={cn(
|
|
229
|
+
"relative z-10 inline-flex shrink-0 cursor-pointer items-center justify-center gap-1.5 whitespace-nowrap border-0 bg-transparent font-normal text-tertiary-foreground transition-colors select-none",
|
|
230
|
+
"hover:text-foreground focus-visible:ring-ring focus-visible:ring-2 focus-visible:outline-none focus-visible:ring-inset",
|
|
231
|
+
"disabled:cursor-not-allowed disabled:opacity-40",
|
|
232
|
+
selected && "text-foreground",
|
|
233
|
+
)}
|
|
234
|
+
style={{
|
|
235
|
+
height: TAB_HEIGHT,
|
|
236
|
+
paddingLeft: 4,
|
|
237
|
+
paddingRight: 4,
|
|
238
|
+
fontSize: 14,
|
|
239
|
+
}}
|
|
240
|
+
onClick={() => {
|
|
241
|
+
if (!item.disabled) select(item.key);
|
|
242
|
+
}}
|
|
243
|
+
>
|
|
244
|
+
<TabIcon icon={item.icon} size={16} />
|
|
245
|
+
{item.label}
|
|
246
|
+
</button>
|
|
247
|
+
);
|
|
248
|
+
})}
|
|
249
|
+
</div>
|
|
250
|
+
</div>
|
|
251
|
+
{overflow.left ? <ScrollButton dir="left" onClick={() => nudge(-1)} /> : null}
|
|
252
|
+
{overflow.right ? <ScrollButton dir="right" onClick={() => nudge(1)} /> : null}
|
|
253
|
+
</div>
|
|
254
|
+
{showPanel ? (
|
|
255
|
+
<div role="tabpanel" id={`${uid}-panel`} aria-labelledby={`${uid}-tab-${active}`} className="pt-3">
|
|
256
|
+
{panel?.children}
|
|
257
|
+
</div>
|
|
258
|
+
) : null}
|
|
259
|
+
</div>
|
|
260
|
+
);
|
|
261
|
+
}
|
package/src/tag/Tag.tsx
CHANGED
|
@@ -27,19 +27,19 @@ const NAMED_VAR: Record<string, string> = {
|
|
|
27
27
|
warning: "var(--warn)",
|
|
28
28
|
processing: "var(--link)",
|
|
29
29
|
brand: "var(--brand)",
|
|
30
|
-
blue: "
|
|
31
|
-
purple: "
|
|
32
|
-
cyan: "
|
|
33
|
-
green: "
|
|
34
|
-
magenta: "
|
|
35
|
-
pink: "
|
|
36
|
-
orange: "
|
|
37
|
-
gold: "
|
|
38
|
-
red: "
|
|
39
|
-
yellow: "
|
|
40
|
-
volcano: "
|
|
41
|
-
geekblue: "
|
|
42
|
-
lime: "
|
|
30
|
+
blue: "light-dark(#3b7fc4, #7bafe9)",
|
|
31
|
+
purple: "light-dark(#6d5bd0, #9386f2)",
|
|
32
|
+
cyan: "light-dark(#3d6a8c, #81a1c1)",
|
|
33
|
+
green: "light-dark(#2f7d4a, #3fa266)",
|
|
34
|
+
magenta: "light-dark(#9a5f8a, #b48ead)",
|
|
35
|
+
pink: "light-dark(#9a5f8a, #b48ead)",
|
|
36
|
+
orange: "light-dark(#c2613f, #d08770)",
|
|
37
|
+
gold: "light-dark(#a16207, #f1b467)",
|
|
38
|
+
red: "light-dark(#e23b57, #fc6b83)",
|
|
39
|
+
yellow: "light-dark(#a16207, #f1b467)",
|
|
40
|
+
volcano: "light-dark(#e23b57, #fc6b83)",
|
|
41
|
+
geekblue: "light-dark(#3b7fc4, #7bafe9)",
|
|
42
|
+
lime: "light-dark(#3d8f5c, #70b489)",
|
|
43
43
|
};
|
|
44
44
|
|
|
45
45
|
const SOFT = "bg-[color-mix(in_oklab,var(--nonla-tag)_12%,transparent)] text-[color-mix(in_oklab,var(--nonla-tag)_72%,black)]";
|
package/src/theme.ts
CHANGED
|
@@ -2,10 +2,8 @@
|
|
|
2
2
|
* Theme knobs for NonlaUI.
|
|
3
3
|
*
|
|
4
4
|
* Defaults live in `styles.css` on `:root` / `.nonla-ui`.
|
|
5
|
+
* Consumers retheme by overriding `--nonla-*` in CSS. Aliases follow those knobs.
|
|
5
6
|
* `--brand-50`…`--brand-800` follow `--nonla-brand` (500 = the knob).
|
|
6
|
-
* Consumers retheme without touching components:
|
|
7
|
-
* 1. CSS: `:root { --nonla-brand: #3b82f6; }`
|
|
8
|
-
* 2. JS: `<App theme={{ colors: { brand: "#3b82f6" } }} />` or `applyNonlaTheme({ brand: "#3b82f6" })`
|
|
9
7
|
* Color mode is `light` | `dark` on `<html>` (`setColorMode`, `<ThemeToggle>`). Dark is the default.
|
|
10
8
|
* `<ThemeSwitcher>` also stores `system`, which follows the OS and still paints `light` or `dark`.
|
|
11
9
|
*/
|
|
@@ -52,83 +50,13 @@ export const NONLA_THEME_KEYS = {
|
|
|
52
50
|
zWindow: "--nonla-z-window",
|
|
53
51
|
zHeader: "--nonla-z-header",
|
|
54
52
|
zPopupBase: "--nonla-z-popup-base",
|
|
55
|
-
blue: "--nonla-blue",
|
|
56
|
-
purple: "--nonla-purple",
|
|
57
|
-
cyan: "--nonla-cyan",
|
|
58
|
-
green: "--nonla-green",
|
|
59
|
-
magenta: "--nonla-magenta",
|
|
60
|
-
pink: "--nonla-pink",
|
|
61
|
-
red: "--nonla-red",
|
|
62
|
-
orange: "--nonla-orange",
|
|
63
|
-
yellow: "--nonla-yellow",
|
|
64
|
-
volcano: "--nonla-volcano",
|
|
65
|
-
geekblue: "--nonla-geekblue",
|
|
66
|
-
lime: "--nonla-lime",
|
|
67
|
-
gold: "--nonla-gold",
|
|
68
53
|
} as const;
|
|
69
54
|
|
|
70
55
|
export type NonlaThemeKnobName = keyof typeof NONLA_THEME_KEYS;
|
|
71
56
|
export type NonlaThemeKnob = (typeof NONLA_THEME_KEYS)[NonlaThemeKnobName];
|
|
72
57
|
|
|
73
|
-
const METRIC_KEYS = new Set<NonlaThemeKnobName>(["radius", "height", "heightSm", "heightLg", "desktopBarHeight", "zBase", "zDesktop", "zWindow", "zHeader", "zPopupBase"]);
|
|
74
|
-
|
|
75
|
-
export type NonlaThemeColorName = Exclude<
|
|
76
|
-
NonlaThemeKnobName,
|
|
77
|
-
"radius" | "height" | "heightSm" | "heightLg" | "desktopBarHeight" | "zBase" | "zDesktop" | "zWindow" | "zHeader" | "zPopupBase"
|
|
78
|
-
>;
|
|
79
|
-
export type NonlaThemeColors = { [K in NonlaThemeColorName]?: string };
|
|
80
|
-
|
|
81
|
-
/** Flat aliases → knob name (`colorBorder` == `colors.border`). */
|
|
82
|
-
const COLOR_ALIASES = {
|
|
83
|
-
colorBorder: "border",
|
|
84
|
-
colorBorderInput: "borderInput",
|
|
85
|
-
/** @deprecated use `borderInput` / `colorBorderInput` */
|
|
86
|
-
input: "borderInput",
|
|
87
|
-
} as const satisfies Record<string, NonlaThemeColorName>;
|
|
88
|
-
|
|
89
58
|
export const NONLA_THEME_KNOBS = Object.values(NONLA_THEME_KEYS);
|
|
90
59
|
|
|
91
|
-
export type NonlaThemeConfig = {
|
|
92
|
-
[K in NonlaThemeKnobName]?: string;
|
|
93
|
-
} & {
|
|
94
|
-
/** Grouped color knobs — same names as the flat keys (`colors.border`, `colors.borderInput`, …). */
|
|
95
|
-
colors?: NonlaThemeColors;
|
|
96
|
-
/** Alias of `colors.border` / `border`. */
|
|
97
|
-
colorBorder?: string;
|
|
98
|
-
/** Alias of `colors.borderInput` / `borderInput`. */
|
|
99
|
-
colorBorderInput?: string;
|
|
100
|
-
/** Extra CSS custom properties (include the leading `--`). */
|
|
101
|
-
vars?: Record<string, string>;
|
|
102
|
-
};
|
|
103
|
-
|
|
104
|
-
function entriesOf(theme: NonlaThemeConfig): [string, string][] {
|
|
105
|
-
const map = new Map<string, string>();
|
|
106
|
-
const set = (key: string, value: unknown) => {
|
|
107
|
-
if (typeof value !== "string" || !value) return;
|
|
108
|
-
const knob = (COLOR_ALIASES as Record<string, NonlaThemeKnobName>)[key] ?? (key as NonlaThemeKnobName);
|
|
109
|
-
const prop = NONLA_THEME_KEYS[knob];
|
|
110
|
-
if (prop) map.set(prop, value);
|
|
111
|
-
};
|
|
112
|
-
|
|
113
|
-
if (theme.colors) {
|
|
114
|
-
for (const [key, value] of Object.entries(theme.colors)) {
|
|
115
|
-
if (METRIC_KEYS.has(key as NonlaThemeKnobName)) continue;
|
|
116
|
-
set(key, value);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
set("colorBorder", theme.colorBorder);
|
|
120
|
-
set("colorBorderInput", theme.colorBorderInput);
|
|
121
|
-
for (const key of Object.keys(NONLA_THEME_KEYS) as NonlaThemeKnobName[]) {
|
|
122
|
-
set(key, theme[key]);
|
|
123
|
-
}
|
|
124
|
-
if (theme.vars) {
|
|
125
|
-
for (const [prop, value] of Object.entries(theme.vars)) {
|
|
126
|
-
if (value) map.set(prop, value);
|
|
127
|
-
}
|
|
128
|
-
}
|
|
129
|
-
return [...map.entries()];
|
|
130
|
-
}
|
|
131
|
-
|
|
132
60
|
export type NonlaTokenSnapshot = { [K in NonlaThemeKnobName]: string };
|
|
133
61
|
|
|
134
62
|
/** Read computed `--nonla-*` knobs (`getDesignToken`). */
|
|
@@ -141,19 +69,6 @@ export function getDesignToken(target?: HTMLElement): NonlaTokenSnapshot {
|
|
|
141
69
|
return token;
|
|
142
70
|
}
|
|
143
71
|
|
|
144
|
-
/** Apply knobs on an element (defaults to `:root`). Returns a restore function. */
|
|
145
|
-
export function applyNonlaTheme(theme: NonlaThemeConfig, target: HTMLElement = document.documentElement): () => void {
|
|
146
|
-
const entries = entriesOf(theme);
|
|
147
|
-
const prev = entries.map(([prop]) => [prop, target.style.getPropertyValue(prop)] as const);
|
|
148
|
-
for (const [prop, value] of entries) target.style.setProperty(prop, value);
|
|
149
|
-
return () => {
|
|
150
|
-
for (const [prop, value] of prev) {
|
|
151
|
-
if (value) target.style.setProperty(prop, value);
|
|
152
|
-
else target.style.removeProperty(prop);
|
|
153
|
-
}
|
|
154
|
-
};
|
|
155
|
-
}
|
|
156
|
-
|
|
157
72
|
export type NonlaColorMode = "light" | "dark";
|
|
158
73
|
|
|
159
74
|
/** Stored choice. `system` follows the OS and still applies `light` or `dark` on `<html>`. */
|
|
@@ -206,8 +121,8 @@ function isColorPreference(value: string | null): value is NonlaColorPreference
|
|
|
206
121
|
}
|
|
207
122
|
|
|
208
123
|
function readStoredPreference(): NonlaColorPreference | null {
|
|
209
|
-
if (typeof localStorage === "undefined") return null;
|
|
210
124
|
try {
|
|
125
|
+
// Sandboxed iframes throw on the property access itself, before getItem.
|
|
211
126
|
const value = localStorage.getItem(NONLA_COLOR_MODE_KEY);
|
|
212
127
|
return isColorPreference(value) ? value : null;
|
|
213
128
|
} catch {
|
|
@@ -1,9 +1,10 @@
|
|
|
1
|
-
import { type AnchorHTMLAttributes, createElement, type HTMLAttributes, type ReactNode } from "react";
|
|
1
|
+
import { type AnchorHTMLAttributes, type CSSProperties, createElement, type HTMLAttributes, type ReactNode } from "react";
|
|
2
2
|
import { cn } from "../lib/cn";
|
|
3
|
+
import { headingClass, markdownBodyClass, markdownParagraphClass, markdownVariantStyle, pageTitleClass } from "../markdown-editor/markdownScale";
|
|
3
4
|
|
|
4
5
|
export type TypographyType = "secondary" | "success" | "warning" | "danger";
|
|
5
6
|
|
|
6
|
-
export type TitleLevel = 1 | 2 | 3 | 4 | 5;
|
|
7
|
+
export type TitleLevel = 1 | 2 | 3 | 4 | 5 | 6;
|
|
7
8
|
|
|
8
9
|
type Decorations = {
|
|
9
10
|
type?: TypographyType;
|
|
@@ -17,12 +18,18 @@ type Decorations = {
|
|
|
17
18
|
italic?: boolean;
|
|
18
19
|
};
|
|
19
20
|
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
/** Same ink as MarkdownViewer. Status tones sit on top of that. */
|
|
22
|
+
const INK = {
|
|
23
|
+
title: "var(--md-heading)",
|
|
24
|
+
body: "var(--md-ink)",
|
|
25
|
+
secondary: "var(--nonla-fg-tertiary)",
|
|
26
|
+
quiet: "var(--nonla-fg-quaternary)",
|
|
27
|
+
success: "var(--nonla-success)",
|
|
28
|
+
warning: "var(--nonla-warn)",
|
|
29
|
+
danger: "var(--nonla-danger)",
|
|
30
|
+
} as const;
|
|
31
|
+
|
|
32
|
+
type InkRole = "title" | "body";
|
|
26
33
|
|
|
27
34
|
const TITLE_TAG = {
|
|
28
35
|
1: "h1",
|
|
@@ -30,20 +37,15 @@ const TITLE_TAG = {
|
|
|
30
37
|
3: "h3",
|
|
31
38
|
4: "h4",
|
|
32
39
|
5: "h5",
|
|
40
|
+
6: "h6",
|
|
33
41
|
} as const;
|
|
34
42
|
|
|
35
|
-
/**
|
|
36
|
-
const BODY =
|
|
43
|
+
/** Same scale as MarkdownViewer `docs`. Body 16/24. Inline code reads `--md-inline-size` (14px). */
|
|
44
|
+
const BODY = markdownBodyClass;
|
|
37
45
|
|
|
38
|
-
const
|
|
39
|
-
1: "text-[38px] leading-[46px] font-bold",
|
|
40
|
-
2: "text-[30px] leading-[38px] font-bold",
|
|
41
|
-
3: "text-[24px] leading-[32px] font-semibold",
|
|
42
|
-
4: "text-[20px] leading-[28px] font-semibold",
|
|
43
|
-
5: "text-[16px] leading-[24px] font-semibold",
|
|
44
|
-
};
|
|
46
|
+
const INLINE_CODE = "nonla-inline-code";
|
|
45
47
|
|
|
46
|
-
const TITLE_SPACE = "nonla-typo
|
|
48
|
+
const TITLE_SPACE = "nonla-typo";
|
|
47
49
|
|
|
48
50
|
function decorationLine(underline?: boolean, deleted?: boolean) {
|
|
49
51
|
if (underline && deleted) return "underline line-through";
|
|
@@ -55,10 +57,10 @@ function decorationLine(underline?: boolean, deleted?: boolean) {
|
|
|
55
57
|
function wrapDecorations(children: ReactNode, props: Decorations) {
|
|
56
58
|
let node = children;
|
|
57
59
|
if (props.mark) {
|
|
58
|
-
node = <mark className="rounded-sm bg-[color-mix(in_oklab,var(--nonla-
|
|
60
|
+
node = <mark className="rounded-sm bg-[color-mix(in_oklab,var(--nonla-warn)_40%,transparent)] px-0.5 text-inherit">{node}</mark>;
|
|
59
61
|
}
|
|
60
62
|
if (props.code) {
|
|
61
|
-
node = <code className=
|
|
63
|
+
node = <code className={INLINE_CODE}>{node}</code>;
|
|
62
64
|
}
|
|
63
65
|
if (props.keyboard) {
|
|
64
66
|
node = <kbd className="inline-block rounded-md border border-b-2 border-border bg-muted px-1.5 font-mono text-[0.85em] leading-5">{node}</kbd>;
|
|
@@ -66,16 +68,24 @@ function wrapDecorations(children: ReactNode, props: Decorations) {
|
|
|
66
68
|
return node;
|
|
67
69
|
}
|
|
68
70
|
|
|
69
|
-
function
|
|
70
|
-
if (disabled) return
|
|
71
|
-
|
|
71
|
+
function rankColor(role: InkRole, props: Decorations): string | undefined {
|
|
72
|
+
if (props.disabled) return INK.quiet;
|
|
73
|
+
if (props.type === "secondary") return INK.secondary;
|
|
74
|
+
if (props.type === "success") return INK.success;
|
|
75
|
+
if (props.type === "warning") return INK.warning;
|
|
76
|
+
if (props.type === "danger") return INK.danger;
|
|
77
|
+
if (role === "title") return INK.title;
|
|
78
|
+
if (role === "body") return INK.body;
|
|
79
|
+
return undefined;
|
|
72
80
|
}
|
|
73
81
|
|
|
74
|
-
function decorated(props: Decorations, children: ReactNode, base: string, className?: string, style?: HTMLAttributes<HTMLElement>["style"]) {
|
|
82
|
+
function decorated(props: Decorations, children: ReactNode, base: string, className?: string, style?: HTMLAttributes<HTMLElement>["style"], role: InkRole = "body") {
|
|
75
83
|
const line = decorationLine(props.underline, props.delete);
|
|
84
|
+
const color = rankColor(role, props);
|
|
85
|
+
const scale: CSSProperties = { ...markdownVariantStyle("docs"), ...(color ? { color } : null), ...(line ? { textDecorationLine: line } : null), ...style };
|
|
76
86
|
return {
|
|
77
|
-
className: cn(base,
|
|
78
|
-
style:
|
|
87
|
+
className: cn(base, props.disabled && "cursor-not-allowed", props.strong && "font-(--md-strong-weight,600)", props.italic && "italic", props.underline && "underline-offset-[3px]", className),
|
|
88
|
+
style: scale,
|
|
79
89
|
children: wrapDecorations(children, props),
|
|
80
90
|
"aria-disabled": props.disabled || undefined,
|
|
81
91
|
};
|
|
@@ -99,7 +109,7 @@ export type TypographyTextProps = Omit<HTMLAttributes<HTMLSpanElement>, "color">
|
|
|
99
109
|
|
|
100
110
|
export function Text({ className, style, children, ...props }: TypographyTextProps) {
|
|
101
111
|
const { decorations, rest } = splitDecorations(props);
|
|
102
|
-
return <span {...rest} {...decorated(decorations, children,
|
|
112
|
+
return <span {...rest} {...decorated(decorations, children, BODY, className, style, "body")} />;
|
|
103
113
|
}
|
|
104
114
|
|
|
105
115
|
export type TypographyTitleProps = Omit<HTMLAttributes<HTMLHeadingElement>, "color"> & Decorations & {
|
|
@@ -109,14 +119,14 @@ export type TypographyTitleProps = Omit<HTMLAttributes<HTMLHeadingElement>, "col
|
|
|
109
119
|
|
|
110
120
|
export function Title({ level = 1, className, style, children, ...props }: TypographyTitleProps) {
|
|
111
121
|
const { decorations, rest } = splitDecorations(props);
|
|
112
|
-
return createElement(TITLE_TAG[level], { ...rest, ...decorated(decorations, children, cn(TITLE_SPACE,
|
|
122
|
+
return createElement(TITLE_TAG[level], { ...rest, ...decorated(decorations, children, cn(TITLE_SPACE, level === 1 ? pageTitleClass : headingClass(level)), className, style, "title") });
|
|
113
123
|
}
|
|
114
124
|
|
|
115
125
|
export type TypographyParagraphProps = Omit<HTMLAttributes<HTMLParagraphElement>, "color"> & Decorations & { children?: ReactNode };
|
|
116
126
|
|
|
117
127
|
export function Paragraph({ className, style, children, ...props }: TypographyParagraphProps) {
|
|
118
128
|
const { decorations, rest } = splitDecorations(props);
|
|
119
|
-
return <p {...rest} {...decorated(decorations, children,
|
|
129
|
+
return <p {...rest} {...decorated(decorations, children, markdownParagraphClass, className, style, "body")} />;
|
|
120
130
|
}
|
|
121
131
|
|
|
122
132
|
export type TypographyLinkProps = Omit<AnchorHTMLAttributes<HTMLAnchorElement>, "color"> & Decorations & { children?: ReactNode };
|
|
@@ -126,14 +136,10 @@ export function Link({ href, target, rel, className, style, children, onClick, .
|
|
|
126
136
|
const view = decorated(
|
|
127
137
|
decorations,
|
|
128
138
|
children,
|
|
129
|
-
cn(
|
|
130
|
-
"cursor-pointer underline-offset-[3px]",
|
|
131
|
-
BODY,
|
|
132
|
-
decorations.type || decorations.disabled ? undefined : "text-brand hover:text-brand-700",
|
|
133
|
-
decorations.disabled || decorations.underline ? undefined : "hover:underline",
|
|
134
|
-
),
|
|
139
|
+
cn("cursor-pointer underline-offset-[3px]", BODY, decorations.disabled || decorations.underline ? undefined : "hover:underline"),
|
|
135
140
|
className,
|
|
136
141
|
style,
|
|
142
|
+
"body",
|
|
137
143
|
);
|
|
138
144
|
|
|
139
145
|
return (
|