|
1 | 1 | import { CodeBlockSyntaxHighlighting } from '@site/src/components/CodeBlockSyntaxHighlighting'; |
2 | 2 | import { Button, Document, Paragraph, Surface } from '@utrecht/component-library-react/dist/css-module'; |
3 | | -import { HTMLContent } from '@utrecht/component-library-react/dist/css-module'; |
4 | 3 | import clsx from 'clsx'; |
5 | 4 | import prettierBabel from 'prettier/plugins/babel.mjs'; |
6 | 5 | import prettierESTree from 'prettier/plugins/estree.mjs'; |
7 | 6 | import prettierHTML from 'prettier/plugins/html.mjs'; |
8 | 7 | import prettierPostcss from 'prettier/plugins/postcss.mjs'; |
9 | 8 | import prettier from 'prettier/standalone'; |
10 | 9 | import type { CSSProperties, ElementType, PropsWithChildren, ReactNode } from 'react'; |
11 | | -import { isValidElement, useEffect, useId, useState } from 'react'; |
12 | | -import { Fragment } from 'react'; |
| 10 | +import { isValidElement, useEffect, useId, useState, Fragment } from 'react'; |
13 | 11 | import * as ReactDOMServer from 'react-dom/server'; |
14 | 12 | import './Canvas.css'; |
| 13 | +import '@nl-design-system-community/editor/theme.css'; |
| 14 | +import { |
| 15 | + ClippyContent, |
| 16 | + ClippyContext, |
| 17 | + ClippyGutter, |
| 18 | + ClippyValidationsList, |
| 19 | +} from '@nl-design-system-community/editor-react'; |
15 | 20 |
|
16 | 21 | export type CanvasContainerType = 'document' | 'paragraph' | 'surface'; |
17 | 22 |
|
@@ -61,6 +66,7 @@ export const Canvas = ({ |
61 | 66 | }: CanvasProps) => { |
62 | 67 | // By default the `children` argument is converted to code. |
63 | 68 | const jsxTree = typeof children === 'function' ? children() : children; |
| 69 | + const contentHtml = ReactDOMServer.renderToStaticMarkup(<>{jsxTree}</>); |
64 | 70 | // You can override the code from `children` with the `code` argument. |
65 | 71 | // The code argument can be a string, or JSX, or a function that generates JSX. |
66 | 72 | const codeJsxTree = typeof code === 'function' ? code() : isValidElement(code) ? code : undefined; |
@@ -104,54 +110,58 @@ export const Canvas = ({ |
104 | 110 | } |
105 | 111 |
|
106 | 112 | return ( |
107 | | - <div className={clsx('nlds-canvas')}> |
108 | | - {jsxTree && ( |
109 | | - <div className={clsx('nlds-canvas__example')}> |
110 | | - <div className="voorbeeld-theme" style={designTokens}> |
111 | | - <Container> |
112 | | - <HTMLContent>{jsxTree}</HTMLContent> |
113 | | - </Container> |
| 113 | + <ClippyContext id={codeBlockId} topLevelHeading={2}> |
| 114 | + <div slot="value" hidden dangerouslySetInnerHTML={{ __html: contentHtml }} /> |
| 115 | + <div className={clsx('nlds-canvas')}> |
| 116 | + {jsxTree && ( |
| 117 | + <> |
| 118 | + <ClippyContent> |
| 119 | + <ClippyGutter mode="list" /> |
| 120 | + </ClippyContent> |
| 121 | + </> |
| 122 | + )} |
| 123 | + {displayCode && ( |
| 124 | + <div className={clsx('nlds-canvas__toolbar')}> |
| 125 | + <Button |
| 126 | + className={clsx('nlds-canvas__button', 'nlds-canvas__toggle-code-button')} |
| 127 | + appearance="subtle-button" |
| 128 | + onClick={toggleExpanded} |
| 129 | + aria-expanded={expandedSourceCode} |
| 130 | + aria-controls={codeBlockId} |
| 131 | + > |
| 132 | + {!expandedSourceCode ? 'Bekijk code' : 'Verberg code'} |
| 133 | + </Button> |
114 | 134 | </div> |
115 | | - </div> |
116 | | - )} |
117 | | - {displayCode && ( |
118 | | - <div className={clsx('nlds-canvas__toolbar')}> |
119 | | - <Button |
120 | | - className={clsx('nlds-canvas__button', 'nlds-canvas__toggle-code-button')} |
121 | | - appearance="subtle-button" |
122 | | - onClick={toggleExpanded} |
123 | | - aria-expanded={expandedSourceCode} |
124 | | - aria-controls={codeBlockId} |
| 135 | + )} |
| 136 | + {displayCode && ( |
| 137 | + <div |
| 138 | + className={clsx('nlds-canvas__code-block', !copy && 'nlds-canvas__code-block--user-select-none')} |
| 139 | + id={codeBlockId} |
| 140 | + hidden={!expandedSourceCode} |
125 | 141 | > |
126 | | - {!expandedSourceCode ? 'Bekijk code' : 'Verberg code'} |
127 | | - </Button> |
128 | | - </div> |
129 | | - )} |
130 | | - {displayCode && ( |
131 | | - <div |
132 | | - className={clsx('nlds-canvas__code-block', !copy && 'nlds-canvas__code-block--user-select-none')} |
133 | | - id={codeBlockId} |
134 | | - hidden={!expandedSourceCode} |
135 | | - > |
136 | | - <CodeBlockSyntaxHighlighting |
137 | | - codeBlockLabel={'Codevoorbeeld'} |
138 | | - syntax={language} |
139 | | - textContent={exampleSourceCode} |
140 | | - trim |
141 | | - /> |
142 | | - {copy && ( |
143 | | - <div className={clsx('nlds-canvas__toolbar', 'nlds-canvas__toolbar--copy')}> |
144 | | - <Button |
145 | | - className={clsx('nlds-canvas__button', 'nlds-canvas__copy-button')} |
146 | | - appearance="subtle-button" |
147 | | - onClick={copyCode} |
148 | | - > |
149 | | - Kopieer |
150 | | - </Button> |
151 | | - </div> |
152 | | - )} |
| 142 | + <CodeBlockSyntaxHighlighting |
| 143 | + codeBlockLabel={'Codevoorbeeld'} |
| 144 | + syntax={language} |
| 145 | + textContent={exampleSourceCode} |
| 146 | + trim |
| 147 | + /> |
| 148 | + {copy && ( |
| 149 | + <div className={clsx('nlds-canvas__toolbar', 'nlds-canvas__toolbar--copy')}> |
| 150 | + <Button |
| 151 | + className={clsx('nlds-canvas__button', 'nlds-canvas__copy-button')} |
| 152 | + appearance="subtle-button" |
| 153 | + onClick={copyCode} |
| 154 | + > |
| 155 | + Kopieer |
| 156 | + </Button> |
| 157 | + </div> |
| 158 | + )} |
| 159 | + </div> |
| 160 | + )} |
| 161 | + <div> |
| 162 | + <ClippyValidationsList /> |
153 | 163 | </div> |
154 | | - )} |
155 | | - </div> |
| 164 | + </div> |
| 165 | + </ClippyContext> |
156 | 166 | ); |
157 | 167 | }; |
0 commit comments