@mdx-js/react#mdx JavaScript Examples
The following examples show how to use
@mdx-js/react#mdx.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: mdx-renderer.js From gatsby-digital-garden with MIT License | 6 votes |
export default function MDXRenderer({ children, ...props }) {
// Memoize the compiled component
const End = React.useMemo(() => {
if (!children) {
return null;
}
const fn = new Function(`_fn`, "React", "mdx", `${children}`);
return fn({}, React, mdx);
}, [children]);
return React.createElement(End, { ...props });
}
Example #2
Source File: index.js From r3f-website with MIT License | 5 votes |
export default function CodeHighlight({
children,
className,
live,
title,
lineNumbers,
}) {
const [copied, setCopied] = useState(false);
const codeString = children.trim();
const language = className.replace(/language-/, "");
if (live) {
return (
<LiveProvider
code={codeString}
noInline
theme={theme}
transformCode={(code) => `/** @jsx mdx */${code}`}
scope={{ mdx, Canvas }}
>
<LiveWrapper>
<StyledEditor>
<LiveEditor />
</StyledEditor>
<LivePreview />
</LiveWrapper>
<LiveError />
</LiveProvider>
);
}
const handleClick = () => {
setCopied(true);
copyToClipboard(codeString);
};
return (
<>
{title && <PreHeader>{title}</PreHeader>}
<div className="gatsby-highlight">
<Highlight
{...defaultProps}
code={codeString}
language={language}
theme={theme}
>
{({
className: blockClassName,
style,
tokens,
getLineProps,
getTokenProps,
}) => (
<Pre className={blockClassName} style={style} hasTitle={title}>
<CopyCode onClick={handleClick}>
{copied ? "Copied!" : "Copy"}
</CopyCode>
<code>
{tokens.map((line, i) => (
<div {...getLineProps({ line, key: i })}>
{lineNumbers && <LineNo>{i + 1}</LineNo>}
{line.map((token, key) => (
<span {...getTokenProps({ token, key })} />
))}
</div>
))}
</code>
</Pre>
)}
</Highlight>
</div>
</>
);
}
Example #3
Source File: CodeBlock.jsx From tonic-ui with MIT License | 4 votes |
CodeBlock = ({
/**
* Do not evaluate and mount the inline code (Default: `false`).
*/
noInline = false,
/**
* Disable editing on the `<LiveEditor />` (Default: `false`)
*/
disabled = false,
/**
* Preview only (Default: `false`)
*/
previewOnly = false,
/**
* Default is expand or collapse (Default: `false`)
*/
expanded: defaultExpanded = false,
className,
children,
...props
}) => {
const [, updateState] = useState();
const forceUpdate = useCallback(() => updateState({}), []);
const [colorMode] = useColorMode();
const [editorCode, setEditorCode] = useState(children.trim());
const {
onCopy: copySource,
hasCopied: hasCopiedSource,
} = useClipboard(editorCode);
const [isLiveEditorVisible, toggleLiveEditorVisibility] = reactHooks.useToggle(defaultExpanded);
const resetDemo = () => {
setEditorCode(children.trim());
toggleLiveEditorVisibility(false);
forceUpdate();
};
const handleLiveEditorChange = useCallback(newCode => {
setEditorCode(newCode.trim());
}, []);
const language = className && className.replace(/language-/, '');
noInline = boolean(noInline);
if (disabled === undefined) {
disabled = (language !== 'jsx');
} else {
disabled = (language !== 'jsx') || boolean(disabled);
}
const liveProviderProps = {
theme: {
dark: codeBlockDark,
light: codeBlockLight,
}[colorMode],
language,
noInline,
disabled,
code: editorCode,
transformCode: code => code,
scope: {
...reactComponents,
...reactLabComponents,
...reactHooks,
...thirdPartyComponents,
Code,
FontAwesomeIcon,
InputTag,
Lorem,
SkeletonBody,
SkeletonContent,
Global, // from '@emotion/react'
css, // from '@emotion/react'
mdx, // from '@mdx-js/react'
sx, // from '@tonic-ui/styled-system'
tmicons, // from '@trendmicro/tmicon'
},
mountStylesheet: false,
...props,
};
if (previewOnly) {
return (
<LiveProvider {...liveProviderProps}>
<LiveCodePreview style={liveCodePreviewStyle} />
</LiveProvider>
);
}
const isEditable = !disabled;
if (!isEditable) {
return (
<LiveProvider {...liveProviderProps}>
<LiveEditor
style={liveEditorStyle}
css={css`
& > textarea { outline: 0; }
`}
/>
</LiveProvider>
);
}
return (
<LiveProvider {...liveProviderProps}>
<LiveCodePreview style={liveCodePreviewStyle} />
<Box display="flex" justifyContent="flex-end">
<IconButton onClick={toggleLiveEditorVisibility}>
<Tooltip label={isLiveEditorVisible ? 'Hide the source' : 'Show the source'}>
<Icon icon="code" size={{ sm: '5x', md: '4x' }} />
</Tooltip>
</IconButton>
<IconButton onClick={copySource}>
<Tooltip label={hasCopiedSource ? 'Copied' : 'Copy the source'}>
<Icon icon="file-copy-o" size={{ sm: '5x', md: '4x' }} />
</Tooltip>
</IconButton>
<IconButton onClick={resetDemo}>
<Tooltip label="Reset the demo">
<Icon icon="redo" size={{ sm: '5x', md: '4x' }} />
</Tooltip>
</IconButton>
</Box>
<Fade in={isLiveEditorVisible}>
<Collapse in={isLiveEditorVisible} unmountOnExit={true}>
<LiveEditor
onChange={handleLiveEditorChange}
style={liveEditorStyle}
css={css`
& > textarea { outline: 0; }
`}
/>
</Collapse>
</Fade>
<LiveError style={liveErrorStyle} />
</LiveProvider>
);
}