Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

pretty-proptypes

atlassian1.4kMIT1.7.0TypeScript support: definitely-typed

prettily render prop types from react components

extract-react-types, react, props, documentation

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>;
  }}
/>;

changelog

pretty-proptypes

1.7.0

Minor Changes

  • 67a16ca: Adds "sortProps" and "requiredPropsFirst" props to LayoutRenderer and HybridLayout

Patch Changes

  • 67a16ca: Permalink generation should also work now when extract-react-types is used.

1.6.1

Patch Changes

  • e0686d4: Changed the heading styles for deprecated props and removed "@deprecated" from the prop description.

1.6.0

Minor Changes

  • 33d0126: Introduces new functionality enabling permalinks to each section of a props list. It's done in a backwards-compatible manner requiring no code changes for a dependency bump, but you might want to double check styles are suitable to the surroundings of where the props are displayed.

1.5.2

Patch Changes

  • 2bef5ff: Reintroduces null check in the case where props can't be parsed and return empty

1.5.0

Minor Changes

  • f34959f: Adds the ability to override the Prop Expander + Fixes a few styling issues

1.4.0

Minor Changes

  • 093e1af: Adds a generic layout renderer which can be used to create bespoke prop tables. Also refactors the internals of the hybrid layout to utilise it

1.3.0

Minor Changes

  • a763063 #199 Thanks @madou! - Props that have comments which start with eslint-ignore or @ts- are no longer rendered, other surrounding comments are still rendered for the prop however.

  • a763063 #199 Thanks @madou! - Props that have comments which contain @internal or @access private are no longer rendered to the props table, essentially having the prop and all of its comments hidden.

1.2.0

Minor Changes

1.1.4

Patch Changes

  • a879c29 #146 Thanks @declan-warn! - Add missing jsx pragma in packages/pretty-proptypes/src/PropsTable/index.js which fixes an erroneous css attribute being rendered in prop table tr's.

1.1.3

Patch Changes

1.1.2

Patch Changes

1.1.1

Patch Changes

1.1.0

Minor Changes

  • e2519d0 #122 Thanks @gwyneplaine! - Table view added to pretty-proptypes in the form of PropTable component, exported from src

1.0.6

Patch Changes

1.0.5

1.0.4

Patch Changes

1.0.3

Patch Changes

1.0.2

Patch Changes

  • [patch]089780f: Change console.error for null members in pretty-proptypes to console.warn

1.0.1

Patch Changes

  • [patch]ac12401: Work around added in object converter to not throw on null value in members list

1.0.0

Major Changes

  • [major]13719db: Upgrade to emotion 10. If you've been using emotion-server to server render this package, you can remove it now because server rendering will work without it.

Patch Changes

0.6.6

0.6.5

Patch Changes

  • [patch]e6cc1f5: Remove dangerous debug code that broke everything

0.6.4

0.6.3

0.6.2

0.6.1

Patch Changes

0.6.0

0.5.0

  • Update to support 0.15.0 of extract-react-types
  • breaking The structure of the Program kind from extract-react-types has changed. 0.5.0 consumes and responds to that change, and will only work with later versions of extract-react-types.

v0.4.2

  • Use <Whitespace /> to mean the prop type can be selected separately to the prop name

Thanks Maciej Adamczak for

v0.4.1

Thanks Michael Blaszczyk for these contributions!

  • Support spreading generic types that cannot be reduced down to objects
  • Move reduceToObj to kind2string and then update kind2string dependency

v0.4.0

  • pull in newer version of kind2string, adding support for 'export' and 'exportSpecifier' kinds.

v0.3.0

  • Add arrayType converter
  • Remove whitespace created by converting a type that wasn't there

v0.2.3

  • fix bug where object converter would assume all spreads had members when they were resolved from generic. Some spreads will resolve to an import. Used simple solution of allowing spreads that did not resolve to have members gets caught with a second call to prettyConvert.