mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-13 00:03:57 +02:00
feat(v2): refactor live code to css modules (#1558)
* feat(v2): Refactor live code to css modules * Add a piece in doc about live coding * Resolve PR discussions
This commit is contained in:
parent
924668a077
commit
c66ae5a507
9 changed files with 50 additions and 63 deletions
|
@ -6,15 +6,16 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
|
import classnames from 'classnames';
|
||||||
import LoadableVisibility from 'react-loadable-visibility/react-loadable';
|
import LoadableVisibility from 'react-loadable-visibility/react-loadable';
|
||||||
import Highlight, {defaultProps} from 'prism-react-renderer';
|
import Highlight, {defaultProps} from 'prism-react-renderer';
|
||||||
import nightOwlTheme from 'prism-react-renderer/themes/nightOwl';
|
import nightOwlTheme from 'prism-react-renderer/themes/nightOwl';
|
||||||
|
|
||||||
import Loading from '@theme/Loading';
|
import Loading from '@theme/Loading';
|
||||||
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
/* Live playground is not small in size, lazy load it is better */
|
/* Live playground is not small in size, lazy load it is better */
|
||||||
const Playground = LoadableVisibility({
|
const Playground = LoadableVisibility({
|
||||||
loader: () => import('@theme/components/Playground'),
|
loader: () => import('@theme/Playground'),
|
||||||
loading: Loading,
|
loading: Loading,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -38,15 +39,7 @@ export default ({children, className: languageClassName, live, ...props}) => {
|
||||||
code={children.trim()}
|
code={children.trim()}
|
||||||
language={language}>
|
language={language}>
|
||||||
{({className, style, tokens, getLineProps, getTokenProps}) => (
|
{({className, style, tokens, getLineProps, getTokenProps}) => (
|
||||||
<pre
|
<pre className={classnames(className, styles.codeBlock)} style={style}>
|
||||||
className={className}
|
|
||||||
style={{
|
|
||||||
...style,
|
|
||||||
overflow: 'hidden',
|
|
||||||
overflowWrap: 'break-word',
|
|
||||||
whiteSpace: 'pre-wrap',
|
|
||||||
fontSize: 'inherit',
|
|
||||||
}}>
|
|
||||||
{tokens.map((line, i) => (
|
{tokens.map((line, i) => (
|
||||||
<div key={i} {...getLineProps({line, key: i})}>
|
<div key={i} {...getLineProps({line, key: i})}>
|
||||||
{line.map((token, key) => (
|
{line.map((token, key) => (
|
|
@ -0,0 +1,8 @@
|
||||||
|
.codeBlock {
|
||||||
|
border-radius: 0;
|
||||||
|
font-size: inherit;
|
||||||
|
margin-bottom: 0;
|
||||||
|
overflow: hidden;
|
||||||
|
overflow-wrap: break-word;
|
||||||
|
white-space: pre-wrap;
|
||||||
|
}
|
|
@ -6,21 +6,11 @@
|
||||||
*/
|
*/
|
||||||
import React from 'react';
|
import React from 'react';
|
||||||
import Link from '@docusaurus/Link';
|
import Link from '@docusaurus/Link';
|
||||||
import CodeBlock from './components/CodeBlock';
|
import CodeBlock from '@theme/CodeBlock';
|
||||||
|
import styles from './styles.module.css';
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
code: CodeBlock,
|
code: CodeBlock,
|
||||||
a: Link,
|
a: Link,
|
||||||
pre: props => (
|
pre: props => <pre className={styles.mdxCodeBlock} {...props} />,
|
||||||
<pre
|
|
||||||
className="pre"
|
|
||||||
style={{
|
|
||||||
backgroundColor: 'transparent',
|
|
||||||
fontFamily: 'inherit',
|
|
||||||
padding: 0,
|
|
||||||
boxSizing: 'border-box',
|
|
||||||
}}
|
|
||||||
{...props}
|
|
||||||
/>
|
|
||||||
),
|
|
||||||
};
|
};
|
|
@ -0,0 +1,9 @@
|
||||||
|
.mdxCodeBlock {
|
||||||
|
background-color: transparent;
|
||||||
|
border: 0.5px solid rgba(31, 34, 39, .6);
|
||||||
|
border-radius: var(--ifm-global-radius);
|
||||||
|
box-shadow: rgba(0,0,0,.1) 1px 1px 4px 1px;
|
||||||
|
box-sizing: border-box;
|
||||||
|
font-family: inherit;
|
||||||
|
padding: 0;
|
||||||
|
}
|
|
@ -17,14 +17,10 @@ function Playground({children, theme, transformCode, ...props}) {
|
||||||
transformCode={transformCode || (code => `${code};`)}
|
transformCode={transformCode || (code => `${code};`)}
|
||||||
theme={theme}
|
theme={theme}
|
||||||
{...props}>
|
{...props}>
|
||||||
<div className={styles.editorHeaderContainer}>
|
<div className={styles.playgroundHeader}>LIVE EDITOR</div>
|
||||||
<div className={styles.headerTitle}>LIVE EDITOR</div>
|
<LiveEditor />
|
||||||
</div>
|
<div className={styles.playgroundHeader}>PREVIEW</div>
|
||||||
<LiveEditor className={styles.editorContainer} />
|
<div className={styles.playgroundPreview}>
|
||||||
<div className={styles.previewHeaderContainer}>
|
|
||||||
<div className={styles.headerTitle}>PREVIEW</div>
|
|
||||||
</div>
|
|
||||||
<div className={styles.previewContainer}>
|
|
||||||
<LivePreview />
|
<LivePreview />
|
||||||
<LiveError />
|
<LiveError />
|
||||||
</div>
|
</div>
|
|
@ -5,34 +5,16 @@
|
||||||
* LICENSE file in the root directory of this source tree.
|
* LICENSE file in the root directory of this source tree.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
.headerTitle {
|
.playgroundHeader {
|
||||||
text-transform: uppercase;
|
|
||||||
color: #e1e6ef;
|
|
||||||
font-size: 11px;
|
|
||||||
font-weight: bold;
|
|
||||||
opacity: 0.4;
|
|
||||||
}
|
|
||||||
|
|
||||||
.editorHeaderContainer {
|
|
||||||
padding: 4px 8px;
|
padding: 4px 8px;
|
||||||
background: #1a2d3c;
|
background: #1a2d3c;
|
||||||
|
text-transform: uppercase;
|
||||||
|
color: rgb(225, 230, 239, .4);
|
||||||
|
font-size: 11px;
|
||||||
|
font-weight: bold;
|
||||||
}
|
}
|
||||||
|
|
||||||
.editorContainer {
|
.playgroundPreview {
|
||||||
padding: 0;
|
|
||||||
border: 0;
|
|
||||||
outline: 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
.previewHeaderContainer {
|
|
||||||
padding: 0 8px;
|
|
||||||
background: #1a2d3c;
|
|
||||||
height: 35px;
|
|
||||||
line-height: 34px;
|
|
||||||
}
|
|
||||||
|
|
||||||
.previewContainer {
|
|
||||||
position: relative;
|
position: relative;
|
||||||
padding: 16px;
|
padding: 16px 16px 0;
|
||||||
border: 0.5px solid #011627;
|
|
||||||
}
|
}
|
|
@ -1,6 +0,0 @@
|
||||||
---
|
|
||||||
id: doc5
|
|
||||||
title: Fifth Document
|
|
||||||
---
|
|
||||||
|
|
||||||
Another one
|
|
14
packages/docusaurus/templates/classic/docs/live-coding.md
Normal file
14
packages/docusaurus/templates/classic/docs/live-coding.md
Normal file
|
@ -0,0 +1,14 @@
|
||||||
|
---
|
||||||
|
id: live-coding
|
||||||
|
title: Live Coding with React Live
|
||||||
|
---
|
||||||
|
|
||||||
|
```js live
|
||||||
|
var hello = 'We got live coding now';
|
||||||
|
```
|
||||||
|
|
||||||
|
Have you ever used [React Live](https://react-live.kitten.sh)? Try it. Imagine your users play with your code _live_. Really don't know a better way to get your users' hands dirty than sharing a coding block with them.
|
||||||
|
|
||||||
|
Docusaurus now supports live coding components in-house, powered by React Live. To see how they can be used for an enriched learning experience, check out our users who are already using it:
|
||||||
|
|
||||||
|
<!-- TODO: add links to sites who uses this feature creatively -->
|
|
@ -1,6 +1,7 @@
|
||||||
{
|
{
|
||||||
"docs": {
|
"docs": {
|
||||||
"Docusaurus": ["doc1", "doc2", "doc3"],
|
"Docusaurus": ["doc1", "doc2", "doc3"],
|
||||||
|
"Docusaurus Cool": ["live-coding"],
|
||||||
"Utilities": ["style-guide"]
|
"Utilities": ["style-guide"]
|
||||||
},
|
},
|
||||||
"docs-other": {
|
"docs-other": {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue