-
Notifications
You must be signed in to change notification settings - Fork 355
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(examples and docs): adding react-live… #666
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
LIVE_EXAMPLES=true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
LIVE_EXAMPLES=true |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import React from 'react'; | ||
import { css } from '@patternfly/react-styles'; | ||
import styles from './example.styles'; | ||
import PropTypes from 'prop-types'; | ||
import * as CoreComponents from '@patternfly/react-core'; | ||
import * as CoreIcons from '@patternfly/react-icons'; | ||
import { LiveProvider, LiveEditor, LiveError, LivePreview, withLive } from 'react-live'; | ||
import { transform } from 'babel-standalone'; | ||
|
||
const propTypes = { | ||
className: PropTypes.string, | ||
raw: PropTypes.string.isRequired, | ||
images: PropTypes.array | ||
}; | ||
|
||
const defaultProps = { | ||
className: '', | ||
images: [] | ||
}; | ||
|
||
const scopePlayground = { React, ...CoreComponents, ...CoreIcons }; | ||
|
||
const transformCode = code => { | ||
try { | ||
// LiveEditor doesn't work properly with these so need to remove | ||
code = code.replace(/^\s*import.*$/gm, ''); | ||
code = code.replace(/^\s*export default class/gm, 'class'); | ||
code = code.replace(/extends Component/gm, 'extends React.Component'); | ||
code = code.replace(/^\s*export.*$/gm, ''); | ||
code = code.replace(/^\s*static.*$/gm, ''); | ||
const transformedCode = transform(code, { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Internally it looks like React-Live uses (buble)[https://buble.surge.sh/guide/#jsx] which supports jsx. I did not try this without babel so it might actually be needed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes I had more issues with buble so I switched to babel |
||
presets: ['react', 'stage-2'] | ||
}).code; | ||
return transformedCode; | ||
} catch (e) { | ||
console.log(e); | ||
// todo: handle error | ||
return code; | ||
} | ||
}; | ||
|
||
const LiveDemo = ({ className, raw, images, ...props }) => { | ||
const scope = { | ||
...scopePlayground | ||
}; | ||
for (const image of images) { | ||
const searchIndex = raw.search(image.name); | ||
if (searchIndex > -1) { | ||
const startIndex = raw.lastIndexOf('import', searchIndex); | ||
const importName = raw.substring(startIndex, searchIndex).split(' ')[1]; | ||
scope[importName] = image.file; | ||
} | ||
} | ||
|
||
return ( | ||
<LiveProvider code={raw} scope={scope} transformCode={transformCode}> | ||
<LivePreview className={css(className, styles.example)} /> | ||
<LiveEditor style={{ marginBottom: '30px' }} /> | ||
<LiveError /> | ||
</LiveProvider> | ||
); | ||
}; | ||
|
||
LiveDemo.propTypes = propTypes; | ||
LiveDemo.defaultProps = defaultProps; | ||
|
||
export default withLive(LiveDemo); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are you able to do this on gatsby node? It might speed up client-side rendering. Not a big deal if it is not possible though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion, I will try it out
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Had trouble getting the raw file contents in gatsby-node. Going to stick with this for now.