2
2
import React from "react"
3
3
import Highlight , { defaultProps } from "prism-react-renderer"
4
4
import theme from "prism-react-renderer/themes/vsDark"
5
- import { LiveProvider , LiveEditor , LiveError , LivePreview } from "react-live "
5
+ import loadable from "@loadable/component "
6
6
import { useMDXScope } from "gatsby-plugin-mdx/context"
7
7
import { useMDXComponents } from "@mdx-js/react"
8
8
import { css } from "@emotion/core"
@@ -19,6 +19,20 @@ const badgeStyle = {
19
19
letterSpacing : `0.05em` ,
20
20
}
21
21
22
+ const LazyLiveProvider = loadable ( async ( ) => {
23
+ const Module = await import ( `react-live` )
24
+ const { LiveProvider, LiveEditor, LiveError, LivePreview } = Module
25
+ return ( props ) => (
26
+ < LiveProvider { ...props } >
27
+ < div css = { css ( badgeStyle ) } > Editor</ div >
28
+ < LiveEditor css = { css ( { marginBottom : `1rem` , borderRadius : `0.25rem` } ) } />
29
+ < LiveError />
30
+ < div css = { css ( badgeStyle ) } > Preview</ div >
31
+ < LivePreview css = { css ( { marginBottom : `3rem` } ) } />
32
+ </ LiveProvider >
33
+ )
34
+ } )
35
+
22
36
const Code = ( { codeString, language, live, noInline } ) => {
23
37
const imported = useMDXScope ( )
24
38
const defined = useMDXComponents ( )
@@ -27,13 +41,12 @@ const Code = ({ codeString, language, live, noInline }) => {
27
41
28
42
if ( live ) {
29
43
return (
30
- < LiveProvider code = { codeString } theme = { theme } noInline = { noInline || false } scope = { { ...defined , ...imported } } >
31
- < div css = { css ( badgeStyle ) } > Editor</ div >
32
- < LiveEditor css = { css ( { marginBottom : `1rem` , borderRadius : `0.25rem` } ) } />
33
- < LiveError />
34
- < div css = { css ( badgeStyle ) } > Preview</ div >
35
- < LivePreview css = { css ( { marginBottom : `3rem` } ) } />
36
- </ LiveProvider >
44
+ < LazyLiveProvider
45
+ code = { codeString }
46
+ theme = { theme }
47
+ noInline = { noInline || false }
48
+ scope = { { ...defined , ...imported } }
49
+ />
37
50
)
38
51
}
39
52
return (
0 commit comments