From 1a94581de394cd2e1b58ec9c1804bc988dbb1445 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 | 69 +++++++++++-------- .../src/shared/tags/ComponentBox.tsx | 19 +---- 3 files changed, 50 insertions(+), 46 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..fffba0a5a89 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-color: var(--color-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..af7913244ec 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 + noInline?: 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 -} - -type LiveCodeDefaultState = LiveCodeProps & { - language?: string -} +} & Omit -class LiveCode extends React.PureComponent< - LiveCodeProps, - LiveCodeDefaultState -> { +class LiveCode extends React.PureComponent { static contextType = Context context!: ContextProps @@ -165,9 +170,10 @@ class LiveCode extends React.PureComponent< render() { const { scope = {}, - useRender, + noInline, noFragments = true, language = 'jsx', + background, code: _code, // eslint-disable-line hideToolbar: _hideToolbar, // eslint-disable-line @@ -188,16 +194,21 @@ class LiveCode extends React.PureComponent< } return ( -
+
- !useRender && noFragments ? `<>${code}` : code + !noInline && noFragments ? `<>${code}` : code } - noInline={useRender} + noInline={noInline} {...props} > {!hidePreview && ( diff --git a/packages/dnb-design-system-portal/src/shared/tags/ComponentBox.tsx b/packages/dnb-design-system-portal/src/shared/tags/ComponentBox.tsx index 13e6a7b659f..5c5bb2bbe5e 100644 --- a/packages/dnb-design-system-portal/src/shared/tags/ComponentBox.tsx +++ b/packages/dnb-design-system-portal/src/shared/tags/ComponentBox.tsx @@ -4,7 +4,7 @@ */ import React from 'react' -import CodeBlock, { LiveCodeProps } from './CodeBlock' +import CodeBlock, { CodeBlockProps } from './CodeBlock' import styled from '@emotion/styled' import { getComponents } from '@dnb/eufemia/src/components/lib' import { getFragments } from '@dnb/eufemia/src/fragments/lib' @@ -16,15 +16,7 @@ if (!globalThis.ComponentBoxMemo) { globalThis.ComponentBoxMemo = {} } -type ComponentBoxProps = { - children: React.ReactNode | (() => 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