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

@temporg/ui-component-examples

v99.10.0

Published

A utility for automatically generating component examples

Downloads

32

Readme


category: packages

ui-component-examples

npm  build-status  MIT License  Code of Conduct

A utility for automatically generating component examples.

Installation

yarn add @temporg/ui-component-examples

Usage

Using the webpack loader

For convenience, this package contains a webpack loader which can be used to load example configuration files. In the loader, each configuration file is passed to the generateComponentExamples function to generate examples.

In your webpack.config.js:

module.exports = {
  module: {
    rules: [
      {
        test: /\.js$/,
        include: [/\.examples\.js/],
        use: [
          'component-examples-loader',
          'babel-loader'
        ]
      }
    ]
  }
}

Calling the generateComponentExamples function directly

The generateComponentExamples function can be called directly as follows:

import generateComponentExamples from '@temporg/ui-component-examples'

const result = generateComponentExamples(config)

Given a configuration object, generateComponentExamples returns an array of generated examples:

Parameters

| Param | Type | Default | Description | |----------|-------------|----------|----------| | config | Object | undefined | the generator config object. See config section below for more details |

Returns

| Type | Description | |----------|-------------| | Array | array of examples broken into sections and pages if configured to do so. See examples section for more details |

Example config
export default {
 sectionProp: 'variant',
 maxExamplesPerPage: 50,
 maxExamples: 200,
 propValues: {
   variant: ['circular', 'rectangular'],
   placement: ['top', 'bottom', 'start', 'end'],
   children: [null, <button>hello</button>, <a href="#">world</a>]
 },
 getComponentProps: (props) => ({
   size: props.variant === 'circular' ? 'large' : 'small'
 }),
 getExampleProps: (props) => ({
   height: props.placement === 'top' ? '50rem' : '10rem'
 }),
 renderExample: ({ Component, componentProps, exampleProps, key }) => {
   return <View key={key} {...exampleProps}><Component {...componentProps} /></View>
 },
 renderPage: ({ examples, key, renderExample }) => {
   return <View key={key}>{examples.map(renderExample)}</View>
 },
 getParameters: ({ examples, index}) {
   return { delay: 200, viewports: [320, 1200] }
 }
}

The config is an object that sets the configuration for the example generation. It has the following properties:

sectionProp

A string value used to divide the resulting examples into sections. It should correspond to an enumerated prop in the Component

| Type | Default | |----------|-------------| | string | undefined |

maxExamplesPerPage

Specifies the max number of examples that can exist in a single page within a section

| Type | Default | |----------|-------------| | number or function | null |

// providing a number
maxExamplesPerPage: 50

// providing a function
maxExamplesPerPage: (sectionName) => sectionName === 'inverse' ? 20 : 50
Parameters

| Param | Type | Default | Description | |----------|-------------|----------|----------| | sectionName | string | undefined | the name of the current example section |

Returns

| Type | Description | |----------|-------------| | number | a number specifying the maximum examples per page |

maxExamples

Specifies the total max number of examples

| Type | Default | |----------|-------------| | number | 500 |

propValues

An object with keys that correspond to the component props. Each key has a corresponding value array. This array contains possible values for that prop.

To avoid having to manually write out every possible value for all props in each example config, we have tools available to facilitate parsing boolean and enumerated prop values. See the README for @temporg/ui-component-examples for documentation and example usage.

| Type | Default | |----------|-------------| | object of keys corresponding to arrays of possible values | undefined |

propValues: {
 variant: ['circular', 'rectangular'],
 placement: ['top', 'bottom', 'start', 'end'],
 children: [null, <button>hello</button>, <a href="#">world</a>]
}

getComponentProps

A function called with the prop combination for the current example. It returns an object of props that will be passed into the renderExample function as componentProps.

| Type | Default | |----------|-------------| | function | undefined |

getComponentProps: (props) => ({
 // Change the size prop passed to the component based on the value of
 // `variant` in the current prop combination
 size: props.variant === 'circular' ? 'large' : 'small'
})
Parameters

| Param | Type | Default | Description | |----------|-------------|----------|----------| | props | Object | undefined | the prop combination for the current example |

Returns

| Type | Description | |----------|-------------| | Object | a props object that will be passed to the renderExample function as componentProps |

getExampleProps

A function called with the prop combination for the current example. It returns an object of props that will be passed into the renderExample function as exampleProps.

| Type | Default | |----------|-------------| | function | undefined |

getExampleProps: (props) => ({
 // Change the height prop passed to the example based on the value of
 // `placement` in the current prop combination
 height: props.placement === 'top' ? '50rem' : '10rem'
})
Parameters

| Param | Type | Default | Description | |----------|-------------|----------|----------| | props | Object | undefined | the prop combination for the current example |

Returns

| Type | Description | |----------|-------------| | Object | a props object that will be passed to the renderExample function as exampleProps |

renderExample

A optional function which receives the component and example props and returns an example.

renderExample: ({ Component, componentProps, exampleProps, key }) => {
 return <View key={key} {...exampleProps}><Component {...componentProps} /></View>
}
Parameters

The parameters consist of an object with the following properties

| Param | Type | Default | Description | |----------|-------------|----------|----------| | Component | ReactComponent | undefined | the component to render | | componentProps | Object | undefined | props corresponding to the component. The result of the getComponentProps method | | exampleProps | Object | undefined | props corresponding to the example. The result of the getExampleProps method | | key | string | undefined | a unique key generated for each example |

Returns

| Type | Description | |----------|-------------| | ReactComponent | the rendered example |

renderPage

An optional function which receives an array of examples and a function to render them.

renderPage: ({ examples, renderExample }) => {
 return <View margin="small">{ examples.map(renderExample) }</View>
}
Parameters

The parameters consist of an object with the following properties

| Param | Type | Default | Description | |----------|-------------|----------|----------| | examples | Array | undefined | array of example objects with properties identical to those outlined in the renderExample params | | renderExample | function | Identical to the default in the renderExample section | The render method for each component example |

Returns

| Type | Description | |----------|-------------| | ReactComponent | the rendered page of examples |

getParameters

A function called with the examples and index for the current page of examples. It returns an object of parameters/meta data for that page of examples (e.g. to be passed in to a visual regression tool like chromatic).

| Type | Default | |----------|-------------| | function | undefined |

getParameters: ({ examples, index }) => ({
  // add a delay for the first page of examples only:
  index === 1 ? { delay: 200 } : {}
})
Parameters

| Param | Type | Default | Description | |----------|-------------|----------|----------| | props | Object | undefined | the examples and index of the current page |

Returns

| Type | Description | |----------|-------------| | Object | a parameters object with delay and viewport sizes configuration for the page |