npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@jetshop/flight-shortcodes

v2.0.10

Published

Render custom React components in your Flight shop using shortcodes.

Downloads

110

Readme

flight-shortcodes

This 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.

Installing

Use yarn to add it as a dependency to your shop:

$ yarn add @jetshop/flight-shortcodes

Usage

Wherever you want to render something that contains shortcodes, use the Shortcodes component exposed by this package.

import { Shortcodes } from '@jetshop/flight-shortcodes';

const ContentPage = ({ htmlString }) => (
  <Shortcodes
    content={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.

Example

Adding a shortcode to change text color:

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 }) => <span style={{ color: '#f00' }}>{children}</span>;

const App = (
  <Shortcodes
    content={content}
    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:

const content = `This is me: [Image src="profile.jpg" /]`;

// src will get passed to the component as you'd expect:
const Image = ({ src }) => <img src={src} alt="Profile picture" />;

API

The Shortcodes component takes a few props.

content (string)

The original content string containing HTML, shortcodes and/or text.

components ([key: string]: Component)

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!

failGracefully (boolean) (default: true)

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.

onError (function)

This function will be triggered if an error is thrown in one of the components provided in the components prop, when rendering the shortcodes.

Issues

If you have any problems with this module, please don't hesitate to open an issue in this repository.

Contributions are always appreciated!

Development

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.