@flamingo-stack/openframe-frontend-core 0.0.201 → 0.0.202

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.
Files changed (45) hide show
  1. package/dist/{chunk-CSW5GYBU.js → chunk-IDULPYOU.js} +3997 -3734
  2. package/dist/chunk-IDULPYOU.js.map +1 -0
  3. package/dist/{chunk-UCY537V4.cjs → chunk-JIKTMXTZ.cjs} +952 -689
  4. package/dist/chunk-JIKTMXTZ.cjs.map +1 -0
  5. package/dist/components/chat/approval-request-message.d.ts.map +1 -1
  6. package/dist/components/chat/chat-container.d.ts.map +1 -1
  7. package/dist/components/chat/chat-message-enhanced.d.ts.map +1 -1
  8. package/dist/components/chat/chat-message-list.d.ts.map +1 -1
  9. package/dist/components/chat/types/message.types.d.ts +34 -0
  10. package/dist/components/chat/types/message.types.d.ts.map +1 -1
  11. package/dist/components/features/index.cjs +14 -2
  12. package/dist/components/features/index.cjs.map +1 -1
  13. package/dist/components/features/index.d.ts +1 -0
  14. package/dist/components/features/index.d.ts.map +1 -1
  15. package/dist/components/features/index.js +15 -3
  16. package/dist/components/index.cjs +14 -2
  17. package/dist/components/index.cjs.map +1 -1
  18. package/dist/components/index.js +13 -1
  19. package/dist/components/navigation/index.cjs +2 -2
  20. package/dist/components/navigation/index.js +1 -1
  21. package/dist/components/providers/theme-provider.d.ts +69 -0
  22. package/dist/components/providers/theme-provider.d.ts.map +1 -0
  23. package/dist/components/ui/index.cjs +2 -2
  24. package/dist/components/ui/index.js +1 -1
  25. package/dist/components/ui/simple-markdown-renderer.d.ts.map +1 -1
  26. package/dist/index.cjs +14 -2
  27. package/dist/index.cjs.map +1 -1
  28. package/dist/index.js +13 -1
  29. package/package.json +2 -1
  30. package/src/components/chat/approval-request-message.tsx +106 -92
  31. package/src/components/chat/chat-container.tsx +8 -4
  32. package/src/components/chat/chat-message-enhanced.tsx +51 -9
  33. package/src/components/chat/chat-message-list.tsx +27 -19
  34. package/src/components/chat/types/message.types.ts +35 -0
  35. package/src/components/features/index.ts +15 -0
  36. package/src/components/providers/theme-provider.tsx +130 -0
  37. package/src/components/ui/simple-markdown-renderer.tsx +248 -2
  38. package/src/stories/Theme.stories.tsx +350 -0
  39. package/src/styles/README.md +271 -174
  40. package/src/styles/dark_theme.tokens.json +982 -0
  41. package/src/styles/light_theme.tokens.json +982 -0
  42. package/src/styles/ods-colors.css +225 -146
  43. package/src/styles/ods_color_tokens.json +1 -300
  44. package/dist/chunk-CSW5GYBU.js.map +0 -1
  45. package/dist/chunk-UCY537V4.cjs.map +0 -1
@@ -6,11 +6,77 @@ import { Button } from "../ui/button"
6
6
  import { Tag } from "../ui/tag"
7
7
  import { CheckCircle, XCircle } from "lucide-react"
8
8
  import type { ApprovalRequestMessageProps } from "./types"
9
+ import type { ApprovalRequestField } from "./types/message.types"
10
+
11
+ /**
12
+ * Stacked label/value rows for the approval card's structured field
13
+ * list. Labels are tiny uppercase muted text; values render as primary
14
+ * text with `whitespace-pre-wrap` so multi-line descriptions
15
+ * (`content`, `resolution`, etc.) keep their structure. Mirrored across
16
+ * the pending + resolved branches so an approved ticket reads the same
17
+ * way it did at decision time.
18
+ */
19
+ function ApprovalFieldList({ fields }: { fields: ApprovalRequestField[] }) {
20
+ return (
21
+ <dl className="flex flex-col gap-2.5 mt-1">
22
+ {fields.map((f, i) => (
23
+ <div key={i} className="flex flex-col gap-0.5">
24
+ <dt className="font-['DM_Sans'] font-semibold text-[11px] uppercase tracking-wide text-ods-text-tertiary leading-4">
25
+ {f.label}
26
+ </dt>
27
+ <dd className="font-['DM_Sans'] text-sm text-ods-text-primary leading-5 whitespace-pre-wrap break-words">
28
+ {f.value}
29
+ </dd>
30
+ </div>
31
+ ))}
32
+ </dl>
33
+ )
34
+ }
35
+
36
+ /**
37
+ * Shared body for both pending and resolved branches of
38
+ * `<ApprovalRequestMessage>`. The pending card adds Approve/Reject
39
+ * buttons below; the resolved card adds an Approved/Rejected `<Tag>`.
40
+ * Everything ABOVE the footer — command bar, icon, structured-fields
41
+ * stack, explanation paragraph — is identical, so the body lives here
42
+ * to prevent silent drift between the two render paths (a prior
43
+ * version already had a `break-words` vs `break-all` mismatch on the
44
+ * `<code>` element from an out-of-sync copy-paste edit).
45
+ */
46
+ function ApprovalCardBody({
47
+ data,
48
+ }: {
49
+ data: ApprovalRequestMessageProps['data']
50
+ }) {
51
+ return (
52
+ <div className="flex flex-col gap-1">
53
+ <div className="bg-ods-bg border border-ods-border rounded-md p-3 flex gap-2 items-start max-h-32 overflow-y-auto">
54
+ <code className="font-['DM_Sans'] font-medium text-sm text-ods-text-primary flex-1 leading-5 whitespace-pre-wrap break-words">
55
+ {data.command}
56
+ </code>
57
+ {data.icon && (
58
+ <div className="w-4 h-4 shrink-0 text-ods-text-tertiary">
59
+ {data.icon}
60
+ </div>
61
+ )}
62
+ </div>
63
+ {data.fields && data.fields.length > 0 ? (
64
+ <ApprovalFieldList fields={data.fields} />
65
+ ) : (
66
+ data.explanation && (
67
+ <p className="font-['DM_Sans'] font-medium text-sm text-ods-text-secondary leading-5 whitespace-pre-line break-words">
68
+ {data.explanation}
69
+ </p>
70
+ )
71
+ )}
72
+ </div>
73
+ )
74
+ }
9
75
 
10
76
  const ApprovalRequestMessage = forwardRef<HTMLDivElement, ApprovalRequestMessageProps>(
11
77
  ({ className, data, onApprove, onReject, status = 'pending', ...props }, ref) => {
12
78
  const [isProcessing, setIsProcessing] = useState(false)
13
-
79
+
14
80
  const handleApprove = async () => {
15
81
  setIsProcessing(true)
16
82
  try {
@@ -28,49 +94,7 @@ const ApprovalRequestMessage = forwardRef<HTMLDivElement, ApprovalRequestMessage
28
94
  setIsProcessing(false)
29
95
  }
30
96
  }
31
-
32
- if (status !== 'pending') {
33
- return (
34
- <div
35
- ref={ref}
36
- className={cn(
37
- "bg-ods-card border border-ods-border rounded-md p-4 mb-2 flex flex-col gap-4",
38
- className
39
- )}
40
- {...props}
41
- >
42
- {/* Command and icon section */}
43
- <div className="flex flex-col gap-1">
44
- <div className="bg-ods-bg border border-ods-border rounded-md p-3 flex gap-2 items-start max-h-32 overflow-y-auto">
45
- <code className="font-['DM_Sans'] font-medium text-sm text-ods-text-primary flex-1 leading-5 whitespace-pre-wrap break-words">
46
- {data.command}
47
- </code>
48
- {data.icon && (
49
- <div className="w-4 h-4 shrink-0 text-ods-text-tertiary">
50
- {data.icon}
51
- </div>
52
- )}
53
- </div>
54
-
55
- {data.explanation && (
56
- <p className="font-['DM_Sans'] font-medium text-sm text-ods-text-secondary leading-5 whitespace-pre-line break-words">
57
- {data.explanation}
58
- </p>
59
- )}
60
- </div>
61
-
62
- {/* Status indicator */}
63
- <div className="flex">
64
- <Tag
65
- label={status === 'approved' ? 'Approved' : 'Rejected'}
66
- variant={status === 'approved' ? 'success' : 'error'}
67
- icon={status === 'approved' ? <CheckCircle className="w-4 h-4" /> : <XCircle className="w-4 h-4" />}
68
- />
69
- </div>
70
- </div>
71
- )
72
- }
73
-
97
+
74
98
  return (
75
99
  <div
76
100
  ref={ref}
@@ -80,55 +104,45 @@ const ApprovalRequestMessage = forwardRef<HTMLDivElement, ApprovalRequestMessage
80
104
  )}
81
105
  {...props}
82
106
  >
83
- {/* Command and icon section */}
84
- <div className="flex flex-col gap-1">
85
- <div className="bg-ods-bg border border-ods-border rounded-md p-3 flex gap-2 items-start max-h-32 overflow-y-auto">
86
- <code className="font-['DM_Sans'] font-medium text-sm text-ods-text-primary flex-1 leading-5 whitespace-pre-wrap break-all">
87
- {data.command}
88
- </code>
89
- {data.icon && (
90
- <div className="w-4 h-4 shrink-0 text-ods-text-tertiary">
91
- {data.icon}
92
- </div>
93
- )}
107
+ <ApprovalCardBody data={data} />
108
+ {status === 'pending' ? (
109
+ <div className="flex gap-4 items-center">
110
+ <Button
111
+ size="small-legacy"
112
+ variant="accent"
113
+ onClick={handleApprove}
114
+ disabled={isProcessing}
115
+ className={cn(
116
+ "bg-ods-accent hover:bg-ods-accent/90",
117
+ "font-mono font-medium md:!text-sm text-ods-bg uppercase tracking-[-0.28px]",
118
+ "px-2 py-1 h-auto"
119
+ )}
120
+ >
121
+ Approve
122
+ </Button>
123
+ <Button
124
+ size="small-legacy"
125
+ variant="outline"
126
+ onClick={handleReject}
127
+ disabled={isProcessing}
128
+ className={cn(
129
+ "bg-ods-card border-ods-border",
130
+ "font-mono font-medium md:!text-sm text-ods-text-primary uppercase tracking-[-0.28px]",
131
+ "hover:bg-ods-bg px-2 py-1 h-auto"
132
+ )}
133
+ >
134
+ Reject
135
+ </Button>
94
136
  </div>
95
-
96
- {data.explanation && (
97
- <p className="font-['DM_Sans'] font-medium text-sm text-ods-text-secondary leading-5">
98
- {data.explanation}
99
- </p>
100
- )}
101
- </div>
102
-
103
- {/* Approve/Reject buttons */}
104
- <div className="flex gap-4 items-center">
105
- <Button
106
- size="small-legacy"
107
- variant="accent"
108
- onClick={handleApprove}
109
- disabled={isProcessing}
110
- className={cn(
111
- "bg-ods-accent hover:bg-ods-accent/90",
112
- "font-mono font-medium md:!text-sm text-ods-bg uppercase tracking-[-0.28px]",
113
- "px-2 py-1 h-auto"
114
- )}
115
- >
116
- Approve
117
- </Button>
118
- <Button
119
- size="small-legacy"
120
- variant="outline"
121
- onClick={handleReject}
122
- disabled={isProcessing}
123
- className={cn(
124
- "bg-ods-card border-ods-border",
125
- "font-mono font-medium md:!text-sm text-ods-text-primary uppercase tracking-[-0.28px]",
126
- "hover:bg-ods-bg px-2 py-1 h-auto"
127
- )}
128
- >
129
- Reject
130
- </Button>
131
- </div>
137
+ ) : (
138
+ <div className="flex">
139
+ <Tag
140
+ label={status === 'approved' ? 'Approved' : 'Rejected'}
141
+ variant={status === 'approved' ? 'success' : 'error'}
142
+ icon={status === 'approved' ? <CheckCircle className="w-4 h-4" /> : <XCircle className="w-4 h-4" />}
143
+ />
144
+ </div>
145
+ )}
132
146
  </div>
133
147
  )
134
148
  }
@@ -11,14 +11,18 @@ import type { ConnectionIndicatorProps, ChatContainerProps, ChatHeaderProps } fr
11
11
  const ConnectionIndicator: React.FC<ConnectionIndicatorProps> = ({ status }) => {
12
12
  const getStatusStyles = () => {
13
13
  switch (status) {
14
+ // ODS attention tokens — same scheme used by the rest of the chat
15
+ // shell (StatusBadge, error toast, etc.). Hex Tailwind palette
16
+ // (`bg-green-500` / `bg-red-500`) would diverge from the theme and
17
+ // is forbidden by the host's design-token policy.
14
18
  case 'connected':
15
- return 'bg-green-500'
19
+ return 'bg-ods-attention-green-success'
16
20
  case 'connecting':
17
- return 'bg-yellow-500 animate-pulse'
21
+ return 'bg-ods-attention-yellow-warning animate-pulse'
18
22
  case 'disconnected':
19
- return 'bg-red-500'
23
+ return 'bg-ods-attention-red-error'
20
24
  default:
21
- return 'bg-gray-500'
25
+ return 'bg-ods-text-tertiary'
22
26
  }
23
27
  }
24
28
 
@@ -111,8 +111,32 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
111
111
  // returns the cached node so React reuses the same instance.
112
112
  let rendered = seenRendered.get(key)
113
113
  if (!seenRendered.has(key)) {
114
+ // Always invoke render() — even when the metadata map has
115
+ // no entry for this marker. Fetch-mode card types
116
+ // (delivery_item, roadmap_item, internal_task, etc.) don't
117
+ // ship metadata in the SSE frame; they self-fetch by `id`
118
+ // via the host's list-API hook, so a minimal {type,id}
119
+ // ChatRef is all the renderer needs to mount the loader.
120
+ // For no-fetch types (hubspot_ticket_self, slack_message,
121
+ // …) without a refMatch the host's render() returns null
122
+ // and we fall through to the bare-cardId fallback in the
123
+ // `<a card://…>` override below.
124
+ //
125
+ // SYNTHETIC REF DEFAULTS: `ChatRef.title` and `ChatRef.url`
126
+ // are non-optional in the type; a bare `{type, id}` cast
127
+ // would lie to consumers that read those fields. Default
128
+ // `title` to the cardId (so any host renderer that prints
129
+ // `ref.title` shows the id rather than `undefined`) and
130
+ // `url` to null (matches the no-link semantics fetch-mode
131
+ // cards rely on — they resolve their own URL after fetch).
114
132
  const refMatch = refs[key]
115
- rendered = refMatch ? render(refMatch) : undefined
133
+ const refForRender: ChatRef = refMatch ?? {
134
+ type: cardType,
135
+ id: cardId,
136
+ title: cardId,
137
+ url: null,
138
+ }
139
+ rendered = render(refForRender)
116
140
  seenRendered.set(key, rendered)
117
141
  }
118
142
  if (React.isValidElement(rendered) && rendered.type === BlockCard) {
@@ -171,15 +195,33 @@ const ChatMessageEnhanced = forwardRef<HTMLDivElement, ChatMessageEnhancedProps>
171
195
  const key = `${cardType}:${cardId}`
172
196
  const inline = inlineByKey?.get(key)
173
197
  if (inline != null) return inline
174
- // No renderer, no ref, OR renderer returned null fall back
175
- // to plain text title-only. Use any same-type ref's title if
176
- // available; otherwise the bare cardId. Never render the
177
- // literal `card://` URL.
198
+ // Three fallback cases keep them DISTINCT, never blur them
199
+ // together. Mixing them up (which the old code did by reaching
200
+ // for "any same-type ref's title") makes LLM hallucinations
201
+ // LOOK like real cards, which the user can't tell apart from
202
+ // genuine references.
203
+ //
204
+ // (1) Exact ref present, renderer returned null:
205
+ // The marker is legit (server confirmed the row exists)
206
+ // but no compact-card type is registered for `cardType`.
207
+ // Render the ref's REAL title as plain text — accurate,
208
+ // just no rich UI.
209
+ //
210
+ // (2) Exact ref absent (refs map has no `${cardType}:${cardId}`):
211
+ // The LLM emitted a marker for an ID the server did NOT
212
+ // surface. Either the LLM hallucinated the id, or the
213
+ // refs map and the snapshot drifted (server-side bug
214
+ // worth fixing — see `MAX_ROWS_PER_ENTITY_GROUP` in
215
+ // `doc-chat-utils.ts:buildSourcesMeta`). Render the raw
216
+ // `cardId` so the breakage is VISIBLE; never borrow a
217
+ // title from an unrelated ref — that hides the bug and
218
+ // deceives the reader into thinking they're looking at
219
+ // a real card.
178
220
  const refMatch: ChatRef | undefined = refs[key]
179
- const fallbackTitle = (refMatch?.title)
180
- ?? Object.values(refs).find((r) => r.type === cardType)?.title
181
- ?? cardId
182
- return <span className="text-ods-text-primary">{fallbackTitle}</span>
221
+ if (refMatch) {
222
+ return <span className="text-ods-text-primary">{refMatch.title}</span>
223
+ }
224
+ return <span className="text-ods-text-secondary opacity-60">{cardId}</span>
183
225
  }
184
226
  }
185
227
  // Unified click rule — delegated to the host's `NavLinkAnchor`
@@ -441,25 +441,33 @@ const ChatMessageList = forwardRef<HTMLDivElement, ChatMessageListProps>(
441
441
  <div ref={sentinelRef} className="h-px" />
442
442
  )}
443
443
  <div className="flex-1" />
444
- {messages.map((message, index) => (
445
- <ChatMessageEnhanced
446
- key={message.id}
447
- ref={getRegisterMessageEl(message.id)}
448
- role={message.role}
449
- name={message.name}
450
- content={message.content}
451
- timestamp={message.timestamp}
452
- isTyping={index === messages.length - 1 && isTyping && message.role === 'assistant'}
453
- avatar={showAvatars ? message.avatar : null}
454
- showAvatar={showAvatars}
455
- assistantType={message.assistantType || assistantType}
456
- authorType={message.authorType}
457
- assistantIcon={message.role !== 'user' ? assistantIcon : undefined}
458
- chatRefs={message.chatRefs}
459
- renderEntityCard={renderEntityCard}
460
- NavLinkAnchor={NavLinkAnchor}
461
- />
462
- ))}
444
+ {messages.map((message, index) => {
445
+ // Hidden messages (synthetic continuation prompts the host
446
+ // injects after an approval card) are part of the API
447
+ // conversation history but never render. Skipping here
448
+ // keeps the visible thread coherent — see
449
+ // `Message.hidden` doc-comment in message.types.ts.
450
+ if (message.hidden) return null
451
+ return (
452
+ <ChatMessageEnhanced
453
+ key={message.id}
454
+ ref={getRegisterMessageEl(message.id)}
455
+ role={message.role}
456
+ name={message.name}
457
+ content={message.content}
458
+ timestamp={message.timestamp}
459
+ isTyping={index === messages.length - 1 && isTyping && message.role === 'assistant'}
460
+ avatar={showAvatars ? message.avatar : null}
461
+ showAvatar={showAvatars}
462
+ assistantType={message.assistantType || assistantType}
463
+ authorType={message.authorType}
464
+ assistantIcon={message.role !== 'user' ? assistantIcon : undefined}
465
+ chatRefs={message.chatRefs}
466
+ renderEntityCard={renderEntityCard}
467
+ NavLinkAnchor={NavLinkAnchor}
468
+ />
469
+ )
470
+ })}
463
471
  </div>
464
472
  </div>
465
473
 
@@ -78,8 +78,24 @@ export interface ExecutingToolState {
78
78
 
79
79
  // ========== Approval Request Types ==========
80
80
 
81
+ export interface ApprovalRequestField {
82
+ /** Short label — e.g. "Subject", "Priority". Rendered in a muted
83
+ * caps style above the value. */
84
+ label: string
85
+ /** Free-text value. Wraps and line-breaks are preserved
86
+ * (`whitespace-pre-wrap`). */
87
+ value: string
88
+ }
89
+
81
90
  export interface ApprovalRequestData {
82
91
  command: string
92
+ /** Structured field list — preferred over `explanation`. When set,
93
+ * the approval card renders a vertical label/value stack with
94
+ * proper spacing. Falls back to `explanation` (a single paragraph)
95
+ * when omitted. Keep BOTH when you want hosts on older lib
96
+ * versions to still see the prose; new hosts should send only
97
+ * `fields`. */
98
+ fields?: ApprovalRequestField[]
83
99
  explanation?: string
84
100
  icon?: React.ReactNode
85
101
  requestId?: string
@@ -333,4 +349,23 @@ export interface Message {
333
349
  * whose body is a long article). The server is the sole decision-
334
350
  * maker — set on the metadata leading frame. */
335
351
  scrollAnchor?: ScrollAnchor
352
+ /** When true the message is part of the API conversation history (sent
353
+ * to the LLM so it has context) but is NOT rendered in the chat UI.
354
+ *
355
+ * Used for "synthetic continuation" turns: when the user clicks Approve
356
+ * on a tool proposal, the host auto-fires a follow-up `sendMessage`
357
+ * with `hidden: true` carrying a directive like "the user just
358
+ * approved <tool>; ask follow-up questions per protocol". The LLM's
359
+ * response IS rendered (as a normal assistant message); only the
360
+ * trigger prompt is suppressed so the chat reads naturally:
361
+ *
362
+ * user: "open a ticket"
363
+ * assistant: preamble + approval card
364
+ * [user clicks Approve]
365
+ * assistant: "Now to triage faster, can you share..." ← auto-fires
366
+ *
367
+ * Without this flag the trigger prompt would surface as a confusing
368
+ * bubble like "(continue per protocol)" between the approval card
369
+ * and the AI's follow-up. */
370
+ hidden?: boolean
336
371
  }
@@ -2,6 +2,21 @@
2
2
 
3
3
  // Feature Components exports
4
4
  export { DynamicThemeProvider, useDynamicTheme } from '../providers/dynamic-theme-provider'
5
+ // Canonical ODS theme system — thin wrapper over `next-themes`
6
+ // (manual light/dark, default dark, no-flash handled by next-themes).
7
+ // Headless by design: apps build their own toggle button via the lib's
8
+ // existing <Button> + `useThemeToggle()`.
9
+ export {
10
+ ThemeProvider,
11
+ useTheme,
12
+ useThemeToggle,
13
+ THEME_STORAGE_KEY,
14
+ THEME_ATTRIBUTE,
15
+ DEFAULT_THEME,
16
+ type Theme,
17
+ type ThemeProviderProps,
18
+ type UseThemeToggleResult,
19
+ } from '../providers/theme-provider'
5
20
  export * from './array-entry-manager'
6
21
  export * from './auth-providers-list'
7
22
  export * from './changelog-manager'
@@ -0,0 +1,130 @@
1
+ "use client";
2
+
3
+ import * as React from "react";
4
+ import {
5
+ ThemeProvider as NextThemesProvider,
6
+ useTheme as useNextTheme,
7
+ } from "next-themes";
8
+ import type { ThemeProviderProps as NextThemesProviderProps } from "next-themes/dist/types";
9
+
10
+ /**
11
+ * ODS theme system — thin wrapper over `next-themes`.
12
+ *
13
+ * We deliberately do NOT hand-roll a provider/no-flash script: `next-themes`
14
+ * is already a dependency, is battle-tested in Next.js (App & Pages router),
15
+ * injects its own pre-paint anti-flash script, handles SSR + localStorage +
16
+ * cross-tab sync, and also works in plain React (Vite/Tauri).
17
+ *
18
+ * Product model (locked): a MANUAL light/dark switch, default DARK,
19
+ * persisted to localStorage. No "system" mode (`enableSystem={false}`).
20
+ *
21
+ * Drives styling by setting `data-theme="light|dark"` on <html>;
22
+ * `src/styles/ods-colors.css` swaps the `--ods-*` primitives accordingly.
23
+ *
24
+ * Public API exposed by this module:
25
+ * • `<ThemeProvider>` — preconfigured next-themes provider.
26
+ * • `useTheme()` — raw next-themes hook (advanced cases).
27
+ * • `useThemeToggle()`— headless convenience hook for building toggle UI
28
+ * in consumer apps (no styled component on purpose;
29
+ * apps own their button visuals via the lib's
30
+ * existing `<Button>`).
31
+ */
32
+
33
+ export type Theme = "light" | "dark";
34
+
35
+ export const THEME_STORAGE_KEY = "ods-theme";
36
+ export const THEME_ATTRIBUTE = "data-theme";
37
+ export const DEFAULT_THEME: Theme = "dark";
38
+
39
+ export type ThemeProviderProps = Partial<NextThemesProviderProps>;
40
+
41
+ /**
42
+ * Pre-configured provider. Wrap the app once (Next.js: in the root layout;
43
+ * apps must keep `suppressHydrationWarning` on <html> — already the case).
44
+ * No `<ThemeScript>` needed — next-themes handles the no-flash script.
45
+ *
46
+ * All next-themes props are overridable, but the ODS defaults below encode
47
+ * the product decision.
48
+ */
49
+ export function ThemeProvider({ children, ...overrides }: ThemeProviderProps) {
50
+ return (
51
+ <NextThemesProvider
52
+ attribute={THEME_ATTRIBUTE}
53
+ defaultTheme={DEFAULT_THEME}
54
+ enableSystem={false}
55
+ themes={["light", "dark"]}
56
+ storageKey={THEME_STORAGE_KEY}
57
+ disableTransitionOnChange={false}
58
+ {...overrides}
59
+ >
60
+ {children}
61
+ </NextThemesProvider>
62
+ );
63
+ }
64
+
65
+ /**
66
+ * Re-export of next-themes' `useTheme` (no custom logic on top).
67
+ * Returns `{ theme, setTheme, resolvedTheme, themes, ... }`.
68
+ */
69
+ export const useTheme = useNextTheme;
70
+
71
+ /* ------------------------------------------------------------------ */
72
+ /* useThemeToggle — headless convenience for building toggle UIs */
73
+ /* ------------------------------------------------------------------ */
74
+
75
+ export interface UseThemeToggleResult {
76
+ /** Becomes `true` after the client has hydrated and resolved the stored
77
+ * preference. Until then, `theme`/`isDark`/`isLight` reflect the SSR
78
+ * default (`DEFAULT_THEME`) — handy for rendering a stable placeholder. */
79
+ mounted: boolean;
80
+ /** Resolved active theme (`"dark"` until mounted, then real value). */
81
+ theme: Theme;
82
+ isDark: boolean;
83
+ isLight: boolean;
84
+ /** Flip dark↔light and persist. */
85
+ toggle: () => void;
86
+ /** Set explicitly to `"light"` or `"dark"` and persist. */
87
+ setTheme: (theme: Theme) => void;
88
+ }
89
+
90
+ /**
91
+ * Headless toggle helper. Build any button you like:
92
+ *
93
+ * const { isDark, toggle, mounted } = useThemeToggle()
94
+ * <Button size="icon" variant="transparent" onClick={toggle} aria-label="…">
95
+ * {mounted && (isDark ? <Sun01Icon /> : <MoonIcon />)}
96
+ * </Button>
97
+ *
98
+ * The mount gate avoids hydration mismatch (next-themes only knows the
99
+ * persisted preference on the client after mount).
100
+ */
101
+ export function useThemeToggle(): UseThemeToggleResult {
102
+ const { resolvedTheme, theme, setTheme } = useTheme();
103
+ const [mounted, setMounted] = React.useState(false);
104
+ React.useEffect(() => setMounted(true), []);
105
+
106
+ const active: Theme = mounted
107
+ ? resolvedTheme === "light" || theme === "light"
108
+ ? "light"
109
+ : "dark"
110
+ : DEFAULT_THEME;
111
+
112
+ const setOdsTheme = React.useCallback(
113
+ (next: Theme) => setTheme(next),
114
+ [setTheme],
115
+ );
116
+
117
+ const toggle = React.useCallback(
118
+ () => setTheme(active === "dark" ? "light" : "dark"),
119
+ [active, setTheme],
120
+ );
121
+
122
+ return {
123
+ mounted,
124
+ theme: active,
125
+ isDark: active === "dark",
126
+ isLight: active === "light",
127
+ toggle,
128
+ setTheme: setOdsTheme,
129
+ };
130
+ }