diff --git a/src/components/code-block/code-block.stories.tsx b/src/components/code-block/code-block.stories.tsx index 8a6b6127..49c19aaa 100644 --- a/src/components/code-block/code-block.stories.tsx +++ b/src/components/code-block/code-block.stories.tsx @@ -1,6 +1,6 @@ import type { Meta, StoryObj } from "@storybook/react"; -import { CodeBlock, CodeBlockBody, CodeBlockContent, CodeBlockHeader } from "."; +import { CodeBlock, CodeBlockBody, CodeBlockContent, CodeBlockCopyButton, CodeBlockHeader, CodeBlockTitle } from "."; const meta = { title: "CodeBlock", @@ -25,13 +25,47 @@ const CodeIcon = () => ( export const WithHeaderHighlightsAndHighlights: Story = { render: () => ( -
- {showLineNumbers && ( -- {lines.map((line, index) => { - const lineNumber = index + 1; - const shouldHighlight = highlightLineNumberSet.has(lineNumber); - - return ( - - {lineNumber} - - ); - })} -- )} -+ useEffect(() => { + setCopyText(trimmedCode); + }, [trimmedCode, setCopyText]); + + return ( +
+ {/* TODO(cody): maybe retry this, but use grid instead? */} + {/* {showLineNumbers && ( ++ ); +}); CodeBlockContent.displayName = "CodeBlockContent"; -const CodeBlockHeader = forwardRef{lines.map((line, index) => { const lineNumber = index + 1; const shouldHighlight = highlightLineNumberSet.has(lineNumber); @@ -89,36 +89,63 @@ const CodeBlockContent = forwardRef+ )} */} +( return ( - {line === "" ? "\n" : line} + {lineNumber} ); })} - - - ); - }, -); + + {lines.map((line, index) => { + const lineNumber = index + 1; + const shouldHighlight = highlightLineNumberSet.has(lineNumber); + + return ( + + {line === "" ? "\n" : line} + + ); + })} +
+( - ({ children, className, style }, ref) => ( - - {children} -- ), -); +const CodeBlockHeader = forwardRef>(({ className, ...props }, ref) => ( + +)); CodeBlockHeader.displayName = "CodeBlockHeader"; +const CodeBlockTitle = forwardRef & { asChild?: boolean }>( + ({ asChild = false, className, ...props }, ref) => { + const Comp = asChild ? Slot : "h3"; + return ; + }, +); +CodeBlockTitle.displayName = "CodeBlockTitle"; + type CodeBlockCopyButtonProps = WithStyleProps & { onCopy?: (value: string) => void; onCopyError?: (error: unknown) => void; @@ -126,21 +153,21 @@ type CodeBlockCopyButtonProps = WithStyleProps & { const CodeBlockCopyButton = forwardRef ( ({ className, onCopy, onCopyError, style }, ref) => { - const ctx = useContext(CodeBlockCopyContext); - const [, setCopied] = useState(false); + const copyText = useContext(CodeBlockCopyContext); + const [, setCopied] = useState(false); // todo: useme return (