Skip to content

Commit 1732add

Browse files
authored
Support a minimal CodeBlock (#766)
1 parent 2fb59f5 commit 1732add

File tree

3 files changed

+71
-35
lines changed

3 files changed

+71
-35
lines changed

.changeset/seven-kangaroos-repeat.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'scoobie': minor
3+
---
4+
5+
CodeBlock: allow copy and labels to be optional

src/components/CodeBlock.stories.tsx

+14
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,17 @@ export const Multi: Story = {
6969
},
7070
},
7171
};
72+
73+
export const Minimal: Story = {
74+
args: {
75+
children: JSON.stringify(
76+
{ stuff: 'things', otherStuff: [{ id: 17 }] },
77+
null,
78+
2,
79+
),
80+
label: '',
81+
language: 'json',
82+
copy: false,
83+
lineNumbers: false,
84+
},
85+
};

src/components/CodeBlock.tsx

+52-35
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface Props {
2828
language?: string;
2929
size?: Size;
3030
trim?: boolean;
31+
lineNumbers?: boolean;
32+
copy?: boolean;
3133
}
3234

3335
export const CodeBlock = ({
@@ -38,6 +40,8 @@ export const CodeBlock = ({
3840
language: rawLanguage,
3941
size = DEFAULT_SIZE,
4042
trim = true,
43+
lineNumbers = true,
44+
copy = true,
4145
}: Props) => {
4246
const children = normaliseChildren(
4347
typeof rawChildren === 'string'
@@ -90,47 +94,58 @@ export const CodeBlock = ({
9094
</Box>
9195
) : undefined;
9296

97+
const topRow =
98+
children.some(({ label }) => label) || copy || graphqlPlaygroundButton;
99+
93100
return (
94101
<Stack space={tablePadding}>
95-
<ScrollableInline whiteSpace="nowrap">
96-
<Box display="flex" justifyContent="spaceBetween">
97-
<Box display="flex">
98-
{children.map(({ label }, labelIndex) => (
99-
<Box
100-
component="span"
101-
key={label}
102-
paddingLeft={labelIndex === 0 ? undefined : tablePadding}
103-
>
104-
<Text
105-
size={smallerSize}
106-
tone={index.value === labelIndex ? 'secondary' : undefined}
107-
weight="medium"
108-
>
109-
{children.length === 1 || index.value === labelIndex ? (
110-
label
111-
) : (
112-
<TextLinkButton
113-
onClick={() =>
114-
setIndex({ dirty: true, value: labelIndex })
102+
{topRow ? (
103+
<ScrollableInline whiteSpace="nowrap">
104+
<Box display="flex" justifyContent="spaceBetween">
105+
<Box display="flex">
106+
{children.map(({ label }, labelIndex) =>
107+
label ? (
108+
<Box
109+
component="span"
110+
key={label}
111+
paddingLeft={labelIndex === 0 ? undefined : tablePadding}
112+
>
113+
<Text
114+
size={smallerSize}
115+
tone={
116+
index.value === labelIndex ? 'secondary' : undefined
115117
}
118+
weight="medium"
116119
>
117-
{label}
118-
</TextLinkButton>
119-
)}
120-
</Text>
121-
</Box>
122-
))}
123-
</Box>
124-
125-
<Box display="flex">
126-
<Box component="span" paddingLeft={tablePadding}>
127-
<CopyableText size={smallerSize}>{child.code}</CopyableText>
120+
{children.length === 1 || index.value === labelIndex ? (
121+
label
122+
) : (
123+
<TextLinkButton
124+
onClick={() =>
125+
setIndex({ dirty: true, value: labelIndex })
126+
}
127+
>
128+
{label}
129+
</TextLinkButton>
130+
)}
131+
</Text>
132+
</Box>
133+
) : null,
134+
)}
128135
</Box>
129136

130-
{graphqlPlaygroundButton}
137+
<Box display="flex">
138+
{copy ? (
139+
<Box component="span" paddingLeft={tablePadding}>
140+
<CopyableText size={smallerSize}>{child.code}</CopyableText>
141+
</Box>
142+
) : null}
143+
144+
{graphqlPlaygroundButton}
145+
</Box>
131146
</Box>
132-
</Box>
133-
</ScrollableInline>
147+
</ScrollableInline>
148+
) : null}
134149

135150
<Box borderRadius="large" className={styles.codeContainer}>
136151
<Highlight
@@ -141,7 +156,9 @@ export const CodeBlock = ({
141156
>
142157
{({ getTokenProps, tokens }) => (
143158
<Box display="flex">
144-
<LineNumbers count={tokens.length} size={size} />
159+
{lineNumbers ? (
160+
<LineNumbers count={tokens.length} size={size} />
161+
) : null}
145162

146163
<Lines getTokenProps={getTokenProps} lines={tokens} size={size} />
147164
</Box>

0 commit comments

Comments
 (0)