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

pretty-proptypes

v1.7.0

Published

prettily render prop types from react components

Downloads

1,987

Readme

PrettyPropTypes

PrettyPropTypes is designed to display the output of extract-react-types. It is designed to read the output from extract-react-types, and display rich prop information for consumers.

Core usage pattern

pretty-proptypes can display props from two sources.

  • Using babel-plugin-extract-react-types and passing the component to Props

.babelrc

{
  "plugins": ["babel-plugin-extract-react-types"]
}
import MyCoolComponent from '../MyCoolComponent';

<Props heading="My Cool Component" component={MyCoolComponent} />;
  • Directly passing a component's props to Props with extract-react-types-loader or getting types from extract-react-types and writing it to a file
<Props
  heading="My Cool Component"
  props={require('!!extract-react-types-loader!../MyCoolComponent')}
/>

This analyses prop type definitions, and default props. It creates descriptions from comments before the type definitions, and will render markdown syntax using react-markings.

Quick Tips

  • Using babel-plugin-extract-react-types is definitely the easiest way to get this information from your components, however you can use extract-react-types-loader or prebuild this data with extract-react-types and read it from a file if you prefer.
  • When using extract-react-types directly or extract-react-types-loader, they will currently only look at the default export of a file. babel-plugin-extract-types will look at the default export as well as named exports.

Customisation Props

Heading

Display a heading for the collection of props. Pass in an empty string if you want no heading, otherwise it defaults to "Props".

shouldCollapseProps

Set whether the prop shapes should be shown by default, or whether they should be hidden, and require being expanded.

Components

Accepts an object that allows you to override particular style components within our prop definition. The currently modifiable components are:

  • Indent
  • Outline
  • Required
  • Type
  • StringType
  • TypeMeta

Any that are not passed in will use the default component.

Overrides

The override prop allows you to override a specific prop's definition. If you want to keep the appearance aligned, we recommend using the Prop export from PrettyPropType.

An override is invoked with all the props passed to the Prop component internally, and renders the result. In the example below, we are changing the type field, and stopping the shape component from appearing, while leaving other parts of the component the same.

import Props, { Prop } from 'pretty-proptypes'

${<Props
  heading=""
  props={require('!!extract-react-types-loader!../../PropTypes/Select')}
  overrides={{
    isACoolComponent: (props) => <Prop {...props} shapeComponent={() => null} type="All Components Object" /> }}
/>}

While you can pass style components directly to Prop, we recommend passing style components in the top level Props, and letting them flow down.

Custom layouts

In cases where a completely bespoke layout is required, use the LayoutRenderer. This component allows you to define a completely custom layout and substitute in your own UI.

The renderTypes prop is called for every prop found on a given component and allows you to specify how that type should be rendered.

If you don't want to override the default components, you can use the components property. Or import them directly from pretty-proptypes.

import { LayoutRenderer } from 'pretty-proptypes';

<LayoutRenderer
  props={require('!!extract-react-types-loader!../MyCoolComponent')}
  renderTypes={({ typeValue, defaultValue, description, required, name, type, components }) => {
    <div>
      <h2>{name}</h2>
      <components.Description>{description}</components.Description>
      {required && <components.Required>Required</components.Required>}
      <components.Type>{type}</components.Type>
      <components.PropType typeValue={typeValue} />
    </div>;
  }}
/>;