babel-plugin-react-code-block
Version:
Display React functional examples with source code.
78 lines (55 loc) • 1.92 kB
Markdown
Display React functional examples with source code.
* [Installation](
* [Documentation](
* [Code of Conduct](
* [Contributing](
* [License](
**npm**
```bash
npm install babel-plugin-react-code-block
```
**yarn**
```bash
yarn add babel-plugin-react-code-block
```
Create a code block component with a `code` property, which will be populated by this plugin:
```js
import React from 'react'
export default ({children, code}) => {
return (
<div>
{children}
<code>{code}</code>
</div>
)
}
```
> Note: In the above example component children is the passed in JSX that you want to render as a demo and code is the passed in JSX converted to a string so you can also print the code sample (which is run through prettier to make it look aesthetically pleasing).
In your Babel configuration include:
```js
module.exports = {
"plugins": [
[
"babel-plugin-react-code-block",
{
"component": "CodeBlock",
},
],
],
}
```
> Note: In the above configuration change `CodeBlock` to the name of your code block component. This plugin will simply apply the transform to any JSX elements matching the component name.
Please see the [code of conduct](CODE_OF_CONDUCT.md).
Please see the [contributing guide](CONTRIBUTING.md).
[](LICENSE.md)
[]: https://img.shields.io/codecov/c/github/dogma-io/babel-plugin-react-code-block.svg "Code Coverage"
[]: https://codecov.io/gh/dogma-io/babel-plugin-react-code-block
[]: https://img.shields.io/npm/v/babel-plugin-react-code-block.svg "NPM Version"
[]: https://www.npmjs.com/package/babel-plugin-react-code-block