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

@caslin/react

v0.9.4

Published

A util for making using Caslin with React easier.

Downloads

8

Readme

Caslin React

A tool library for making use Caslin in React easier.

English | 中文

Installation

npm install @caslin/feature @caslin/react --save

Getting start

Assume that a feature.js file already exists, which exposes the feature instance generated by FeatureBuilder.define().

1. <Can> component

The attributes is the same as calling style of @caslin/feature. When the checking result is true, the children of the <Can> component is rendered, you could pass a fallback render prop as fallback element.

import { Can } from '@caslin/react';
import feature from './feature';

// 'chidlren' as render function
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// not
<Can not env="featEnv1" action="create" subject="Article" feature={feature}>
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// passThrough, pass match result as parameter
<Can passThrough env="featEnv1" action="create" subject="Article" feature={feature}>
  {(match) => <button disabled={match} onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

// 'children' element
<Can env="featEnv1" action="create" subject="Article" feature={feature}>
  <button onClick={this.createArticle.bind(this)}>Create Article</button>
</Can>

// fallback property will be rendered if feature checking result is false
<Can env="featEnv1" action="create" subject="Article" feature={feature} fallback={<div>You don't have permissioin to create Article.</div>}>
  <button>Create Article</button>
</Can>

2. <Env> component

In general case, when this component is used, the feature.setEnv() is called and the current default environment is set, you could also pass a fallback render prop as fallback element.

import { Env } from '@caslin/react';
import feature from './feature';

<Env is="featEnv1" feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

<Env not="featEnv1" feature={feature}>
  {() => <div>feature env 2</div>}
</Env>

<Env in={['featEnv1', 'featEnv2']} feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

<Env notIn={['featEnv2', 'featEnv3']} feature={feature}>
  {() => <div>feature env 1</div>}
</Env>

// passThrough, pass match result as parameter
<Env passThrough is="featEnv1" feature={feature}>
  {(match) => <div>{match ? 'is' : 'not'} env 1</div>}
</Env>

// fallback property will be rendered if feature checking result is false
<Env is="featEnv1" feature={feature} fallback={<div>Current env is not featureEnv1.</div>}>
  {() => <div>feature env 1</div>}
</Env>

3. Pass defined "feature" to avoid having to pass it every time

createCheckerBoundTo()

Pass feature as a parameter to createCheckerBoundTo() to get the higher-order component.

// featChecker.js
import { createCheckerBoundTo } from '@caslin/react';
import feature from './feature';

export default createCheckerBoundTo(feature); // { Can, Env }

You can now use the Can> and <Env> directly, instead of passing feature again:

import featUtil from './featChecker';
const { Can, Env } = featUtil;

<Can action="create" subject="Article">
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

<Env is="featEnv1">
  {() => <div>feature env 1</div>}
</Env>

createContextualChecker()

Pass Context.Consumer as a parameter to createContextualChecker() to get the higher-order component.

// featChecker.js
import React from 'react';
import { createContextualChecker } from '@caslin/react';
import feature from './feature';

export const FeatContext = React.createContext(feature);

export default createContextualChecker(FeatContext.Consumer); // { Can, Env }
// High hierarchy component
import feature from './feature';
import { FeatContext } from './featChecker';

...
<FeatContext.Provider value={feature}>
  {// ...chidlren}
</FeatContext.Provider>
...
// Use HOC
import featUtil from './featChecker';
const { Can, Env } = featUtil;

<Can action="create" subject="Article">
  {() => <button onClick={this.createArticle.bind(this)}>Create Article</button>}
</Can>

<Env is="featEnv1">
  {() => <div>feature env 1</div>}
</Env>

License

MIT License