-
Notifications
You must be signed in to change notification settings - Fork 239
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from probablyup/master
upgrade to use React v16 context, polyfill for older Reacts
- Loading branch information
Showing
8 changed files
with
427 additions
and
293 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,26 @@ | ||
import React from 'react' | ||
import PropTypes from 'prop-types' | ||
import { LiveContextTypes } from './LiveProvider' | ||
import Editor from '../Editor' | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import { LiveContext } from './LiveProvider'; | ||
import Editor from '../Editor'; | ||
|
||
const LiveEditor = (props, { live }) => ( | ||
<Editor | ||
{...props} | ||
code={live.code} | ||
onChange={code => { | ||
live.onChange(code) | ||
export default function LiveEditor(props) { | ||
return ( | ||
<LiveContext.Consumer> | ||
{({ code, onChange }) => ( | ||
<Editor | ||
{...props} | ||
code={code} | ||
onChange={code => { | ||
onChange(code); | ||
|
||
if (typeof props.onChange === 'function') { | ||
props.onChange(code) | ||
} | ||
}} | ||
/> | ||
) | ||
|
||
LiveEditor.contextTypes = LiveContextTypes | ||
LiveEditor.propTypes = { onChange: PropTypes.func } | ||
|
||
export default LiveEditor | ||
if (typeof props.onChange === 'function') { | ||
props.onChange(code); | ||
} | ||
}} | ||
/> | ||
)} | ||
</LiveContext.Consumer> | ||
); | ||
} | ||
|
||
LiveEditor.propTypes = { onChange: PropTypes.func }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,17 @@ | ||
import React from 'react' | ||
import { LiveContextTypes } from './LiveProvider' | ||
import cn from '../../utils/cn' | ||
import React from 'react'; | ||
import { LiveContext } from './LiveProvider'; | ||
import cn from '../../utils/cn'; | ||
|
||
const LiveError = ({ className, ...rest }, { live }) => live.error ? ( | ||
<div | ||
{...rest} | ||
className={cn('react-live-error', className)} | ||
> | ||
{live.error} | ||
</div> | ||
) : null | ||
|
||
LiveError.contextTypes = LiveContextTypes | ||
|
||
export default LiveError | ||
export default function LiveError({ className, ...rest }) { | ||
return ( | ||
<LiveContext.Consumer> | ||
{({ error }) => | ||
error ? ( | ||
<div {...rest} className={cn('react-live-error', className)}> | ||
{error} | ||
</div> | ||
) : null | ||
} | ||
</LiveContext.Consumer> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,13 @@ | ||
import React from 'react' | ||
import { LiveContextTypes } from './LiveProvider' | ||
import cn from '../../utils/cn' | ||
|
||
const LivePreview = ({ className, ...rest }, { live: { element }}) => { | ||
const Element = element; | ||
import React from 'react'; | ||
import { LiveContext } from './LiveProvider'; | ||
import cn from '../../utils/cn'; | ||
|
||
export default function LivePreview({ className, ...rest }) { | ||
return ( | ||
<div | ||
{...rest} | ||
className={cn('react-live-preview', className)} | ||
> | ||
{Element && <Element />} | ||
<div {...rest} className={cn('react-live-preview', className)}> | ||
<LiveContext.Consumer> | ||
{({ element: Element }) => Element && <Element />} | ||
</LiveContext.Consumer> | ||
</div> | ||
); | ||
} | ||
|
||
LivePreview.contextTypes = LiveContextTypes | ||
|
||
export default LivePreview |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,16 @@ | ||
import React, { Component } from 'react' | ||
import { LiveContextTypes } from '../components/Live/LiveProvider' | ||
import React, { Component } from 'react'; | ||
import { LiveContext } from '../components/Live/LiveProvider'; | ||
|
||
const withLive = WrappedComponent => { | ||
export default function withLive(WrappedComponent) { | ||
class WithLive extends Component { | ||
static contextTypes = LiveContextTypes | ||
|
||
render() { | ||
const { live } = this.context | ||
return <WrappedComponent live={live} {...this.props} /> | ||
return ( | ||
<LiveContext.Consumer> | ||
{live => <WrappedComponent live={live} {...this.props} />} | ||
</LiveContext.Consumer> | ||
); | ||
} | ||
} | ||
|
||
return WithLive | ||
return WithLive; | ||
} | ||
|
||
export default withLive |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,30 +1,36 @@ | ||
import React from 'react'; | ||
import styled from 'styled-components'; | ||
import { storiesOf } from '@storybook/react'; | ||
import { withKnobs, boolean } from '@storybook/addon-knobs/react'; | ||
|
||
import { | ||
LiveProvider, | ||
LiveEditor, | ||
LiveError, | ||
LivePreview | ||
} from '../src/index' | ||
import { LiveProvider, LiveEditor, LiveError, LivePreview } from '../src/index'; | ||
|
||
const code = (` | ||
const code = ` | ||
<strong> | ||
Hello World! | ||
Next Indent Level | ||
</strong> | ||
`).trim() | ||
`.trim(); | ||
|
||
const StyledLivePreview = styled(LivePreview)` | ||
background: green; | ||
color: white; | ||
padding: 3px; | ||
`; | ||
|
||
storiesOf('Live', module) | ||
.addDecorator(withKnobs) | ||
.add('default', () => ( | ||
<LiveProvider | ||
code={code} | ||
noInline={boolean('No inline evaluation', false)} | ||
> | ||
<LiveProvider code={code} noInline={boolean('No inline evaluation', false)}> | ||
<LiveEditor /> | ||
<LiveError /> | ||
<LivePreview /> | ||
</LiveProvider> | ||
)) | ||
.add('styled subcomponents', () => ( | ||
<LiveProvider code={code} noInline={boolean('No inline evaluation', false)}> | ||
<LiveEditor /> | ||
<LiveError /> | ||
<StyledLivePreview /> | ||
</LiveProvider> | ||
)); |
Oops, something went wrong.