diff --git a/.changeset/plain-code-highlighted.md b/.changeset/plain-code-highlighted.md new file mode 100644 index 000000000..4ba0e6dc4 --- /dev/null +++ b/.changeset/plain-code-highlighted.md @@ -0,0 +1,5 @@ +--- +"@cloudflare/kumo": patch +--- + +Add a plain CodeHighlighted variant for frameless, padding-free code blocks with a correctly positioned copy button. diff --git a/packages/kumo-docs-astro/src/components/demos/CodeHighlightedDemo.tsx b/packages/kumo-docs-astro/src/components/demos/CodeHighlightedDemo.tsx index 867b2cd68..b4f882b35 100644 --- a/packages/kumo-docs-astro/src/components/demos/CodeHighlightedDemo.tsx +++ b/packages/kumo-docs-astro/src/components/demos/CodeHighlightedDemo.tsx @@ -186,6 +186,53 @@ export function CodeHighlightedCopyButtonDemo() { ); } +/** Frameless code block for embedding in another surface */ +export function CodeHighlightedPlainDemo() { + return ( + +
+
+ +
+
+ +
+
+
+ ); +} + /** Full featured example */ export function CodeHighlightedFullFeaturedDemo() { return ( diff --git a/packages/kumo-docs-astro/src/components/skill/design-tips.tsx b/packages/kumo-docs-astro/src/components/skill/design-tips.tsx index 11204992a..19a46b2f8 100644 --- a/packages/kumo-docs-astro/src/components/skill/design-tips.tsx +++ b/packages/kumo-docs-astro/src/components/skill/design-tips.tsx @@ -24,9 +24,10 @@ interface CodeExampleProps { export function CodeExample({ code }: CodeExampleProps) { return ( ); } diff --git a/packages/kumo-docs-astro/src/pages/components/code-highlighted.mdx b/packages/kumo-docs-astro/src/pages/components/code-highlighted.mdx index 2910ecdb8..3df018353 100644 --- a/packages/kumo-docs-astro/src/pages/components/code-highlighted.mdx +++ b/packages/kumo-docs-astro/src/pages/components/code-highlighted.mdx @@ -16,6 +16,7 @@ import { CodeHighlightedHighlightLinesDemo, CodeHighlightedLineNumbersDemo, CodeHighlightedCopyButtonDemo, + CodeHighlightedPlainDemo, CodeHighlightedFullFeaturedDemo, CodeHighlightedSharedProviderDemo, CodeHighlightedCssDemo, @@ -173,6 +174,16 @@ export function App() { +### Plain + +

+ Use `variant="plain"` to remove the outer surface, border, and rounded corners + when embedding highlighted code inside another container. +

+ + + + ### Full Featured

Combine all features for a complete code display experience.

@@ -568,6 +579,16 @@ import { ShikiProvider, CodeHighlighted } from "@cloudflare/kumo/code"; Language identifier (must be in provider's languages) + + + variant + + + "default" | "plain" + + No + Visual style of the code block + showLineNumbers diff --git a/packages/kumo/src/code/code-highlighted.tsx b/packages/kumo/src/code/code-highlighted.tsx index 05b027816..8939b07d8 100644 --- a/packages/kumo/src/code/code-highlighted.tsx +++ b/packages/kumo/src/code/code-highlighted.tsx @@ -34,6 +34,7 @@ import type { CodeHighlightedProps } from "./types"; export function CodeHighlighted({ code, lang, + variant = "default", showLineNumbers = false, highlightLines, showCopyButton = false, @@ -76,17 +77,45 @@ export function CodeHighlighted({ // Container styles - use flex layout for single-line with copy button // Includes defensive resets (m-0, p-0) to prevent global CSS pollution const containerClasses = cn( - "group relative m-0 w-full min-w-0 rounded-md border border-kumo-fill bg-kumo-base p-0", + "group relative m-0 w-full min-w-0 p-0", + variant === "plain" + ? "rounded-none border-0 bg-transparent" + : "rounded-md border border-kumo-fill bg-kumo-base", showCopyButton && isSingleLine && "flex items-center", className, ); + const fallbackCodeClasses = cn( + // Prevent global pre styles from affecting the loading and error states. + "!m-0 min-w-0 flex-1 overflow-x-auto", + "font-mono text-sm leading-relaxed text-kumo-subtle", + variant === "plain" ? "!p-0" : "!p-4", + ); + + const highlightedCodeClasses = cn( + "kumo-shiki", + // Reset the code element generated by Shiki. + "[&_code]:!m-0 [&_code]:!border-0 [&_code]:!bg-transparent [&_code]:!p-0", + // Let the Kumo container provide the frame and background. + "[&>pre]:!m-0 [&>pre]:!rounded-none [&>pre]:!border-0 [&>pre]:!bg-transparent", + "[&>pre]:font-mono [&>pre]:text-sm [&>pre]:leading-relaxed", + variant === "plain" ? "[&>pre]:!p-0" : "[&>pre]:!p-4", + // Highlighted lines normally extend into the default padding. A plain + // block has no padding, so keep its highlights within the content width. + variant === "plain" && + "[&_.line-highlighted]:!m-0 [&_.line-highlighted]:!w-full [&_.line-highlighted]:!px-0", + ); + // Copy button - inline for single-line, absolute for multi-line // Hidden until hover (or when showing "Copied!" feedback) const copyButton = showCopyButton ? (
@@ -105,7 +134,10 @@ export function CodeHighlighted({ const lineNumbers = showLineNumbers && !isSingleLine ? (