From ac35cead19cdbc046e4b91c417d4a11a51673d8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20H=C3=B8egh?= Date: Fri, 19 Jan 2024 08:59:24 +0100 Subject: [PATCH] chore(CodeBlock): refactor and add support for a white background --- .../src/shared/tags/CodeBlock.module.scss | 8 +++ .../src/shared/tags/CodeBlock.tsx | 63 +++++++++++-------- .../src/shared/tags/ComponentBox.tsx | 19 +----- 3 files changed, 47 insertions(+), 43 deletions(-) diff --git a/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.module.scss b/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.module.scss index a614ccd5090..56634de7b30 100644 --- a/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.module.scss +++ b/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.module.scss @@ -8,6 +8,14 @@ } } +.whiteBackgroundStyle { + :global { + .example-box { + background: white; + } + } +} + .toolbarStyle { position: absolute; z-index: 100; // over generally used z-indexes, like table rows diff --git a/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.tsx b/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.tsx index 72743d848a8..b38b12e5551 100644 --- a/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.tsx +++ b/packages/dnb-design-system-portal/src/shared/tags/CodeBlock.tsx @@ -5,7 +5,11 @@ import React from 'react' import classnames from 'classnames' -import Highlight, { Prism, defaultProps } from 'prism-react-renderer' +import Highlight, { + Language, + Prism, + defaultProps, +} from 'prism-react-renderer' import Tag from './Tag' import { Button } from '@dnb/eufemia/src/components' import { makeUniqueId } from '@dnb/eufemia/src/shared/component-helper' @@ -15,6 +19,7 @@ import { liveCodeEditorStyle, toolbarStyle, codeBlockStyle, + whiteBackgroundStyle, } from './CodeBlock.module.scss' import { LiveProvider, @@ -27,18 +32,33 @@ import { import prismTheme from '@dnb/eufemia/src/style/themes/theme-ui/prism/dnb-prism-theme' import { ContextProps } from '@dnb/eufemia/src/shared/Context' +export type CodeBlockProps = { + scope?: Record + useRender?: boolean + hideToolbar?: boolean + hideCode?: boolean + hidePreview?: boolean + reactLive?: boolean + language?: Language + className?: string + background?: 'grid' | 'white' + children: string | React.ReactNode | (() => React.ReactNode) + tabMode?: 'focus' | 'indentation' + 'data-visual-test'?: string +} + const CodeBlock = ({ language, children: exampleCode, reactLive: isReactLive, ...props -}) => { +}: CodeBlockProps) => { const context = React.useContext(Context) if (!language) { - language = - (String(props && props.className).match(/language-(.*)$|\s/) || - [])[1] || 'jsx' + language = ((String(props && props.className).match( + /language-(.*)$|\s/, + ) || [])[1] || 'jsx') as Language } if (((props && props.scope) || isReactLive) && language === 'jsx') { @@ -49,14 +69,14 @@ const CodeBlock = ({ createSkeletonClass('code', context.skeleton), )} > - + ) } else { return ( @@ -86,27 +106,12 @@ const CodeBlock = ({ export default CodeBlock -export type LiveCodeProps = { +type LiveCodeProps = { code: string - scope?: Record - useRender?: boolean noFragments?: boolean - hideToolbar?: boolean - hideCode?: boolean - hidePreview?: boolean - language?: string - tabMode?: 'focus' | 'indentation' - 'data-visual-test'?: string -} +} & Omit -type LiveCodeDefaultState = LiveCodeProps & { - language?: string -} - -class LiveCode extends React.PureComponent< - LiveCodeProps, - LiveCodeDefaultState -> { +class LiveCode extends React.PureComponent { static contextType = Context context!: ContextProps @@ -168,6 +173,7 @@ class LiveCode extends React.PureComponent< useRender, noFragments = true, language = 'jsx', + background, code: _code, // eslint-disable-line hideToolbar: _hideToolbar, // eslint-disable-line @@ -188,7 +194,12 @@ class LiveCode extends React.PureComponent< } return ( -
+
React.ReactNode) - useRender?: boolean - - /** @deprecated Use "useRender" instead */ - noInline?: boolean -} & Omit - -function ComponentBox(props: ComponentBoxProps) { +function ComponentBox(props: CodeBlockProps) { const { children, scope = {}, ...rest } = props const hash = children as string @@ -32,13 +24,7 @@ function ComponentBox(props: ComponentBoxProps) { return globalThis.ComponentBoxMemo[hash] } - if (rest.noInline) { - rest.useRender = true - } - return (globalThis.ComponentBoxMemo[hash] = ( - // eslint-disable-next-line @typescript-eslint/ban-ts-comment - // @ts-ignore