@vvedantb/ui 0.84.0 → 0.85.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.
@@ -1,8 +1,26 @@
1
1
  import { c as _c } from "react/compiler-runtime";
2
- import { code as shiki } from "@streamdown/code";
3
2
  import { useState, useSyncExternalStore } from "react";
4
- const THEMES = shiki.getThemes();
5
- const LANGUAGES = new Map(shiki.getSupportedLanguages().map(id => [id, id]));
3
+ let engine = null;
4
+ let loading = null;
5
+ const engineListeners = new Set();
6
+ function subscribeEngine(notify) {
7
+ engineListeners.add(notify);
8
+ return () => engineListeners.delete(notify);
9
+ }
10
+ function loadEngine() {
11
+ loading ??= import("@streamdown/code").then(({
12
+ code: shiki
13
+ }) => {
14
+ engine = {
15
+ shiki,
16
+ themes: shiki.getThemes(),
17
+ languages: new Map(shiki.getSupportedLanguages().map(id => [id, id]))
18
+ };
19
+ for (const notify of engineListeners) notify();
20
+ }, () => {
21
+ loading = null;
22
+ });
23
+ }
6
24
  const CACHE_LIMIT = 200;
7
25
  const cache = new Map();
8
26
  const listeners = new Map();
@@ -16,7 +34,10 @@ function store(key, entry) {
16
34
  const oldest = cache.keys().next().value;
17
35
  if (oldest !== undefined && !listeners.has(oldest)) cache.delete(oldest);
18
36
  }
19
- function request(language, source) {
37
+ function request({
38
+ shiki,
39
+ themes
40
+ }, language, source) {
20
41
  const key = cacheKey(language, source);
21
42
  const hit = cache.get(key);
22
43
  if (hit !== undefined) return hit.status === "done" ? hit.result : null;
@@ -26,7 +47,7 @@ function request(language, source) {
26
47
  const sync = shiki.highlight({
27
48
  code: source,
28
49
  language,
29
- themes: THEMES
50
+ themes
30
51
  }, result => {
31
52
  store(key, {
32
53
  status: "done",
@@ -82,42 +103,48 @@ export function buildCodeLines(code, highlighted) {
82
103
  * for), so streaming never flashes back to plain text.
83
104
  */
84
105
  export function useHighlightedLines(code, language, isIncomplete) {
85
- const $ = _c(18);
106
+ const $ = _c(20);
107
+ const loaded = useSyncExternalStore(subscribeEngine, _temp, _temp2);
108
+ if (loaded === null && language !== "") {
109
+ loadEngine();
110
+ }
86
111
  let bundled;
87
112
  let source;
88
113
  let t0;
89
- if ($[0] !== code || $[1] !== isIncomplete || $[2] !== language) {
90
- bundled = LANGUAGES.get(language.toLowerCase()) ?? null;
114
+ if ($[0] !== code || $[1] !== isIncomplete || $[2] !== language || $[3] !== loaded?.languages) {
115
+ bundled = loaded?.languages.get(language.toLowerCase()) ?? null;
91
116
  source = bundled === null ? "" : highlightSource(code, isIncomplete);
92
117
  t0 = bundled === null ? "" : cacheKey(bundled, source);
93
118
  $[0] = code;
94
119
  $[1] = isIncomplete;
95
120
  $[2] = language;
96
- $[3] = bundled;
97
- $[4] = source;
98
- $[5] = t0;
121
+ $[3] = loaded?.languages;
122
+ $[4] = bundled;
123
+ $[5] = source;
124
+ $[6] = t0;
99
125
  } else {
100
- bundled = $[3];
101
- source = $[4];
102
- t0 = $[5];
126
+ bundled = $[4];
127
+ source = $[5];
128
+ t0 = $[6];
103
129
  }
104
130
  const key = t0;
105
131
  let t1;
106
- if ($[6] !== key) {
107
- t1 = notify => key === "" ? _temp : subscribe(key, notify);
108
- $[6] = key;
109
- $[7] = t1;
132
+ if ($[7] !== key) {
133
+ t1 = notify => key === "" ? _temp3 : subscribe(key, notify);
134
+ $[7] = key;
135
+ $[8] = t1;
110
136
  } else {
111
- t1 = $[7];
137
+ t1 = $[8];
112
138
  }
113
139
  let t2;
114
- if ($[8] !== bundled || $[9] !== source) {
115
- t2 = () => bundled === null || source === "" ? null : request(bundled, source);
116
- $[8] = bundled;
117
- $[9] = source;
118
- $[10] = t2;
140
+ if ($[9] !== bundled || $[10] !== loaded || $[11] !== source) {
141
+ t2 = () => loaded === null || bundled === null || source === "" ? null : request(loaded, bundled, source);
142
+ $[9] = bundled;
143
+ $[10] = loaded;
144
+ $[11] = source;
145
+ $[12] = t2;
119
146
  } else {
120
- t2 = $[10];
147
+ t2 = $[12];
121
148
  }
122
149
  const result = useSyncExternalStore(t1, t2);
123
150
  const [shown, setShown] = useState(null);
@@ -128,27 +155,33 @@ export function useHighlightedLines(code, language, isIncomplete) {
128
155
  });
129
156
  }
130
157
  let t3;
131
- if ($[11] !== result || $[12] !== shown || $[13] !== source) {
158
+ if ($[13] !== result || $[14] !== shown || $[15] !== source) {
132
159
  t3 = result !== null ? {
133
160
  source,
134
161
  result
135
162
  } : shown;
136
- $[11] = result;
137
- $[12] = shown;
138
- $[13] = source;
139
- $[14] = t3;
163
+ $[13] = result;
164
+ $[14] = shown;
165
+ $[15] = source;
166
+ $[16] = t3;
140
167
  } else {
141
- t3 = $[14];
168
+ t3 = $[16];
142
169
  }
143
170
  let t4;
144
- if ($[15] !== code || $[16] !== t3) {
171
+ if ($[17] !== code || $[18] !== t3) {
145
172
  t4 = buildCodeLines(code, t3);
146
- $[15] = code;
147
- $[16] = t3;
148
- $[17] = t4;
173
+ $[17] = code;
174
+ $[18] = t3;
175
+ $[19] = t4;
149
176
  } else {
150
- t4 = $[17];
177
+ t4 = $[19];
151
178
  }
152
179
  return t4;
153
180
  }
154
- function _temp() {}
181
+ function _temp3() {}
182
+ function _temp2() {
183
+ return engine;
184
+ }
185
+ function _temp() {
186
+ return engine;
187
+ }
@@ -0,0 +1,36 @@
1
+ import { useSyncExternalStore } from "react";
2
+ const MATH_HINT = /\$\$|```math/;
3
+ let math = null;
4
+ let loading = null;
5
+ const listeners = new Set();
6
+ function subscribe(notify) {
7
+ listeners.add(notify);
8
+ return () => listeners.delete(notify);
9
+ }
10
+ function load() {
11
+ loading ??= import("./mathPlugin.js").then(module => {
12
+ math = module.math;
13
+ for (const notify of listeners) notify();
14
+ }, () => {
15
+ loading = null;
16
+ });
17
+ }
18
+
19
+ /**
20
+ * KaTeX and its CSS load on the first text that holds `$$` or a `math`
21
+ * fence (single `$` is off), so pages without maths never fetch them.
22
+ */
23
+ export function useMathPlugin(text) {
24
+ const loaded = useSyncExternalStore(subscribe, _temp, _temp2);
25
+ const needed = MATH_HINT.test(text);
26
+ if (needed && loaded === null) {
27
+ load();
28
+ }
29
+ return needed ? loaded : null;
30
+ }
31
+ function _temp2() {
32
+ return math;
33
+ }
34
+ function _temp() {
35
+ return math;
36
+ }
@@ -0,0 +1,2 @@
1
+ import "katex/dist/katex.min.css";
2
+ export { math } from "@streamdown/math";
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvedantb/ui",
3
- "version": "0.84.0",
3
+ "version": "0.85.0",
4
4
  "license": "MIT",
5
5
  "description": "Shared React 19 UI for web apps: one main JavaScript entry (primitives, shell pieces, app helpers including the Convex and Clerk provider, VITE env parsing and ensure-user, artifact viewers over the @vvedantb/server artifacts core, native tokens), third-party brand logos (also @vvedantb/ui/logos), motion-driven animated SVG icons (@vvedantb/ui/icons/animated), a client entry without @vvedantb/server, convex or Clerk for Chrome extensions, design-token CSS, shared Tailwind v4 CSS, a Tailwind preset and packaged Instrument Sans fonts.",
6
6
  "type": "module",
@@ -1,8 +1,9 @@
1
- import { useLayoutEffect, type ComponentProps, type RefObject } from "react";
1
+ import { useLayoutEffect, useRef, type ComponentProps, type RefObject } from "react";
2
2
  import { dockPopIn, dockSwitch, type DockEdge } from "../../src/motion/dock-motion";
3
3
  import { cn } from "../../src/utils/cn";
4
4
 
5
5
  const ARROW_HALF_PX = 8;
6
+ const TILE_STATE = ["data-active", "aria-current", "data-state", "aria-selected"];
6
7
 
7
8
  export type DockArrowProps = Omit<ComponentProps<"div">, "children"> & {
8
9
  /** Which edge of the card touches the rail. The arrow points across it. Default `"right"`. */
@@ -48,8 +49,14 @@ export type UseDockArrowOptions = {
48
49
  tile: (frame: HTMLElement) => HTMLElement | null;
49
50
  /** Which edge of the card touches the rail. Default `"right"`. */
50
51
  edge?: DockEdge;
51
- /** Returns an element whose size change moves the tiles (the tab list). */
52
+ /**
53
+ * Returns the element that holds the tiles (the tab list). Its size and its
54
+ * tiles' active state (`data-active`, `aria-current`, `data-state`,
55
+ * `aria-selected`) are watched, so the arrow moves in the same frame.
56
+ */
52
57
  watch?: (frame: HTMLElement) => Element | null;
58
+ /** Re-places the arrow when it changes, for a `tile` that reads outside state. */
59
+ syncKey?: unknown;
53
60
  };
54
61
 
55
62
  /**
@@ -61,7 +68,16 @@ export type UseDockArrowOptions = {
61
68
  * arrow hides while the card has no width or the tile is outside the card's
62
69
  * height.
63
70
  */
64
- export function useDockArrow({ frameRef, cardRef, arrowRef, tile, edge = "right", watch }: UseDockArrowOptions) {
71
+ export function useDockArrow({
72
+ frameRef,
73
+ cardRef,
74
+ arrowRef,
75
+ tile,
76
+ edge = "right",
77
+ watch,
78
+ syncKey,
79
+ }: UseDockArrowOptions) {
80
+ const syncRef = useRef<(() => void) | null>(null);
65
81
  useLayoutEffect(() => {
66
82
  const frame = frameRef.current;
67
83
  const card = cardRef.current;
@@ -105,21 +121,35 @@ export function useDockArrow({ frameRef, cardRef, arrowRef, tile, edge = "right"
105
121
  if (opened) dockPopIn(card, tab, edge, arrow);
106
122
  else if (switched) dockSwitch(card, previousTab, tab);
107
123
  };
124
+ syncRef.current = sync;
108
125
  sync();
109
- if (typeof ResizeObserver === "undefined") return;
126
+ if (typeof ResizeObserver === "undefined") {
127
+ return () => {
128
+ syncRef.current = null;
129
+ };
130
+ }
110
131
  const resize = new ResizeObserver(sync);
111
132
  resize.observe(frame);
112
133
  resize.observe(card);
113
134
  const mutation = new MutationObserver(sync);
114
- mutation.observe(frame, { subtree: true, attributes: true, attributeFilter: ["data-state", "aria-selected"] });
135
+ mutation.observe(frame, { subtree: true, attributes: true, attributeFilter: TILE_STATE });
115
136
  const list = watch?.(frame);
116
- if (list) resize.observe(list);
137
+ if (list) {
138
+ resize.observe(list);
139
+ if (!frame.contains(list)) {
140
+ mutation.observe(list, { subtree: true, attributes: true, attributeFilter: TILE_STATE });
141
+ }
142
+ }
117
143
  const settle = ["scroll", "animationend", "transitionend"] as const;
118
144
  for (const type of settle) frame.addEventListener(type, sync, true);
119
145
  return () => {
146
+ syncRef.current = null;
120
147
  resize.disconnect();
121
148
  mutation.disconnect();
122
149
  for (const type of settle) frame.removeEventListener(type, sync, true);
123
150
  };
124
151
  }, [frameRef, cardRef, arrowRef, tile, edge, watch]);
152
+ useLayoutEffect(() => {
153
+ syncRef.current?.();
154
+ }, [syncKey]);
125
155
  }
@@ -230,14 +230,15 @@ function Aside({
230
230
  onPointerLeave,
231
231
  onFocus,
232
232
  onBlur,
233
+ onContextMenu,
233
234
  children,
234
235
  ...props
235
236
  }: SidebarAsideProps) {
236
237
  const sidebar = useSidebar("Aside");
237
238
  const resize = useSidebarResizeContext();
238
239
  const { isDesktop, mobileOpen, collapsed, panelHidden, peek } = sidebar;
239
- const hover = isDesktop && sidebar.unpinned && !panelHidden ? peek : null;
240
- const peeking = hover !== null && !collapsed;
240
+ const hover = isDesktop && sidebar.unpinned ? peek : null;
241
+ const peeking = hover !== null && !collapsed && !panelHidden;
241
242
 
242
243
  const handleDrawerDragEnd = (_event: MouseEvent | TouchEvent | PointerEvent, info: PanInfo) => {
243
244
  if (isDesktop || !mobileOpen) return;
@@ -270,6 +271,10 @@ function Aside({
270
271
  onBlur?.(event);
271
272
  hover?.handlers.onBlur(event);
272
273
  }}
274
+ onContextMenu={(event) => {
275
+ onContextMenu?.(event);
276
+ hover?.handlers.onContextMenu(event);
277
+ }}
273
278
  onKeyDown={(event) => {
274
279
  onKeyDown?.(event);
275
280
  closeOnEscape(sidebar, event);
@@ -95,7 +95,7 @@ function FloatingCard({ hoverTile, children }: { hoverTile: HTMLElement | null;
95
95
  hoverRef.current = hoverTile;
96
96
  }, [hoverTile]);
97
97
  const tile = useCallback((frame: HTMLElement) => hoverRef.current ?? activeRailTile(frame), []);
98
- useDockArrow({ frameRef, cardRef, arrowRef, edge: "left", tile, watch: sidebarRail });
98
+ useDockArrow({ frameRef, cardRef, arrowRef, edge: "left", tile, watch: sidebarRail, syncKey: hoverTile });
99
99
  return (
100
100
  <div ref={frameRef} className="relative flex min-h-0 flex-1 flex-col">
101
101
  <DockArrow ref={arrowRef} edge="left" hairline className="z-10 bg-background" />
@@ -1,11 +1,13 @@
1
- import { useRef, useState, type FocusEvent, type PointerEvent } from "react";
1
+ import { useRef, useState, type FocusEvent, type MouseEvent, type PointerEvent } from "react";
2
2
  import { flushSync } from "react-dom";
3
3
  import { dockPopIn, dockPopOut } from "../../src/motion/dock-motion";
4
4
 
5
5
  const OPEN_DELAY_MS = 120;
6
6
  const CLOSE_DELAY_MS = 350;
7
+ const MENU_CLOSE_DELAY_MS = 2000;
7
8
  const OPEN_MENU_TRIGGER = '[aria-haspopup][aria-expanded="true"]';
8
9
  const PANEL_SELECTOR = "[data-sidebar-panel]";
10
+ const PORTAL_MENU = '[role="menu"]';
9
11
 
10
12
  function peekTile(target: EventTarget | null): HTMLElement | null {
11
13
  if (!(target instanceof Element)) return null;
@@ -17,11 +19,31 @@ function inPanel(target: EventTarget | null): boolean {
17
19
  return target instanceof Element && target.closest(PANEL_SELECTOR) !== null;
18
20
  }
19
21
 
22
+ function inPortal(aside: HTMLElement, target: EventTarget | null): boolean {
23
+ return target instanceof Node && !aside.contains(target);
24
+ }
25
+
26
+ function inPortalMenu(aside: HTMLElement, target: EventTarget | null): boolean {
27
+ return target instanceof Element && target.closest(PORTAL_MENU) !== null && !aside.contains(target);
28
+ }
29
+
30
+ function openPortalMenu(aside: HTMLElement): boolean {
31
+ return Array.from(document.querySelectorAll(PORTAL_MENU)).some(
32
+ (menu) => !aside.contains(menu) && menu.getAttribute("data-state") !== "closed",
33
+ );
34
+ }
35
+
36
+ export type SidebarPeekOptions = {
37
+ /** Delay before the peek closes after a menu opened from it closes with the pointer outside. Default 2000 ms. */
38
+ menuCloseDelay?: number;
39
+ };
40
+
20
41
  export type SidebarPeekHandlers = {
21
42
  onPointerOver: (event: PointerEvent<HTMLElement>) => void;
22
43
  onPointerLeave: (event: PointerEvent<HTMLElement>) => void;
23
44
  onFocus: (event: FocusEvent<HTMLElement>) => void;
24
45
  onBlur: (event: FocusEvent<HTMLElement>) => void;
46
+ onContextMenu: (event: MouseEvent<HTMLElement>) => void;
25
47
  };
26
48
 
27
49
  export type SidebarPeek = {
@@ -47,21 +69,33 @@ export type SidebarPeek = {
47
69
  * links and tabs have it), and stays open while the pointer or focus is on a
48
70
  * rail item or in the panel. It grows out of the hovered item with
49
71
  * `dockPopIn` and shrinks back with `dockPopOut`. It stays open while a menu
50
- * opened from the sidebar is open. `Sidebar.Root` makes one when none is
72
+ * opened from the sidebar is open, a right-click menu included, and closes
73
+ * `menuCloseDelay` after that menu closes with the pointer outside. A rail item
74
+ * whose page has no panel closes it, like empty rail. `Sidebar.Root` makes one when none is
51
75
  * passed; pass your own to read `previewPath` or `previewValue` and show the
52
76
  * hovered item's panel without navigating.
53
77
  */
54
- export function useSidebarPeek(): SidebarPeek {
78
+ export function useSidebarPeek({ menuCloseDelay = MENU_CLOSE_DELAY_MS }: SidebarPeekOptions = {}): SidebarPeek {
55
79
  const [peeking, setPeeking] = useState(false);
56
80
  const [tile, setTile] = useState<HTMLElement | null>(null);
57
81
  const timer = useRef<number | undefined>(undefined);
58
82
  const open = useRef(false);
59
83
  const tileRef = useRef<HTMLElement | null>(null);
84
+ const noPanelTile = useRef<HTMLElement | null>(null);
85
+ const contextMenu = useRef(false);
86
+ const stopWatch = useRef<(() => void) | null>(null);
60
87
 
61
88
  const cancelPending = () => window.clearTimeout(timer.current);
62
89
 
90
+ const endMenuWatch = () => {
91
+ stopWatch.current?.();
92
+ stopWatch.current = null;
93
+ contextMenu.current = false;
94
+ };
95
+
63
96
  const reset = () => {
64
97
  cancelPending();
98
+ endMenuWatch();
65
99
  open.current = false;
66
100
  setPeeking(false);
67
101
  setTile(null);
@@ -73,52 +107,125 @@ export function useSidebarPeek(): SidebarPeek {
73
107
  setPeeking(true);
74
108
  };
75
109
 
76
- const scheduleOpen = (aside: HTMLElement, delayMs: number) => {
110
+ const menuHeld = (aside: HTMLElement) =>
111
+ aside.querySelector(OPEN_MENU_TRIGGER) !== null || (contextMenu.current && openPortalMenu(aside));
112
+
113
+ const closeWithoutPanel = () => {
114
+ noPanelTile.current = tileRef.current;
115
+ reset();
116
+ };
117
+
118
+ const scheduleClose = (aside: HTMLElement, delayMs = CLOSE_DELAY_MS) => {
77
119
  cancelPending();
78
- if (open.current) return;
120
+ if (!open.current) return;
79
121
  timer.current = window.setTimeout(() => {
80
- open.current = true;
81
- flushSync(() => setPeeking(true));
122
+ if (menuHeld(aside)) {
123
+ watchMenu(aside, true);
124
+ return;
125
+ }
82
126
  const panel = aside.querySelector<HTMLElement>(PANEL_SELECTOR);
83
- if (panel && !panel.hasAttribute("data-sidebar-floating")) dockPopIn(panel, tileRef.current, "left");
127
+ if (!panel) {
128
+ reset();
129
+ return;
130
+ }
131
+ const card = panel.querySelector<HTMLElement>("[data-sidebar-card]") ?? panel;
132
+ const arrow = panel.querySelector<HTMLElement>("[data-dock-arrow]");
133
+ dockPopOut(card, tileRef.current, "left", reset, arrow);
84
134
  }, delayMs);
85
135
  };
86
136
 
87
- const scheduleClose = (aside: HTMLElement) => {
137
+ const watchMenu = (aside: HTMLElement, alreadyOpen: boolean) => {
138
+ if (stopWatch.current) return;
139
+ let seenOpen = alreadyOpen;
140
+ let pointerInside = aside.matches(":hover");
141
+ const enter = () => {
142
+ pointerInside = true;
143
+ };
144
+ const leave = () => {
145
+ pointerInside = false;
146
+ };
147
+ const observer = new MutationObserver(() => {
148
+ if (!aside.isConnected) return endMenuWatch();
149
+ if (menuHeld(aside)) {
150
+ seenOpen = true;
151
+ return;
152
+ }
153
+ if (!seenOpen) return;
154
+ endMenuWatch();
155
+ if (!pointerInside) scheduleClose(aside, menuCloseDelay);
156
+ });
157
+ observer.observe(document.body, {
158
+ childList: true,
159
+ subtree: true,
160
+ attributes: true,
161
+ attributeFilter: ["aria-expanded", "data-state"],
162
+ });
163
+ aside.addEventListener("pointerenter", enter);
164
+ aside.addEventListener("pointerleave", leave);
165
+ stopWatch.current = () => {
166
+ observer.disconnect();
167
+ aside.removeEventListener("pointerenter", enter);
168
+ aside.removeEventListener("pointerleave", leave);
169
+ };
170
+ };
171
+
172
+ const scheduleOpen = (aside: HTMLElement, delayMs: number) => {
88
173
  cancelPending();
89
- if (!open.current) return;
174
+ if (open.current) return;
90
175
  timer.current = window.setTimeout(() => {
91
- if (aside.querySelector(OPEN_MENU_TRIGGER)) return;
176
+ open.current = true;
177
+ flushSync(() => setPeeking(true));
92
178
  const panel = aside.querySelector<HTMLElement>(PANEL_SELECTOR);
93
- const card = panel?.querySelector<HTMLElement>("[data-sidebar-card]") ?? panel;
94
- const arrow = panel?.querySelector<HTMLElement>("[data-dock-arrow]");
95
- dockPopOut(card, tileRef.current, "left", reset, arrow);
96
- }, CLOSE_DELAY_MS);
179
+ if (!panel) return closeWithoutPanel();
180
+ if (!panel.hasAttribute("data-sidebar-floating")) dockPopIn(panel, tileRef.current, "left");
181
+ }, delayMs);
97
182
  };
98
183
 
99
184
  const onTile = (aside: HTMLElement, next: HTMLElement, delayMs: number) => {
185
+ if (next === noPanelTile.current && !open.current) return;
186
+ noPanelTile.current = null;
187
+ const changed = next !== tileRef.current;
100
188
  tileRef.current = next;
189
+ if (open.current && changed) {
190
+ cancelPending();
191
+ flushSync(() => setTile(next));
192
+ if (!aside.querySelector(PANEL_SELECTOR)) closeWithoutPanel();
193
+ return;
194
+ }
101
195
  setTile(next);
102
196
  scheduleOpen(aside, delayMs);
103
197
  };
104
198
 
105
199
  const handlers: SidebarPeekHandlers = {
106
200
  onPointerOver: (event) => {
201
+ const aside = event.currentTarget;
107
202
  const next = peekTile(event.target);
108
- if (next) onTile(event.currentTarget, next, OPEN_DELAY_MS);
109
- else if (inPanel(event.target)) cancelPending();
110
- else scheduleClose(event.currentTarget);
203
+ if (next) return onTile(aside, next, OPEN_DELAY_MS);
204
+ noPanelTile.current = null;
205
+ if (inPanel(event.target) || inPortal(aside, event.target)) cancelPending();
206
+ else scheduleClose(aside);
207
+ },
208
+ onPointerLeave: (event) => {
209
+ noPanelTile.current = null;
210
+ scheduleClose(event.currentTarget);
111
211
  },
112
- onPointerLeave: (event) => scheduleClose(event.currentTarget),
113
212
  onFocus: (event) => {
213
+ const aside = event.currentTarget;
114
214
  const next = peekTile(event.target);
115
- if (next) onTile(event.currentTarget, next, 0);
116
- else if (inPanel(event.target)) cancelPending();
215
+ if (next) onTile(aside, next, 0);
216
+ else if (inPanel(event.target) || inPortal(aside, event.target)) cancelPending();
117
217
  },
118
218
  onBlur: (event) => {
119
- if (peekTile(event.relatedTarget) === null && !inPanel(event.relatedTarget)) {
120
- scheduleClose(event.currentTarget);
121
- }
219
+ const aside = event.currentTarget;
220
+ const to = event.relatedTarget;
221
+ if (peekTile(to) !== null || inPanel(to) || inPortalMenu(aside, to)) return;
222
+ scheduleClose(aside);
223
+ },
224
+ onContextMenu: (event) => {
225
+ if (!open.current) return;
226
+ cancelPending();
227
+ contextMenu.current = true;
228
+ watchMenu(event.currentTarget, false);
122
229
  },
123
230
  };
124
231
 
package/src/client.ts CHANGED
@@ -237,7 +237,7 @@ export type {
237
237
  SidePanelTabsTabProps,
238
238
  } from "../shell/src/SidePanelTabs";
239
239
  export { useSidebarPeek } from "../shell/src/SidebarPeek";
240
- export type { SidebarPeek, SidebarPeekHandlers } from "../shell/src/SidebarPeek";
240
+ export type { SidebarPeek, SidebarPeekHandlers, SidebarPeekOptions } from "../shell/src/SidebarPeek";
241
241
  export { DockArrow, useDockArrow } from "../shell/src/DockArrow";
242
242
  export type { DockArrowProps, UseDockArrowOptions } from "../shell/src/DockArrow";
243
243
  export { AppSidebar } from "../shell/src/AppSidebar";
package/src/index.ts CHANGED
@@ -240,7 +240,7 @@ export type {
240
240
  SidePanelTabsTabProps,
241
241
  } from "../shell/src/SidePanelTabs";
242
242
  export { useSidebarPeek } from "../shell/src/SidebarPeek";
243
- export type { SidebarPeek, SidebarPeekHandlers } from "../shell/src/SidebarPeek";
243
+ export type { SidebarPeek, SidebarPeekHandlers, SidebarPeekOptions } from "../shell/src/SidebarPeek";
244
244
  export { DockArrow, useDockArrow } from "../shell/src/DockArrow";
245
245
  export type { DockArrowProps, UseDockArrowOptions } from "../shell/src/DockArrow";
246
246
  export { AppSidebar } from "../shell/src/AppSidebar";
@@ -1,9 +1,7 @@
1
1
  import type { ComponentProps } from "react";
2
2
  import { memo } from "react";
3
3
  import { cjk } from "@streamdown/cjk";
4
- import { code } from "@streamdown/code";
5
- import { math } from "@streamdown/math";
6
- import type { Components } from "streamdown";
4
+ import type { Components, MathPlugin } from "streamdown";
7
5
  import {
8
6
  Streamdown,
9
7
  defaultRehypePlugins,
@@ -16,13 +14,20 @@ import { MarkdownBlockquote } from "./MarkdownBlockquote";
16
14
  import { MarkdownPre } from "./MarkdownCodeBlock";
17
15
  import { MarkdownInlineCode } from "./MarkdownInlineCode";
18
16
  import { MarkdownLink } from "./MarkdownLink";
17
+ import { useMathPlugin } from "./lazyMath";
19
18
  import { mermaidPlugin } from "./mermaid";
20
19
  import { copyMarkdownSelection } from "./markdownClipboard";
21
20
  import { rehypeGithubAlerts } from "./rehypeGithubAlerts";
22
- import "katex/dist/katex.min.css";
23
21
  import { useSmoothText } from "./useSmoothText";
24
22
 
25
- const PLUGINS = { cjk, code, math, mermaid: mermaidPlugin };
23
+ const PLUGINS = { cjk, mermaid: mermaidPlugin };
24
+ let pluginsWithMath: typeof PLUGINS & { math: MathPlugin } | null = null;
25
+
26
+ function pluginsFor(math: MathPlugin | null) {
27
+ if (math === null) return PLUGINS;
28
+ pluginsWithMath ??= { ...PLUGINS, math };
29
+ return pluginsWithMath;
30
+ }
26
31
 
27
32
  const REMARK_PLUGINS = Object.values(defaultRemarkPlugins);
28
33
  const REHYPE_PLUGINS = [...Object.values(defaultRehypePlugins), rehypeGithubAlerts];
@@ -76,11 +81,12 @@ export const Markdown = memo(function Markdown({
76
81
  }: MarkdownProps) {
77
82
  const merge = useCn();
78
83
  const text = useSmoothText(children, smooth && props.mode === "streaming");
84
+ const math = useMathPlugin(text);
79
85
  return (
80
86
  <div className="contents" onCopy={copyMarkdownSelection}>
81
87
  <Streamdown
82
88
  className={merge("markdown", className)}
83
- plugins={PLUGINS}
89
+ plugins={pluginsFor(math)}
84
90
  components={
85
91
  components ? { ...MARKDOWN_COMPONENTS, ...components } : MARKDOWN_COMPONENTS
86
92
  }