doccupine 0.0.133 → 0.0.135
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 +50 -10
- package/dist/index.d.ts +1 -1
- package/dist/index.js +478 -45
- package/dist/lib/config-manager.d.ts +8 -1
- package/dist/lib/config-manager.js +98 -4
- package/dist/lib/metadata.d.ts +5 -0
- package/dist/lib/metadata.js +15 -4
- package/dist/lib/openapi-types.d.ts +120 -0
- package/dist/lib/openapi-types.js +14 -0
- package/dist/lib/openapi.d.ts +52 -0
- package/dist/lib/openapi.js +603 -0
- package/dist/lib/rss.d.ts +24 -0
- package/dist/lib/rss.js +137 -0
- package/dist/lib/structures.js +50 -4
- package/dist/lib/types.d.ts +22 -0
- package/dist/lib/utils.d.ts +8 -0
- package/dist/lib/utils.js +14 -0
- package/dist/templates/app/api/playground/route.d.ts +1 -0
- package/dist/templates/app/api/playground/route.js +199 -0
- package/dist/templates/app/rssRoute.d.ts +11 -0
- package/dist/templates/app/rssRoute.js +30 -0
- package/dist/templates/components/Docs.d.ts +1 -1
- package/dist/templates/components/Docs.js +15 -3
- package/dist/templates/components/MDXComponents.d.ts +1 -1
- package/dist/templates/components/MDXComponents.js +29 -0
- package/dist/templates/components/SideBar.d.ts +1 -1
- package/dist/templates/components/SideBar.js +13 -0
- package/dist/templates/components/layout/Accordion.d.ts +1 -1
- package/dist/templates/components/layout/Accordion.js +3 -2
- package/dist/templates/components/layout/ActionBar.d.ts +1 -1
- package/dist/templates/components/layout/ActionBar.js +65 -27
- package/dist/templates/components/layout/ApiPlayground.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlayground.js +1075 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.d.ts +1 -0
- package/dist/templates/components/layout/ApiPlaygroundDemo.js +81 -0
- package/dist/templates/components/layout/Badge.d.ts +1 -0
- package/dist/templates/components/layout/Badge.js +455 -0
- package/dist/templates/components/layout/Code.d.ts +1 -1
- package/dist/templates/components/layout/Code.js +10 -22
- package/dist/templates/components/layout/CopyButton.d.ts +1 -0
- package/dist/templates/components/layout/CopyButton.js +68 -0
- package/dist/templates/components/layout/DocsComponents.d.ts +1 -1
- package/dist/templates/components/layout/DocsComponents.js +14 -0
- package/dist/templates/components/layout/Mermaid.d.ts +1 -1
- package/dist/templates/components/layout/Mermaid.js +21 -3
- package/dist/templates/components/layout/Prompt.d.ts +1 -0
- package/dist/templates/components/layout/Prompt.js +149 -0
- package/dist/templates/components/layout/SharedStyles.d.ts +1 -1
- package/dist/templates/components/layout/SharedStyles.js +0 -4
- package/dist/templates/components/layout/Steps.d.ts +1 -1
- package/dist/templates/components/layout/Steps.js +0 -1
- package/dist/templates/components/layout/Tooltip.d.ts +1 -0
- package/dist/templates/components/layout/Tooltip.js +222 -0
- package/dist/templates/components/layout/Tree.d.ts +1 -0
- package/dist/templates/components/layout/Tree.js +322 -0
- package/dist/templates/components/layout/TreeData.d.ts +1 -0
- package/dist/templates/components/layout/TreeData.js +131 -0
- package/dist/templates/components/layout/Update.d.ts +1 -1
- package/dist/templates/components/layout/Update.js +5 -1
- package/dist/templates/lib/rss.d.ts +1 -0
- package/dist/templates/lib/rss.js +78 -0
- package/dist/templates/mdx/accordion.mdx.d.ts +1 -1
- package/dist/templates/mdx/accordion.mdx.js +19 -1
- package/dist/templates/mdx/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/ai-assistant.mdx.js +3 -2
- package/dist/templates/mdx/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/analytics.mdx.js +2 -2
- package/dist/templates/mdx/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/api-playground.mdx.js +158 -0
- package/dist/templates/mdx/authentication.mdx.d.ts +1 -1
- package/dist/templates/mdx/authentication.mdx.js +3 -3
- package/dist/templates/mdx/badges.mdx.d.ts +1 -0
- package/dist/templates/mdx/badges.mdx.js +237 -0
- package/dist/templates/mdx/buttons.mdx.d.ts +1 -1
- package/dist/templates/mdx/buttons.mdx.js +1 -1
- package/dist/templates/mdx/callouts.mdx.d.ts +1 -1
- package/dist/templates/mdx/callouts.mdx.js +1 -1
- package/dist/templates/mdx/cards.mdx.d.ts +1 -1
- package/dist/templates/mdx/cards.mdx.js +1 -1
- package/dist/templates/mdx/color-swatches.mdx.d.ts +1 -1
- package/dist/templates/mdx/color-swatches.mdx.js +1 -1
- package/dist/templates/mdx/columns.mdx.d.ts +1 -1
- package/dist/templates/mdx/columns.mdx.js +1 -1
- package/dist/templates/mdx/commands.mdx.d.ts +1 -1
- package/dist/templates/mdx/commands.mdx.js +3 -2
- package/dist/templates/mdx/components.mdx.d.ts +1 -1
- package/dist/templates/mdx/components.mdx.js +25 -10
- package/dist/templates/mdx/deployment-and-hosting.mdx.d.ts +1 -1
- package/dist/templates/mdx/deployment-and-hosting.mdx.js +4 -3
- package/dist/templates/mdx/fields.mdx.d.ts +1 -1
- package/dist/templates/mdx/fields.mdx.js +1 -1
- package/dist/templates/mdx/fonts.mdx.d.ts +1 -1
- package/dist/templates/mdx/fonts.mdx.js +2 -2
- package/dist/templates/mdx/footer-links.mdx.d.ts +1 -1
- package/dist/templates/mdx/footer-links.mdx.js +1 -1
- package/dist/templates/mdx/frames.mdx.d.ts +1 -1
- package/dist/templates/mdx/frames.mdx.js +1 -1
- package/dist/templates/mdx/globals.mdx.d.ts +1 -1
- package/dist/templates/mdx/globals.mdx.js +1 -1
- package/dist/templates/mdx/icons.mdx.d.ts +1 -1
- package/dist/templates/mdx/icons.mdx.js +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.d.ts +1 -1
- package/dist/templates/mdx/image-and-embeds.mdx.js +1 -1
- package/dist/templates/mdx/images-and-embeds.mdx.d.ts +1 -0
- package/dist/templates/mdx/images-and-embeds.mdx.js +118 -0
- package/dist/templates/mdx/media-and-assets.mdx.d.ts +1 -1
- package/dist/templates/mdx/media-and-assets.mdx.js +2 -2
- package/dist/templates/mdx/mermaid.mdx.d.ts +1 -1
- package/dist/templates/mdx/mermaid.mdx.js +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.d.ts +1 -1
- package/dist/templates/mdx/model-context-protocol.mdx.js +2 -2
- package/dist/templates/mdx/navigation.mdx.d.ts +1 -1
- package/dist/templates/mdx/navigation.mdx.js +27 -12
- package/dist/templates/mdx/platform/ai-assistant.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/ai-assistant.mdx.js +4 -3
- package/dist/templates/mdx/platform/analytics.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/analytics.mdx.js +2 -2
- package/dist/templates/mdx/platform/api-playground.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/api-playground.mdx.js +62 -0
- package/dist/templates/mdx/platform/billing.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/billing.mdx.js +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/build-and-deploy.mdx.js +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/creating-a-project.mdx.js +12 -2
- package/dist/templates/mdx/platform/custom-domains.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/custom-domains.mdx.js +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/fonts-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/footer-links.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/{external-links.mdx.js → footer-links.mdx.js} +5 -5
- package/dist/templates/mdx/platform/import-from-github.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/import-from-github.mdx.js +91 -0
- package/dist/templates/mdx/platform/mcp.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/mcp.mdx.js +3 -3
- package/dist/templates/mdx/platform/project-settings.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/project-settings.mdx.js +1 -1
- package/dist/templates/mdx/platform/self-updating-docs.mdx.d.ts +1 -0
- package/dist/templates/mdx/platform/self-updating-docs.mdx.js +113 -0
- package/dist/templates/mdx/platform/team-members.mdx.d.ts +1 -1
- package/dist/templates/mdx/platform/team-members.mdx.js +1 -1
- package/dist/templates/mdx/prompt.mdx.d.ts +1 -0
- package/dist/templates/mdx/prompt.mdx.js +69 -0
- package/dist/templates/mdx/sections.mdx.d.ts +1 -1
- package/dist/templates/mdx/sections.mdx.js +1 -1
- package/dist/templates/mdx/steps.mdx.d.ts +1 -1
- package/dist/templates/mdx/steps.mdx.js +1 -1
- package/dist/templates/mdx/tabs.mdx.d.ts +1 -1
- package/dist/templates/mdx/tabs.mdx.js +1 -1
- package/dist/templates/mdx/theme.mdx.d.ts +1 -1
- package/dist/templates/mdx/theme.mdx.js +1 -1
- package/dist/templates/mdx/tooltips.mdx.d.ts +1 -0
- package/dist/templates/mdx/tooltips.mdx.js +57 -0
- package/dist/templates/mdx/tree.mdx.d.ts +1 -0
- package/dist/templates/mdx/tree.mdx.js +158 -0
- package/dist/templates/mdx/update.mdx.d.ts +1 -1
- package/dist/templates/mdx/update.mdx.js +42 -2
- package/dist/templates/mdx/what-is-doccupine.mdx.d.ts +1 -1
- package/dist/templates/mdx/what-is-doccupine.mdx.js +1 -1
- package/dist/templates/package.js +7 -7
- package/dist/templates/services/openapi/playgroundAllowlistStub.d.ts +1 -0
- package/dist/templates/services/openapi/playgroundAllowlistStub.js +4 -0
- package/dist/templates/types/openapi.d.ts +1 -0
- package/dist/templates/types/openapi.js +93 -0
- package/dist/templates/utils/apiSnippets.d.ts +1 -0
- package/dist/templates/utils/apiSnippets.js +94 -0
- package/dist/templates/utils/orderNavItems.d.ts +1 -1
- package/dist/templates/utils/orderNavItems.js +3 -0
- package/dist/templates/utils/playgroundAllowlist.d.ts +1 -0
- package/dist/templates/utils/playgroundAllowlist.js +70 -0
- package/dist/templates/utils/ssrfGuard.d.ts +1 -0
- package/dist/templates/utils/ssrfGuard.js +217 -0
- package/package.json +4 -3
- package/dist/templates/mdx/platform/external-links.mdx.d.ts +0 -1
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const apiPlaygroundDemoTemplate = "\"use client\";\nimport dynamic from \"next/dynamic\";\nimport type { OperationDescriptor } from \"@/types/openapi\";\nimport type { AllowlistEntry } from \"@/utils/playgroundAllowlist\";\n\n// Lazy-load the playground so it only ships to pages that actually render the\n// demo (this wrapper is registered globally for MDX, but the heavy component\n// stays in its own chunk). SSR is left on (the default) so the compact bar is\n// present in the initial HTML and does not flash in after hydration - the\n// modal is closed on first render, so there is nothing browser-only to run.\nconst ApiPlayground = dynamic(() =>\n import(\"@/components/layout/ApiPlayground\").then((m) => m.ApiPlayground),\n);\n\n// A self-contained live demo for the docs. It calls a public, CORS-enabled test\n// API in direct mode, so it works even without a configured OpenAPI spec. The\n// server proxy is unaffected - it still only forwards to allowlisted servers;\n// this inline allowlist only relaxes the client-side pre-flight for the demo.\nconst DEMO_OPERATION: OperationDescriptor = {\n specName: \"demo\",\n operationId: \"createPost\",\n method: \"post\",\n path: \"/posts\",\n summary: \"Create a post\",\n description:\n \"Creates a blog post for a user. This demo calls JSONPlaceholder, a free public test API, so it is safe to run - it echoes your request back with a new id. Edit the body below and press Send.\",\n tags: [\"Posts\"],\n parameters: [],\n requestBody: {\n required: true,\n content: [\n {\n contentType: \"application/json\",\n schema: {\n type: \"object\",\n required: [\"title\", \"userId\"],\n properties: {\n title: { type: \"string\", description: \"The title of the post.\" },\n body: { type: \"string\", description: \"The post's body text.\" },\n userId: {\n type: \"integer\",\n description: \"Id of the user creating the post.\",\n },\n },\n },\n example: {\n title: \"Hello from the playground\",\n body: \"This request was sent straight from the docs.\",\n userId: 1,\n },\n },\n ],\n },\n responses: [\n {\n status: \"201\",\n description: \"The created post, echoed back with a new id.\",\n content: [{ contentType: \"application/json\" }],\n headers: [],\n },\n ],\n servers: [{ url: \"https://jsonplaceholder.typicode.com\" }],\n security: [],\n slug: \"\",\n endpointAnchor: \"demo\",\n};\n\nconst DEMO_ALLOWLIST: AllowlistEntry[] = [\n { scheme: \"https\", host: \"jsonplaceholder.typicode.com\", port: null },\n];\n\nexport function ApiPlaygroundDemo() {\n return (\n <ApiPlayground\n operation={DEMO_OPERATION}\n allowlist={DEMO_ALLOWLIST}\n defaultMode=\"direct\"\n />\n );\n}\n";
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
export const apiPlaygroundDemoTemplate = `"use client";
|
|
2
|
+
import dynamic from "next/dynamic";
|
|
3
|
+
import type { OperationDescriptor } from "@/types/openapi";
|
|
4
|
+
import type { AllowlistEntry } from "@/utils/playgroundAllowlist";
|
|
5
|
+
|
|
6
|
+
// Lazy-load the playground so it only ships to pages that actually render the
|
|
7
|
+
// demo (this wrapper is registered globally for MDX, but the heavy component
|
|
8
|
+
// stays in its own chunk). SSR is left on (the default) so the compact bar is
|
|
9
|
+
// present in the initial HTML and does not flash in after hydration - the
|
|
10
|
+
// modal is closed on first render, so there is nothing browser-only to run.
|
|
11
|
+
const ApiPlayground = dynamic(() =>
|
|
12
|
+
import("@/components/layout/ApiPlayground").then((m) => m.ApiPlayground),
|
|
13
|
+
);
|
|
14
|
+
|
|
15
|
+
// A self-contained live demo for the docs. It calls a public, CORS-enabled test
|
|
16
|
+
// API in direct mode, so it works even without a configured OpenAPI spec. The
|
|
17
|
+
// server proxy is unaffected - it still only forwards to allowlisted servers;
|
|
18
|
+
// this inline allowlist only relaxes the client-side pre-flight for the demo.
|
|
19
|
+
const DEMO_OPERATION: OperationDescriptor = {
|
|
20
|
+
specName: "demo",
|
|
21
|
+
operationId: "createPost",
|
|
22
|
+
method: "post",
|
|
23
|
+
path: "/posts",
|
|
24
|
+
summary: "Create a post",
|
|
25
|
+
description:
|
|
26
|
+
"Creates a blog post for a user. This demo calls JSONPlaceholder, a free public test API, so it is safe to run - it echoes your request back with a new id. Edit the body below and press Send.",
|
|
27
|
+
tags: ["Posts"],
|
|
28
|
+
parameters: [],
|
|
29
|
+
requestBody: {
|
|
30
|
+
required: true,
|
|
31
|
+
content: [
|
|
32
|
+
{
|
|
33
|
+
contentType: "application/json",
|
|
34
|
+
schema: {
|
|
35
|
+
type: "object",
|
|
36
|
+
required: ["title", "userId"],
|
|
37
|
+
properties: {
|
|
38
|
+
title: { type: "string", description: "The title of the post." },
|
|
39
|
+
body: { type: "string", description: "The post's body text." },
|
|
40
|
+
userId: {
|
|
41
|
+
type: "integer",
|
|
42
|
+
description: "Id of the user creating the post.",
|
|
43
|
+
},
|
|
44
|
+
},
|
|
45
|
+
},
|
|
46
|
+
example: {
|
|
47
|
+
title: "Hello from the playground",
|
|
48
|
+
body: "This request was sent straight from the docs.",
|
|
49
|
+
userId: 1,
|
|
50
|
+
},
|
|
51
|
+
},
|
|
52
|
+
],
|
|
53
|
+
},
|
|
54
|
+
responses: [
|
|
55
|
+
{
|
|
56
|
+
status: "201",
|
|
57
|
+
description: "The created post, echoed back with a new id.",
|
|
58
|
+
content: [{ contentType: "application/json" }],
|
|
59
|
+
headers: [],
|
|
60
|
+
},
|
|
61
|
+
],
|
|
62
|
+
servers: [{ url: "https://jsonplaceholder.typicode.com" }],
|
|
63
|
+
security: [],
|
|
64
|
+
slug: "",
|
|
65
|
+
endpointAnchor: "demo",
|
|
66
|
+
};
|
|
67
|
+
|
|
68
|
+
const DEMO_ALLOWLIST: AllowlistEntry[] = [
|
|
69
|
+
{ scheme: "https", host: "jsonplaceholder.typicode.com", port: null },
|
|
70
|
+
];
|
|
71
|
+
|
|
72
|
+
export function ApiPlaygroundDemo() {
|
|
73
|
+
return (
|
|
74
|
+
<ApiPlayground
|
|
75
|
+
operation={DEMO_OPERATION}
|
|
76
|
+
allowlist={DEMO_ALLOWLIST}
|
|
77
|
+
defaultMode="direct"
|
|
78
|
+
/>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
`;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const badgeTemplate = "\"use client\";\nimport styled, { css } from \"styled-components\";\nimport { Theme } from \"@/app/theme\";\nimport { Icon } from \"@/components/layout/Icon\";\n\ntype BadgeColor =\n | \"gray\"\n | \"blue\"\n | \"green\"\n | \"yellow\"\n | \"orange\"\n | \"red\"\n | \"purple\"\n | \"white\"\n | \"surface\"\n | \"white-destructive\"\n | \"surface-destructive\"\n | \"info\"\n | \"success\"\n | \"warning\"\n | \"error\";\n\ntype BadgeSize = \"xs\" | \"sm\" | \"md\" | \"lg\";\n\ntype BadgeShape = \"rounded\" | \"pill\";\n\n// The semantic colors read theme tokens instead of the fixed palette, so\n// they follow theme.json and the light/dark toggle. They are also what the\n// HTTP method chips use.\nconst THEME_TOKEN_COLORS = [\"info\", \"success\", \"warning\", \"error\"] as const;\n\ntype ThemeTokenColor = (typeof THEME_TOKEN_COLORS)[number];\n\ntype HexBadgeColor = Exclude<BadgeColor, ThemeTokenColor>;\n\nfunction isThemeTokenColor(color: BadgeColor): color is ThemeTokenColor {\n return (THEME_TOKEN_COLORS as readonly string[]).includes(color);\n}\n\n// Single source for verb -> badge color, shared by the sidebar method tags\n// and the API playground so every surface colors HTTP verbs identically.\nconst HTTP_METHOD_COLORS: Record<string, BadgeColor> = {\n get: \"info\",\n post: \"success\",\n put: \"warning\",\n patch: \"warning\",\n delete: \"error\",\n head: \"gray\",\n options: \"gray\",\n trace: \"gray\",\n};\n\nfunction httpMethodBadgeColor(method: string): BadgeColor {\n return HTTP_METHOD_COLORS[method.toLowerCase()] ?? \"gray\";\n}\n\ninterface BadgePalette {\n background: string;\n text: string;\n border: string;\n darkBackground: string;\n darkText: string;\n darkBorder: string;\n}\n\n// Strong fills for the hex palette; the white and surface variants have no\n// distinct solid form and fall back to their filled palette.\nconst SOLID_ACCENTS: Partial<Record<HexBadgeColor, string>> = {\n gray: \"#71717a\",\n blue: \"#3b82f6\",\n green: \"#22c55e\",\n yellow: \"#eab308\",\n orange: \"#f97316\",\n red: \"#ef4444\",\n purple: \"#a855f7\",\n};\n\n// Color tints are label-semantic and intentionally independent of theme.json\n// (same rationale as Callout's alert tints); only the surface and semantic\n// token variants read theme tokens, so they follow the active theme on both\n// modes. When both are set, solid wins over stroke.\nfunction badgePalette(\n theme: Theme,\n color: BadgeColor,\n stroke: boolean,\n solid: boolean,\n): BadgePalette {\n if (isThemeTokenColor(color)) {\n const token = theme.colors[color];\n if (solid) {\n return {\n background: token,\n text: theme.colors.surface,\n border: \"transparent\",\n darkBackground: token,\n darkText: theme.colors.surface,\n darkBorder: \"transparent\",\n };\n }\n if (stroke) {\n return {\n background: \"transparent\",\n text: token,\n border: token,\n darkBackground: \"transparent\",\n darkText: token,\n darkBorder: token,\n };\n }\n const tint = `color-mix(in srgb, ${token} 15%, transparent)`;\n return {\n background: tint,\n text: token,\n border: \"transparent\",\n darkBackground: tint,\n darkText: token,\n darkBorder: \"transparent\",\n };\n }\n\n const solidAccent = SOLID_ACCENTS[color];\n if (solid && solidAccent) {\n return {\n background: solidAccent,\n text: \"#ffffff\",\n border: \"transparent\",\n darkBackground: solidAccent,\n darkText: \"#ffffff\",\n darkBorder: \"transparent\",\n };\n }\n\n const surface = {\n background: theme.colors.surface,\n text: theme.colors.grayDark,\n border: theme.colors.grayLight,\n darkBackground: theme.colors.surface,\n darkText: theme.colors.grayDark,\n darkBorder: theme.colors.grayLight,\n };\n\n if (stroke && !solid) {\n const strokes: Record<HexBadgeColor, BadgePalette> = {\n gray: {\n background: \"transparent\",\n text: \"#52525b\",\n border: \"#a1a1aa\",\n darkBackground: \"transparent\",\n darkText: \"#d4d4d8\",\n darkBorder: \"#71717a\",\n },\n blue: {\n background: \"transparent\",\n text: \"#1d4ed8\",\n border: \"#3b82f6\",\n darkBackground: \"transparent\",\n darkText: \"#93c5fd\",\n darkBorder: \"#3b82f6\",\n },\n green: {\n background: \"transparent\",\n text: \"#15803d\",\n border: \"#22c55e\",\n darkBackground: \"transparent\",\n darkText: \"#86efac\",\n darkBorder: \"#22c55e\",\n },\n yellow: {\n background: \"transparent\",\n text: \"#a16207\",\n border: \"#eab308\",\n darkBackground: \"transparent\",\n darkText: \"#fde047\",\n darkBorder: \"#eab308\",\n },\n orange: {\n background: \"transparent\",\n text: \"#c2410c\",\n border: \"#f97316\",\n darkBackground: \"transparent\",\n darkText: \"#fdba74\",\n darkBorder: \"#f97316\",\n },\n red: {\n background: \"transparent\",\n text: \"#b91c1c\",\n border: \"#ef4444\",\n darkBackground: \"transparent\",\n darkText: \"#fca5a5\",\n darkBorder: \"#ef4444\",\n },\n purple: {\n background: \"transparent\",\n text: \"#7e22ce\",\n border: \"#a855f7\",\n darkBackground: \"transparent\",\n darkText: \"#d8b4fe\",\n darkBorder: \"#a855f7\",\n },\n white: {\n background: \"transparent\",\n text: \"#18181b\",\n border: \"#e4e4e7\",\n darkBackground: \"transparent\",\n darkText: \"#fafafa\",\n darkBorder: \"#3f3f46\",\n },\n surface: { ...surface, background: \"transparent\" },\n \"white-destructive\": {\n background: \"transparent\",\n text: \"#b91c1c\",\n border: \"#ef4444\",\n darkBackground: \"transparent\",\n darkText: \"#fca5a5\",\n darkBorder: \"#ef4444\",\n },\n \"surface-destructive\": {\n background: \"transparent\",\n text: \"#b91c1c\",\n border: \"#ef4444\",\n darkBackground: \"transparent\",\n darkText: \"#fca5a5\",\n darkBorder: \"#ef4444\",\n },\n };\n return strokes[color];\n }\n\n const filled: Record<HexBadgeColor, BadgePalette> = {\n gray: {\n background: \"#f4f4f5\",\n text: \"#3f3f46\",\n border: \"transparent\",\n darkBackground: \"#71717a33\",\n darkText: \"#d4d4d8\",\n darkBorder: \"transparent\",\n },\n blue: {\n background: \"#dbeafe\",\n text: \"#1e40af\",\n border: \"transparent\",\n darkBackground: \"#3b82f633\",\n darkText: \"#93c5fd\",\n darkBorder: \"transparent\",\n },\n green: {\n background: \"#dcfce7\",\n text: \"#166534\",\n border: \"transparent\",\n darkBackground: \"#22c55e33\",\n darkText: \"#86efac\",\n darkBorder: \"transparent\",\n },\n yellow: {\n background: \"#fef9c3\",\n text: \"#854d0e\",\n border: \"transparent\",\n darkBackground: \"#eab30833\",\n darkText: \"#fde047\",\n darkBorder: \"transparent\",\n },\n orange: {\n background: \"#ffedd5\",\n text: \"#9a3412\",\n border: \"transparent\",\n darkBackground: \"#f9731633\",\n darkText: \"#fdba74\",\n darkBorder: \"transparent\",\n },\n red: {\n background: \"#fee2e2\",\n text: \"#991b1b\",\n border: \"transparent\",\n darkBackground: \"#ef444433\",\n darkText: \"#fca5a5\",\n darkBorder: \"transparent\",\n },\n purple: {\n background: \"#f3e8ff\",\n text: \"#6b21a8\",\n border: \"transparent\",\n darkBackground: \"#a855f733\",\n darkText: \"#d8b4fe\",\n darkBorder: \"transparent\",\n },\n white: {\n background: \"#ffffff\",\n text: \"#18181b\",\n border: \"#e4e4e7\",\n darkBackground: \"#ffffff\",\n darkText: \"#18181b\",\n darkBorder: \"#e4e4e7\",\n },\n surface,\n \"white-destructive\": {\n background: \"#ffffff\",\n text: \"#b91c1c\",\n border: \"#fecaca\",\n darkBackground: \"#ffffff\",\n darkText: \"#b91c1c\",\n darkBorder: \"#fecaca\",\n },\n \"surface-destructive\": {\n ...surface,\n text: \"#b91c1c\",\n border: \"#fecaca\",\n darkText: \"#fca5a5\",\n darkBorder: \"#ef44444d\",\n },\n };\n return filled[color];\n}\n\nconst badgeSizes = {\n xs: css`\n font-size: 11px;\n line-height: 16px;\n padding: 1px 6px;\n gap: 3px;\n `,\n sm: css`\n font-size: 12px;\n line-height: 18px;\n padding: 2px 8px;\n gap: 4px;\n `,\n md: css`\n font-size: 13px;\n line-height: 20px;\n padding: 3px 10px;\n gap: 5px;\n `,\n lg: css`\n font-size: 14px;\n line-height: 22px;\n padding: 4px 12px;\n gap: 6px;\n `,\n};\n\nconst badgeIconSizes: Record<BadgeSize, number> = {\n xs: 10,\n sm: 12,\n md: 14,\n lg: 16,\n};\n\nconst StyledBadge = styled.span<{\n theme: Theme;\n $color: BadgeColor;\n $size: BadgeSize;\n $shape: BadgeShape;\n $stroke: boolean;\n $solid: boolean;\n $mono: boolean;\n $disabled: boolean;\n}>`\n display: inline-flex;\n align-items: center;\n vertical-align: middle;\n max-width: max-content;\n font-family: inherit;\n font-weight: 600;\n white-space: nowrap;\n\n & svg.lucide {\n flex-shrink: 0;\n color: inherit;\n }\n\n ${({ $size }) => badgeSizes[$size]}\n\n ${({ theme, $mono }) =>\n $mono &&\n css`\n font-family: ${theme.fonts.mono};\n font-weight: 700;\n text-transform: uppercase;\n letter-spacing: 0.04em;\n `}\n\n ${({ theme, $shape }) =>\n $shape === \"pill\"\n ? css`\n border-radius: 999px;\n `\n : css`\n border-radius: ${theme.spacing.radius.xs};\n `}\n\n ${({ theme, $color, $stroke, $solid }) => {\n const palette = badgePalette(theme, $color, $stroke, $solid);\n return css`\n background: ${palette.background};\n color: ${palette.text};\n border: solid 1px ${palette.border};\n\n :root.dark & {\n background: ${palette.darkBackground};\n color: ${palette.darkText};\n border-color: ${palette.darkBorder};\n }\n `;\n }}\n\n ${({ $disabled }) =>\n $disabled &&\n css`\n opacity: 0.5;\n `}\n`;\n\ninterface BadgeProps extends React.HTMLAttributes<HTMLSpanElement> {\n children: React.ReactNode;\n color?: BadgeColor;\n size?: BadgeSize;\n shape?: BadgeShape;\n icon?: string;\n stroke?: boolean;\n solid?: boolean;\n mono?: boolean;\n disabled?: boolean;\n}\n\nfunction Badge({\n children,\n color = \"gray\",\n size = \"md\",\n shape = \"rounded\",\n icon,\n stroke = false,\n solid = false,\n mono = false,\n disabled = false,\n className,\n}: BadgeProps) {\n return (\n <StyledBadge\n className={className}\n $color={color}\n $size={size}\n $shape={shape}\n $stroke={stroke}\n $solid={solid}\n $mono={mono}\n $disabled={disabled}\n >\n {icon ? <Icon name={icon} size={badgeIconSizes[size]} /> : null}\n {children}\n </StyledBadge>\n );\n}\n\nexport { Badge, httpMethodBadgeColor };\n";
|
|
@@ -0,0 +1,455 @@
|
|
|
1
|
+
export const badgeTemplate = `"use client";
|
|
2
|
+
import styled, { css } from "styled-components";
|
|
3
|
+
import { Theme } from "@/app/theme";
|
|
4
|
+
import { Icon } from "@/components/layout/Icon";
|
|
5
|
+
|
|
6
|
+
type BadgeColor =
|
|
7
|
+
| "gray"
|
|
8
|
+
| "blue"
|
|
9
|
+
| "green"
|
|
10
|
+
| "yellow"
|
|
11
|
+
| "orange"
|
|
12
|
+
| "red"
|
|
13
|
+
| "purple"
|
|
14
|
+
| "white"
|
|
15
|
+
| "surface"
|
|
16
|
+
| "white-destructive"
|
|
17
|
+
| "surface-destructive"
|
|
18
|
+
| "info"
|
|
19
|
+
| "success"
|
|
20
|
+
| "warning"
|
|
21
|
+
| "error";
|
|
22
|
+
|
|
23
|
+
type BadgeSize = "xs" | "sm" | "md" | "lg";
|
|
24
|
+
|
|
25
|
+
type BadgeShape = "rounded" | "pill";
|
|
26
|
+
|
|
27
|
+
// The semantic colors read theme tokens instead of the fixed palette, so
|
|
28
|
+
// they follow theme.json and the light/dark toggle. They are also what the
|
|
29
|
+
// HTTP method chips use.
|
|
30
|
+
const THEME_TOKEN_COLORS = ["info", "success", "warning", "error"] as const;
|
|
31
|
+
|
|
32
|
+
type ThemeTokenColor = (typeof THEME_TOKEN_COLORS)[number];
|
|
33
|
+
|
|
34
|
+
type HexBadgeColor = Exclude<BadgeColor, ThemeTokenColor>;
|
|
35
|
+
|
|
36
|
+
function isThemeTokenColor(color: BadgeColor): color is ThemeTokenColor {
|
|
37
|
+
return (THEME_TOKEN_COLORS as readonly string[]).includes(color);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
// Single source for verb -> badge color, shared by the sidebar method tags
|
|
41
|
+
// and the API playground so every surface colors HTTP verbs identically.
|
|
42
|
+
const HTTP_METHOD_COLORS: Record<string, BadgeColor> = {
|
|
43
|
+
get: "info",
|
|
44
|
+
post: "success",
|
|
45
|
+
put: "warning",
|
|
46
|
+
patch: "warning",
|
|
47
|
+
delete: "error",
|
|
48
|
+
head: "gray",
|
|
49
|
+
options: "gray",
|
|
50
|
+
trace: "gray",
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
function httpMethodBadgeColor(method: string): BadgeColor {
|
|
54
|
+
return HTTP_METHOD_COLORS[method.toLowerCase()] ?? "gray";
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
interface BadgePalette {
|
|
58
|
+
background: string;
|
|
59
|
+
text: string;
|
|
60
|
+
border: string;
|
|
61
|
+
darkBackground: string;
|
|
62
|
+
darkText: string;
|
|
63
|
+
darkBorder: string;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// Strong fills for the hex palette; the white and surface variants have no
|
|
67
|
+
// distinct solid form and fall back to their filled palette.
|
|
68
|
+
const SOLID_ACCENTS: Partial<Record<HexBadgeColor, string>> = {
|
|
69
|
+
gray: "#71717a",
|
|
70
|
+
blue: "#3b82f6",
|
|
71
|
+
green: "#22c55e",
|
|
72
|
+
yellow: "#eab308",
|
|
73
|
+
orange: "#f97316",
|
|
74
|
+
red: "#ef4444",
|
|
75
|
+
purple: "#a855f7",
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
// Color tints are label-semantic and intentionally independent of theme.json
|
|
79
|
+
// (same rationale as Callout's alert tints); only the surface and semantic
|
|
80
|
+
// token variants read theme tokens, so they follow the active theme on both
|
|
81
|
+
// modes. When both are set, solid wins over stroke.
|
|
82
|
+
function badgePalette(
|
|
83
|
+
theme: Theme,
|
|
84
|
+
color: BadgeColor,
|
|
85
|
+
stroke: boolean,
|
|
86
|
+
solid: boolean,
|
|
87
|
+
): BadgePalette {
|
|
88
|
+
if (isThemeTokenColor(color)) {
|
|
89
|
+
const token = theme.colors[color];
|
|
90
|
+
if (solid) {
|
|
91
|
+
return {
|
|
92
|
+
background: token,
|
|
93
|
+
text: theme.colors.surface,
|
|
94
|
+
border: "transparent",
|
|
95
|
+
darkBackground: token,
|
|
96
|
+
darkText: theme.colors.surface,
|
|
97
|
+
darkBorder: "transparent",
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
if (stroke) {
|
|
101
|
+
return {
|
|
102
|
+
background: "transparent",
|
|
103
|
+
text: token,
|
|
104
|
+
border: token,
|
|
105
|
+
darkBackground: "transparent",
|
|
106
|
+
darkText: token,
|
|
107
|
+
darkBorder: token,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
const tint = \`color-mix(in srgb, \${token} 15%, transparent)\`;
|
|
111
|
+
return {
|
|
112
|
+
background: tint,
|
|
113
|
+
text: token,
|
|
114
|
+
border: "transparent",
|
|
115
|
+
darkBackground: tint,
|
|
116
|
+
darkText: token,
|
|
117
|
+
darkBorder: "transparent",
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const solidAccent = SOLID_ACCENTS[color];
|
|
122
|
+
if (solid && solidAccent) {
|
|
123
|
+
return {
|
|
124
|
+
background: solidAccent,
|
|
125
|
+
text: "#ffffff",
|
|
126
|
+
border: "transparent",
|
|
127
|
+
darkBackground: solidAccent,
|
|
128
|
+
darkText: "#ffffff",
|
|
129
|
+
darkBorder: "transparent",
|
|
130
|
+
};
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
const surface = {
|
|
134
|
+
background: theme.colors.surface,
|
|
135
|
+
text: theme.colors.grayDark,
|
|
136
|
+
border: theme.colors.grayLight,
|
|
137
|
+
darkBackground: theme.colors.surface,
|
|
138
|
+
darkText: theme.colors.grayDark,
|
|
139
|
+
darkBorder: theme.colors.grayLight,
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
if (stroke && !solid) {
|
|
143
|
+
const strokes: Record<HexBadgeColor, BadgePalette> = {
|
|
144
|
+
gray: {
|
|
145
|
+
background: "transparent",
|
|
146
|
+
text: "#52525b",
|
|
147
|
+
border: "#a1a1aa",
|
|
148
|
+
darkBackground: "transparent",
|
|
149
|
+
darkText: "#d4d4d8",
|
|
150
|
+
darkBorder: "#71717a",
|
|
151
|
+
},
|
|
152
|
+
blue: {
|
|
153
|
+
background: "transparent",
|
|
154
|
+
text: "#1d4ed8",
|
|
155
|
+
border: "#3b82f6",
|
|
156
|
+
darkBackground: "transparent",
|
|
157
|
+
darkText: "#93c5fd",
|
|
158
|
+
darkBorder: "#3b82f6",
|
|
159
|
+
},
|
|
160
|
+
green: {
|
|
161
|
+
background: "transparent",
|
|
162
|
+
text: "#15803d",
|
|
163
|
+
border: "#22c55e",
|
|
164
|
+
darkBackground: "transparent",
|
|
165
|
+
darkText: "#86efac",
|
|
166
|
+
darkBorder: "#22c55e",
|
|
167
|
+
},
|
|
168
|
+
yellow: {
|
|
169
|
+
background: "transparent",
|
|
170
|
+
text: "#a16207",
|
|
171
|
+
border: "#eab308",
|
|
172
|
+
darkBackground: "transparent",
|
|
173
|
+
darkText: "#fde047",
|
|
174
|
+
darkBorder: "#eab308",
|
|
175
|
+
},
|
|
176
|
+
orange: {
|
|
177
|
+
background: "transparent",
|
|
178
|
+
text: "#c2410c",
|
|
179
|
+
border: "#f97316",
|
|
180
|
+
darkBackground: "transparent",
|
|
181
|
+
darkText: "#fdba74",
|
|
182
|
+
darkBorder: "#f97316",
|
|
183
|
+
},
|
|
184
|
+
red: {
|
|
185
|
+
background: "transparent",
|
|
186
|
+
text: "#b91c1c",
|
|
187
|
+
border: "#ef4444",
|
|
188
|
+
darkBackground: "transparent",
|
|
189
|
+
darkText: "#fca5a5",
|
|
190
|
+
darkBorder: "#ef4444",
|
|
191
|
+
},
|
|
192
|
+
purple: {
|
|
193
|
+
background: "transparent",
|
|
194
|
+
text: "#7e22ce",
|
|
195
|
+
border: "#a855f7",
|
|
196
|
+
darkBackground: "transparent",
|
|
197
|
+
darkText: "#d8b4fe",
|
|
198
|
+
darkBorder: "#a855f7",
|
|
199
|
+
},
|
|
200
|
+
white: {
|
|
201
|
+
background: "transparent",
|
|
202
|
+
text: "#18181b",
|
|
203
|
+
border: "#e4e4e7",
|
|
204
|
+
darkBackground: "transparent",
|
|
205
|
+
darkText: "#fafafa",
|
|
206
|
+
darkBorder: "#3f3f46",
|
|
207
|
+
},
|
|
208
|
+
surface: { ...surface, background: "transparent" },
|
|
209
|
+
"white-destructive": {
|
|
210
|
+
background: "transparent",
|
|
211
|
+
text: "#b91c1c",
|
|
212
|
+
border: "#ef4444",
|
|
213
|
+
darkBackground: "transparent",
|
|
214
|
+
darkText: "#fca5a5",
|
|
215
|
+
darkBorder: "#ef4444",
|
|
216
|
+
},
|
|
217
|
+
"surface-destructive": {
|
|
218
|
+
background: "transparent",
|
|
219
|
+
text: "#b91c1c",
|
|
220
|
+
border: "#ef4444",
|
|
221
|
+
darkBackground: "transparent",
|
|
222
|
+
darkText: "#fca5a5",
|
|
223
|
+
darkBorder: "#ef4444",
|
|
224
|
+
},
|
|
225
|
+
};
|
|
226
|
+
return strokes[color];
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
const filled: Record<HexBadgeColor, BadgePalette> = {
|
|
230
|
+
gray: {
|
|
231
|
+
background: "#f4f4f5",
|
|
232
|
+
text: "#3f3f46",
|
|
233
|
+
border: "transparent",
|
|
234
|
+
darkBackground: "#71717a33",
|
|
235
|
+
darkText: "#d4d4d8",
|
|
236
|
+
darkBorder: "transparent",
|
|
237
|
+
},
|
|
238
|
+
blue: {
|
|
239
|
+
background: "#dbeafe",
|
|
240
|
+
text: "#1e40af",
|
|
241
|
+
border: "transparent",
|
|
242
|
+
darkBackground: "#3b82f633",
|
|
243
|
+
darkText: "#93c5fd",
|
|
244
|
+
darkBorder: "transparent",
|
|
245
|
+
},
|
|
246
|
+
green: {
|
|
247
|
+
background: "#dcfce7",
|
|
248
|
+
text: "#166534",
|
|
249
|
+
border: "transparent",
|
|
250
|
+
darkBackground: "#22c55e33",
|
|
251
|
+
darkText: "#86efac",
|
|
252
|
+
darkBorder: "transparent",
|
|
253
|
+
},
|
|
254
|
+
yellow: {
|
|
255
|
+
background: "#fef9c3",
|
|
256
|
+
text: "#854d0e",
|
|
257
|
+
border: "transparent",
|
|
258
|
+
darkBackground: "#eab30833",
|
|
259
|
+
darkText: "#fde047",
|
|
260
|
+
darkBorder: "transparent",
|
|
261
|
+
},
|
|
262
|
+
orange: {
|
|
263
|
+
background: "#ffedd5",
|
|
264
|
+
text: "#9a3412",
|
|
265
|
+
border: "transparent",
|
|
266
|
+
darkBackground: "#f9731633",
|
|
267
|
+
darkText: "#fdba74",
|
|
268
|
+
darkBorder: "transparent",
|
|
269
|
+
},
|
|
270
|
+
red: {
|
|
271
|
+
background: "#fee2e2",
|
|
272
|
+
text: "#991b1b",
|
|
273
|
+
border: "transparent",
|
|
274
|
+
darkBackground: "#ef444433",
|
|
275
|
+
darkText: "#fca5a5",
|
|
276
|
+
darkBorder: "transparent",
|
|
277
|
+
},
|
|
278
|
+
purple: {
|
|
279
|
+
background: "#f3e8ff",
|
|
280
|
+
text: "#6b21a8",
|
|
281
|
+
border: "transparent",
|
|
282
|
+
darkBackground: "#a855f733",
|
|
283
|
+
darkText: "#d8b4fe",
|
|
284
|
+
darkBorder: "transparent",
|
|
285
|
+
},
|
|
286
|
+
white: {
|
|
287
|
+
background: "#ffffff",
|
|
288
|
+
text: "#18181b",
|
|
289
|
+
border: "#e4e4e7",
|
|
290
|
+
darkBackground: "#ffffff",
|
|
291
|
+
darkText: "#18181b",
|
|
292
|
+
darkBorder: "#e4e4e7",
|
|
293
|
+
},
|
|
294
|
+
surface,
|
|
295
|
+
"white-destructive": {
|
|
296
|
+
background: "#ffffff",
|
|
297
|
+
text: "#b91c1c",
|
|
298
|
+
border: "#fecaca",
|
|
299
|
+
darkBackground: "#ffffff",
|
|
300
|
+
darkText: "#b91c1c",
|
|
301
|
+
darkBorder: "#fecaca",
|
|
302
|
+
},
|
|
303
|
+
"surface-destructive": {
|
|
304
|
+
...surface,
|
|
305
|
+
text: "#b91c1c",
|
|
306
|
+
border: "#fecaca",
|
|
307
|
+
darkText: "#fca5a5",
|
|
308
|
+
darkBorder: "#ef44444d",
|
|
309
|
+
},
|
|
310
|
+
};
|
|
311
|
+
return filled[color];
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const badgeSizes = {
|
|
315
|
+
xs: css\`
|
|
316
|
+
font-size: 11px;
|
|
317
|
+
line-height: 16px;
|
|
318
|
+
padding: 1px 6px;
|
|
319
|
+
gap: 3px;
|
|
320
|
+
\`,
|
|
321
|
+
sm: css\`
|
|
322
|
+
font-size: 12px;
|
|
323
|
+
line-height: 18px;
|
|
324
|
+
padding: 2px 8px;
|
|
325
|
+
gap: 4px;
|
|
326
|
+
\`,
|
|
327
|
+
md: css\`
|
|
328
|
+
font-size: 13px;
|
|
329
|
+
line-height: 20px;
|
|
330
|
+
padding: 3px 10px;
|
|
331
|
+
gap: 5px;
|
|
332
|
+
\`,
|
|
333
|
+
lg: css\`
|
|
334
|
+
font-size: 14px;
|
|
335
|
+
line-height: 22px;
|
|
336
|
+
padding: 4px 12px;
|
|
337
|
+
gap: 6px;
|
|
338
|
+
\`,
|
|
339
|
+
};
|
|
340
|
+
|
|
341
|
+
const badgeIconSizes: Record<BadgeSize, number> = {
|
|
342
|
+
xs: 10,
|
|
343
|
+
sm: 12,
|
|
344
|
+
md: 14,
|
|
345
|
+
lg: 16,
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const StyledBadge = styled.span<{
|
|
349
|
+
theme: Theme;
|
|
350
|
+
$color: BadgeColor;
|
|
351
|
+
$size: BadgeSize;
|
|
352
|
+
$shape: BadgeShape;
|
|
353
|
+
$stroke: boolean;
|
|
354
|
+
$solid: boolean;
|
|
355
|
+
$mono: boolean;
|
|
356
|
+
$disabled: boolean;
|
|
357
|
+
}>\`
|
|
358
|
+
display: inline-flex;
|
|
359
|
+
align-items: center;
|
|
360
|
+
vertical-align: middle;
|
|
361
|
+
max-width: max-content;
|
|
362
|
+
font-family: inherit;
|
|
363
|
+
font-weight: 600;
|
|
364
|
+
white-space: nowrap;
|
|
365
|
+
|
|
366
|
+
& svg.lucide {
|
|
367
|
+
flex-shrink: 0;
|
|
368
|
+
color: inherit;
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
\${({ $size }) => badgeSizes[$size]}
|
|
372
|
+
|
|
373
|
+
\${({ theme, $mono }) =>
|
|
374
|
+
$mono &&
|
|
375
|
+
css\`
|
|
376
|
+
font-family: \${theme.fonts.mono};
|
|
377
|
+
font-weight: 700;
|
|
378
|
+
text-transform: uppercase;
|
|
379
|
+
letter-spacing: 0.04em;
|
|
380
|
+
\`}
|
|
381
|
+
|
|
382
|
+
\${({ theme, $shape }) =>
|
|
383
|
+
$shape === "pill"
|
|
384
|
+
? css\`
|
|
385
|
+
border-radius: 999px;
|
|
386
|
+
\`
|
|
387
|
+
: css\`
|
|
388
|
+
border-radius: \${theme.spacing.radius.xs};
|
|
389
|
+
\`}
|
|
390
|
+
|
|
391
|
+
\${({ theme, $color, $stroke, $solid }) => {
|
|
392
|
+
const palette = badgePalette(theme, $color, $stroke, $solid);
|
|
393
|
+
return css\`
|
|
394
|
+
background: \${palette.background};
|
|
395
|
+
color: \${palette.text};
|
|
396
|
+
border: solid 1px \${palette.border};
|
|
397
|
+
|
|
398
|
+
:root.dark & {
|
|
399
|
+
background: \${palette.darkBackground};
|
|
400
|
+
color: \${palette.darkText};
|
|
401
|
+
border-color: \${palette.darkBorder};
|
|
402
|
+
}
|
|
403
|
+
\`;
|
|
404
|
+
}}
|
|
405
|
+
|
|
406
|
+
\${({ $disabled }) =>
|
|
407
|
+
$disabled &&
|
|
408
|
+
css\`
|
|
409
|
+
opacity: 0.5;
|
|
410
|
+
\`}
|
|
411
|
+
\`;
|
|
412
|
+
|
|
413
|
+
interface BadgeProps extends React.HTMLAttributes<HTMLSpanElement> {
|
|
414
|
+
children: React.ReactNode;
|
|
415
|
+
color?: BadgeColor;
|
|
416
|
+
size?: BadgeSize;
|
|
417
|
+
shape?: BadgeShape;
|
|
418
|
+
icon?: string;
|
|
419
|
+
stroke?: boolean;
|
|
420
|
+
solid?: boolean;
|
|
421
|
+
mono?: boolean;
|
|
422
|
+
disabled?: boolean;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
function Badge({
|
|
426
|
+
children,
|
|
427
|
+
color = "gray",
|
|
428
|
+
size = "md",
|
|
429
|
+
shape = "rounded",
|
|
430
|
+
icon,
|
|
431
|
+
stroke = false,
|
|
432
|
+
solid = false,
|
|
433
|
+
mono = false,
|
|
434
|
+
disabled = false,
|
|
435
|
+
className,
|
|
436
|
+
}: BadgeProps) {
|
|
437
|
+
return (
|
|
438
|
+
<StyledBadge
|
|
439
|
+
className={className}
|
|
440
|
+
$color={color}
|
|
441
|
+
$size={size}
|
|
442
|
+
$shape={shape}
|
|
443
|
+
$stroke={stroke}
|
|
444
|
+
$solid={solid}
|
|
445
|
+
$mono={mono}
|
|
446
|
+
$disabled={disabled}
|
|
447
|
+
>
|
|
448
|
+
{icon ? <Icon name={icon} size={badgeIconSizes[size]} /> : null}
|
|
449
|
+
{children}
|
|
450
|
+
</StyledBadge>
|
|
451
|
+
);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
export { Badge, httpMethodBadgeColor };
|
|
455
|
+
`;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare const codeTemplate = "\"use client\";\nimport { useState, useCallback, useMemo, useId } from \"react\";\nimport styled, { css } from \"styled-components\";\nimport {\n interactiveStyles,\n resetButton,\n styledCode,\n} from \"cherry-styled-components\";\nimport { Theme } from \"@/app/theme\";\nimport { unified } from \"unified\";\nimport rehypeParse from \"rehype-parse\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeStringify from \"rehype-stringify\";\nimport { Icon } from \"@/components/layout/Icon\";\nimport { thinScrollbar } from \"@/components/layout/SharedStyled\";\n\ninterface CodeProps extends Omit<\n React.HTMLAttributes<HTMLDivElement>,\n \"theme\"\n> {\n code: string;\n language?: string;\n title?: string;\n theme?: Theme;\n}\n\ninterface CodeTab {\n label: string;\n code: string;\n language?: string;\n}\n\ninterface CodeTabsProps extends Omit<\n React.HTMLAttributes<HTMLDivElement>,\n \"theme\"\n> {\n tabs: CodeTab[];\n theme?: Theme;\n}\n\nconst CodeWrapper = styled.span<{ theme: Theme }>`\n position: relative;\n z-index: 2;\n display: block;\n width: 100%;\n border-radius: ${({ theme }) => theme.spacing.radius.lg};\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n background: ${({ theme }) => theme.colors.light};\n`;\n\n/* Code block uses a fixed GitHub-style palette for the syntax tokens in both\n modes so highlighting stays legible regardless of brand colors. The dark-mode\n surface, however, matches the left sidebar's translucent brand tint\n (color-mix of theme.colors.primaryLight over the black page) instead of\n GitHub's #0d1117, so the window sits on the same background as the nav. Dark\n variants live in :root.dark & blocks so the swap happens via the active\n <html> class with no re-render. */\nconst TopBar = styled.div<{ theme: Theme }>`\n position: relative;\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 5%, transparent)`};\n border-top-left-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-top-right-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-bottom: solid 1px ${({ theme }) => theme.colors.grayLight};\n height: 33px;\n width: 100%;\n display: flex;\n justify-content: space-between;\n align-items: center;\n gap: 5px;\n padding: 0 10px;\n`;\n\nconst DotsContainer = styled.div`\n display: flex;\n gap: 5px;\n`;\n\nconst Dot = styled.span<{ theme: Theme }>`\n width: 10px;\n height: 10px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.1);\n\n :root.dark & {\n background: rgba(255, 255, 255, 0.1);\n }\n`;\n\n/* Icon-only copy button. interactiveStyles supplies the border highlight on\n hover plus the focus/active rings (no scale effect). Colors come from theme\n tokens (grayLight border, success/grayDark icon) that swap for dark mode via\n the theme prop, so no :root.dark & override is needed and the copied state\n reads consistently in both modes. */\nconst CopyButton = styled.button<{ theme: Theme; $copied: boolean }>`\n ${resetButton}\n ${interactiveStyles}\n background: ${({ theme }) => theme.colors.light};\n border-color: ${({ theme }) => theme.colors.grayLight};\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n padding: 4px;\n display: flex;\n align-items: center;\n justify-content: center;\n margin-right: -6px;\n\n & svg.lucide {\n margin: 0;\n color: ${({ theme, $copied }) =>\n $copied ? theme.colors.success : theme.colors.grayDark};\n }\n`;\n\n/* Centered file name in the TopBar. Sits absolutely over the flex row so the\n dots and copy button keep their edge alignment. Monospace + a muted grayDark\n token that swaps for dark mode via the theme prop. pointer-events off so\n clicks fall through to nothing and never steal focus from the buttons. */\nconst TopBarTitle = styled.span<{ theme: Theme }>`\n position: absolute;\n left: 50%;\n transform: translateX(-50%);\n max-width: 60%;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n pointer-events: none;\n font-family: ${({ theme }) => theme.fonts.mono};\n font-size: 12px;\n line-height: 2;\n color: ${({ theme }) => theme.colors.grayDark};\n`;\n\n/* Segmented control living in the TopBar for CodeTabs. Scrolls horizontally\n with the thin scrollbar when the labels overflow the 33px bar. */\nconst TabList = styled.div<{ theme: Theme }>`\n display: flex;\n align-items: center;\n gap: 2px;\n min-width: 0;\n overflow-x: auto;\n ${thinScrollbar};\n margin-left: -6px;\n`;\n\n/* Individual tab button. The active tab reads as part of the window: a\n theme.colors.light fill that echoes the code body, with a soft grayLight\n border. Inactive tabs are muted (grayDark) and transparent. resetButton\n strips native styling; focus-visible draws an inset brand primary ring on a\n pseudo-element so the scrolling TabList can't clip it. Colors come from theme\n tokens that swap for dark mode via the theme prop, so no :root.dark &\n override is needed. */\nconst CodeTab = styled.button<{ theme: Theme; $active: boolean }>`\n ${resetButton}\n flex: 0 0 auto;\n position: relative;\n font-family: ${({ theme }) => theme.fonts.mono};\n font-size: 12px;\n line-height: 1;\n padding: 5px;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n cursor: pointer;\n white-space: nowrap;\n transition:\n color 0.15s ease,\n background 0.15s ease,\n border-color 0.15s ease;\n border: solid 1px\n ${({ theme, $active }) =>\n $active ? theme.colors.grayLight : \"transparent\"};\n background: ${({ theme, $active }) =>\n $active ? theme.colors.light : \"transparent\"};\n color: ${({ theme, $active }) =>\n $active ? theme.colors.dark : theme.colors.grayDark};\n\n &:hover {\n color: ${({ theme }) => theme.colors.dark};\n background: ${({ theme, $active }) =>\n $active\n ? theme.colors.light\n : `color-mix(in srgb, ${theme.colors.dark} 4%, transparent)`};\n }\n\n &:focus-visible {\n outline: none;\n }\n\n &:focus-visible::after {\n content: \"\";\n position: absolute;\n inset: 2px;\n border: solid 2px ${({ theme }) => theme.colors.primary};\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n pointer-events: none;\n }\n`;\n\n/* GitHub Light syntax highlighting by default; GitHub Dark in :root.dark.\n Browser resolves which rule wins based on the active <html> class with no\n JS or React re-render involved. */\nconst lightSyntaxHighlight = css`\n & .hljs {\n color: #24292f;\n background: #ffffff;\n }\n & .hljs-doctag,\n & .hljs-keyword,\n & .hljs-meta .hljs-keyword,\n & .hljs-template-tag,\n & .hljs-template-variable,\n & .hljs-type,\n & .hljs-variable.language_ {\n color: #cf222e;\n }\n & .hljs-title,\n & .hljs-title.class_,\n & .hljs-title.class_.inherited__,\n & .hljs-title.function_ {\n color: #8250df;\n }\n & .hljs-attr,\n & .hljs-attribute,\n & .hljs-literal,\n & .hljs-meta,\n & .hljs-number,\n & .hljs-operator,\n & .hljs-selector-attr,\n & .hljs-selector-class,\n & .hljs-selector-id,\n & .hljs-variable {\n color: #0550ae;\n }\n & .hljs-meta .hljs-string,\n & .hljs-regexp,\n & .hljs-string {\n color: #0a3069;\n }\n & .hljs-built_in,\n & .hljs-symbol {\n color: #953800;\n }\n & .hljs-code,\n & .hljs-comment,\n & .hljs-formula {\n color: #6e7781;\n }\n & .hljs-name,\n & .hljs-quote,\n & .hljs-selector-pseudo,\n & .hljs-selector-tag {\n color: #116329;\n }\n & .hljs-subst {\n color: #24292f;\n }\n & .hljs-section {\n color: #0550ae;\n font-weight: 700;\n }\n & .hljs-bullet {\n color: #953800;\n }\n & .hljs-emphasis {\n color: #24292f;\n font-style: italic;\n }\n & .hljs-strong {\n color: #24292f;\n font-weight: 700;\n }\n`;\n\nconst darkSyntaxHighlight = css`\n & .hljs {\n color: #c9d1d9;\n background: transparent;\n }\n & .hljs-doctag,\n & .hljs-keyword,\n & .hljs-meta .hljs-keyword,\n & .hljs-template-tag,\n & .hljs-template-variable,\n & .hljs-type,\n & .hljs-variable.language_ {\n color: #ff7b72;\n }\n & .hljs-title,\n & .hljs-title.class_,\n & .hljs-title.class_.inherited__,\n & .hljs-title.function_ {\n color: #d2a8ff;\n }\n & .hljs-attr,\n & .hljs-attribute,\n & .hljs-literal,\n & .hljs-meta,\n & .hljs-number,\n & .hljs-operator,\n & .hljs-selector-attr,\n & .hljs-selector-class,\n & .hljs-selector-id,\n & .hljs-variable {\n color: #79c0ff;\n }\n & .hljs-meta .hljs-string,\n & .hljs-regexp,\n & .hljs-string {\n color: #a5d6ff;\n }\n & .hljs-built_in,\n & .hljs-symbol {\n color: #ffa657;\n }\n & .hljs-code,\n & .hljs-comment,\n & .hljs-formula {\n color: #8b949e;\n }\n & .hljs-name,\n & .hljs-quote,\n & .hljs-selector-pseudo,\n & .hljs-selector-tag {\n color: #7ee787;\n }\n & .hljs-subst {\n color: #c9d1d9;\n }\n & .hljs-section {\n color: #1f6feb;\n font-weight: 700;\n }\n & .hljs-bullet {\n color: #f2cc60;\n }\n & .hljs-emphasis {\n color: #c9d1d9;\n font-style: italic;\n }\n & .hljs-strong {\n color: #c9d1d9;\n font-weight: 700;\n }\n`;\n\nconst Body = styled.div<{ theme: Theme }>`\n background: #ffffff;\n border-bottom-left-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-bottom-right-radius: ${({ theme }) => theme.spacing.radius.lg};\n color: #24292f;\n padding: 20px 0;\n font-family: ${({ theme }) => theme.fonts.mono};\n text-align: left;\n overflow-x: auto;\n overflow-y: auto;\n max-height: calc(100dvh - 400px);\n ${thinScrollbar};\n ${({ theme }) => styledCode(theme)};\n ${lightSyntaxHighlight};\n\n /* The 20px side gutter sits on the <code> element instead of the Body so it\n scrolls with the content: the right gutter survives a scroll to the end of\n a long line, and diff rows can bleed back through it to the window edge. */\n & .hljs {\n display: block;\n width: 100%;\n min-width: min-content;\n padding: 0 20px;\n }\n\n /* Diff rows: highlight.js wraps each +/- line in its own span, so tinting it\n edge to edge reads like a GitHub diff. inline-table keeps the trailing\n newline as the line break instead of adding an empty row, and the negative\n margin pulls the row back through the gutter so the accent bar sits flush\n against the window edge. border-left + padding-left add up to the same\n 20px, keeping the code itself aligned with the untouched lines. The tints\n are the success/error tokens at low alpha, so a custom theme.json palette\n carries through and the syntax colors underneath stay legible. */\n & .hljs-addition,\n & .hljs-deletion {\n display: inline-table;\n width: calc(100% + 40px);\n margin: 0 -20px;\n padding: 0 20px 0 17px;\n border-left: solid 3px transparent;\n }\n\n & .hljs-addition {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.success} 12%, transparent)`};\n border-left-color: ${({ theme }) => theme.colors.success};\n }\n\n & .hljs-deletion {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.error} 12%, transparent)`};\n border-left-color: ${({ theme }) => theme.colors.error};\n }\n\n :root.dark & {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 5%, transparent)`};\n color: #ffffff;\n ${darkSyntaxHighlight};\n\n /* The tints sit on a near-black surface in dark mode, so they need a few\n more points of alpha to read at the same strength as in light. */\n & .hljs-addition {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.success} 16%, transparent)`};\n }\n\n & .hljs-deletion {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.error} 16%, transparent)`};\n }\n }\n`;\n\nconst escapeHtml = (unsafe: string): string => {\n return unsafe\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\");\n};\n\nconst sanitizeLanguage = (lang: string): string =>\n lang.replace(/[^a-zA-Z0-9_-]/g, \"\");\n\nconst highlightCode = (code: string, language: string): string => {\n const escapedCode = escapeHtml(code);\n const safeLang = sanitizeLanguage(language);\n const result = unified()\n .use(rehypeParse, { fragment: true })\n .use(rehypeHighlight, {\n detect: true,\n ignoreMissing: true,\n })\n .use(rehypeStringify)\n .processSync(\n `<pre><code class=\"language-${safeLang}\">${escapedCode}</code></pre>`,\n );\n\n return String(result);\n};\n\nfunction Code({\n code,\n language = \"javascript\",\n title,\n theme,\n className,\n}: CodeProps) {\n const [copied, setCopied] = useState(false);\n const highlightedCode = useMemo(\n () => highlightCode(code, language),\n [code, language],\n );\n\n const handleCopy = useCallback(async () => {\n try {\n await navigator.clipboard.writeText(code);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n } catch (err) {\n console.error(\"Failed to copy code:\", err);\n }\n }, [code]);\n\n return (\n <CodeWrapper\n className={`${className ?? \"\"} code-wrapper`.trim()}\n theme={theme}\n >\n <TopBar theme={theme}>\n <DotsContainer>\n <Dot theme={theme} />\n <Dot theme={theme} />\n <Dot theme={theme} />\n </DotsContainer>\n {title ? <TopBarTitle theme={theme}>{title}</TopBarTitle> : null}\n <CopyButton\n onClick={handleCopy}\n $copied={copied}\n theme={theme}\n aria-label={copied ? \"Copied\" : \"Copy code\"}\n >\n <Icon name={copied ? \"check\" : \"copy\"} size={12} />\n </CopyButton>\n </TopBar>\n <Body\n dangerouslySetInnerHTML={{ __html: highlightedCode }}\n theme={theme}\n className=\"code-wrapper-body\"\n />\n </CodeWrapper>\n );\n}\n\n/* Multi-variant code block (npm / pnpm / yarn, etc). Tabs replace the macOS\n dots in the TopBar as a keyboard-accessible tablist; the copy button copies\n whichever tab is active and resets its copied state when the tab changes. */\nfunction CodeTabs({ tabs, theme, className }: CodeTabsProps) {\n const baseId = useId();\n const [active, setActive] = useState(0);\n const [copied, setCopied] = useState(false);\n\n const safeActive = active < tabs.length ? active : 0;\n const activeTab = tabs[safeActive];\n\n const highlightedCode = useMemo(\n () =>\n activeTab\n ? highlightCode(activeTab.code, activeTab.language ?? \"bash\")\n : \"\",\n [activeTab],\n );\n\n const handleSelect = useCallback((index: number) => {\n setActive(index);\n setCopied(false);\n }, []);\n\n const handleCopy = useCallback(async () => {\n if (!activeTab) return;\n try {\n await navigator.clipboard.writeText(activeTab.code);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n } catch (err) {\n console.error(\"Failed to copy code:\", err);\n }\n }, [activeTab]);\n\n const handleKeyDown = useCallback(\n (event: React.KeyboardEvent<HTMLButtonElement>) => {\n let next = safeActive;\n if (event.key === \"ArrowRight\" || event.key === \"ArrowDown\") {\n next = (safeActive + 1) % tabs.length;\n } else if (event.key === \"ArrowLeft\" || event.key === \"ArrowUp\") {\n next = (safeActive - 1 + tabs.length) % tabs.length;\n } else if (event.key === \"Home\") {\n next = 0;\n } else if (event.key === \"End\") {\n next = tabs.length - 1;\n } else {\n return;\n }\n event.preventDefault();\n handleSelect(next);\n const target = document.getElementById(`${baseId}-tab-${next}`);\n if (target) target.focus();\n },\n [baseId, handleSelect, safeActive, tabs.length],\n );\n\n if (!tabs || tabs.length === 0) return null;\n\n return (\n <CodeWrapper\n className={`${className ?? \"\"} code-wrapper`.trim()}\n theme={theme}\n >\n <TopBar theme={theme}>\n <TabList role=\"tablist\" aria-label=\"Code variants\">\n {tabs.map((tab, index) => (\n <CodeTab\n key={`${tab.label}-${index}`}\n type=\"button\"\n role=\"tab\"\n id={`${baseId}-tab-${index}`}\n aria-selected={index === safeActive}\n aria-controls={`${baseId}-panel`}\n tabIndex={index === safeActive ? 0 : -1}\n onClick={() => handleSelect(index)}\n onKeyDown={handleKeyDown}\n $active={index === safeActive}\n theme={theme}\n >\n {tab.label}\n </CodeTab>\n ))}\n </TabList>\n <CopyButton\n onClick={handleCopy}\n $copied={copied}\n theme={theme}\n aria-label={copied ? \"Copied\" : \"Copy code\"}\n >\n <Icon name={copied ? \"check\" : \"copy\"} size={12} />\n </CopyButton>\n </TopBar>\n <Body\n id={`${baseId}-panel`}\n role=\"tabpanel\"\n aria-labelledby={`${baseId}-tab-${safeActive}`}\n dangerouslySetInnerHTML={{ __html: highlightedCode }}\n theme={theme}\n className=\"code-wrapper-body\"\n />\n </CodeWrapper>\n );\n}\n\nexport { Code, CodeTabs };\n";
|
|
1
|
+
export declare const codeTemplate = "\"use client\";\nimport { useState, useCallback, useMemo, useId } from \"react\";\nimport styled, { css } from \"styled-components\";\nimport {\n interactiveStyles,\n resetButton,\n styledCode,\n} from \"cherry-styled-components\";\nimport { Theme } from \"@/app/theme\";\nimport { unified } from \"unified\";\nimport rehypeParse from \"rehype-parse\";\nimport rehypeHighlight from \"rehype-highlight\";\nimport rehypeStringify from \"rehype-stringify\";\nimport { Icon } from \"@/components/layout/Icon\";\nimport { thinScrollbar } from \"@/components/layout/SharedStyled\";\n\ninterface CodeProps extends Omit<\n React.HTMLAttributes<HTMLDivElement>,\n \"theme\"\n> {\n code: string;\n language?: string;\n title?: string;\n theme?: Theme;\n}\n\ninterface CodeTab {\n label: string;\n code: string;\n language?: string;\n}\n\ninterface CodeTabsProps extends Omit<\n React.HTMLAttributes<HTMLDivElement>,\n \"theme\"\n> {\n tabs: CodeTab[];\n theme?: Theme;\n}\n\nconst CodeWrapper = styled.span<{ theme: Theme }>`\n position: relative;\n z-index: 2;\n display: block;\n width: 100%;\n border-radius: ${({ theme }) => theme.spacing.radius.lg};\n border: solid 1px ${({ theme }) => theme.colors.grayLight};\n background: ${({ theme }) => theme.colors.light};\n`;\n\n/* Code block uses a fixed GitHub-style palette for the syntax tokens in both\n modes so highlighting stays legible regardless of brand colors. The dark-mode\n surface, however, matches the left sidebar's translucent brand tint\n (color-mix of theme.colors.primaryLight over the black page) instead of\n GitHub's #0d1117, so the window sits on the same background as the nav. Dark\n variants live in :root.dark & blocks so the swap happens via the active\n <html> class with no re-render. */\nconst TopBar = styled.div<{ theme: Theme }>`\n position: relative;\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 5%, transparent)`};\n border-top-left-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-top-right-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-bottom: solid 1px ${({ theme }) => theme.colors.grayLight};\n height: 33px;\n width: 100%;\n display: flex;\n justify-content: space-between;\n align-items: center;\n gap: 5px;\n padding: 0 10px;\n`;\n\nconst DotsContainer = styled.div`\n display: flex;\n gap: 5px;\n`;\n\nconst Dot = styled.span<{ theme: Theme }>`\n width: 10px;\n height: 10px;\n border-radius: 50%;\n background: rgba(0, 0, 0, 0.1);\n\n :root.dark & {\n background: rgba(255, 255, 255, 0.1);\n }\n`;\n\n/* Icon-only copy button. interactiveStyles supplies the border highlight on\n hover plus the focus/active rings (no scale effect). Colors come from theme\n tokens (grayLight border, success/grayDark icon) that swap for dark mode via\n the theme prop, so no :root.dark & override is needed and the copied state\n reads consistently in both modes. */\nconst CopyButton = styled.button<{ theme: Theme; $copied: boolean }>`\n ${resetButton}\n ${interactiveStyles}\n background: ${({ theme }) => theme.colors.light};\n border-color: ${({ theme }) => theme.colors.grayLight};\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n padding: 4px;\n display: flex;\n align-items: center;\n justify-content: center;\n margin-right: -6px;\n\n & svg.lucide {\n margin: 0;\n color: ${({ theme, $copied }) =>\n $copied ? theme.colors.success : theme.colors.grayDark};\n }\n`;\n\n/* Centered file name in the TopBar. Sits absolutely over the flex row so the\n dots and copy button keep their edge alignment. Monospace + a muted grayDark\n token that swaps for dark mode via the theme prop. pointer-events off so\n clicks fall through to nothing and never steal focus from the buttons. */\nconst TopBarTitle = styled.span<{ theme: Theme }>`\n position: absolute;\n left: 50%;\n transform: translateX(-50%);\n max-width: 60%;\n overflow: hidden;\n text-overflow: ellipsis;\n white-space: nowrap;\n pointer-events: none;\n font-family: ${({ theme }) => theme.fonts.mono};\n font-size: 12px;\n line-height: 2;\n color: ${({ theme }) => theme.colors.grayDark};\n`;\n\n/* Segmented control living in the TopBar for CodeTabs. Scrolls horizontally\n with the thin scrollbar when the labels overflow the 33px bar. */\nconst TabList = styled.div<{ theme: Theme }>`\n display: flex;\n align-items: center;\n gap: 2px;\n min-width: 0;\n overflow-x: auto;\n ${thinScrollbar};\n padding: 2px;\n margin-left: -8px;\n`;\n\nconst CodeTab = styled.button<{ theme: Theme; $active: boolean }>`\n ${resetButton}\n ${interactiveStyles}\n flex: 0 0 auto;\n font-family: ${({ theme }) => theme.fonts.mono};\n font-size: 12px;\n line-height: 1;\n padding: 5px;\n border-radius: ${({ theme }) => theme.spacing.radius.xs};\n cursor: pointer;\n white-space: nowrap;\n transition: all 0.3s ease;\n border: solid 1px\n ${({ theme, $active }) =>\n $active ? theme.colors.grayLight : \"transparent\"};\n background: ${({ theme, $active }) =>\n $active ? theme.colors.light : \"transparent\"};\n color: ${({ theme, $active }) =>\n $active ? theme.colors.dark : theme.colors.grayDark};\n box-shadow: 0 0 0 0px ${({ theme }) => theme.colors.primary};\n\n &:hover {\n color: ${({ theme }) => theme.colors.dark};\n background: ${({ theme, $active }) =>\n $active\n ? theme.colors.light\n : `color-mix(in srgb, ${theme.colors.dark} 4%, transparent)`};\n }\n\n &:focus {\n border-color: ${({ theme }) => theme.colors.primary};\n box-shadow: 0 0 0 2px ${({ theme }) => theme.colors.primaryLight};\n }\n\n &:active {\n box-shadow: 0 0 0 1px ${({ theme }) => theme.colors.primaryLight};\n }\n`;\n\n/* GitHub Light syntax highlighting by default; GitHub Dark in :root.dark.\n Browser resolves which rule wins based on the active <html> class with no\n JS or React re-render involved. */\nconst lightSyntaxHighlight = css`\n & .hljs {\n color: #24292f;\n background: #ffffff;\n }\n & .hljs-doctag,\n & .hljs-keyword,\n & .hljs-meta .hljs-keyword,\n & .hljs-template-tag,\n & .hljs-template-variable,\n & .hljs-type,\n & .hljs-variable.language_ {\n color: #cf222e;\n }\n & .hljs-title,\n & .hljs-title.class_,\n & .hljs-title.class_.inherited__,\n & .hljs-title.function_ {\n color: #8250df;\n }\n & .hljs-attr,\n & .hljs-attribute,\n & .hljs-literal,\n & .hljs-meta,\n & .hljs-number,\n & .hljs-operator,\n & .hljs-selector-attr,\n & .hljs-selector-class,\n & .hljs-selector-id,\n & .hljs-variable {\n color: #0550ae;\n }\n & .hljs-meta .hljs-string,\n & .hljs-regexp,\n & .hljs-string {\n color: #0a3069;\n }\n & .hljs-built_in,\n & .hljs-symbol {\n color: #953800;\n }\n & .hljs-code,\n & .hljs-comment,\n & .hljs-formula {\n color: #6e7781;\n }\n & .hljs-name,\n & .hljs-quote,\n & .hljs-selector-pseudo,\n & .hljs-selector-tag {\n color: #116329;\n }\n & .hljs-subst {\n color: #24292f;\n }\n & .hljs-section {\n color: #0550ae;\n font-weight: 700;\n }\n & .hljs-bullet {\n color: #953800;\n }\n & .hljs-emphasis {\n color: #24292f;\n font-style: italic;\n }\n & .hljs-strong {\n color: #24292f;\n font-weight: 700;\n }\n`;\n\nconst darkSyntaxHighlight = css`\n & .hljs {\n color: #c9d1d9;\n background: transparent;\n }\n & .hljs-doctag,\n & .hljs-keyword,\n & .hljs-meta .hljs-keyword,\n & .hljs-template-tag,\n & .hljs-template-variable,\n & .hljs-type,\n & .hljs-variable.language_ {\n color: #ff7b72;\n }\n & .hljs-title,\n & .hljs-title.class_,\n & .hljs-title.class_.inherited__,\n & .hljs-title.function_ {\n color: #d2a8ff;\n }\n & .hljs-attr,\n & .hljs-attribute,\n & .hljs-literal,\n & .hljs-meta,\n & .hljs-number,\n & .hljs-operator,\n & .hljs-selector-attr,\n & .hljs-selector-class,\n & .hljs-selector-id,\n & .hljs-variable {\n color: #79c0ff;\n }\n & .hljs-meta .hljs-string,\n & .hljs-regexp,\n & .hljs-string {\n color: #a5d6ff;\n }\n & .hljs-built_in,\n & .hljs-symbol {\n color: #ffa657;\n }\n & .hljs-code,\n & .hljs-comment,\n & .hljs-formula {\n color: #8b949e;\n }\n & .hljs-name,\n & .hljs-quote,\n & .hljs-selector-pseudo,\n & .hljs-selector-tag {\n color: #7ee787;\n }\n & .hljs-subst {\n color: #c9d1d9;\n }\n & .hljs-section {\n color: #1f6feb;\n font-weight: 700;\n }\n & .hljs-bullet {\n color: #f2cc60;\n }\n & .hljs-emphasis {\n color: #c9d1d9;\n font-style: italic;\n }\n & .hljs-strong {\n color: #c9d1d9;\n font-weight: 700;\n }\n`;\n\nconst Body = styled.div<{ theme: Theme }>`\n background: #ffffff;\n border-bottom-left-radius: ${({ theme }) => theme.spacing.radius.lg};\n border-bottom-right-radius: ${({ theme }) => theme.spacing.radius.lg};\n color: #24292f;\n padding: 20px 0;\n font-family: ${({ theme }) => theme.fonts.mono};\n text-align: left;\n overflow-x: auto;\n overflow-y: auto;\n max-height: calc(100dvh - 400px);\n ${thinScrollbar};\n ${({ theme }) => styledCode(theme)};\n ${lightSyntaxHighlight};\n\n /* The 20px side gutter sits on the <code> element instead of the Body so it\n scrolls with the content: the right gutter survives a scroll to the end of\n a long line, and diff rows can bleed back through it to the window edge. */\n & .hljs {\n display: block;\n width: 100%;\n min-width: min-content;\n padding: 0 20px;\n }\n\n /* Diff rows: highlight.js wraps each +/- line in its own span, so tinting it\n edge to edge reads like a GitHub diff. inline-table keeps the trailing\n newline as the line break instead of adding an empty row, and the negative\n margin pulls the row back through the gutter so the accent bar sits flush\n against the window edge. border-left + padding-left add up to the same\n 20px, keeping the code itself aligned with the untouched lines. The tints\n are the success/error tokens at low alpha, so a custom theme.json palette\n carries through and the syntax colors underneath stay legible. */\n & .hljs-addition,\n & .hljs-deletion {\n display: inline-table;\n width: calc(100% + 40px);\n margin: 0 -20px;\n padding: 0 20px 0 17px;\n border-left: solid 3px transparent;\n }\n\n & .hljs-addition {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.success} 12%, transparent)`};\n border-left-color: ${({ theme }) => theme.colors.success};\n }\n\n & .hljs-deletion {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.error} 12%, transparent)`};\n border-left-color: ${({ theme }) => theme.colors.error};\n }\n\n :root.dark & {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.primaryLight} 5%, transparent)`};\n color: #ffffff;\n ${darkSyntaxHighlight};\n\n /* The tints sit on a near-black surface in dark mode, so they need a few\n more points of alpha to read at the same strength as in light. */\n & .hljs-addition {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.success} 16%, transparent)`};\n }\n\n & .hljs-deletion {\n background: ${({ theme }) =>\n `color-mix(in srgb, ${theme.colors.error} 16%, transparent)`};\n }\n }\n`;\n\nconst escapeHtml = (unsafe: string): string => {\n return unsafe\n .replace(/&/g, \"&\")\n .replace(/</g, \"<\")\n .replace(/>/g, \">\")\n .replace(/\"/g, \""\")\n .replace(/'/g, \"'\");\n};\n\nconst sanitizeLanguage = (lang: string): string =>\n lang.replace(/[^a-zA-Z0-9_-]/g, \"\");\n\nconst highlightCode = (code: string, language: string): string => {\n const escapedCode = escapeHtml(code);\n const safeLang = sanitizeLanguage(language);\n const result = unified()\n .use(rehypeParse, { fragment: true })\n .use(rehypeHighlight, {\n detect: true,\n ignoreMissing: true,\n })\n .use(rehypeStringify)\n .processSync(\n `<pre><code class=\"language-${safeLang}\">${escapedCode}</code></pre>`,\n );\n\n return String(result);\n};\n\nfunction Code({\n code,\n language = \"javascript\",\n title,\n theme,\n className,\n}: CodeProps) {\n const [copied, setCopied] = useState(false);\n const highlightedCode = useMemo(\n () => highlightCode(code, language),\n [code, language],\n );\n\n const handleCopy = useCallback(async () => {\n try {\n await navigator.clipboard.writeText(code);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n } catch (err) {\n console.error(\"Failed to copy code:\", err);\n }\n }, [code]);\n\n return (\n <CodeWrapper\n className={`${className ?? \"\"} code-wrapper`.trim()}\n theme={theme}\n >\n <TopBar theme={theme}>\n <DotsContainer>\n <Dot theme={theme} />\n <Dot theme={theme} />\n <Dot theme={theme} />\n </DotsContainer>\n {title ? <TopBarTitle theme={theme}>{title}</TopBarTitle> : null}\n <CopyButton\n onClick={handleCopy}\n $copied={copied}\n theme={theme}\n aria-label={copied ? \"Copied\" : \"Copy code\"}\n >\n <Icon name={copied ? \"check\" : \"copy\"} size={12} />\n </CopyButton>\n </TopBar>\n <Body\n dangerouslySetInnerHTML={{ __html: highlightedCode }}\n theme={theme}\n className=\"code-wrapper-body\"\n />\n </CodeWrapper>\n );\n}\n\n/* Multi-variant code block (npm / pnpm / yarn, etc). Tabs replace the macOS\n dots in the TopBar as a keyboard-accessible tablist; the copy button copies\n whichever tab is active and resets its copied state when the tab changes. */\nfunction CodeTabs({ tabs, theme, className }: CodeTabsProps) {\n const baseId = useId();\n const [active, setActive] = useState(0);\n const [copied, setCopied] = useState(false);\n\n const safeActive = active < tabs.length ? active : 0;\n const activeTab = tabs[safeActive];\n\n const highlightedCode = useMemo(\n () =>\n activeTab\n ? highlightCode(activeTab.code, activeTab.language ?? \"bash\")\n : \"\",\n [activeTab],\n );\n\n const handleSelect = useCallback((index: number) => {\n setActive(index);\n setCopied(false);\n }, []);\n\n const handleCopy = useCallback(async () => {\n if (!activeTab) return;\n try {\n await navigator.clipboard.writeText(activeTab.code);\n setCopied(true);\n setTimeout(() => setCopied(false), 2000);\n } catch (err) {\n console.error(\"Failed to copy code:\", err);\n }\n }, [activeTab]);\n\n const handleKeyDown = useCallback(\n (event: React.KeyboardEvent<HTMLButtonElement>) => {\n let next = safeActive;\n if (event.key === \"ArrowRight\" || event.key === \"ArrowDown\") {\n next = (safeActive + 1) % tabs.length;\n } else if (event.key === \"ArrowLeft\" || event.key === \"ArrowUp\") {\n next = (safeActive - 1 + tabs.length) % tabs.length;\n } else if (event.key === \"Home\") {\n next = 0;\n } else if (event.key === \"End\") {\n next = tabs.length - 1;\n } else {\n return;\n }\n event.preventDefault();\n handleSelect(next);\n const target = document.getElementById(`${baseId}-tab-${next}`);\n if (target) target.focus();\n },\n [baseId, handleSelect, safeActive, tabs.length],\n );\n\n if (!tabs || tabs.length === 0) return null;\n\n return (\n <CodeWrapper\n className={`${className ?? \"\"} code-wrapper`.trim()}\n theme={theme}\n >\n <TopBar theme={theme}>\n <TabList role=\"tablist\" aria-label=\"Code variants\">\n {tabs.map((tab, index) => (\n <CodeTab\n key={`${tab.label}-${index}`}\n type=\"button\"\n role=\"tab\"\n id={`${baseId}-tab-${index}`}\n aria-selected={index === safeActive}\n aria-controls={`${baseId}-panel`}\n tabIndex={index === safeActive ? 0 : -1}\n onClick={() => handleSelect(index)}\n onKeyDown={handleKeyDown}\n $active={index === safeActive}\n theme={theme}\n >\n {tab.label}\n </CodeTab>\n ))}\n </TabList>\n <CopyButton\n onClick={handleCopy}\n $copied={copied}\n theme={theme}\n aria-label={copied ? \"Copied\" : \"Copy code\"}\n >\n <Icon name={copied ? \"check\" : \"copy\"} size={12} />\n </CopyButton>\n </TopBar>\n <Body\n id={`${baseId}-panel`}\n role=\"tabpanel\"\n aria-labelledby={`${baseId}-tab-${safeActive}`}\n dangerouslySetInnerHTML={{ __html: highlightedCode }}\n theme={theme}\n className=\"code-wrapper-body\"\n />\n </CodeWrapper>\n );\n}\n\nexport { Code, CodeTabs };\n";
|