-
Notifications
You must be signed in to change notification settings - Fork 47.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Gracefully fallback to textarea in React 14
- Loading branch information
Showing
3 changed files
with
82 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
/** | ||
* react-live does not support React 14. For this version, just return | ||
* a textarea. | ||
* | ||
* TODO: Could we just get rid of react-live? It only adds syntax highlighting | ||
*/ | ||
|
||
import semver from 'semver'; | ||
import {LiveProvider, LiveEditor, LiveError} from 'react-live'; | ||
|
||
const React = window.React; | ||
const supportsReactLive = semver(React.version).major > 1; // React 15 or higher | ||
|
||
export class CodeProvider extends React.Component { | ||
render() { | ||
const {code, children} = this.props; | ||
|
||
if (supportsReactLive) { | ||
return <LiveProvider code={code} children={children} />; | ||
} | ||
|
||
return <div>{children}</div>; | ||
} | ||
} | ||
|
||
export class CodeEditor extends React.Component { | ||
render() { | ||
const {code, onChange} = this.props; | ||
|
||
if (supportsReactLive) { | ||
return <LiveEditor onChange={onChange} />; | ||
} | ||
|
||
return ( | ||
<textarea value={code} onChange={event => onChange(event.target.value)} /> | ||
); | ||
} | ||
} | ||
|
||
export class CodeError extends React.Component { | ||
render() { | ||
let {error, className} = this.props; | ||
|
||
if (supportsReactLive) { | ||
return <LiveError className={className} />; | ||
} | ||
|
||
return error ? <div className={className}>{error.message}</div> : null; | ||
} | ||
} |
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