Skip to content

Commit 7cc5953

Browse files
authored
Merge pull request #13801 from ethereum/homepage-codeblock
migrate Codeblock component to tailwind, update colors
2 parents 4d13857 + 752a87f commit 7cc5953

File tree

1 file changed

+47
-65
lines changed

1 file changed

+47
-65
lines changed

src/components/Codeblock.tsx

Lines changed: 47 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -6,34 +6,32 @@ import Highlight, {
66
PrismTheme,
77
} from "prism-react-renderer"
88
import Prism from "prism-react-renderer/prism"
9-
import { Box, BoxProps, Flex, useColorModeValue } from "@chakra-ui/react"
109

1110
// https://github.com/FormidableLabs/prism-react-renderer/tree/master#custom-language-support
1211
import CopyToClipboard from "@/components/CopyToClipboard"
1312
import Emoji from "@/components/Emoji"
13+
import { Flex } from "@/components/ui/flex"
14+
15+
import { cn } from "@/lib/utils/cn"
1416

1517
import { LINES_BEFORE_COLLAPSABLE } from "@/lib/constants"
18+
19+
import useColorModeValue from "@/hooks/useColorModeValue"
1620
;(typeof global !== "undefined" ? global : window).Prism = Prism
1721
require("prismjs/components/prism-solidity")
1822

19-
const TopBarItem = (props: BoxProps) => {
20-
const bgColor = useColorModeValue("#f7f7f7", "#363641")
21-
23+
const TopBarItem = ({
24+
className,
25+
...props
26+
}: React.HTMLAttributes<HTMLDivElement>) => {
2227
return (
23-
<Box
24-
border="1px"
25-
borderRadius="base"
26-
borderColor="searchBorder"
27-
bg={bgColor}
28-
ms={2}
29-
py={1}
30-
px={2}
31-
_hover={{
32-
cursor: "pointer",
33-
color: "text100",
34-
transform: "scale(1.04)",
35-
boxShadow: "1px 1px 8px 1px rgba(0, 0, 0, 0.5)",
36-
}}
28+
<div
29+
className={cn(
30+
"ms-2 rounded border px-2 py-1 shadow-[1px_1px_8px_1px_rgba(var(--black),_0.5)] transition-transform duration-100",
31+
"hover:scale-105 hover:cursor-pointer hover:bg-gray-200 hover:shadow-md hover:transition-transform hover:duration-100",
32+
"bg-background-highlight hover:bg-background",
33+
className
34+
)}
3735
{...props}
3836
/>
3937
)
@@ -42,8 +40,8 @@ const TopBarItem = (props: BoxProps) => {
4240
const codeTheme = {
4341
light: {
4442
plain: {
45-
backgroundColor: "#fafafa",
46-
color: "#333333",
43+
backgroundColor: "#f7f7f7", // background-highlight (gray-50)
44+
color: "#6C24DF", // primary (purple-600)
4745
},
4846
styles: [
4947
{
@@ -114,8 +112,8 @@ const codeTheme = {
114112
dark: {
115113
// Pulled from `defaultProps.theme` for potential customization
116114
plain: {
117-
backgroundColor: "#2a2734",
118-
color: "#9a86fd",
115+
backgroundColor: "#121212", // background-highlight (gray-900)
116+
color: "#B38DF0", // primary (purple-400)
119117
},
120118
styles: [
121119
{
@@ -251,18 +249,17 @@ const Codeblock = ({
251249
return (
252250
/* Overwrites codeblocks inheriting RTL styling in Right-To-Left script languages (e.g. Arabic) */
253251
/* Context: https://github.com/ethereum/ethereum-org-website/issues/6202 */
254-
<Box position="relative" dir="ltr">
255-
<Box
256-
borderRadius="base"
257-
border={fromHomepage ? "none" : "1px solid"}
258-
borderColor="border"
259-
maxH={
260-
isCollapsed
252+
<div className="relative" dir="ltr">
253+
<div
254+
className={cn(
255+
"mb-4 overflow-scroll rounded",
256+
fromHomepage && "mb-0 border"
257+
)}
258+
style={{
259+
maxHeight: isCollapsed
261260
? `calc((1.2rem * ${LINES_BEFORE_COLLAPSABLE}) + 4.185rem)`
262-
: "none"
263-
}
264-
overflow="scroll"
265-
mb={fromHomepage ? 0 : 4}
261+
: "none",
262+
}}
266263
>
267264
<Highlight
268265
{...defaultProps}
@@ -271,53 +268,38 @@ const Codeblock = ({
271268
theme={selectedTheme as PrismTheme}
272269
>
273270
{({ className, style, tokens, getLineProps, getTokenProps }) => (
274-
<Box
275-
as="pre"
271+
<pre
272+
className={cn(
273+
"m-0 w-fit min-w-full overflow-visible py-6 ps-4",
274+
hasTopBar && "pt-[2.75rem]",
275+
className
276+
)}
276277
style={style}
277-
className={className}
278-
pt={hasTopBar ? "2.75rem" : 6}
279-
pb={6}
280-
ps={4}
281-
m={0}
282-
overflow="visible"
283-
minW="full"
284-
w="fit-content"
285278
>
286279
{tokens.map((line, i) => {
287280
return i === tokens.length - 1 &&
288281
line[0].content === "" ? null : (
289-
<Box
282+
<div
290283
key={i}
291-
display="table-row"
284+
style={{ display: "table-row" }}
292285
{...getLineProps({ line, key: i })}
293286
>
294287
{shouldShowLineNumbers && (
295-
<Box
296-
as="span"
297-
display="table-cell"
298-
textAlign="end"
299-
pe={8}
300-
userSelect="none"
301-
opacity={0.4}
302-
>
288+
<span className="table-cell select-none pe-8 text-end opacity-40">
303289
{i + 1}
304-
</Box>
290+
</span>
305291
)}
306-
<Box as="span" display="table-cell">
292+
<span className="table-cell">
307293
{line.map((token, key) => (
308294
<span key={key} {...getTokenProps({ token, key })} />
309295
))}
310-
</Box>
311-
</Box>
296+
</span>
297+
</div>
312298
)
313299
})}
314300
{!fromHomepage && (
315301
<Flex
316-
className={className}
317-
justify="flex-end"
318-
position="absolute"
319-
top={3}
320-
insetInlineEnd={4}
302+
className={cn("absolute end-4 top-3 justify-end", className)}
321303
>
322304
{allowCollapse &&
323305
totalLines - 1 > LINES_BEFORE_COLLAPSABLE && (
@@ -349,11 +331,11 @@ const Codeblock = ({
349331
)}
350332
</Flex>
351333
)}
352-
</Box>
334+
</pre>
353335
)}
354336
</Highlight>
355-
</Box>
356-
</Box>
337+
</div>
338+
</div>
357339
)
358340
}
359341

0 commit comments

Comments
 (0)