docs: redesign code highlights + add default code titles (#8628)

* docs: code highlights improvements + default titles

* spacing fixes

* fix terminal detection

* add padding for non-terminal one liners
This commit is contained in:
Shahed Nasser authored and GitHub committed 2024-08-19 13:34:07 +03:00
1 parent d571d564c3
commit a77c23c915
9 files changed
+90 -41

No files matched your search

@@ -28,11 +28,8 @@ export const CodeBlockCopyAction = ({
<CopyButton <CopyButton
text={source} text={source}
tooltipClassName="font-base" tooltipClassName="font-base"
className={clsx( className={clsx("h-fit", !inHeader && "p-[6px]", inHeader && "p-[4.5px]")}
"h-fit", tooltipInnerClassName={clsx(inHeader && "flex")}
!inHeader && "p-[6px]",
inHeader && "px-[6px] pb-[6px]"
)}
onCopy={() => setCopied(true)} onCopy={() => setCopied(true)}
> >
{!copied && <SquareTwoStack className={clsx(iconColor)} />} {!copied && <SquareTwoStack className={clsx(iconColor)} />}
@@ -51,10 +51,12 @@ export const CodeBlockActions = ({
return ( return (
<div <div
className={clsx( className={clsx(
"absolute hidden md:block", "hidden md:block",
!inHeader &&
"xs:rounded xs:absolute xs:right-0 xs:top-0 xs:w-[calc(17%+10px)] xs:h-full" "xs:rounded xs:absolute xs:right-0 xs:top-0 xs:w-[calc(17%+10px)] xs:h-full"
)} )}
> >
{!inHeader && (
<div <div
className={clsx( className={clsx(
!inHeader && !inHeader &&
@@ -68,9 +70,10 @@ export const CodeBlockActions = ({
"z-[9] w-full h-full absolute top-0 left-0" "z-[9] w-full h-full absolute top-0 left-0"
)} )}
/> />
)}
<div <div
className={clsx( className={clsx(
"md:flex md:justify-end z-[11] relative", "md:flex md:justify-end md:gap-docs_0.25 z-[11] relative",
!inHeader && [ !inHeader && [
"md:pr-docs_0.5", "md:pr-docs_0.5",
isCollapsed && "md:pt-docs_2.5", isCollapsed && "md:pt-docs_2.5",
@@ -78,8 +81,7 @@ export const CodeBlockActions = ({
isSingleLine && "md:pt-docs_0.25", isSingleLine && "md:pt-docs_0.25",
!isSingleLine && "md:pt-docs_0.5", !isSingleLine && "md:pt-docs_0.5",
], ],
], ]
inHeader && "md:pr-docs_1 md:pt-docs_0.5"
)} )}
> >
{canShowApiTesting && ( {canShowApiTesting && (
@@ -89,8 +91,9 @@ export const CodeBlockActions = ({
className={clsx( className={clsx(
"h-fit", "h-fit",
!inHeader && "p-[6px]", !inHeader && "p-[6px]",
inHeader && "px-[6px] pb-[6px]" inHeader && "p-[4.5px]"
)} )}
innerClassName={clsx(inHeader && "flex")}
> >
<PlaySolid <PlaySolid
className={clsx("cursor-pointer", iconColor)} className={clsx("cursor-pointer", iconColor)}
@@ -105,8 +108,9 @@ export const CodeBlockActions = ({
className={clsx( className={clsx(
"h-fit", "h-fit",
!inHeader && "p-[6px]", !inHeader && "p-[6px]",
inHeader && "px-[6px] pb-[6px]" inHeader && "p-[4.5px]"
)} )}
innerClassName={clsx(inHeader && "flex")}
> >
<Link <Link
href={`${GITHUB_ISSUES_PREFIX}&title=${encodeURIComponent( href={`${GITHUB_ISSUES_PREFIX}&title=${encodeURIComponent(
@@ -54,7 +54,7 @@ export const CodeBlockHeader = ({
<div <div
className={clsx( className={clsx(
"py-docs_0.5 px-docs_1 mb-0", "py-docs_0.5 px-docs_1 mb-0",
"rounded-t-docs_lg relative", "rounded-t-docs_lg relative flex justify-between items-center",
blockStyle === "subtle" && [ blockStyle === "subtle" && [
"border border-solid border-b-0", "border border-solid border-b-0",
colorMode === "light" && "border-medusa-border-base", colorMode === "light" && "border-medusa-border-base",
@@ -63,7 +63,7 @@ export const CodeBlockHeader = ({
bgColor bgColor
)} )}
> >
<div className={clsx("xs:max-w-[83%]", "flex gap-docs_0.75 items-start")}> <div className={clsx("flex-1", "flex gap-docs_0.75 items-start")}>
{badgeLabel && ( {badgeLabel && (
<Badge variant={badgeColor || "code"} className="font-base"> <Badge variant={badgeColor || "code"} className="font-base">
{badgeLabel} {badgeLabel}
@@ -23,6 +23,7 @@ type CodeBlockLineProps = {
showLineNumber: boolean showLineNumber: boolean
lineNumberColorClassName: string lineNumberColorClassName: string
lineNumberBgClassName: string lineNumberBgClassName: string
isTerminal: boolean
} & Pick<RenderProps, "getLineProps" | "getTokenProps"> } & Pick<RenderProps, "getLineProps" | "getTokenProps">
export const CodeBlockLine = ({ export const CodeBlockLine = ({
@@ -34,6 +35,7 @@ export const CodeBlockLine = ({
showLineNumber, showLineNumber,
lineNumberColorClassName, lineNumberColorClassName,
lineNumberBgClassName, lineNumberBgClassName,
isTerminal,
}: CodeBlockLineProps) => { }: CodeBlockLineProps) => {
const lineProps = getLineProps({ line, key: lineNumber }) const lineProps = getLineProps({ line, key: lineNumber })
@@ -194,19 +196,18 @@ export const CodeBlockLine = ({
const getTokensElm = ({ const getTokensElm = ({
tokens, tokens,
isHighlighted, isTokenHighlighted,
isLineHighlighted,
offset, offset,
}: { }: {
tokens: Token[] tokens: Token[]
isHighlighted: boolean isTokenHighlighted: boolean
isLineHighlighted: boolean
offset: number offset: number
}) => ( }) => (
<span <span
className={clsx( className={clsx(
isHighlighted && [ isTokenHighlighted && "lg:bg-medusa-contrast-border-base cursor-default"
"lg:py-px lg:px-[6px] lg:border-medusa-contrast-border-top lg:rounded-docs_sm",
"lg:bg-medusa-contrast-bg-highlight lg:cursor-pointer",
]
)} )}
> >
{tokens.map((token, key) => { {tokens.map((token, key) => {
@@ -216,14 +217,22 @@ export const CodeBlockLine = ({
key: tokenKey, key: tokenKey,
}) })
return ( return (
<span key={tokenKey} className={clsx(tokenClassName)} {...rest} /> <span
key={tokenKey}
className={clsx(
tokenClassName,
(isTerminal || isTokenHighlighted || isLineHighlighted) &&
"!text-medusa-contrast-fg-primary"
)}
{...rest}
/>
) )
})} })}
</span> </span>
) )
const isHighlightedLine = useMemo( const isHighlightedLine = useMemo(
() => highlights.length && !highlightedTokens.length, () => highlights.length !== 0 && highlightedTokens.length === 0,
[highlights, highlightedTokens] [highlights, highlightedTokens]
) )
@@ -233,11 +242,11 @@ export const CodeBlockLine = ({
{...lineProps} {...lineProps}
className={clsx( className={clsx(
"table-row", "table-row",
isHighlightedLine && "bg-medusa-contrast-bg-highlight", isHighlightedLine && "bg-medusa-contrast-border-base",
lineProps.className lineProps.className
)} )}
> >
{showLineNumber && ( {(showLineNumber || isTerminal) && (
<span <span
className={clsx( className={clsx(
"mr-docs_1 table-cell select-none", "mr-docs_1 table-cell select-none",
@@ -246,7 +255,7 @@ export const CodeBlockLine = ({
lineNumberBgClassName lineNumberBgClassName
)} )}
> >
{lineNumber + 1} {isTerminal ? "❯" : showLineNumber ? lineNumber + 1 : ""}
</span> </span>
)} )}
<span> <span>
@@ -274,10 +283,21 @@ export const CodeBlockLine = ({
</MarkdownContent> </MarkdownContent>
)} )}
> >
{getTokensElm({ tokens, isHighlighted, offset })} {getTokensElm({
tokens,
isTokenHighlighted: isHighlighted,
offset,
isLineHighlighted: isHighlightedLine,
})}
</Tooltip> </Tooltip>
)} )}
{!tooltipText && getTokensElm({ tokens, isHighlighted, offset })} {!tooltipText &&
getTokensElm({
tokens,
isTokenHighlighted: isHighlighted,
offset,
isLineHighlighted: isHighlightedLine,
})}
</React.Fragment> </React.Fragment>
) )
})} })}
@@ -38,6 +38,7 @@ export type CodeBlockMetaFields = {
noLineNumbers?: boolean noLineNumbers?: boolean
collapsibleLines?: string collapsibleLines?: string
expandButtonLabel?: string expandButtonLabel?: string
isTerminal?: boolean
} & CodeBlockHeaderMeta } & CodeBlockHeaderMeta
export type CodeBlockStyle = "loud" | "subtle" export type CodeBlockStyle = "loud" | "subtle"
@@ -68,6 +69,7 @@ export const CodeBlock = ({
children, children,
collapsibleLines, collapsibleLines,
expandButtonLabel, expandButtonLabel,
isTerminal,
...rest ...rest
}: CodeBlockProps) => { }: CodeBlockProps) => {
if (!source && typeof children === "string") { if (!source && typeof children === "string") {
@@ -80,9 +82,25 @@ export const CodeBlock = ({
const codeRef = useRef<HTMLElement>(null) const codeRef = useRef<HTMLElement>(null)
const apiRunnerRef = useRef<HTMLDivElement>(null) const apiRunnerRef = useRef<HTMLDivElement>(null)
const [scrollable, setScrollable] = useState(false) const [scrollable, setScrollable] = useState(false)
const isTerminalCode = useMemo(() => {
return isTerminal === undefined
? lang === "bash" && !source.startsWith("curl")
: isTerminal
}, [isTerminal, lang])
const codeTitle = useMemo(() => {
if (title || hasTabs) {
return title
}
if (isTerminalCode) {
return "Terminal"
}
return "Code"
}, [title, isTerminalCode, hasTabs])
const hasInnerCodeBlock = useMemo( const hasInnerCodeBlock = useMemo(
() => hasTabs || title.length > 0, () => hasTabs || codeTitle.length > 0,
[hasTabs, title] [hasTabs, codeTitle]
) )
const canShowApiTesting = useMemo( const canShowApiTesting = useMemo(
() => () =>
@@ -198,6 +216,7 @@ export const CodeBlock = ({
key={offsettedLineNumber} key={offsettedLineNumber}
lineNumberColorClassName={lineNumbersColor} lineNumberColorClassName={lineNumbersColor}
lineNumberBgClassName={innerBgColor} lineNumberBgClassName={innerBgColor}
isTerminal={isTerminalCode}
{...highlightProps} {...highlightProps}
/> />
) )
@@ -260,16 +279,15 @@ export const CodeBlock = ({
hasInnerCodeBlock && "rounded-docs_lg", hasInnerCodeBlock && "rounded-docs_lg",
!hasInnerCodeBlock && "rounded-docs_DEFAULT", !hasInnerCodeBlock && "rounded-docs_DEFAULT",
!hasTabs && boxShadow, !hasTabs && boxShadow,
(blockStyle === "loud" || colorMode !== "light") && blockStyle === "loud" && "code-block-highlight",
"code-block-highlight-dark",
blockStyle === "subtle" && blockStyle === "subtle" &&
colorMode === "light" && colorMode === "light" &&
"code-block-highlight-light" "code-block-highlight-light"
)} )}
> >
{title && ( {codeTitle && (
<CodeBlockHeader <CodeBlockHeader
title={title} title={codeTitle}
blockStyle={blockStyle} blockStyle={blockStyle}
badgeLabel={rest.badgeLabel} badgeLabel={rest.badgeLabel}
badgeColor={rest.badgeColor} badgeColor={rest.badgeColor}
@@ -334,17 +352,20 @@ export const CodeBlock = ({
"rounded-docs_DEFAULT", "rounded-docs_DEFAULT",
!hasInnerCodeBlock && !hasInnerCodeBlock &&
tokens.length <= 1 && tokens.length <= 1 &&
"px-docs_0.5 py-[6px]", "px-docs_1 py-[6px]",
!title.length && (!noCopy || !noReport) && "xs:max-w-[83%]", !codeTitle.length &&
noLineNumbers && "pl-docs_1", (!noCopy || !noReport) &&
"xs:max-w-[83%]",
(noLineNumbers ||
(tokens.length <= 1 && !isTerminalCode)) &&
"pl-docs_1",
preClassName preClassName
)} )}
> >
<code <code
className={clsx( className={clsx(
"text-code-body font-monospace table min-w-full print:whitespace-pre-wrap", "text-code-body font-monospace table min-w-full print:whitespace-pre-wrap",
tokens.length > 1 && "py-docs_0.75", "py-docs_0.75"
tokens.length <= 1 && "!py-[6px] px-docs_0.5"
)} )}
ref={codeRef} ref={codeRef}
> >
@@ -364,7 +385,7 @@ export const CodeBlock = ({
})} })}
</code> </code>
</pre> </pre>
{!title && ( {!hasInnerCodeBlock && (
<CodeBlockActions <CodeBlockActions
{...actionsProps} {...actionsProps}
inHeader={false} inHeader={false}
@@ -9,6 +9,7 @@ export type CopyButtonProps = {
text: string text: string
buttonClassName?: string buttonClassName?: string
tooltipClassName?: string tooltipClassName?: string
tooltipInnerClassName?: string
tooltipText?: string tooltipText?: string
onCopy?: ( onCopy?: (
e: e:
@@ -27,6 +28,7 @@ export const CopyButton = ({
className, className,
onCopy, onCopy,
handleTouch, handleTouch,
tooltipInnerClassName,
}: CopyButtonProps) => { }: CopyButtonProps) => {
const { isCopied, handleCopy } = useCopy(text) const { isCopied, handleCopy } = useCopy(text)
const [touchCount, setTouchCount] = useState(0) const [touchCount, setTouchCount] = useState(0)
@@ -36,6 +38,7 @@ export const CopyButton = ({
text={isCopied ? `Copied!` : tooltipText} text={isCopied ? `Copied!` : tooltipText}
tooltipClassName={clsx(tooltipClassName, handleTouch && "!block")} tooltipClassName={clsx(tooltipClassName, handleTouch && "!block")}
className={className} className={className}
innerClassName={tooltipInnerClassName}
> >
<span <span
className={clsx("cursor-pointer", buttonClassName)} className={clsx("cursor-pointer", buttonClassName)}
@@ -11,6 +11,7 @@ export type TooltipProps = {
tooltipClassName?: string tooltipClassName?: string
html?: string html?: string
tooltipChildren?: React.ReactNode tooltipChildren?: React.ReactNode
innerClassName?: string
} & React.HTMLAttributes<HTMLSpanElement> & } & React.HTMLAttributes<HTMLSpanElement> &
ITooltip ITooltip
@@ -21,6 +22,7 @@ export const Tooltip = ({
html = "", html = "",
tooltipChildren, tooltipChildren,
className, className,
innerClassName,
...tooltipProps ...tooltipProps
}: TooltipProps) => { }: TooltipProps) => {
const elementId = useId() const elementId = useId()
@@ -32,6 +34,7 @@ export const Tooltip = ({
data-tooltip-content={text} data-tooltip-content={text}
data-tooltip-html={html} data-tooltip-html={html}
data-tooltip-id={elementId} data-tooltip-id={elementId}
className={innerClassName}
> >
{children} {children}
</span> </span>
@@ -768,9 +768,10 @@ module.exports = {
".animate-bg-surface": { ".animate-bg-surface": {
"--animation-color": "var(--docs-bg-subtle-pressed)", "--animation-color": "var(--docs-bg-subtle-pressed)",
}, },
".code-block-highlight-dark": { ".code-block-highlight": {
"*::selection": { "*::selection": {
"background-color": "var(--docs-contrast-bg-highlight)", "background-color": "var(--docs-contrast-fg-secondary)",
color: "var(--docs-contrast-bg-base)",
}, },
}, },
".code-block-highlight-light": { ".code-block-highlight-light": {
+1 -1
View File
@@ -96,7 +96,7 @@ const light = {
"--docs-contrast-bg-alpha": "rgba(9, 9, 11, 0.8)", "--docs-contrast-bg-alpha": "rgba(9, 9, 11, 0.8)",
"--docs-contrast-fg-primary": "rgba(255, 255, 255, 0.88)", "--docs-contrast-fg-primary": "rgba(255, 255, 255, 0.88)",
"--docs-contrast-fg-secondary": "rgba(255, 255, 255, 0.56)", "--docs-contrast-fg-secondary": "rgba(255, 255, 255, 0.56)",
"--docs-contrast-border-base": "rgba(255, 255, 255, 0.15)", "--docs-contrast-border-base": "rgba(255, 255, 255, 0.16)",
"--docs-contrast-border-top": "rgba(9, 9, 11, 1)", "--docs-contrast-border-top": "rgba(9, 9, 11, 1)",
"--docs-contrast-border-bot": "rgba(255, 255, 255, 0.1)", "--docs-contrast-border-bot": "rgba(255, 255, 255, 0.1)",
} }