From a4b409c93b5ca4bca084f610a38af9541aa0b851 Mon Sep 17 00:00:00 2001 From: Alexey Pyltsyn Date: Thu, 4 Mar 2021 20:22:34 +0300 Subject: [PATCH] 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 --- .../src/theme/CodeBlock/index.tsx | 4 +- .../src/theme/CodeBlock/styles.module.css | 16 +++-- .../src/theme/MDXComponents/index.tsx | 7 +- .../src/theme/MDXComponents/styles.module.css | 16 ----- .../docusaurus-theme-classic/src/types.d.ts | 2 +- .../src/theme/Playground/index.js | 70 ++++++++++--------- .../src/theme/Playground/styles.module.css | 11 ++- .../src/pages/examples/markdownPageExample.md | 59 ++++++++++++++++ 8 files changed, 120 insertions(+), 65 deletions(-) delete mode 100644 packages/docusaurus-theme-classic/src/theme/MDXComponents/styles.module.css diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx index 1793a0aa1f..49d14a4cc4 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/index.tsx @@ -201,7 +201,7 @@ export default function CodeBlock({ code={code} language={language}> {({className, style, tokens, getLineProps, getTokenProps}) => ( - <> +
{codeBlockTitle && (
{codeBlockTitle} @@ -262,7 +262,7 @@ export default function CodeBlock({ )}
- +
)} ); diff --git a/packages/docusaurus-theme-classic/src/theme/CodeBlock/styles.module.css b/packages/docusaurus-theme-classic/src/theme/CodeBlock/styles.module.css index ecf0d8e24b..6aedbd715d 100644 --- a/packages/docusaurus-theme-classic/src/theme/CodeBlock/styles.module.css +++ b/packages/docusaurus-theme-classic/src/theme/CodeBlock/styles.module.css @@ -5,6 +5,10 @@ * LICENSE file in the root directory of this source tree. */ +.codeBlockContainer { + margin-bottom: var(--ifm-leading); +} + .codeBlockContent { position: relative; /*rtl:ignore*/ @@ -14,11 +18,10 @@ .codeBlockTitle { border-top-left-radius: var(--ifm-global-radius); border-top-right-radius: var(--ifm-global-radius); - border-bottom: 1px solid var(--ifm-color-emphasis-200); - font-family: var(--ifm-font-family-monospace); - font-weight: bold; + border-bottom: 1px solid var(--ifm-color-emphasis-300); + font-size: var(--ifm-code-font-size); + font-weight: 500; padding: 0.75rem var(--ifm-pre-padding); - width: 100%; } .codeBlock { @@ -53,9 +56,8 @@ } .codeBlockLines { - font-family: var(--ifm-font-family-monospace); - font-size: inherit; - line-height: var(--ifm-pre-line-height); + font: var(--ifm-code-font-size) / var(--ifm-pre-line-height) + var(--ifm-font-family-monospace); white-space: pre; float: left; min-width: 100%; diff --git a/packages/docusaurus-theme-classic/src/theme/MDXComponents/index.tsx b/packages/docusaurus-theme-classic/src/theme/MDXComponents/index.tsx index 0feff446fe..286a86fd50 100644 --- a/packages/docusaurus-theme-classic/src/theme/MDXComponents/index.tsx +++ b/packages/docusaurus-theme-classic/src/theme/MDXComponents/index.tsx @@ -11,8 +11,6 @@ import CodeBlock from '@theme/CodeBlock'; import Heading from '@theme/Heading'; import type {MDXComponentsObject} from '@theme/MDXComponents'; -import styles from './styles.module.css'; - const MDXComponents: MDXComponentsObject = { code: (props) => { const {children} = props; @@ -25,7 +23,10 @@ const MDXComponents: MDXComponentsObject = { return children; }, a: (props) => , - pre: (props) =>
, + pre: (props: any) => { + const {children} = props; + return ; + }, h1: Heading('h1'), h2: Heading('h2'), h3: Heading('h3'), diff --git a/packages/docusaurus-theme-classic/src/theme/MDXComponents/styles.module.css b/packages/docusaurus-theme-classic/src/theme/MDXComponents/styles.module.css deleted file mode 100644 index 1dba84ecf1..0000000000 --- a/packages/docusaurus-theme-classic/src/theme/MDXComponents/styles.module.css +++ /dev/null @@ -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; -} diff --git a/packages/docusaurus-theme-classic/src/types.d.ts b/packages/docusaurus-theme-classic/src/types.d.ts index b4d5866db1..ba860b2046 100644 --- a/packages/docusaurus-theme-classic/src/types.d.ts +++ b/packages/docusaurus-theme-classic/src/types.d.ts @@ -265,7 +265,7 @@ declare module '@theme/MDXComponents' { export type MDXComponentsObject = { readonly code: typeof CodeBlock; 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 h2: (props: ComponentProps<'h2'>) => JSX.Element; readonly h3: (props: ComponentProps<'h3'>) => JSX.Element; diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js index a4eb12f906..84891e1fce 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js +++ b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/index.js @@ -19,39 +19,41 @@ export default function Playground({children, transformCode, ...props}) { const prismTheme = usePrismTheme(); return ( - `${code};`)} - theme={prismTheme} - {...props}> -
- - Live Editor - -
- -
- - Result - -
-
- - -
-
+
+ `${code};`)} + theme={prismTheme} + {...props}> +
+ + Live Editor + +
+ +
+ + Result + +
+
+ + +
+
+
); } diff --git a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/styles.module.css b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/styles.module.css index 50dc7f6553..6f5cf0805c 100644 --- a/packages/docusaurus-theme-live-codeblock/src/theme/Playground/styles.module.css +++ b/packages/docusaurus-theme-live-codeblock/src/theme/Playground/styles.module.css @@ -5,16 +5,23 @@ * LICENSE file in the root directory of this source tree. */ +.playgroundContainer { + margin-bottom: var(--ifm-leading); +} + .playgroundHeader { letter-spacing: 0.08rem; padding: 0.75rem; text-transform: uppercase; font-weight: bold; + font-size: var(--ifm-code-font-size); } .playgroundEditorHeader { background: var(--ifm-color-emphasis-600); color: var(--ifm-color-content-inverse); + border-top-left-radius: var(--ifm-global-radius); + border-top-right-radius: var(--ifm-global-radius); } .playgroundPreviewHeader { @@ -23,7 +30,8 @@ } .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*/ direction: ltr; } @@ -32,6 +40,5 @@ border: 1px solid var(--ifm-color-emphasis-200); border-bottom-left-radius: var(--ifm-global-radius); border-bottom-right-radius: var(--ifm-global-radius); - position: relative; padding: 1rem; } diff --git a/website/src/pages/examples/markdownPageExample.md b/website/src/pages/examples/markdownPageExample.md index d0f7c2db5c..a25ddf4582 100644 --- a/website/src/pages/examples/markdownPageExample.md +++ b/website/src/pages/examples/markdownPageExample.md @@ -128,3 +128,62 @@ function Demo() { 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 ( +
+

It is {date.toLocaleTimeString()}.

+ // highlight-start + {/* prettier-ignore */} + long long long long long long long long long long long long line + {/* prettier-ignore */} + // highlight-end +
+ ); +} +``` + +```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 ( +
+

It is {date.toLocaleTimeString()}.

+
+ ); +} +``` + + + test + + +test