Skip to content
This repository was archived by the owner on Jan 24, 2025. It is now read-only.

Commit cecbea1

Browse files
committed
fix(docz-theme-default): use class inside playground
1 parent 3116ff1 commit cecbea1

File tree

3 files changed

+21
-24
lines changed

3 files changed

+21
-24
lines changed

packages/docz-theme-default/src/components/ui/Render/index.tsx

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as React from 'react'
22
import { SFC, Fragment, Component } from 'react'
33
import { renderToStaticMarkup } from 'react-dom/server'
4-
import { RenderComponentProps, ThemeConfig, theme } from 'docz'
4+
import { RenderComponentProps, ThemeConfig } from 'docz'
55
import { LiveProvider, LiveError, LivePreview } from 'react-live'
66
import styled, { css } from 'react-emotion'
77
import lighten from 'polished/lib/color/lighten'
@@ -71,7 +71,7 @@ const StyledPreview = styled(LivePreview)`
7171
width: 100%;
7272
`
7373

74-
const DummyPlayground = styled('div')`
74+
const Playground = styled('div')`
7575
width: 100%;
7676
`
7777

@@ -106,8 +106,7 @@ const Actions = styled('div')`
106106
: darken(0.04, borderColor(p))};
107107
border-left: 1px solid ${themeGet('colors.border')};
108108
border-bottom: 1px solid ${themeGet('colors.border')};
109-
border-radius: ${p =>
110-
themeGet('showPlaygroundEditor')(p) ? '0' : '0 0 0 4px'};
109+
border-radius: ${p => (p.withRadius ? '0' : '0 0 0 4px')};
111110
`
112111

113112
const actionClass = (p: any) => css`
@@ -218,7 +217,7 @@ class RenderBase extends Component<RenderProps, RenderState> {
218217
const showHtml = this.handleShow('html')
219218

220219
return (
221-
<Actions>
220+
<Actions withRadius={this.state.showEditor}>
222221
<Tabs>
223222
{showEditor && (
224223
<>
@@ -313,7 +312,6 @@ class RenderBase extends Component<RenderProps, RenderState> {
313312

314313
return (
315314
<LiveProvider
316-
noInline
317315
code={this.state.code}
318316
scope={scope}
319317
transformCode={this.transformCode}
@@ -327,9 +325,9 @@ class RenderBase extends Component<RenderProps, RenderState> {
327325
{(live: any) => (
328326
<PlaygroundWrapper full={fullscreen}>
329327
{live.error && (
330-
<DummyPlayground className={className} style={style}>
328+
<Playground className={className} style={style}>
331329
{this.props.component}
332-
</DummyPlayground>
330+
</Playground>
333331
)}
334332
<StyledPreview className={className} style={style} />
335333
<StyledError />
@@ -411,16 +409,9 @@ class RenderBase extends Component<RenderProps, RenderState> {
411409
this.setState(state => ({ key: state.key + 1 }))
412410
}
413411

414-
private transformCode(code: string): string {
415-
return `
416-
const DoczApp = ({ children }) => (
417-
<React.Fragment>
418-
{children && typeof children === 'function' ? children() : children}
419-
</React.Fragment>
420-
)
421-
422-
render(<DoczApp>${code}</DoczApp>)
423-
`
412+
private transformCode = (code: string) => {
413+
if (code.startsWith('()') || code.startsWith('class')) return code
414+
return `<React.Fragment>${code}</React.Fragment>`
424415
}
425416

426417
private codesandboxUrl = (native: boolean): string => {

packages/docz-utils/src/jsx.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,18 @@ export const removeTags = (code: string) => {
1616
return code.replace(open(code), '').replace(close(code), '')
1717
}
1818

19-
export const sanitizeCode = (code: string) =>
20-
strip(code)
19+
export const sanitizeCode = (code: string) => {
20+
const trimmed = strip(code).trim()
21+
const newCode =
22+
trimmed.startsWith('{') && trimmed.endsWith('}')
23+
? trimmed.substr(1, trimmed.length - 2)
24+
: trimmed
25+
26+
return strip(newCode)
2127
.trim()
2228
.replace(/'/g, `\\'`)
2329
.replace(/`/g, '\\`')
30+
}
2431

2532
export const componentName = (value: any) => {
2633
const match = value.match(/^\<\\?(\w+)/)

packages/docz/src/components/Playground.tsx

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@ import * as React from 'react'
22
import { ComponentType, SFC } from 'react'
33
import { withMDXComponents } from '@mdx-js/tag/dist/mdx-provider'
44

5-
import { ComponentsMap, Identity } from './DocPreview'
6-
import { isFn } from '../utils/helpers'
5+
import { ComponentsMap } from './DocPreview'
76

87
export interface PlaygroundProps {
98
components: ComponentsMap
@@ -21,7 +20,7 @@ const BasePlayground: SFC<PlaygroundProps> = ({
2120
components,
2221
className,
2322
style,
24-
wrapper: Wrapper = Identity,
23+
wrapper: Wrapper,
2524
children,
2625
__scope,
2726
__position,
@@ -35,7 +34,7 @@ const BasePlayground: SFC<PlaygroundProps> = ({
3534
className={className}
3635
style={style}
3736
components={components}
38-
component={<Wrapper>{isFn(children) ? children() : children}</Wrapper>}
37+
component={Wrapper ? <Wrapper>{children}</Wrapper> : children}
3938
scope={__scope}
4039
position={__position}
4140
code={__code}

0 commit comments

Comments
 (0)