|
@ -1,9 +1,20 @@ |
|
|
|
|
|
/** @jsx jsx */ |
|
|
import React, { Children } from 'react'; |
|
|
import React, { Children } from 'react'; |
|
|
|
|
|
import { |
|
|
import { Box, Flex, BoxProps, color, space, useClipboard, themeColor } from '@stacks/ui'; |
|
|
Box, |
|
|
|
|
|
Flex, |
|
|
|
|
|
BoxProps, |
|
|
|
|
|
Fade, |
|
|
|
|
|
color, |
|
|
|
|
|
space, |
|
|
|
|
|
useClipboard, |
|
|
|
|
|
themeColor, |
|
|
|
|
|
FlexProps, |
|
|
|
|
|
} from '@stacks/ui'; |
|
|
import { ClipboardCheckIcon } from '@components/icons/clipboard-check'; |
|
|
import { ClipboardCheckIcon } from '@components/icons/clipboard-check'; |
|
|
import { border, onlyText } from '@common/utils'; |
|
|
import { border, onlyText } from '@common/utils'; |
|
|
import { css } from '@stacks/ui-core'; |
|
|
import { css, ForwardRefExoticComponentWithAs, forwardRefWithAs, Theme } from '@stacks/ui-core'; |
|
|
|
|
|
import { jsx } from '@emotion/react'; |
|
|
import { Text } from '@components/typography'; |
|
|
import { Text } from '@components/typography'; |
|
|
import { useHover } from 'use-events'; |
|
|
import { useHover } from 'use-events'; |
|
|
import { IconButton } from '@components/icon-button'; |
|
|
import { IconButton } from '@components/icon-button'; |
|
@ -39,63 +50,85 @@ const generateCssStylesForHighlightedLines = (numbers: number[] = []) => { |
|
|
return record; |
|
|
return record; |
|
|
}; |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
const CodeCopyButton: React.FC< |
|
|
|
|
|
{ onCopy?: () => void; lines?: number; hasCopied?: boolean; styles?: any } & FlexProps |
|
|
|
|
|
> = ({ onCopy, lines, hasCopied, styles, ...props }) => { |
|
|
|
|
|
const CopyIcon = hasCopied ? ClipboardCheckIcon : BaseCopyIcon; |
|
|
|
|
|
return ( |
|
|
|
|
|
<Flex |
|
|
|
|
|
size="56px" |
|
|
|
|
|
justifyContent="flex-end" |
|
|
|
|
|
alignItems="flex-start" |
|
|
|
|
|
position="absolute" |
|
|
|
|
|
right="0" |
|
|
|
|
|
top="0" |
|
|
|
|
|
zIndex={999999} |
|
|
|
|
|
px={space('base')} |
|
|
|
|
|
py={lines === 1 ? '10px' : space('base')} |
|
|
|
|
|
display={['none', 'none', 'flex']} |
|
|
|
|
|
pointerEvents="none" |
|
|
|
|
|
style={styles} |
|
|
|
|
|
{...(props as any)} |
|
|
|
|
|
> |
|
|
|
|
|
<IconButton |
|
|
|
|
|
title="Copy to clipboard" |
|
|
|
|
|
bg="ink.900" |
|
|
|
|
|
_hover={{ |
|
|
|
|
|
color: 'white', |
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
bg: themeColor('ink.900'), |
|
|
|
|
|
}} |
|
|
|
|
|
color={themeColor('ink.400') as any} |
|
|
|
|
|
onClick={onCopy} |
|
|
|
|
|
size="35px" |
|
|
|
|
|
p="0" |
|
|
|
|
|
display="grid" |
|
|
|
|
|
placeItems="center" |
|
|
|
|
|
style={{ |
|
|
|
|
|
pointerEvents: 'all', |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
<CopyIcon size="20px" /> |
|
|
|
|
|
</IconButton> |
|
|
|
|
|
</Flex> |
|
|
|
|
|
); |
|
|
|
|
|
}; |
|
|
|
|
|
|
|
|
export const Code: React.FC< |
|
|
export const Code: React.FC< |
|
|
BoxProps & { highlight?: string; lang?: string; lines: number } |
|
|
BoxProps & { highlight?: string; lang?: string; lines: number } |
|
|
> = React.memo( |
|
|
> = React.memo( |
|
|
React.forwardRef(({ children, highlight, lang, lines, ...rest }, ref) => { |
|
|
React.forwardRef(({ children, highlight, lang, lines, ...rest }, ref) => { |
|
|
const [hover, bind] = useHover(); |
|
|
const [hover, bind] = useHover(); |
|
|
|
|
|
|
|
|
const numbers = getHighlightLineNumbers(highlight); |
|
|
const numbers = getHighlightLineNumbers(highlight); |
|
|
|
|
|
|
|
|
const convertSingleChildToString = child => onlyText(child).replace(/\n/g, ''); |
|
|
const convertSingleChildToString = child => onlyText(child).replace(/\n/g, ''); |
|
|
const tokenLines = Children.toArray(children).map(convertSingleChildToString); |
|
|
const tokenLines = Children.toArray(children).map(convertSingleChildToString); |
|
|
|
|
|
|
|
|
const codeString = tokenLines.join('\n').replace(/\n\n\n/g, '\n\n'); |
|
|
const codeString = tokenLines.join('\n').replace(/\n\n\n/g, '\n\n'); |
|
|
|
|
|
|
|
|
const { hasCopied, onCopy } = useClipboard(codeString); |
|
|
const { hasCopied, onCopy } = useClipboard(codeString); |
|
|
|
|
|
const hasLineNumbers = lines > LINE_MINIMUM && lang !== 'bash'; |
|
|
const CopyIcon = hasCopied ? ClipboardCheckIcon : BaseCopyIcon; |
|
|
|
|
|
|
|
|
|
|
|
return ( |
|
|
return ( |
|
|
<Box overflow="hidden" position="relative" {...bind}> |
|
|
|
|
|
<Box |
|
|
<Box |
|
|
className={lines <= 3 ? 'no-line-numbers' : ''} |
|
|
overflow="hidden" |
|
|
position="relative" |
|
|
position="relative" |
|
|
ref={ref as any} |
|
|
css={(theme: Theme) => |
|
|
overflowX="auto" |
|
|
css({ |
|
|
> |
|
|
|
|
|
<Box |
|
|
|
|
|
as="code" |
|
|
|
|
|
{...{ |
|
|
|
|
|
width: '100%', |
|
|
|
|
|
display: 'flex', |
|
|
|
|
|
flexDirection: 'column', |
|
|
|
|
|
minWidth: 'fit-content', |
|
|
|
|
|
counterReset: 'line', |
|
|
|
|
|
pr: space(['base-loose', 'base-loose', 'extra-loose', 'extra-loose']), |
|
|
|
|
|
pl: |
|
|
|
|
|
lines <= LINE_MINIMUM || lang === 'bash' |
|
|
|
|
|
? space(['extra-loose', 'extra-loose', 'base-loose', 'base-loose']) |
|
|
|
|
|
: 'unset', |
|
|
|
|
|
}} |
|
|
|
|
|
css={css({ |
|
|
|
|
|
'.token-line': { |
|
|
'.token-line': { |
|
|
display: 'inline-block', |
|
|
counterIncrement: 'line', |
|
|
...generateCssStylesForHighlightedLines(numbers), |
|
|
|
|
|
}, |
|
|
|
|
|
'& .token-line': { |
|
|
|
|
|
'&__empty': { |
|
|
'&__empty': { |
|
|
height: '24px', |
|
|
height: '24px', |
|
|
}, |
|
|
}, |
|
|
'.comment': { |
|
|
'.comment': { |
|
|
color: 'rgba(255,255,255,0.5) !important', |
|
|
color: 'rgba(255,255,255,0.5) !important', |
|
|
}, |
|
|
}, |
|
|
|
|
|
...generateCssStylesForHighlightedLines(numbers), |
|
|
display: 'flex', |
|
|
display: 'flex', |
|
|
fontSize: '14px', |
|
|
fontSize: '14px', |
|
|
'&::before': |
|
|
pl: !hasLineNumbers |
|
|
lines > LINE_MINIMUM && lang !== 'bash' |
|
|
? space(['extra-loose', 'extra-loose', '20px', '20px']) |
|
|
|
|
|
: undefined, |
|
|
|
|
|
'&:before': hasLineNumbers |
|
|
? { |
|
|
? { |
|
|
counterIncrement: 'line', |
|
|
flexShrink: 0, |
|
|
content: 'counter(line, decimal-leading-zero)', |
|
|
content: 'counter(line, decimal-leading-zero)', |
|
|
display: 'grid', |
|
|
display: 'grid', |
|
|
placeItems: 'center', |
|
|
placeItems: 'center', |
|
@ -108,15 +141,24 @@ export const Code: React.FC< |
|
|
} |
|
|
} |
|
|
: {}, |
|
|
: {}, |
|
|
}, |
|
|
}, |
|
|
})} |
|
|
})(theme) |
|
|
{...rest} |
|
|
} |
|
|
|
|
|
{...bind} |
|
|
|
|
|
> |
|
|
|
|
|
<Box |
|
|
|
|
|
className={lines <= 3 ? 'no-line-numbers' : ''} |
|
|
|
|
|
position="relative" |
|
|
|
|
|
ref={ref as any} |
|
|
|
|
|
overflowX="auto" |
|
|
> |
|
|
> |
|
|
|
|
|
<Box as="code" {...(rest as any)}> |
|
|
<Box height="16px" width="100%" /> |
|
|
<Box height="16px" width="100%" /> |
|
|
<Box |
|
|
<Box |
|
|
as="span" |
|
|
as="span" |
|
|
position="absolute" |
|
|
position="absolute" |
|
|
color="transparent" |
|
|
color="transparent" |
|
|
top="16px" |
|
|
top="16px" |
|
|
|
|
|
pr={space(['extra-loose', 'extra-loose', '20px', '20px'])} |
|
|
left={ |
|
|
left={ |
|
|
lines <= LINE_MINIMUM || lang === 'bash' |
|
|
lines <= LINE_MINIMUM || lang === 'bash' |
|
|
? space(['extra-loose', 'extra-loose', '20px', '20px']) |
|
|
? space(['extra-loose', 'extra-loose', '20px', '20px']) |
|
@ -132,46 +174,19 @@ export const Code: React.FC< |
|
|
userSelect: 'none', |
|
|
userSelect: 'none', |
|
|
pointerEvents: 'none', |
|
|
pointerEvents: 'none', |
|
|
}} |
|
|
}} |
|
|
|
|
|
display="flex" |
|
|
|
|
|
flexDirection="column" |
|
|
> |
|
|
> |
|
|
{children} |
|
|
{children} |
|
|
</Box> |
|
|
</Box> |
|
|
<Box height="16px" width="100%" /> |
|
|
<Box height="16px" width="100%" /> |
|
|
</Box> |
|
|
</Box> |
|
|
</Box> |
|
|
</Box> |
|
|
{hover ? ( |
|
|
<Fade in={hover}> |
|
|
<Flex |
|
|
{styles => ( |
|
|
size="80%" |
|
|
<CodeCopyButton styles={styles} hasCopied={hasCopied} lines={lines} onCopy={onCopy} /> |
|
|
justifyContent="flex-end" |
|
|
)} |
|
|
alignItems="flex-start" |
|
|
</Fade> |
|
|
position="absolute" |
|
|
|
|
|
right="0" |
|
|
|
|
|
top="0" |
|
|
|
|
|
zIndex={999999} |
|
|
|
|
|
px={space('base')} |
|
|
|
|
|
py={lines === 1 ? '10px' : space('base')} |
|
|
|
|
|
display={['none', 'none', 'flex']} |
|
|
|
|
|
style={{ |
|
|
|
|
|
pointerEvents: 'none', |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
<IconButton |
|
|
|
|
|
title="Copy to clipboard" |
|
|
|
|
|
bg="ink.900" |
|
|
|
|
|
_hover={{ |
|
|
|
|
|
color: 'white', |
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
|
bg: themeColor('ink.900'), |
|
|
|
|
|
}} |
|
|
|
|
|
color={themeColor('ink.400')} |
|
|
|
|
|
onClick={onCopy} |
|
|
|
|
|
style={{ |
|
|
|
|
|
pointerEvents: 'all', |
|
|
|
|
|
}} |
|
|
|
|
|
> |
|
|
|
|
|
<CopyIcon size="20px" /> |
|
|
|
|
|
</IconButton> |
|
|
|
|
|
</Flex> |
|
|
|
|
|
) : null} |
|
|
|
|
|
</Box> |
|
|
</Box> |
|
|
); |
|
|
); |
|
|
}) |
|
|
}) |
|
@ -185,18 +200,15 @@ const preProps = { |
|
|
padding: '2px 6px', |
|
|
padding: '2px 6px', |
|
|
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.04)', |
|
|
boxShadow: '0 1px 2px rgba(0, 0, 0, 0.04)', |
|
|
bg: color('bg'), |
|
|
bg: color('bg'), |
|
|
}; |
|
|
|
|
|
|
|
|
|
|
|
export const InlineCode: React.FC<BoxProps> = ({ children, ...rest }) => ( |
|
|
|
|
|
<Text |
|
|
|
|
|
as="code" |
|
|
|
|
|
{...preProps} |
|
|
|
|
|
{...{ |
|
|
|
|
|
fontSize: '14px', |
|
|
fontSize: '14px', |
|
|
lineHeight: '20px', |
|
|
lineHeight: '20px', |
|
|
}} |
|
|
}; |
|
|
{...rest} |
|
|
|
|
|
> |
|
|
export const InlineCode: ForwardRefExoticComponentWithAs<BoxProps, 'code'> = forwardRefWithAs< |
|
|
|
|
|
BoxProps, |
|
|
|
|
|
'code' |
|
|
|
|
|
>(({ as = 'code', children, ...rest }, ref) => ( |
|
|
|
|
|
<Text ref={ref} as={as} {...(preProps as any)} {...(rest as any)}> |
|
|
{children} |
|
|
{children} |
|
|
</Text> |
|
|
</Text> |
|
|
); |
|
|
)); |
|
|