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

doc-prop-types

v1.0.0

Published

A library for generating prop documentation at runtime in tandem with prop-types

Downloads

2

Readme

doc-prop-types

Generate prop documentation at runtime in tandem with prop-types.

This library is for users building development environments, styleguides, storybooks, or anything where you'd like to know about a React component's props at runtime. Because the same API sets the PropTypes as well as the documentation, there can never be a divergence between the two!

Unlike react-docgen, which parses source files of React components and uses that to output JSON that describes that component's props, doc-prop-types is used at runtime, which means that it can do two things that react-docgen has some difficulty with:

  • Use prop-types from imported components (e.g. ImageButton.PropTypes = { ...Button.propTypes })
  • Allow you to write and structure your components any way you like (multiple components per file, unusual component declarations)

The only price of this is that this is nearly a drop-in replacement for React's own prop-types package: props are assigned using a assignPropTypes function instead of by assigning to the propTypes property.

Installation

yarn add doc-prop-types

Usage example

Before

import PropTypes from "prop-types";

PetOwnerInfo.propTypes = {
  name: PropTypes.string.isRequired,
  age: PropTypes.number,
  pet: PropTypes.shape({
    name: PropTypes.string.isRequired,
    species: PropTypes.oneOf("Dog", "Cat")
  })
};

After

import DocPropTypes, { assignPropTypes } from "doc-prop-types";
// Or with ES5 and `require`:
// var DocPropTypes = require('doc-prop-types').default ;
// var assignPropTypes = require('doc-prop-types').assignPropTypes;

assignPropTypes(PetOwnerInfo, {
  name: DocPropTypes.string.isRequired,
  age: DocPropTypes.number,
  pet: DocPropTypes.shape({
    name: DocPropTypes.string.isRequired,
    species: DocPropTypes.oneOf("Dog", "Cat")
  })
});

The API of DocPropTypes is identical to that of PropTypes, with an exception for custom prop types (see below).

assignPropTypes will mutate and return the component given as the first argument, with the following new properties added to it:

  • Component.propTypes will have vanilla proptypes so that React can do its type-checking.
  • Component.docPropTypes will have DocPropTypes so that you can import them into other components.
  • Component.propInfo will have a description of the props assigned to the component (the good bit).

The description on propInfo will look something like this (in the case of the above example):

{
    name: {
      type: 'string',
      isRequired: true
    },
    age: {
      type: 'number',
      isRequired: false
    },
    pet: {
      type: 'shape',
      isRequired: false,
      shape: {
        name: {
          type: 'string',
          isRequired: true
        },
        species: {
          type: 'oneOf',
          isRequired: false,
          enums: [
            'Dog',
            'Cat'
          ]
        }
      }
    }
}

Importing PropTypes from other files

Imported propTypes must also be DocPropTypes:

import Button from "./Button";

const ImagePropTypes = {
  src: DocPropTypes.string.isRequired
};

assignPropTypes(ImageButton, {
  ...ImagePropTypes,
  ...Button.docPropTypes
});

If it's valid JavaScript, you can structure and combine your DocPropTypes in whatever way you wish.

Custom prop types

DocPropTypes has one API difference with prop-types, and that's with how you specify custom proptypes:

// In component proptype definition
{
  customProp: myCustomPropType;
}

// With DocPropTypes
{
  customProp: DocPropTypes.custom(myCustomPropType);
}

Things this package doesn't do (yet)

The items listed below haven't been excluded for any philosophical reason. We either just haven't gotten around to it or don't know how to make a nice API for it!

  • Doesn't document descriptions/comments to props

Contributing

Pull requests are more than welcome. Please make sure that tests pass (with yarn test), that linting passes (with yarn lint), and that any new functionality also includes tests. Thanks!