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

react-desc

v4.1.3

Published

Add an schema to your React components based on React PropTypes.

Downloads

18,613

Readme

react-desc

Slack Build Status Code Climate Test Coverage

Add a schema to your React components based on React PropTypes

Installation

npm install react-desc

Usage

Adding documentation

// Anchor.js

import React from 'react';
import ReactPropTypes from 'prop-types';
import { describe, PropTypes } from 'react-desc';

const Anchor = (props) => {
  const { path, ...rest } = props;
  return (
    <a href={path} {...rest}>{props.children}</a>
  );
};

export const AnchorWithSchema = describe(Anchor)
  .availableAt([
    {
      badge: 'https://codesandbox.io/static/img/play-codesandbox.svg',
      url: 'https://codesandbox.io/s/github/grommet/grommet-site?initialpath=anchor&amp;module=%2Fscreens%2FAnchor.js',
    },
  ])
  .description('A text link');

AnchorWithSchema.propTypes = {
  path: PropTypes.string.describe('React-router path to navigate to when clicked').isRequired,
  href: PropTypes.string.describe('link location').deprecated('use path instead'),
  id: ReactPropTypes.string, // this will be ignored for documentation purposes
  title: PropTypes.custom(() => {}).description('title used for accessibility').format('XXX-XX'),
  target: PropTypes.string.describe('target link location').defaultValue('_blank'),
};

export default Anchor;

Accessing documentation

  • JSON output

      import { AnchorWithSchema } from './Anchor';
    
      const documentation = AnchorWithSchema.toJSON();

    Expected output:

      {
          "name": "Anchor",
          "description": "A text link",
          "properties": [
            {
              "description": "React-router path to navigate to when clicked",
              "name": "path",
              "required": true,
              "format": "string"
            },
            {
              "description": "link location.",
              "name": "href",
              "deprecated": "use path instead",
              "format": "string"
            },
            {
              "description": "title used for accessibility.",
              "name": "title",
              "format": "XXX-XX"
            },
            {
              "description": "target link location.",
              "name": "target",
              "defaultValue": "_blank",
              "format": "string"
            }
          ]
        }
  • Markdown output

      import Anchor from './Anchor';
    
      const documentation = Anchor.toMarkdown();

    Expected output:

      ## Anchor Component
      A text link
    
      ### Properties
    
      | Property | Description | Format | Default Value | Required | Details |
      | ---- | ---- | ---- | ---- | ---- | ---- |
      | **path** | React-router path to navigate to when clicked | string |  | Yes |  |
      | **~~href~~** | link location. | string |  | No | **Deprecated**: use path instead |
      | **title** | title used for accessibility. | XXX-XX |  | No |  |
      | **target** | target link location. | string | _blank | No |  |
  • Typescript output

    Format entry will be a valid typescript definition.

      import { AnchorWithSchema } from './Anchor';
    
      const documentation = AnchorWithSchema.toTypescript();

    Expected output:

      {
          "name": "Anchor",
          "description": "A text link",
          "properties": [
            {
              "description": "React-router path to navigate to when clicked",
              "name": "path",
              "required": true,
              "format": "string"
            },
            {
              "description": "link location.",
              "name": "href",
              "deprecated": "use path instead",
              "format": "string"
            },
            {
              "description": "title used for accessibility.",
              "name": "title",
              "format": "any"
            },
            {
              "description": "target link location.",
              "name": "target",
              "defaultValue": "_blank",
              "format": "string"
            }
          ]
        }

API

  • describe(component)

    Creates a proxy to the actual react component with support for the following functions:

    • availableAt([{ badge: string, url: string }]): function that receives an object or an array of objects that will render where the component is available.
    • description(value): function that receives a string with the component description.
    • deprecated(value): function that receives a string with the deprecation message.
    • toJSON(): function that returns the component schema as a JSON object.
    • toMarkdown(): function that returns the component schema as a Markdown string.
    • usage(value): function that receives a string with the component usage example.
  • PropTypes

    Proxy around the React propTypes, all properties are supported. See all options here. This proxy supports the following functions:

    • defaultValue(value): function that receives a value that represents the default prop.
    • description(value): function that receives a string with the PropType description.
    • deprecated(value): function that receives a string with the deprecation message.
    • format(value): function that receives a string with the PropTypex format.

Why not react-docgen?

react-docgen is a great project but it relies on an AST parser to generate documentation. Most of the time this is ok, but for us the following use cases were hard to solve without a more verbose way to define propTypes:

  • Define deprecated properties

  • Define a required property for custom function:

    Anchor.propTypes = {
      test: () => { ... } // isRequired is not present here
    }
  • Allow internal comments for properties without it showing up in the documentation