Render custom React components in your Flight shop using shortcodes.
npm install @jetshop/flight-shortcodesThis module enables the use of Shortcodes in any place where you render an HTML string. It is built specifically for Jetshop Flight, but can be used in any React app.
Use yarn to add it as a dependency to your shop:
``bash`
$ yarn add @jetshop/flight-shortcodes
Wherever you want to render something that contains shortcodes, use the Shortcodes component exposed by this package.
`javascript
import { Shortcodes } from '@jetshop/flight-shortcodes';
const ContentPage = ({ htmlString }) => (
components={{
gallery: MyCustomGalleryComponent,
}}
/>
);
`
This code will take a string containing HTML and/or shortcodes, turn it into React components and if a shortcode or HTML tag matches a component passed via the components prop, it will use that for rendering.
Adding a shortcode to change text color:
`javascript
import React from 'react';
import ReactDOM from 'react-dom';
import { Shortcodes } from '@jetshop/flight-shortcodes';
const content = This text will be [red]of a different color[/red].;
const Red = ({ children }) => {children};
const App = (
components={{
Red,
}}
/>
);
ReactDOM.render(App, document.getElementById('root'));
`
Note: Shortcodes are case-insensitive due to how the parser works.
You can also pass props to your component using shortcodes:
`javascriptThis is me: [Image src="profile.jpg" /]
const content = ;
// src will get passed to the component as you'd expect:
const Image = ({ src }) => ;
`
The Shortcodes component takes a few props.
The original content string containing HTML, shortcodes and/or text.
An object containing shortcode/HTML tag name as key (case-insensitive), and a React component as value. Whenever a shortcode or HTML tag name matches a key provided in this object, it will use that React component to render instead of the native HTML tag.
Any attributes you provide through shortcode or HTML syntax will be passed as props to the React component.
Note: There's a little caveat when using this syntax to pass attributes: [Container visible]Contents[/Container]. In pure React, you would expect this to mean visible={true}, but due to how the HTML spec and the parser used under the hood works, it is parsed to visible="". So be careful if your React component relies on this!
If this is set to true, we will wrap all components passed in the components prop in an error boundary to gracefully handle errors in the components.
This function will be triggered if an error is thrown in one of the components provided in the components prop, when rendering the shortcodes.
If you have any problems with this module, please don't hesitate to open an issue in this repository.
Contributions are always appreciated!
To build this module in watch mode run yarn dev.
To build a production release, run yarn build.
To start the test runner, run yarn test`.