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

riot-opt-types-mixin

v1.0.2

Published

Like React component PropTypes, but for Riot tags

Downloads

6

Readme

riot-opt-types-mixin

A direct port of React's PropTypes for usage in Riot tags.

This mixin will monitor the opts being passed to your tags. If your tag is passed opts it doesn't expect, this mixin will warn you via a console error.

Installation

npm install riot-opt-types-mixin --save-dev

Usage

Define this.optTypes = {} in your riotjs tag, then include riot-opt-types-mixin as a mixin like so:

Simple Example

index.js:

import riotOptTypesMixin from 'riot-opt-types-mixin';
riot.mixin(riotOptTypesMixin);  // apply mixin to all tags

app.tag:

import { optTypes } from 'riot-opt-types-mixin';
<app>
    <script>
        // define expected opts (attributes) for your tag
        this.optTypes = {
            name: optTypes.string.isRequired
        };
    </script>
    
    <h1>Hello, {opts.name}</h1>
</app>

Now, if you pass name incorrectly to <my-tag> you get a nice descriptive error in your browser's console

<my-tag name={100}/> = Invalid opt 'name' of type 'number' supplied to 'my-tag', expected 'string'.

Advanced Example

You can also nest opt validation which can be useful when combined state management tools such as Redux

this.optTypes = {
    state: optTypes.shape({
        data: optTypes.shape({
            name: optTypes.string.isRequired
        })
    }),
    dispatch: optTypes.func.isRequired
};

Please refer to React's PropTypes Documentation for more info on how to use these optTypes.

Supported optTypes

  • optTypes.any
  • optTypes.arrayOf
  • optTypes.array
  • optTypes.bool
  • optTypes.func
  • optTypes.instanceOf
  • optTypes.number
  • optTypes.objectOf
  • optTypes.object
  • optTypes.oneOf
  • optTypes.oneOfType
  • optTypes.shape
  • optTypes.string

Please refer to React's PropTypes Documentation if you have any questions about how each of these function.

This mixin feature all the same optTypes listed in React's documentation except for element and node which validates React components. I plan on appending a "tag" checker soon for Riot.

Why?

I'm a big fan of this functionality in React and wanted to be able to utilize it in Riot apps.

This mixin isn't really benefitial to small projects, but it can help while developing larger applications because it forces your tags to strictly define what inputs they expect from their parent tags, or application state (redux, flux, etc).

Size

This mixin, when compressed, clocks in at about 5kb BUT, really it is only benefitial for local development, so you can (and probably should) trim it out for deployment builds.

If using webpack, strip-loader and NormalModuleReplacementPlugin are good places to start. (TODO: add more documentation about how to do this...)

Alternatives

Using Riot in conjunction with TypeScript allows for setting of expected prop types. RiotTS looks like a good option if you're interested in using TypeScript for your Riot app.

Contributing

PRs welcome! Please contact me if you have any questions, suggestions, or issues too!

Thanks

Most of the functionality is a direct port of React's PropTypes.