fix(v2): make code blocks more standalone (#4315)

* refactor(v2): make code blocks styles standalone

* Rework

* Revert font size in playground headers

Co-authored-by: slorber <lorber.sebastien@gmail.com>
This commit is contained in:
Alexey Pyltsyn 2021-03-04 20:22:34 +03:00 committed by GitHub
parent 0383dd1496
commit a4b409c93b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 120 additions and 65 deletions

View file

@ -201,7 +201,7 @@ export default function CodeBlock({
code={code} code={code}
language={language}> language={language}>
{({className, style, tokens, getLineProps, getTokenProps}) => ( {({className, style, tokens, getLineProps, getTokenProps}) => (
<> <div className={styles.codeBlockContainer}>
{codeBlockTitle && ( {codeBlockTitle && (
<div style={style} className={styles.codeBlockTitle}> <div style={style} className={styles.codeBlockTitle}>
{codeBlockTitle} {codeBlockTitle}
@ -262,7 +262,7 @@ export default function CodeBlock({
)} )}
</button> </button>
</div> </div>
</> </div>
)} )}
</Highlight> </Highlight>
); );

View file

@ -5,6 +5,10 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
.codeBlockContainer {
margin-bottom: var(--ifm-leading);
}
.codeBlockContent { .codeBlockContent {
position: relative; position: relative;
/*rtl:ignore*/ /*rtl:ignore*/
@ -14,11 +18,10 @@
.codeBlockTitle { .codeBlockTitle {
border-top-left-radius: var(--ifm-global-radius); border-top-left-radius: var(--ifm-global-radius);
border-top-right-radius: var(--ifm-global-radius); border-top-right-radius: var(--ifm-global-radius);
border-bottom: 1px solid var(--ifm-color-emphasis-200); border-bottom: 1px solid var(--ifm-color-emphasis-300);
font-family: var(--ifm-font-family-monospace); font-size: var(--ifm-code-font-size);
font-weight: bold; font-weight: 500;
padding: 0.75rem var(--ifm-pre-padding); padding: 0.75rem var(--ifm-pre-padding);
width: 100%;
} }
.codeBlock { .codeBlock {
@ -53,9 +56,8 @@
} }
.codeBlockLines { .codeBlockLines {
font-family: var(--ifm-font-family-monospace); font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
font-size: inherit; var(--ifm-font-family-monospace);
line-height: var(--ifm-pre-line-height);
white-space: pre; white-space: pre;
float: left; float: left;
min-width: 100%; min-width: 100%;

View file

@ -11,8 +11,6 @@ import CodeBlock from '@theme/CodeBlock';
import Heading from '@theme/Heading'; import Heading from '@theme/Heading';
import type {MDXComponentsObject} from '@theme/MDXComponents'; import type {MDXComponentsObject} from '@theme/MDXComponents';
import styles from './styles.module.css';
const MDXComponents: MDXComponentsObject = { const MDXComponents: MDXComponentsObject = {
code: (props) => { code: (props) => {
const {children} = props; const {children} = props;
@ -25,7 +23,10 @@ const MDXComponents: MDXComponentsObject = {
return children; return children;
}, },
a: (props) => <Link {...props} />, a: (props) => <Link {...props} />,
pre: (props) => <div className={styles.mdxCodeBlock} {...props} />, pre: (props: any) => {
const {children} = props;
return <CodeBlock {...children?.props} />;
},
h1: Heading('h1'), h1: Heading('h1'),
h2: Heading('h2'), h2: Heading('h2'),
h3: Heading('h3'), h3: Heading('h3'),

View file

@ -1,16 +0,0 @@
/**
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
.mdxCodeBlock {
position: relative;
margin-bottom: var(--ifm-leading);
font-size: var(--ifm-code-font-size);
}
.mdxCodeBlock pre {
font-size: inherit;
}

View file

@ -265,7 +265,7 @@ declare module '@theme/MDXComponents' {
export type MDXComponentsObject = { export type MDXComponentsObject = {
readonly code: typeof CodeBlock; readonly code: typeof CodeBlock;
readonly a: (props: ComponentProps<'a'>) => JSX.Element; readonly a: (props: ComponentProps<'a'>) => JSX.Element;
readonly pre: (props: ComponentProps<'div'>) => JSX.Element; readonly pre: typeof CodeBlock;
readonly h1: (props: ComponentProps<'h1'>) => JSX.Element; readonly h1: (props: ComponentProps<'h1'>) => JSX.Element;
readonly h2: (props: ComponentProps<'h2'>) => JSX.Element; readonly h2: (props: ComponentProps<'h2'>) => JSX.Element;
readonly h3: (props: ComponentProps<'h3'>) => JSX.Element; readonly h3: (props: ComponentProps<'h3'>) => JSX.Element;

View file

@ -19,39 +19,41 @@ export default function Playground({children, transformCode, ...props}) {
const prismTheme = usePrismTheme(); const prismTheme = usePrismTheme();
return ( return (
<LiveProvider <div className={styles.playgroundContainer}>
key={isClient} <LiveProvider
code={isClient ? children.replace(/\n$/, '') : ''} key={isClient}
transformCode={transformCode || ((code) => `${code};`)} code={isClient ? children.replace(/\n$/, '') : ''}
theme={prismTheme} transformCode={transformCode || ((code) => `${code};`)}
{...props}> theme={prismTheme}
<div {...props}>
className={clsx( <div
styles.playgroundHeader, className={clsx(
styles.playgroundEditorHeader, styles.playgroundHeader,
)}> styles.playgroundEditorHeader,
<Translate )}>
id="theme.Playground.liveEditor" <Translate
description="The live editor label of the live codeblocks"> id="theme.Playground.liveEditor"
Live Editor description="The live editor label of the live codeblocks">
</Translate> Live Editor
</div> </Translate>
<LiveEditor className={styles.playgroundEditor} /> </div>
<div <LiveEditor className={styles.playgroundEditor} />
className={clsx( <div
styles.playgroundHeader, className={clsx(
styles.playgroundPreviewHeader, styles.playgroundHeader,
)}> styles.playgroundPreviewHeader,
<Translate )}>
id="theme.Playground.result" <Translate
description="The result label of the live codeblocks"> id="theme.Playground.result"
Result description="The result label of the live codeblocks">
</Translate> Result
</div> </Translate>
<div className={styles.playgroundPreview}> </div>
<LivePreview /> <div className={styles.playgroundPreview}>
<LiveError /> <LivePreview />
</div> <LiveError />
</LiveProvider> </div>
</LiveProvider>
</div>
); );
} }

View file

@ -5,16 +5,23 @@
* LICENSE file in the root directory of this source tree. * LICENSE file in the root directory of this source tree.
*/ */
.playgroundContainer {
margin-bottom: var(--ifm-leading);
}
.playgroundHeader { .playgroundHeader {
letter-spacing: 0.08rem; letter-spacing: 0.08rem;
padding: 0.75rem; padding: 0.75rem;
text-transform: uppercase; text-transform: uppercase;
font-weight: bold; font-weight: bold;
font-size: var(--ifm-code-font-size);
} }
.playgroundEditorHeader { .playgroundEditorHeader {
background: var(--ifm-color-emphasis-600); background: var(--ifm-color-emphasis-600);
color: var(--ifm-color-content-inverse); color: var(--ifm-color-content-inverse);
border-top-left-radius: var(--ifm-global-radius);
border-top-right-radius: var(--ifm-global-radius);
} }
.playgroundPreviewHeader { .playgroundPreviewHeader {
@ -23,7 +30,8 @@
} }
.playgroundEditor { .playgroundEditor {
font-family: var(--ifm-font-family-monospace) !important; font: var(--ifm-code-font-size) / var(--ifm-pre-line-height)
var(--ifm-font-family-monospace) !important;
/*rtl:ignore*/ /*rtl:ignore*/
direction: ltr; direction: ltr;
} }
@ -32,6 +40,5 @@
border: 1px solid var(--ifm-color-emphasis-200); border: 1px solid var(--ifm-color-emphasis-200);
border-bottom-left-radius: var(--ifm-global-radius); border-bottom-left-radius: var(--ifm-global-radius);
border-bottom-right-radius: var(--ifm-global-radius); border-bottom-right-radius: var(--ifm-global-radius);
position: relative;
padding: 1rem; padding: 1rem;
} }

View file

@ -128,3 +128,62 @@ function Demo() {
return null; return null;
} }
``` ```
## Code block test
```js title="Title"
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);
return function cleanup() {
clearInterval(timerID);
};
});
function tick() {
setDate(new Date());
}
return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
// highlight-start
{/* prettier-ignore */}
long long long long long long long long long long long long line
{/* prettier-ignore */}
// highlight-end
</div>
);
}
```
```jsx live
function Clock(props) {
const [date, setDate] = useState(new Date());
useEffect(() => {
var timerID = setInterval(() => tick(), 1000);
return function cleanup() {
clearInterval(timerID);
};
});
function tick() {
setDate(new Date());
}
return (
<div>
<h2>It is {date.toLocaleTimeString()}.</h2>
</div>
);
}
```
<CodeBlock className="language-yaml" title="test">
test
</CodeBlock>
<code>test</code>