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

onno-react

v1.0.0

Published

Onno propTypes for React

Downloads

9,499

Readme

onno-react

Code Coverage Bundle Size Build Status License

Onno propTypes for React

yarn add onno-react

Usage

You can use onno-react in place of onno since it re-exports the API:

import styled from "styled-components"
import { display, padding } from "onno-react"

const Box = styled.div(display, padding)

// [{ display: "flex" }, { padding: "10px" }]
<Box display="flex" padding="10px" />

// [{ display: "grid" }, { padding: "20em" }]
<Box d="grid" p="20em" />

To add propTypes for onno's render functions to your components:

import styled from "styled-components"
import { display, padding, propTypes } from "onno-react"

const Box = styled.div(display, padding)

Box.propTypes = propTypes(display, padding)

// Warning: Failed prop type: Invalid prop "display" supplied to "Box".
// Warning: Failed prop type: Invalid prop "padding" supplied to "Box".
<Box display={true} padding={[false]} />

// Warning: Failed prop type: Invalid prop "d" supplied to "Box".
// Warning: Failed prop type: Invalid prop "p" supplied to "Box".
<Box d={true} p={[false]} />

Render functions can be passed to propTypes as an array or list of arguments:

import { display, padding, propTypes } from "onno-react"

// Array of render functions
const propTypes2 = propTypes([display, padding])

// List of render functions
const propTypes1 = propTypes(display, padding)

Following DRY principles, wrap your render functions in an array or use onno's compose method:

import styled from "styled-components"
import { display, padding, compose, propTypes } from "onno-react"

const styles = [display, padding]
const boxSet = compose({
  name: "box",
  renderers: styles
})

// Spread the styles array
const Box1 = styled.div(...styles)
Box1.propTypes = propTypes(styles)

// Pass the composed renderer
const Box2 = styled.div(boxSet)
Box2.propTypes = propTypes(boxSet)

To add other propTypes alongside the render function props:

import styled from "styled-components"
import { display, padding, propTypes } from "onno-react"
import { number, string } from "prop-types"

const styles = [display, padding]

const Box = styled.div(...styles)

Box.propTypes = {
  ...propTypes(styles),
  foo: string.isRequired,
  bar: number.isRequired
}

Sanitizing Props

To sanitize props before applying them to a component, you can use onno's omit function:

import { omit, display, padding } from "onno-react"

const omitPropsKeys = omit({
  propsKeys: ["foo", "bar"]
})

const omitRenderers = omit({
  renderers: [display, padding]
})

const omitComposite = omit({
  propsKeys: ["foo", "bar"],
  renderers: [display, padding]
})

const props = {
  foo: "foo",
  bar: "bar",
  baz: "baz",
  display: "block",
  d: "inline-block",
  padding: 4,
  p: 8
}

// {
//   baz: "baz",
//   display: "block",
//   d: "inline-block",
//   padding: 4,
//   p: 8
// }
omitPropsKeys(props)

// {
//   foo: "foo",
//   bar: "bar",
//   baz: "baz"
// }
omitRenderers(props)

// {
//   baz: "baz"
// }
omitComposite(props)

Author

Matthew Wagerfield

License

MIT