From 2de3be05dfb9a856db6e44702b7e4895142a87e2 Mon Sep 17 00:00:00 2001 From: Kasper Date: Wed, 15 Nov 2023 12:38:02 +0100 Subject: [PATCH] fix(ui): Fixed CodeBlock line number width --- .changeset/fast-glasses-grow.md | 5 ++ .../code-block/code-block.stories.tsx | 37 +++++++++++++- .../src/components/code-block/code-block.tsx | 50 +++++++++++++------ www/apps/ui/src/props/code-block.tsx | 2 +- 4 files changed, 76 insertions(+), 18 deletions(-) create mode 100644 .changeset/fast-glasses-grow.md diff --git a/.changeset/fast-glasses-grow.md b/.changeset/fast-glasses-grow.md new file mode 100644 index 0000000000000..bf6da0e6f3215 --- /dev/null +++ b/.changeset/fast-glasses-grow.md @@ -0,0 +1,5 @@ +--- +"@medusajs/ui": patch +--- + +fix(ui): Fix the width of line numbers in the CodeBlock component, such that they are always the same width as the widest line number. diff --git a/packages/design-system/ui/src/components/code-block/code-block.stories.tsx b/packages/design-system/ui/src/components/code-block/code-block.stories.tsx index a981f9798a7b3..17ad7cd35cd7f 100644 --- a/packages/design-system/ui/src/components/code-block/code-block.stories.tsx +++ b/packages/design-system/ui/src/components/code-block/code-block.stories.tsx @@ -1,8 +1,8 @@ -import React from "react" import type { Meta, StoryObj } from "@storybook/react" +import React from "react" -import { CodeBlock } from "./code-block" import { Label } from "../label" +import { CodeBlock } from "./code-block" const meta: Meta = { title: "Components/CodeBlock", @@ -50,3 +50,36 @@ export const Default: Story = { ) }, } + +const code = `medusa develop +✔ Models initialized – 14ms +✔ Repositories initialized – 35ms +✔ Strategies initialized – 24ms +✔ Modules initialized – 1ms +✔ Database initialized – 654ms +✔ Services initialized – 7ms +✔ Express intialized – 5ms +✔ Plugins intialized – 7ms +✔ Subscribers initialized – 6ms +✔ API initialized – 28ms +✔ Server is ready on port: 9000` + +export const ManyLines: Story = { + render: () => { + return ( + + + + ) + }, +} diff --git a/packages/design-system/ui/src/components/code-block/code-block.tsx b/packages/design-system/ui/src/components/code-block/code-block.tsx index a7f716a85fc56..b617159822be0 100644 --- a/packages/design-system/ui/src/components/code-block/code-block.tsx +++ b/packages/design-system/ui/src/components/code-block/code-block.tsx @@ -10,6 +10,7 @@ export type CodeSnippet = { language: string code: string hideLineNumbers?: boolean + hideCopy?: boolean } type CodeBlockState = { @@ -47,7 +48,7 @@ const Root = ({
- + {!active.hideCopy && ( + + )}
{({ style, tokens, getLineProps, getTokenProps }) => (
-              {tokens.map((line, i) => (
-                
- {!active.hideLineNumbers && ( - {i + 1} - )} -
+ {!active.hideLineNumbers && ( +
+ {tokens.map((_, i) => ( + + {i + 1} + + ))} +
+ )} +
+ {tokens.map((line, i) => ( +
{line.map((token, key) => ( ))}
-
- ))} + ))} +
)}
diff --git a/www/apps/ui/src/props/code-block.tsx b/www/apps/ui/src/props/code-block.tsx index 090216e5f87f1..abbc7681c5d74 100644 --- a/www/apps/ui/src/props/code-block.tsx +++ b/www/apps/ui/src/props/code-block.tsx @@ -8,7 +8,7 @@ const codeBlockProps: PropDataMap = [ type: "object", name: "CodeSnippet[]", shape: - "{\n label: string\n language: string\n code: string\n hideLineNumbers?: boolean\n}[]", + "{\n label: string\n language: string\n code: string\n hideLineNumbers?: boolean\n hideCopy?: boolean\n}[]", }, }, ]