fix(ui): Fixed CodeBlock line number width (#5640)
**What** - Fixes the width of lineNumbers in the CodeBlock component according to the widest number. - Introduces a new prop on snippets that allows users to hide the copy button. Also updated docs to reflect this. Before:  After: <img width="742" alt="image" src="https://github.com/medusajs/medusa/assets/45367945/5ce845f7-b8c7-411c-abfb-fa03448aeb1a"> Closes #5639
This commit is contained in:
@@ -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.
|
||||||
@@ -1,8 +1,8 @@
|
|||||||
import React from "react"
|
|
||||||
import type { Meta, StoryObj } from "@storybook/react"
|
import type { Meta, StoryObj } from "@storybook/react"
|
||||||
|
import React from "react"
|
||||||
|
|
||||||
import { CodeBlock } from "./code-block"
|
|
||||||
import { Label } from "../label"
|
import { Label } from "../label"
|
||||||
|
import { CodeBlock } from "./code-block"
|
||||||
|
|
||||||
const meta: Meta<typeof CodeBlock> = {
|
const meta: Meta<typeof CodeBlock> = {
|
||||||
title: "Components/CodeBlock",
|
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 (
|
||||||
|
<CodeBlock
|
||||||
|
snippets={[
|
||||||
|
{
|
||||||
|
code: code,
|
||||||
|
label: "Test",
|
||||||
|
language: "bash",
|
||||||
|
hideCopy: true,
|
||||||
|
},
|
||||||
|
]}
|
||||||
|
className="w-[700px]"
|
||||||
|
>
|
||||||
|
<CodeBlock.Body />
|
||||||
|
</CodeBlock>
|
||||||
|
)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ export type CodeSnippet = {
|
|||||||
language: string
|
language: string
|
||||||
code: string
|
code: string
|
||||||
hideLineNumbers?: boolean
|
hideLineNumbers?: boolean
|
||||||
|
hideCopy?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
type CodeBlockState = {
|
type CodeBlockState = {
|
||||||
@@ -47,7 +48,7 @@ const Root = ({
|
|||||||
<CodeBlockContext.Provider value={{ snippets, active, setActive }}>
|
<CodeBlockContext.Provider value={{ snippets, active, setActive }}>
|
||||||
<div
|
<div
|
||||||
className={clx(
|
className={clx(
|
||||||
"border-ui-code-border overflow-hidden rounded-lg border",
|
"border-ui-code-border flex flex-col overflow-hidden rounded-lg border",
|
||||||
className
|
className
|
||||||
)}
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
@@ -119,13 +120,18 @@ const Body = ({
|
|||||||
const { active } = useCodeBlockContext()
|
const { active } = useCodeBlockContext()
|
||||||
return (
|
return (
|
||||||
<div
|
<div
|
||||||
className={clx("bg-ui-code-bg-base relative p-4", className)}
|
className={clx(
|
||||||
|
"bg-ui-code-bg-base relative h-full overflow-y-auto p-4",
|
||||||
|
className
|
||||||
|
)}
|
||||||
{...props}
|
{...props}
|
||||||
>
|
>
|
||||||
<Copy
|
{!active.hideCopy && (
|
||||||
content={active.code}
|
<Copy
|
||||||
className="text-ui-code-icon absolute right-4 top-4"
|
content={active.code}
|
||||||
/>
|
className="text-ui-code-icon absolute right-4 top-4"
|
||||||
|
/>
|
||||||
|
)}
|
||||||
<div className="max-w-[90%]">
|
<div className="max-w-[90%]">
|
||||||
<Highlight
|
<Highlight
|
||||||
theme={{
|
theme={{
|
||||||
@@ -155,24 +161,38 @@ const Body = ({
|
|||||||
>
|
>
|
||||||
{({ style, tokens, getLineProps, getTokenProps }) => (
|
{({ style, tokens, getLineProps, getTokenProps }) => (
|
||||||
<pre
|
<pre
|
||||||
className="txt-compact-small whitespace-pre-wrap bg-transparent font-mono"
|
className={clx(
|
||||||
|
"txt-compact-small whitespace-pre-wrap bg-transparent font-mono",
|
||||||
|
{
|
||||||
|
"grid grid-cols-[auto,1fr] gap-x-4": !active.hideLineNumbers,
|
||||||
|
}
|
||||||
|
)}
|
||||||
style={{
|
style={{
|
||||||
...style,
|
...style,
|
||||||
background: "transparent",
|
background: "transparent",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{tokens.map((line, i) => (
|
{!active.hideLineNumbers && (
|
||||||
<div key={i} {...getLineProps({ line })} className="flex">
|
<div role="presentation" className="flex flex-col text-right">
|
||||||
{!active.hideLineNumbers && (
|
{tokens.map((_, i) => (
|
||||||
<span className="text-ui-code-text-subtle">{i + 1}</span>
|
<span
|
||||||
)}
|
key={i}
|
||||||
<div className="pl-4">
|
className="text-ui-code-text-subtle tabular-nums"
|
||||||
|
>
|
||||||
|
{i + 1}
|
||||||
|
</span>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
<div>
|
||||||
|
{tokens.map((line, i) => (
|
||||||
|
<div key={i} {...getLineProps({ line })}>
|
||||||
{line.map((token, key) => (
|
{line.map((token, key) => (
|
||||||
<span key={key} {...getTokenProps({ token })} />
|
<span key={key} {...getTokenProps({ token })} />
|
||||||
))}
|
))}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
))}
|
||||||
))}
|
</div>
|
||||||
</pre>
|
</pre>
|
||||||
)}
|
)}
|
||||||
</Highlight>
|
</Highlight>
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ const codeBlockProps: PropDataMap = [
|
|||||||
type: "object",
|
type: "object",
|
||||||
name: "CodeSnippet[]",
|
name: "CodeSnippet[]",
|
||||||
shape:
|
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}[]",
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
]
|
]
|
||||||
|
|||||||
Reference in New Issue
Block a user