Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): Fixed CodeBlock line number width #5640

Merged
merged 2 commits into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/fast-glasses-grow.md
Original file line number Diff line number Diff line change
@@ -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.
Original file line number Diff line number Diff line change
@@ -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<typeof CodeBlock> = {
title: "Components/CodeBlock",
Expand Down Expand Up @@ -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>
)
},
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export type CodeSnippet = {
language: string
code: string
hideLineNumbers?: boolean
hideCopy?: boolean
}

type CodeBlockState = {
Expand Down Expand Up @@ -47,7 +48,7 @@ const Root = ({
<CodeBlockContext.Provider value={{ snippets, active, setActive }}>
<div
className={clx(
"border-ui-code-border overflow-hidden rounded-lg border",
"border-ui-code-border flex flex-col overflow-hidden rounded-lg border",
className
)}
{...props}
Expand Down Expand Up @@ -119,13 +120,18 @@ const Body = ({
const { active } = useCodeBlockContext()
return (
<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}
>
<Copy
content={active.code}
className="text-ui-code-icon absolute right-4 top-4"
/>
{!active.hideCopy && (
<Copy
content={active.code}
className="text-ui-code-icon absolute right-4 top-4"
/>
)}
<div className="max-w-[90%]">
<Highlight
theme={{
Expand Down Expand Up @@ -155,24 +161,38 @@ const Body = ({
>
{({ style, tokens, getLineProps, getTokenProps }) => (
<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,
background: "transparent",
}}
>
{tokens.map((line, i) => (
<div key={i} {...getLineProps({ line })} className="flex">
{!active.hideLineNumbers && (
<span className="text-ui-code-text-subtle">{i + 1}</span>
)}
<div className="pl-4">
{!active.hideLineNumbers && (
<div role="presentation" className="flex flex-col text-right">
{tokens.map((_, i) => (
<span
key={i}
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) => (
<span key={key} {...getTokenProps({ token })} />
))}
</div>
</div>
))}
))}
</div>
</pre>
)}
</Highlight>
Expand Down
2 changes: 1 addition & 1 deletion www/apps/ui/src/props/code-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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}[]",
},
},
]
Expand Down