sanity-plugin-seo-copilot 0.1.2 → 0.2.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.
@@ -0,0 +1,370 @@
1
+ import { useState, useRef, useLayoutEffect, useEffect } from "react";
2
+ import { PANEL_BG, MJH_BLUE } from "./styles";
3
+ import type { TabId } from "../lib/types";
4
+
5
+ interface TabDef {
6
+ id: TabId;
7
+ label: string;
8
+ loading?: boolean;
9
+ badge?: number;
10
+ /** When true, the tab is styled as a "hub" landmark (not the regular flow). */
11
+ isHub?: boolean;
12
+ /** When true, a small beta marker is shown next to the label. */
13
+ beta?: boolean;
14
+ }
15
+
16
+ // Small BETA pill shown next to the label. Tabs are equal-width and wrap
17
+ // vertically when the panel is narrow (see button styles), so all six stay
18
+ // visible and the pill never pushes a tab off-screen.
19
+ function BetaPill({ active }: { active?: boolean }) {
20
+ return (
21
+ <span
22
+ title="Beta - this tab references Sanity fields and related articles that may need configuration in your live environment, so it needs more testing"
23
+ style={{
24
+ display: "inline-block",
25
+ fontSize: 7.5,
26
+ fontWeight: 700,
27
+ letterSpacing: "0.03em",
28
+ padding: "1px 3px",
29
+ borderRadius: 3,
30
+ // On the active tab the blue glider sits behind, so flip to a white chip
31
+ // (blue text) to stay legible; off the glider it's a blue-tint chip.
32
+ background: active ? "rgba(255,255,255,0.92)" : "rgba(0,93,172,0.14)",
33
+ color: MJH_BLUE,
34
+ textTransform: "uppercase",
35
+ lineHeight: 1.1,
36
+ verticalAlign: "middle",
37
+ marginLeft: 3,
38
+ whiteSpace: "nowrap",
39
+ }}
40
+ >
41
+ Beta
42
+ </span>
43
+ );
44
+ }
45
+
46
+ // Renders a tab label so it only ever wraps between whole segments, never inside
47
+ // a word. A single word ("Summary", "Keywords") never splits. "AEO/GEO" wraps as
48
+ // "AEO/" / "GEO" - the slash STAYS on the first line with the preceding text (a
49
+ // line never begins with a slash; see brand kit "Label line-breaking"). The Beta
50
+ // pill drops onto its own line (below the label) before any word would break.
51
+ function TabLabel({ label, beta, active }: { label: string; beta?: boolean; active?: boolean }) {
52
+ let body: React.ReactNode;
53
+ const slash = label.indexOf("/");
54
+ if (slash > 0) {
55
+ const head = label.slice(0, slash + 1); // "AEO/" - slash stays on the first line
56
+ const tail = label.slice(slash + 1); // "GEO"
57
+ body = (
58
+ <>
59
+ <span style={{ whiteSpace: "nowrap" }}>{head}</span>
60
+ <wbr />
61
+ <span style={{ whiteSpace: "nowrap" }}>{tail}</span>
62
+ </>
63
+ );
64
+ } else {
65
+ // Single word stays whole (no break point inside it); a two-word label like
66
+ // "Other Recs" wraps at its space naturally.
67
+ body = label;
68
+ }
69
+ return (
70
+ <span>
71
+ {body}
72
+ {beta && (
73
+ <>
74
+ <wbr />
75
+ <BetaPill active={active} />
76
+ </>
77
+ )}
78
+ </span>
79
+ );
80
+ }
81
+
82
+ interface TabBarProps {
83
+ tabs: TabDef[];
84
+ activeTab: TabId;
85
+ onTabChange: (id: TabId) => void;
86
+ }
87
+
88
+ // Tab-bar text colors are tuned for the pale-gold PANEL_BG surface - translucent
89
+ // black reads as "ink" and stays legible where a light gray would wash out.
90
+ const ACTIVE_INK = "#1f2937";
91
+ const HUB_INK = "rgba(0,0,0,0.7)";
92
+ const INACTIVE_INK = "rgba(0,0,0,0.52)";
93
+
94
+ function HubGlyph({ active }: { active: boolean }) {
95
+ // 4-cell grid icon - reads as "overview / dashboard" without being heavy-handed.
96
+ // White when active (it sits on the blue glider), muted ink otherwise.
97
+ const fill = active ? "#ffffff" : HUB_INK;
98
+ return (
99
+ <svg
100
+ width="11"
101
+ height="11"
102
+ viewBox="0 0 12 12"
103
+ fill="none"
104
+ style={{ flexShrink: 0 }}
105
+ aria-hidden="true"
106
+ >
107
+ <rect x="1" y="1" width="4" height="4" rx="0.8" fill={fill} />
108
+ <rect x="7" y="1" width="4" height="4" rx="0.8" fill={fill} opacity="0.55" />
109
+ <rect x="1" y="7" width="4" height="4" rx="0.8" fill={fill} opacity="0.55" />
110
+ <rect x="7" y="7" width="4" height="4" rx="0.8" fill={fill} />
111
+ </svg>
112
+ );
113
+ }
114
+
115
+ interface IndicatorBox {
116
+ left: number;
117
+ top: number;
118
+ width: number;
119
+ height: number;
120
+ ready: boolean;
121
+ // Slide duration in ms, scaled by how far the indicator travels: a long
122
+ // Summary->Meta jump takes LONGER so it glides smoothly instead of whipping
123
+ // across; a short neighbour hop stays snappy.
124
+ duration: number;
125
+ }
126
+
127
+ export function TabBar({ tabs, activeTab, onTabChange }: TabBarProps) {
128
+ const [hoveredId, setHoveredId] = useState<TabId | null>(null);
129
+ const barRef = useRef<HTMLDivElement>(null);
130
+ const tabRefs = useRef<Map<TabId, HTMLButtonElement>>(new Map());
131
+ const prevActiveRef = useRef<TabId>(activeTab);
132
+ // The sliding "liquid glass" indicator that marks the active tab. We measure
133
+ // the active button's box relative to the bar and animate to it - this works
134
+ // with the responsive wrapping because we read the real rendered box (which
135
+ // may sit on a second row on a narrow panel) rather than assuming a layout.
136
+ const [ind, setInd] = useState<IndicatorBox>({ left: 0, top: 0, width: 0, height: 0, ready: false, duration: 360 });
137
+ // While the panel is actively being resized we track the active tab INSTANTLY
138
+ // (no slide/bounce) so the indicator doesn't wobble during a drag. A click sets
139
+ // this false, so selecting a tab always slides + bounces. Crucially the
140
+ // transition is NEVER "none" (it only swaps bounce <-> instant) - that is what
141
+ // keeps every click animating; the old "none" gate was getting reset by a
142
+ // reflow on tab-switch, which is why clicks were snapping with zero slide.
143
+ const [resizing, setResizing] = useState(false);
144
+ // True for a beat right after a click, so a click-induced reflow (a scrollbar
145
+ // appearing as tab content changes) isn't mistaken for a resize and doesn't
146
+ // cancel the slide that's mid-flight.
147
+ const justClickedRef = useRef(false);
148
+ const clickTimerRef = useRef<ReturnType<typeof setTimeout> | null>(null);
149
+ const resizeSettleRef = useRef<ReturnType<typeof setTimeout> | null>(null);
150
+
151
+ // Signature changes whenever something that can move the active tab changes
152
+ // (label wrap, loading dot, beta pill), so the indicator re-measures.
153
+ const signature = tabs.map((t) => `${t.id}:${t.label}:${t.loading ? 1 : 0}:${t.beta ? 1 : 0}`).join("|");
154
+
155
+ useLayoutEffect(() => {
156
+ const bar = barRef.current;
157
+ const el = tabRefs.current.get(activeTab);
158
+ if (!bar || !el) return;
159
+ const br = bar.getBoundingClientRect();
160
+ const er = el.getBoundingClientRect();
161
+ const next = {
162
+ left: er.left - br.left + bar.scrollLeft,
163
+ top: er.top - br.top + bar.scrollTop,
164
+ width: er.width,
165
+ height: er.height,
166
+ };
167
+ const changed = prevActiveRef.current !== activeTab;
168
+ prevActiveRef.current = activeTab;
169
+ if (changed) {
170
+ // A real selection: drop out of resize mode so this move slides + bounces,
171
+ // and shield the next beat so a reflow can't cancel the slide.
172
+ setResizing(false);
173
+ justClickedRef.current = true;
174
+ if (clickTimerRef.current) clearTimeout(clickTimerRef.current);
175
+ clickTimerRef.current = setTimeout(() => { justClickedRef.current = false; }, 650);
176
+ }
177
+ setInd((prev) => {
178
+ if (prev.left === next.left && prev.top === next.top && prev.width === next.width && prev.height === next.height && prev.ready) {
179
+ return prev;
180
+ }
181
+ // Scale the slide time to the travel distance: ~constant perceived speed,
182
+ // clamped so a neighbour hop is snappy (~280ms) and the longest jump glides
183
+ // (~560ms) rather than darting across. Fixes "slides way too fast" on long jumps.
184
+ const dist = Math.hypot(next.left - prev.left, next.top - prev.top);
185
+ const duration = Math.min(560, Math.max(280, Math.round(250 + dist * 0.6)));
186
+ return { ...next, ready: true, duration };
187
+ });
188
+ }, [activeTab, signature]);
189
+
190
+ // Re-measure when the bar resizes (panel resize -> tabs may reflow / wrap).
191
+ useEffect(() => {
192
+ const bar = barRef.current;
193
+ if (!bar || typeof ResizeObserver === "undefined") return;
194
+ const ro = new ResizeObserver(() => {
195
+ // A reflow caused by a just-happened click isn't a resize - let the slide
196
+ // that's already in flight finish instead of snapping it.
197
+ if (justClickedRef.current) return;
198
+ const el = tabRefs.current.get(activeTab);
199
+ if (!el) return;
200
+ const br = bar.getBoundingClientRect();
201
+ const er = el.getBoundingClientRect();
202
+ // Track instantly while resizing, then settle back to sliding so the next
203
+ // click bounces again.
204
+ setResizing(true);
205
+ if (resizeSettleRef.current) clearTimeout(resizeSettleRef.current);
206
+ resizeSettleRef.current = setTimeout(() => setResizing(false), 160);
207
+ setInd((prev) => ({
208
+ left: er.left - br.left + bar.scrollLeft,
209
+ top: er.top - br.top + bar.scrollTop,
210
+ width: er.width,
211
+ height: er.height,
212
+ ready: true,
213
+ duration: prev.duration,
214
+ }));
215
+ });
216
+ ro.observe(bar);
217
+ return () => {
218
+ ro.disconnect();
219
+ if (resizeSettleRef.current) clearTimeout(resizeSettleRef.current);
220
+ };
221
+ }, [activeTab]);
222
+
223
+ return (
224
+ <div
225
+ ref={barRef}
226
+ style={{
227
+ position: "relative",
228
+ display: "flex",
229
+ alignItems: "stretch",
230
+ background: PANEL_BG,
231
+ borderBottom: "1px solid rgba(0,0,0,0.14)",
232
+ overflowX: "auto",
233
+ overflowY: "hidden",
234
+ flexShrink: 0,
235
+ }}
236
+ role="tablist"
237
+ onMouseLeave={() => setHoveredId(null)}
238
+ >
239
+ {/* Sliding liquid-glass active indicator (behind the tab labels). */}
240
+ {ind.ready && (
241
+ <div
242
+ aria-hidden="true"
243
+ style={{
244
+ position: "absolute",
245
+ left: ind.left,
246
+ top: ind.top,
247
+ width: ind.width,
248
+ height: ind.height,
249
+ borderRadius: 9,
250
+ background: "linear-gradient(135deg, rgba(0,93,172,0.62), rgba(0,93,172,0.95))",
251
+ backdropFilter: "blur(16px) saturate(170%)",
252
+ WebkitBackdropFilter: "blur(16px) saturate(170%)",
253
+ // Beveled glass: top-left light edge + bottom-right inner shadow + a soft blue glow.
254
+ boxShadow:
255
+ "inset 1px 1px 3px rgba(255,255,255,0.4), inset -1px -1px 5px rgba(0,0,0,0.28), 0 0 14px rgba(0,93,172,0.40), 0 2px 8px rgba(0,0,0,0.15)",
256
+ // Always a DEFINED transition (never "none") - that's what keeps every
257
+ // click animating. Normally: a real slide with a gentle bounce that lands
258
+ // on arrival (ease-out-back), for the distance-scaled duration so long
259
+ // jumps glide instead of darting. While resizing: 0ms = track instantly
260
+ // so the indicator doesn't wobble during a drag.
261
+ transition: resizing
262
+ ? "left 0ms linear, top 0ms linear, width 0ms linear, height 0ms linear"
263
+ : `left ${ind.duration}ms cubic-bezier(0.34,1.5,0.64,1), top ${ind.duration}ms cubic-bezier(0.34,1.5,0.64,1), width ${Math.round(ind.duration * 0.82)}ms cubic-bezier(0.22,1,0.36,1), height ${Math.round(ind.duration * 0.82)}ms cubic-bezier(0.22,1,0.36,1)`,
264
+ pointerEvents: "none",
265
+ zIndex: 0,
266
+ }}
267
+ />
268
+ )}
269
+ {tabs.map((tab, index) => {
270
+ const isActive = tab.id === activeTab;
271
+ const nextTab = tabs[index + 1];
272
+ // Visual separator: after a hub tab, before a non-hub tab
273
+ const showDivider = tab.isHub && nextTab && !nextTab.isHub;
274
+ return (
275
+ <div key={tab.id} style={{ display: "flex", alignItems: "stretch", flex: "1 1 auto" }}>
276
+ <button
277
+ ref={(el) => {
278
+ if (el) tabRefs.current.set(tab.id, el);
279
+ else tabRefs.current.delete(tab.id);
280
+ }}
281
+ role="tab"
282
+ aria-selected={isActive}
283
+ onClick={() => onTabChange(tab.id)}
284
+ onMouseEnter={() => setHoveredId(tab.id)}
285
+ style={{
286
+ position: "relative",
287
+ zIndex: 1,
288
+ flex: 1,
289
+ margin: 3,
290
+ padding: "5px 4px",
291
+ borderRadius: 9,
292
+ // The active highlight is the sliding indicator behind the tab, so the
293
+ // active button stays transparent. Hover gets a faint tint; the hub
294
+ // keeps its soft landmark fill when it isn't the active tab.
295
+ background:
296
+ !isActive && hoveredId === tab.id
297
+ ? "rgba(255,255,255,0.4)"
298
+ : !isActive && tab.isHub
299
+ ? "rgba(255,255,255,0.55)"
300
+ : "transparent",
301
+ border: "none",
302
+ cursor: "pointer",
303
+ fontSize: 10.5,
304
+ fontWeight: isActive ? 700 : tab.isHub ? 600 : 500,
305
+ color: isActive ? "#ffffff" : tab.isHub ? HUB_INK : INACTIVE_INK,
306
+ letterSpacing: "0.005em",
307
+ // normal (not nowrap) lets a multi-word label wrap at its space or
308
+ // after the slash on a narrow panel (e.g. "AEO/" over "GEO"). No
309
+ // break-word, so a single word like "Summary" never splits mid-word.
310
+ whiteSpace: "normal",
311
+ textAlign: "center",
312
+ lineHeight: 1.25,
313
+ transition: "color 150ms, background 150ms",
314
+ fontFamily: "inherit",
315
+ display: "flex",
316
+ alignItems: "center",
317
+ justifyContent: "center",
318
+ gap: 4,
319
+ }}
320
+ >
321
+ {tab.isHub && <HubGlyph active={isActive} />}
322
+ <TabLabel label={tab.label} beta={tab.beta} active={isActive} />
323
+ {tab.loading && (
324
+ <span
325
+ style={{
326
+ width: 5,
327
+ height: 5,
328
+ borderRadius: "50%",
329
+ background: "#ffffff",
330
+ boxShadow: "0 0 0 1px rgba(0,0,0,0.15)",
331
+ animation: "content-pulse 1.4s ease-in-out infinite",
332
+ }}
333
+ />
334
+ )}
335
+ {typeof tab.badge === "number" && tab.badge > 0 && !tab.loading && (
336
+ <span
337
+ style={{
338
+ fontSize: 9,
339
+ fontWeight: 700,
340
+ padding: "1px 5px",
341
+ borderRadius: 99,
342
+ background: isActive ? ACTIVE_INK : "rgba(0,0,0,0.16)",
343
+ color: isActive ? "#ffffff" : "rgba(0,0,0,0.7)",
344
+ minWidth: 16,
345
+ textAlign: "center",
346
+ }}
347
+ >
348
+ {tab.badge}
349
+ </span>
350
+ )}
351
+ </button>
352
+ {showDivider && (
353
+ <div
354
+ aria-hidden="true"
355
+ style={{
356
+ width: 1,
357
+ alignSelf: "center",
358
+ height: 16,
359
+ margin: "0 2px",
360
+ background: "rgba(0,0,0,0.18)",
361
+ zIndex: 1,
362
+ }}
363
+ />
364
+ )}
365
+ </div>
366
+ );
367
+ })}
368
+ </div>
369
+ );
370
+ }
@@ -46,6 +46,20 @@ export const INJECTED_CSS = `
46
46
  .prose-chat td { padding: 6px 10px; border-bottom: 1px solid #f8fafc; color: #64748b; }
47
47
  .prose-chat tr:last-child td { border-bottom: none; }
48
48
 
49
+ /* Panel surface - a pale warm-gold base (PANEL_BG, set inline) lifted with a premium
50
+ sheen: a soft top-down light pool, a faint warm radial bloom near the top edge, and
51
+ a whisper of fine grain so the surface reads as a polished material, not a flat fill.
52
+ The solid color is set inline (backgroundColor) so this image layer is never reset. */
53
+ .seo-copilot-panel {
54
+ background-image:
55
+ linear-gradient(180deg, rgba(255,255,255,0.78) 0%, rgba(255,255,255,0.22) 150px, rgba(255,255,255,0) 340px),
56
+ radial-gradient(ellipse 130% 48% at 50% -6%, rgba(255,253,244,0.9) 0%, rgba(255,253,244,0) 62%),
57
+ url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='nz' x='0' y='0'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='160' height='160' filter='url(%23nz)' opacity='0.025'/%3E%3C/svg%3E");
58
+ background-size: 100% 100%, 100% 100%, 140px 140px;
59
+ background-repeat: no-repeat, no-repeat, repeat;
60
+ background-position: center top, center top, center;
61
+ }
62
+
49
63
  /* Thin scrollbars */
50
64
  .seo-copilot-panel { scrollbar-width: thin; scrollbar-color: #e5e7eb transparent; }
51
65
  .seo-copilot-panel ::-webkit-scrollbar { width: 4px; }
@@ -66,6 +80,38 @@ export const INJECTED_CSS = `
66
80
  [data-ui-color-scheme="dark"] .seo-copilot-panel * {
67
81
  color-scheme: light;
68
82
  }
83
+
84
+ /* Deep-link highlight flash - triggered when jumping from the Summary tab to a card */
85
+ @keyframes seo-copilot-flash {
86
+ 0% { box-shadow: 0 0 0 2px rgba(230,192,27,0), 0 0 0 6px rgba(230,192,27,0); }
87
+ 15% { box-shadow: 0 0 0 2px rgba(230,192,27,0.9), 0 0 0 6px rgba(230,192,27,0.22); }
88
+ 100% { box-shadow: 0 0 0 2px rgba(230,192,27,0), 0 0 0 6px rgba(230,192,27,0); }
89
+ }
90
+ .seo-copilot-flash { animation: seo-copilot-flash 1.3s ease-out; border-radius: 14px; }
91
+
92
+ /* Visible focus ring for keyboard nav - browser defaults wash out on this background */
93
+ .seo-copilot-panel button:focus-visible,
94
+ .seo-copilot-panel [role="tab"]:focus-visible,
95
+ .seo-copilot-panel a:focus-visible,
96
+ .seo-copilot-panel input:focus-visible,
97
+ .seo-copilot-panel textarea:focus-visible {
98
+ outline: 2px solid #005DAC;
99
+ outline-offset: 2px;
100
+ border-radius: 4px;
101
+ }
102
+
103
+ /* Respect reduced-motion preferences */
104
+ @media (prefers-reduced-motion: reduce) {
105
+ .seo-copilot-panel *, .seo-copilot-flash {
106
+ animation-duration: 0.01ms !important;
107
+ animation-iteration-count: 1 !important;
108
+ transition-duration: 0.01ms !important;
109
+ }
110
+ .seo-copilot-flash {
111
+ animation: none !important;
112
+ box-shadow: 0 0 0 2px rgba(230,192,27,0.9) !important;
113
+ }
114
+ }
69
115
  `;
70
116
 
71
117
  // Brand colors
@@ -75,3 +121,23 @@ export const MJH_BLUE = "#005DAC";
75
121
  export const MJH_SLATE = "#4D596A";
76
122
  export const USER_BLUE = "#DFF4FD";
77
123
  export const USER_BLUE_BORDER = "#B8E3F9";
124
+ // Panel surface fill - a pale warm-gold base behind the white content cards.
125
+ // Light enough that the white cards read as raised; the sheen/grain is layered
126
+ // on via the .seo-copilot-panel rule in INJECTED_CSS.
127
+ export const PANEL_BG = "#F3EAD2";
128
+
129
+ // Chat zone backdrop - the AI conversation zone. A cool, subtly multi-shaded
130
+ // MJH-blue wash with a soft glow rising from the composer; sets the chat apart
131
+ // from the gold analysis zone and reads "AI". ONE token so the collapse/expand
132
+ // toggle bar, the messages, and the composer all share a single continuous
133
+ // gradient (the toggle is part of the chat zone, not the gold panel).
134
+ export const CHAT_ZONE_BG =
135
+ "radial-gradient(120% 90% at 50% 106%, rgba(0,93,172,0.22), rgba(0,93,172,0) 62%), linear-gradient(180deg, #EAF2FA 0%, #DCEAF7 50%, #CFE2F4 100%)";
136
+
137
+ // Recommendations zone backdrop - mirrors the chat gradient's STRUCTURE in the
138
+ // warm GOLD family (analysis / copilot identity). A soft gold bloom near the top
139
+ // echoes the chat's bottom glow, giving the recommendations the same gradient
140
+ // depth without crossing into the blue chat zone. Gold above, blue below; the
141
+ // toggle bar is the seam between the two zones.
142
+ export const RECS_ZONE_BG =
143
+ "radial-gradient(120% 90% at 50% -8%, rgba(230,192,27,0.26), rgba(230,192,27,0) 60%), linear-gradient(180deg, #FBF4DD 0%, #F0E1B4 52%, #E5D096 100%)";