devnonla-ui 0.16.1 → 0.17.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 CHANGED
@@ -23,4 +23,11 @@ export function Root() {
23
23
  }
24
24
  ```
25
25
 
26
+ Chat and markdown are separate entries. Theme tokens live on the `.nonla-ui` host that `App` renders, not on the rest of the page.
27
+
28
+ ```tsx
29
+ import { AgentPanel } from "devnonla-ui/chat";
30
+ import { MarkdownViewer } from "devnonla-ui/markdown";
31
+ ```
32
+
26
33
  Install, theme, and each control are in the [docs](https://devnonla.github.io/devnonla-ui/).
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "devnonla-ui",
3
- "version": "0.16.1",
3
+ "version": "0.17.0",
4
4
  "type": "module",
5
5
  "description": "NonlaUI — React controls for Nonla Agents",
6
6
  "author": "devlangla <devlangla0x01@gmail.com>",
@@ -21,6 +21,8 @@
21
21
  ],
22
22
  "exports": {
23
23
  ".": "./src/index.ts",
24
+ "./markdown": "./src/markdown-editor/index.ts",
25
+ "./chat": "./src/chat/index.ts",
24
26
  "./styles.css": "./src/styles.css"
25
27
  },
26
28
  "main": "./src/index.ts",
package/src/app/App.tsx CHANGED
@@ -1,9 +1,10 @@
1
1
  import * as TooltipPrimitive from "@radix-ui/react-tooltip";
2
- import { type ReactNode, useMemo } from "react";
2
+ import { type ReactNode, useLayoutEffect, useMemo, useRef } from "react";
3
+ import { cn } from "../lib/cn";
3
4
  import { type ControlSize, ControlSizeContext, normalizeSize } from "../lib/sizes";
4
5
  import { MessageHolder, message } from "../message/message";
5
6
  import { ConfirmHolder, Modal } from "../modal/Modal";
6
- import { initColorMode } from "../theme";
7
+ import { bindColorModeTarget, initColorMode } from "../theme";
7
8
  import { AppContext, type NonlaAppConfig, useAppConfig, usePopupContainer } from "./context";
8
9
 
9
10
  export type { NonlaAppConfig };
@@ -14,25 +15,42 @@ export type AppProps = {
14
15
  getPopupContainer?: () => HTMLElement;
15
16
  /** Default control size (`componentSize`). Per-control `size` still wins. */
16
17
  componentSize?: ControlSize;
18
+ /** Extra classes on the `.nonla-ui` host. */
19
+ className?: string;
17
20
  };
18
21
 
19
- /** Root host for NonlaUI — ConfigProvider + message/modal holders. Mount once near app root. */
20
- export function App({ children, getPopupContainer, componentSize }: AppProps) {
21
- // Before children paint, so a sandbox iframe can open on the saved mode instead of flashing the default.
22
- initColorMode();
22
+ /** Root host for NonlaUI — theme scope + message/modal holders. Mount once near app root. */
23
+ export function App({ children, getPopupContainer, componentSize, className }: AppProps) {
24
+ const hostRef = useRef<HTMLDivElement>(null);
23
25
  const size = componentSize ? normalizeSize(componentSize) : undefined;
24
- const value = useMemo<NonlaAppConfig>(() => ({ getPopupContainer, componentSize: size }), [getPopupContainer, size]);
26
+ const value = useMemo<NonlaAppConfig>(
27
+ () => ({
28
+ getPopupContainer: getPopupContainer ?? (() => hostRef.current ?? document.body),
29
+ componentSize: size,
30
+ }),
31
+ [getPopupContainer, size],
32
+ );
33
+
34
+ useLayoutEffect(() => {
35
+ const host = hostRef.current;
36
+ if (!host) return;
37
+ bindColorModeTarget(host);
38
+ initColorMode(host);
39
+ return () => bindColorModeTarget(null);
40
+ }, []);
25
41
 
26
42
  return (
27
- <AppContext.Provider value={value}>
28
- <ControlSizeContext.Provider value={size}>
29
- <TooltipPrimitive.Provider delayDuration={100}>
30
- {children}
31
- <MessageHolder />
32
- <ConfirmHolder />
33
- </TooltipPrimitive.Provider>
34
- </ControlSizeContext.Provider>
35
- </AppContext.Provider>
43
+ <div ref={hostRef} className={cn("nonla-ui h-full min-h-0", className)}>
44
+ <AppContext.Provider value={value}>
45
+ <ControlSizeContext.Provider value={size}>
46
+ <TooltipPrimitive.Provider delayDuration={100}>
47
+ {children}
48
+ <MessageHolder />
49
+ <ConfirmHolder />
50
+ </TooltipPrimitive.Provider>
51
+ </ControlSizeContext.Provider>
52
+ </AppContext.Provider>
53
+ </div>
36
54
  );
37
55
  }
38
56
 
@@ -12,7 +12,7 @@ export function useAppConfig() {
12
12
  return useContext(AppContext);
13
13
  }
14
14
 
15
- /** Portal container for overlays (`getPopupContainer`). Always leaves the desktop stacking context. */
15
+ /** Portal container for overlays (`getPopupContainer`). Defaults to the `App` `.nonla-ui` host, then `document.body`. */
16
16
  export function usePopupContainer(override?: () => HTMLElement) {
17
17
  const { getPopupContainer } = useAppConfig();
18
18
  return () => (override ?? getPopupContainer)?.() ?? document.body;
package/src/index.ts CHANGED
@@ -23,71 +23,6 @@ export type { CalendarProps } from "./calendar/Calendar";
23
23
  export { Calendar } from "./calendar/Calendar";
24
24
  export type { CardFooterProps, CardItemProps, CardProps } from "./card/Card";
25
25
  export { Card } from "./card/Card";
26
- export type {
27
- AgentChatboxProps,
28
- AgentHistoryMessage,
29
- AgentMessage,
30
- AgentMessageRole,
31
- AgentPanelEndpoint,
32
- AgentPanelProps,
33
- AgentSseCallbacks,
34
- AgentStreamRequest,
35
- AgentToolCallEvent,
36
- AgentToolHook,
37
- AgentToolNameMatch,
38
- AgentToolResultEvent,
39
- AgentToolUI,
40
- AgentToolUIName,
41
- BackgroundTasksBarProps,
42
- ChatAgentMessageProps,
43
- ChatBgTask,
44
- ChatErrorProps,
45
- ChatInputProps,
46
- ChatMarkdownProps,
47
- ChatThinkingProps,
48
- ChatToolCallProps,
49
- ChatToolMessage,
50
- ChatUserMessageProps,
51
- ChatWelcomeProps,
52
- ToolUIProps,
53
- } from "./chat";
54
- export {
55
- AgentChatbox,
56
- AgentPanel,
57
- BackgroundTasksBar,
58
- BackgroundTaskToolUI,
59
- buildAgentHistory,
60
- builtinToolUis,
61
- CallAgentToolUI,
62
- ChatAgentMessage,
63
- ChatError,
64
- ChatInput,
65
- ChatMarkdown,
66
- ChatSpinner,
67
- ChatThinking,
68
- ChatToolCall,
69
- ChatUserMessage,
70
- ChatWelcome,
71
- formatBgElapsed,
72
- formatToolName,
73
- GetCurrentTimeToolUI,
74
- isCallAgentToolName,
75
- isToolRunning,
76
- matchesToolHook,
77
- matchesToolName,
78
- matchesToolUIName,
79
- parseBgTaskRef,
80
- parseCallAgentToolTargetId,
81
- parseSseStream,
82
- prettyJson,
83
- ReadSkillToolUI,
84
- RunJsToolUI,
85
- resolveToolUI,
86
- ToolUiTrailing,
87
- useAgentChatStream,
88
- useAgentStream,
89
- WebFetchToolUI,
90
- } from "./chat";
91
26
  export type { CheckboxColor, CheckboxProps } from "./checkbox/Checkbox";
92
27
  export { Checkbox } from "./checkbox/Checkbox";
93
28
  export type { CodeBlockCopyButtonProps, CodeBlockProps } from "./codeblock/CodeBlock";
@@ -96,15 +31,6 @@ export type { ColorFormat, ColorPickerProps, ColorPickerSemanticSlot, ColorType,
96
31
  export { Color, ColorPicker } from "./colorpicker/ColorPicker";
97
32
  export type { DatePickerProps, RangePickerProps, RangeValue } from "./datepicker/DatePicker";
98
33
  export { DatePicker, RangePicker } from "./datepicker/DatePicker";
99
- export type { DesktopHeaderProps } from "./desktop/DesktopHeader";
100
- export { DesktopBarButton, DesktopBarDivider, DesktopBarItem, DesktopHeader } from "./desktop/DesktopHeader";
101
- export { DesktopIcon } from "./desktop/DesktopIcon";
102
- export { DesktopStage } from "./desktop/DesktopStage";
103
- export type { DesktopWindowProps, WindowHeaderProps } from "./desktop/DesktopWindow";
104
- export { DesktopWindow, WindowHeader } from "./desktop/DesktopWindow";
105
- export { MeadowDesktop } from "./desktop/MeadowDesktop";
106
- export { MeadowShell } from "./desktop/MeadowShell";
107
- export { MeadowWallpaper } from "./desktop/MeadowWallpaper";
108
34
  export type { DrawerPlacement, DrawerProps } from "./drawer/Drawer";
109
35
  export { Drawer } from "./drawer/Drawer";
110
36
  export type { ContextMenuProps } from "./dropdown/ContextMenu";
@@ -147,19 +73,6 @@ export { CONTROL_SIZES, controlHeightVar, controlRadiusVar, controlStatusClass,
147
73
  export { glassOverlayClass, glassSurfaceClass, meadowSurfaceClass } from "./lib/surface";
148
74
  export type { LogoProps, LogoVariant } from "./logo/Logo";
149
75
  export { Logo } from "./logo/Logo";
150
- export type { MarkdownDiffLayout, MarkdownEditorMode, MarkdownEditorProps } from "./markdown-editor/MarkdownEditor";
151
- export { MarkdownEditor } from "./markdown-editor/MarkdownEditor";
152
- export type { MarkdownViewerProps } from "./markdown-editor/MarkdownViewer";
153
- export { MarkdownViewer } from "./markdown-editor/MarkdownViewer";
154
- export type { MermaidBlockProps } from "./markdown-editor/MermaidBlock";
155
- export { MermaidBlock } from "./markdown-editor/MermaidBlock";
156
- export type { ReactCodeProps } from "./markdown-editor/ReactCode";
157
- export { ReactCode } from "./markdown-editor/ReactCode";
158
- export type { ReactCodeFrameProps } from "./markdown-editor/ReactCodeFrame";
159
- export { ReactCodeFrame } from "./markdown-editor/ReactCodeFrame";
160
- export { mountReactCodeRunner } from "./markdown-editor/ReactCodeRunner";
161
- export type { ReactCodeSandboxProps } from "./markdown-editor/ReactCodeSandbox";
162
- export { ReactCodeSandbox } from "./markdown-editor/ReactCodeSandbox";
163
76
  export type { MenuItemProps, MenuRootProps, MenuTriggerProps } from "./menu/Menu";
164
77
  export { Menu, MenuAction, MenuDivider, MenuItem, MenuTrigger } from "./menu/Menu";
165
78
  export type { MessageConfig, MessageType } from "./message/message";
@@ -4,10 +4,10 @@ import remarkGfm from "remark-gfm";
4
4
  import { Checkbox } from "../checkbox/Checkbox";
5
5
  import { CodeBlock } from "../codeblock/CodeBlock";
6
6
  import { cn } from "../lib/cn";
7
+ import { headingClass, markdownScaleStyle } from "../lib/markdownScale";
7
8
  import { type InlineEdit, type MarkdownBlock, parseMarkdownDoc, readFence, readInlineEdit } from "./blocks";
8
9
  import { MarkdownTable } from "./MarkdownTable";
9
10
  import { MermaidBlock } from "./MermaidBlock";
10
- import { headingClass, markdownScaleStyle } from "./markdownScale";
11
11
  import { ReactCode } from "./ReactCode";
12
12
  import { ReactCodeSandbox } from "./ReactCodeSandbox";
13
13
 
@@ -1,7 +1,7 @@
1
1
  import { Component, type ReactNode } from "react";
2
2
  import { flushSync } from "react-dom";
3
3
  import { createRoot } from "react-dom/client";
4
- import { applyColorMode } from "../theme";
4
+ import { applyColorMode, bindColorModeTarget } from "../theme";
5
5
  import { REACT_CODE_SOURCE, type RunnerPayload, readHostMessage } from "./reactCodeProtocol";
6
6
  import { compileReactCode } from "./reactCompile";
7
7
 
@@ -32,6 +32,13 @@ class Boundary extends Component<{ report: Report; children: ReactNode }, { erro
32
32
  * `Access-Control-Allow-Origin: *` on its assets, since a sandboxed frame loads them cross-origin.
33
33
  */
34
34
  export function mountReactCodeRunner(root: HTMLElement, modules: Record<string, unknown>): () => void {
35
+ root.classList.add("nonla-ui");
36
+ bindColorModeTarget(root);
37
+ const pending = document.documentElement.dataset.pendingMode;
38
+ if (pending === "light" || pending === "dark") {
39
+ applyColorMode(pending, root);
40
+ delete document.documentElement.dataset.pendingMode;
41
+ }
35
42
  const reactRoot = createRoot(root);
36
43
  const report: Report = (message) => window.parent.postMessage({ source: REACT_CODE_SOURCE, ...message }, "*");
37
44
 
@@ -40,7 +47,7 @@ export function mountReactCodeRunner(root: HTMLElement, modules: Record<string,
40
47
  if (event.source !== window.parent) return;
41
48
  const msg = readHostMessage(event.data);
42
49
  if (!msg) return;
43
- applyColorMode(msg.mode);
50
+ applyColorMode(msg.mode, root);
44
51
  if (msg.type === "mode") return;
45
52
  waitingForRun = false;
46
53
  try {
@@ -77,6 +84,7 @@ export function mountReactCodeRunner(root: HTMLElement, modules: Record<string,
77
84
  window.clearInterval(readyBeat);
78
85
  window.removeEventListener("message", onMessage);
79
86
  observer.disconnect();
87
+ bindColorModeTarget(null);
80
88
  reactRoot.unmount();
81
89
  };
82
90
  }
@@ -0,0 +1,13 @@
1
+ export type { MarkdownDiffLayout, MarkdownEditorMode, MarkdownEditorProps } from "./MarkdownEditor";
2
+ export { MarkdownEditor } from "./MarkdownEditor";
3
+ export type { MarkdownViewerProps } from "./MarkdownViewer";
4
+ export { MarkdownViewer } from "./MarkdownViewer";
5
+ export type { MermaidBlockProps } from "./MermaidBlock";
6
+ export { MermaidBlock } from "./MermaidBlock";
7
+ export type { ReactCodeProps } from "./ReactCode";
8
+ export { ReactCode } from "./ReactCode";
9
+ export type { ReactCodeFrameProps } from "./ReactCodeFrame";
10
+ export { ReactCodeFrame } from "./ReactCodeFrame";
11
+ export { mountReactCodeRunner } from "./ReactCodeRunner";
12
+ export type { ReactCodeSandboxProps } from "./ReactCodeSandbox";
13
+ export { ReactCodeSandbox } from "./ReactCodeSandbox";
package/src/styles.css CHANGED
@@ -4,20 +4,21 @@
4
4
  /**
5
5
  * NonlaUI theme.
6
6
  *
7
- * Config is the first block in `:root` and `.light`. Set those.
7
+ * Tokens and chrome live on `.nonla-ui` (the host `App` renders). They do not
8
+ * paint `:root`, `body`, or the rest of the consumer page.
9
+ *
10
+ * Config is the first block in `.nonla-ui`. `.nonla-ui.light` repeats those keys.
8
11
  * Derived (`--background`, `--nonla-fill`, `--brand-500`, borders) follows Config.
9
12
  * `@theme` only maps tokens to Tailwind utilities (`bg-card`, `text-brand`).
10
13
  *
11
- * Dark is the default. Put `class="light"` or `class="dark"` on `<html>`
12
- * (or call `setColorMode`). Override Config in the consuming app's CSS.
14
+ * Dark is the default on `.nonla-ui`. `App` / `setColorMode` toggle `light` or
15
+ * `dark` on that host. Override Config on `.nonla-ui` in the consuming app's CSS.
13
16
  */
14
17
 
15
- :root,
16
- .dark,
17
18
  .nonla-ui {
18
19
  color-scheme: dark;
19
20
 
20
- /* ── Config. Set these. `.light` repeats the same keys. ────────────── */
21
+ /* ── Config. Set these. `.nonla-ui.light` repeats the same keys. ───── */
21
22
  /* Claude dark (claude.com). Warm gray, hue ~60. Page 950, sidebar 900, cards 850, bar 800. */
22
23
  --nonla-bg: #141413;
23
24
  --nonla-text-main: #faf9f5;
@@ -57,13 +58,10 @@
57
58
  --nonla-fill: color-mix(in oklab, var(--nonla-text-main) 6%, var(--nonla-surface));
58
59
  --nonla-fill-strong: color-mix(in oklab, var(--nonla-text-main) 10%, var(--nonla-surface));
59
60
  --nonla-input-focus: rgb(from var(--nonla-input) r g b / calc(alpha * 2));
60
- /* Hover and pressed/selected. Sidebar, menus, selects, buttons, and the desktop header share these. */
61
+ /* Hover and pressed/selected. Sidebar, menus, selects, and buttons share these. */
61
62
  --nonla-ink-hover: color-mix(in oklab, var(--nonla-text-main) 6%, transparent);
62
63
  --nonla-ink-active: color-mix(in oklab, var(--nonla-text-main) 10%, transparent);
63
64
  --nonla-z-base: 0;
64
- --nonla-z-desktop: 20;
65
- --nonla-z-window: 30;
66
- --nonla-z-header: 40;
67
65
  --nonla-z-popup-base: 1000;
68
66
 
69
67
  /* ── Aliases. Same rule: do not set. ────────────────────────────────── */
@@ -126,17 +124,13 @@
126
124
  color: var(--foreground);
127
125
  font-family: var(--nonla-font);
128
126
  font-weight: 400;
129
- }
130
-
131
- /* Size stays off `html`, so `1rem` and Tailwind spacing do not shrink. */
132
- body {
127
+ /* Size stays on the host, so `1rem` and Tailwind spacing do not shrink globally. */
133
128
  font-size: var(--nonla-base-text-size);
134
129
  line-height: 1.6;
135
130
  }
136
131
 
137
132
  /* Same color keys as Config above. Brand and the label on it stay. */
138
- .light:not(.dark),
139
- .light .nonla-ui:not(.dark) {
133
+ .nonla-ui.light {
140
134
  color-scheme: light;
141
135
 
142
136
  /* Light grays are Tailwind neutral. Page is white. Sidebar and cards are neutral-50. */
@@ -191,7 +185,6 @@ body {
191
185
  --color-destructive: var(--destructive);
192
186
  --color-border: var(--border);
193
187
  --color-border-secondary: var(--border-secondary);
194
- --default-border-color: var(--border);
195
188
  --color-input: var(--input);
196
189
  --color-input-focus: var(--input-focus);
197
190
  --color-brand: var(--brand);
@@ -233,10 +226,8 @@ body {
233
226
 
234
227
  --font-sans: var(--nonla-font);
235
228
  --font-family-sans: var(--font-sans);
236
- --default-font-family: var(--font-sans);
237
229
  --font-mono: Menlo, Monaco, "Courier New", monospace;
238
230
  --font-family-mono: var(--font-mono);
239
- --default-mono-font-family: var(--font-mono);
240
231
 
241
232
  --animate-fadeIn: fadeIn 0.28s ease-out both;
242
233
  }
@@ -252,23 +243,24 @@ body {
252
243
  }
253
244
  }
254
245
 
255
- /* Page scrollbar. */
256
- * {
246
+ /* Scrollbar inside the Nonla host only. */
247
+ .nonla-ui,
248
+ .nonla-ui * {
257
249
  scrollbar-width: thin;
258
250
  scrollbar-color: var(--nonla-scrollbar-thumb) transparent;
259
251
  }
260
- ::-webkit-scrollbar {
252
+ .nonla-ui ::-webkit-scrollbar {
261
253
  width: var(--nonla-scrollbar-size);
262
254
  height: var(--nonla-scrollbar-size);
263
255
  }
264
- ::-webkit-scrollbar-track {
256
+ .nonla-ui ::-webkit-scrollbar-track {
265
257
  background: transparent;
266
258
  }
267
- ::-webkit-scrollbar-thumb {
259
+ .nonla-ui ::-webkit-scrollbar-thumb {
268
260
  background: var(--nonla-scrollbar-thumb);
269
261
  border-radius: 99px;
270
262
  }
271
- ::-webkit-scrollbar-thumb:hover {
263
+ .nonla-ui ::-webkit-scrollbar-thumb:hover {
272
264
  background: var(--nonla-scrollbar-thumb-hover);
273
265
  }
274
266
 
@@ -567,70 +559,70 @@ body {
567
559
  font-size: 11px;
568
560
  }
569
561
 
570
- /* GitHub light syntax. The imported sheet is github-dark-dimmed; these beat it under `.light`. */
571
- .light .hljs-doctag,
572
- .light .hljs-keyword,
573
- .light .hljs-meta .hljs-keyword,
574
- .light .hljs-template-tag,
575
- .light .hljs-template-variable,
576
- .light .hljs-type,
577
- .light .hljs-variable.language_ {
562
+ /* GitHub light syntax. The imported sheet is github-dark-dimmed; these beat it under `.nonla-ui.light`. */
563
+ .nonla-ui.light .hljs-doctag,
564
+ .nonla-ui.light .hljs-keyword,
565
+ .nonla-ui.light .hljs-meta .hljs-keyword,
566
+ .nonla-ui.light .hljs-template-tag,
567
+ .nonla-ui.light .hljs-template-variable,
568
+ .nonla-ui.light .hljs-type,
569
+ .nonla-ui.light .hljs-variable.language_ {
578
570
  color: #d73a49;
579
571
  }
580
- .light .hljs-title,
581
- .light .hljs-title.class_,
582
- .light .hljs-title.class_.inherited__,
583
- .light .hljs-title.function_ {
572
+ .nonla-ui.light .hljs-title,
573
+ .nonla-ui.light .hljs-title.class_,
574
+ .nonla-ui.light .hljs-title.class_.inherited__,
575
+ .nonla-ui.light .hljs-title.function_ {
584
576
  color: #6f42c1;
585
577
  }
586
- .light .hljs-attr,
587
- .light .hljs-attribute,
588
- .light .hljs-literal,
589
- .light .hljs-meta,
590
- .light .hljs-number,
591
- .light .hljs-operator,
592
- .light .hljs-variable,
593
- .light .hljs-selector-attr,
594
- .light .hljs-selector-class,
595
- .light .hljs-selector-id {
578
+ .nonla-ui.light .hljs-attr,
579
+ .nonla-ui.light .hljs-attribute,
580
+ .nonla-ui.light .hljs-literal,
581
+ .nonla-ui.light .hljs-meta,
582
+ .nonla-ui.light .hljs-number,
583
+ .nonla-ui.light .hljs-operator,
584
+ .nonla-ui.light .hljs-variable,
585
+ .nonla-ui.light .hljs-selector-attr,
586
+ .nonla-ui.light .hljs-selector-class,
587
+ .nonla-ui.light .hljs-selector-id {
596
588
  color: #005cc5;
597
589
  }
598
- .light .hljs-regexp,
599
- .light .hljs-string,
600
- .light .hljs-meta .hljs-string {
590
+ .nonla-ui.light .hljs-regexp,
591
+ .nonla-ui.light .hljs-string,
592
+ .nonla-ui.light .hljs-meta .hljs-string {
601
593
  color: #032f62;
602
594
  }
603
- .light .hljs-built_in,
604
- .light .hljs-symbol {
595
+ .nonla-ui.light .hljs-built_in,
596
+ .nonla-ui.light .hljs-symbol {
605
597
  color: #e36209;
606
598
  }
607
- .light .hljs-comment,
608
- .light .hljs-code,
609
- .light .hljs-formula {
599
+ .nonla-ui.light .hljs-comment,
600
+ .nonla-ui.light .hljs-code,
601
+ .nonla-ui.light .hljs-formula {
610
602
  color: #6a737d;
611
603
  }
612
- .light .hljs-name,
613
- .light .hljs-quote,
614
- .light .hljs-selector-tag,
615
- .light .hljs-selector-pseudo {
604
+ .nonla-ui.light .hljs-name,
605
+ .nonla-ui.light .hljs-quote,
606
+ .nonla-ui.light .hljs-selector-tag,
607
+ .nonla-ui.light .hljs-selector-pseudo {
616
608
  color: #22863a;
617
609
  }
618
- .light .hljs-subst,
619
- .light .hljs-emphasis,
620
- .light .hljs-strong {
610
+ .nonla-ui.light .hljs-subst,
611
+ .nonla-ui.light .hljs-emphasis,
612
+ .nonla-ui.light .hljs-strong {
621
613
  color: #24292e;
622
614
  }
623
- .light .hljs-section {
615
+ .nonla-ui.light .hljs-section {
624
616
  color: #005cc5;
625
617
  }
626
- .light .hljs-bullet {
618
+ .nonla-ui.light .hljs-bullet {
627
619
  color: #735c0f;
628
620
  }
629
- .light .hljs-addition {
621
+ .nonla-ui.light .hljs-addition {
630
622
  color: #22863a;
631
623
  background-color: #f0fff4;
632
624
  }
633
- .light .hljs-deletion {
625
+ .nonla-ui.light .hljs-deletion {
634
626
  color: #b31d28;
635
627
  background-color: #ffeef0;
636
628
  }
package/src/theme.ts CHANGED
@@ -1,11 +1,15 @@
1
1
  /**
2
- * Paint `light` or `dark` on `<html>`. Dark is the default.
2
+ * Paint `light` or `dark` on the Nonla host (`.nonla-ui`). Dark is the default.
3
3
  * The stored choice may be `system`, which follows the OS and still paints one of those two.
4
+ *
5
+ * `App` binds the host via `bindColorModeTarget`. Callers can still pass an explicit
6
+ * `target` to `setColorMode` / `initColorMode`. Without a bound host, the fallback is
7
+ * `<html>` for sandboxes that mount their own root.
4
8
  */
5
9
 
6
10
  export type NonlaColorMode = "light" | "dark";
7
11
 
8
- /** Stored choice. `system` follows the OS and still applies `light` or `dark` on `<html>`. */
12
+ /** Stored choice. `system` follows the OS and still applies `light` or `dark` on the host. */
9
13
  export type NonlaColorPreference = NonlaColorMode | "system";
10
14
 
11
15
  /** `localStorage` key written by `setColorMode` / `setColorPreference`. */
@@ -16,6 +20,7 @@ let mode: NonlaColorMode = "dark";
16
20
  let preference: NonlaColorPreference = "dark";
17
21
  let ready = false;
18
22
  let schemeQuery: MediaQueryList | null = null;
23
+ let boundTarget: HTMLElement | null = null;
19
24
 
20
25
  function notify() {
21
26
  for (const listener of listeners) listener();
@@ -39,6 +44,15 @@ export function getColorPreference(): NonlaColorPreference {
39
44
  return preference;
40
45
  }
41
46
 
47
+ /** Host `App` paints mode on. Cleared on unmount. */
48
+ export function bindColorModeTarget(target: HTMLElement | null) {
49
+ boundTarget = target;
50
+ }
51
+
52
+ function colorTarget(explicit?: HTMLElement): HTMLElement {
53
+ return explicit ?? boundTarget ?? document.documentElement;
54
+ }
55
+
42
56
  function readStored(): NonlaColorPreference | null {
43
57
  try {
44
58
  // Sandboxed iframes throw on the property access itself, before getItem.
@@ -74,12 +88,19 @@ function watchSystem() {
74
88
  });
75
89
  }
76
90
 
77
- /** Set `light` or `dark` on `target` (default `<html>`) so portals inherit the palette. */
78
- export function applyColorMode(next: NonlaColorMode, target: HTMLElement = document.documentElement) {
91
+ function modeFromClass(el: HTMLElement): NonlaColorMode | null {
92
+ if (el.classList.contains("light")) return "light";
93
+ if (el.classList.contains("dark")) return "dark";
94
+ return null;
95
+ }
96
+
97
+ /** Set `light` or `dark` on `target` (bound host, or `<html>` fallback) so portals inherit the palette. */
98
+ export function applyColorMode(next: NonlaColorMode, target?: HTMLElement) {
79
99
  mode = next;
80
- target.classList.toggle("light", next === "light");
81
- target.classList.toggle("dark", next === "dark");
82
- target.style.colorScheme = next;
100
+ const el = colorTarget(target);
101
+ el.classList.toggle("light", next === "light");
102
+ el.classList.toggle("dark", next === "dark");
103
+ el.style.colorScheme = next;
83
104
  notify();
84
105
  }
85
106
 
@@ -104,13 +125,17 @@ export function setColorMode(next: NonlaColorMode, target?: HTMLElement) {
104
125
 
105
126
  /** Apply the saved mode once. Falls back to dark. A stored `system` choice follows the OS. */
106
127
  export function initColorMode(target?: HTMLElement): NonlaColorMode {
107
- if (typeof document === "undefined" || ready) return mode;
128
+ if (typeof document === "undefined") return mode;
129
+ const el = colorTarget(target);
130
+ if (ready) {
131
+ applyColorMode(mode, el);
132
+ return mode;
133
+ }
108
134
  ready = true;
109
- const root = document.documentElement;
110
- const fromClass = root.classList.contains("light") ? "light" : root.classList.contains("dark") ? "dark" : null;
135
+ const fromClass = modeFromClass(el) ?? modeFromClass(document.documentElement);
111
136
  const next = readStored() ?? fromClass ?? "dark";
112
137
  preference = next;
113
138
  if (next === "system") watchSystem();
114
- applyColorMode(resolved(next), target);
139
+ applyColorMode(resolved(next), el);
115
140
  return mode;
116
141
  }
@@ -1,6 +1,6 @@
1
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, markdownScaleStyle, pageTitleClass } from "../markdown-editor/markdownScale";
3
+ import { headingClass, markdownBodyClass, markdownParagraphClass, markdownScaleStyle, pageTitleClass } from "../lib/markdownScale";
4
4
 
5
5
  export type TypographyType = "secondary" | "success" | "warning" | "danger";
6
6