-
Notifications
You must be signed in to change notification settings - Fork 582
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add <CodeBlock /> component (#168)
This ports over a component from v1 that is needed for the v2 workspace page, and adds theme colors for it: <img width="772" alt="Screen Shot 2022-02-05 at 10 39 28 AM" src="https://user-images.githubusercontent.com/88213859/152654601-bab5acca-2e4a-4d53-890f-067a3b864040.png"> Component in storybook: ![image](https://user-images.githubusercontent.com/88213859/152654750-35565e5e-1f49-4b29-a140-cda757fb19aa.png) By bringing in these components ahead of time - it'll make the workspaces page PR smaller.
- Loading branch information
1 parent
6006436
commit e3089e9
Showing
4 changed files
with
108 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import { Story } from "@storybook/react" | ||
import React from "react" | ||
import { CodeBlock, CodeBlockProps } from "./index" | ||
|
||
const sampleLines = `Successfully assigned coder/image-jcws7 to cluster-1 | ||
Container image "gcr.io/coder-dogfood/master/coder-dev-ubuntu@sha256" already present on machine | ||
Created container user | ||
Started container user | ||
Using user 'coder' with shell '/bin/bash'`.split("\n") | ||
|
||
export default { | ||
title: "CodeBlock", | ||
component: CodeBlock, | ||
argTypes: { | ||
lines: { control: "object", defaultValue: sampleLines }, | ||
}, | ||
} | ||
|
||
const Template: Story<CodeBlockProps> = (args: CodeBlockProps) => <CodeBlock {...args} /> | ||
|
||
export const Example = Template.bind({}) | ||
Example.args = { | ||
lines: sampleLines, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { screen } from "@testing-library/react" | ||
import { render } from "../../test_helpers" | ||
import React from "react" | ||
import { CodeBlock } from "./index" | ||
|
||
describe("CodeBlock", () => { | ||
it("renders lines)", async () => { | ||
// When | ||
render(<CodeBlock lines={["line1", "line2"]} />) | ||
|
||
// Then | ||
// Both lines should be rendered | ||
await screen.findByText("line1") | ||
await screen.findByText("line2") | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { makeStyles } from "@material-ui/core/styles" | ||
import React from "react" | ||
|
||
export interface CodeBlockProps { | ||
lines: string[] | ||
} | ||
|
||
export const CodeBlock: React.FC<CodeBlockProps> = ({ lines }) => { | ||
const styles = useStyles() | ||
|
||
return ( | ||
<div className={styles.root}> | ||
{lines.map((line, idx) => ( | ||
<div className={styles.line} key={idx}> | ||
{line} | ||
</div> | ||
))} | ||
</div> | ||
) | ||
} | ||
const MONOSPACE_FONT_FAMILY = | ||
"'Fira Code', 'Lucida Console', 'Lucida Sans Typewriter', 'Liberation Mono', 'Monaco', 'Courier New', Courier, monospace" | ||
|
||
const useStyles = makeStyles((theme) => ({ | ||
root: { | ||
minHeight: 156, | ||
background: theme.palette.background.default, | ||
color: theme.palette.codeBlock.contrastText, | ||
fontFamily: MONOSPACE_FONT_FAMILY, | ||
fontSize: 13, | ||
wordBreak: "break-all", | ||
padding: theme.spacing(2), | ||
borderRadius: theme.shape.borderRadius, | ||
}, | ||
line: { | ||
whiteSpace: "pre-wrap", | ||
}, | ||
})) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters