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

recondition

v1.2.4

Published

Render Prop Condition

Downloads

158

Readme

API

Trigger

Trigger - dispatch an action when some condition is true. Trigger just executes actions in the right time, in the right React Life Cycles.

import {Trigger} from 'recondition';

<div>
 <Trigger 
     when={someCondition & this.state.variable} 
     then={() => someAction(1)} 
     finally={() => someAction(0)}
     async
     delay={100}
 />
</div>
  • when - boolean prop, activates Trigger
  • then - callback
  • finally - event on unmount, optional.
  • async - defer execution by one "tick", always fires, even if Trigger got unmounted, optional, overrides delay
  • delay - execution delay, if Trigger got unmounted before timeout - it will not fire, optiona

Mask

Mask - mask based selector. Masks in declaration form, like react-router. FeatureFlags, Media selectors, A/B tests - any condition based logic.

import {createMaskedProvider} from 'recondition';

// define the shape of mask
const Mask = createMaskedProvider({ flag1: true, flag2: false });

<div>
 <Mask.Case flag1> // only of flag1 is defined
   will render, as long flag1 is true
 </Mask.Case>
 
 <Mask.Case flag1 flag2>
   will NOT render, as long flag1 is true, but flag is false
 </Mask.Case>
  
 <Mask.Case flag1 flag2={false}>
   will  render, as long flag1 is true, but flag is false, but(!) we are looking for false
 </Mask.Case>
 
 // more complex example?
 <Mask.Switch>
   <Mask.Case flag1 flag2>
      display when flags are met
   </Mask.Case>
   <Mask.Default>
      display the default, when nothing got renderer
   </Mask.Default>   
 </Mask.Switch>   
 
 <Mask.Return flag1>
    { (match) => ( <div>this condition is { match ? true : false }</div>)}
 </Mask.Return>
</div>

By default flags are compared using strict equal, but you can override rule

const Mask = createMaskedProvider(
  { flag:'html,js,css' }, 
  { flag: (base, flag) => base.indexOf(flag)>-1}
);

<Mask.Case flag="html" />

You also might create your own react-router. Switch is a Switch and Case is a Route.

import {createMaskedProvider} from 'recondition';

const Mask = createMaskedProvider(
  { path: 'https://github.com/theKashey/recondition' },
  { path: (base, right) => base.startsWith(right)}
);

<div>
 <Mask.Switch>
   <Mask.Case path="https://github.com/theKashey/recondition">
      You are here
   </Mask.Case>
   <Mask.Case path="https://github.com/theKashey/faste">
      Another great library!
   </Mask.Case>
   <Mask.Default>
      More to come!
   </Mask.Default>   
 </Mask.Switch>   
</div>

LatestSource

LatestSource - data source "ziper". Gets multiple source as input, and provide last changed source as output. Additional feature - it would keep the last value passed thought filter(optional), making multi-source data picking easier.

Could help with controlled from more that one place components, and also capable to "Freeze", values is they are not acceptable. For example - after mouseout value from "mouse in", is not acceptable, but required for fade animation.

import {LatestSource} from 'recondition';

<LatestSource 
  input={{
    x: this.state.sourceX,
    y: this.state.sourceY,
  }}
  filter={ x => x.enabled }
>
 {(value, real, key) => (
   <>
     current value {value.position}
     in real {value.position}
     from source {value.enabled}
   </>
 )}
</LatestSource> 
  • there is GhostValue component, which does the same for a single value.

Both components are more about preserving some value, you have to preserve. Tooltips are quite good example.

Phased

Phased - the Schrodinger's state - once value changed - it will be actually changed after few phases. Useful when you have react flip some value, and have to react on that change.

Phased could be useful for animation to simulate transition, or any boolean flip which is not instant.

Accepts value and 2 optional props - phases and timeouts.

import {Phased} from 'recondition';

// semi-instant flip, but "long" enough to setup className-based animation.
<Phased value={value} phases={1}>
  {({value, nextValue, phase, phasing}) => {
    value && <SomeComponent animated={phase && nextValue}/>
  }} 
</Phased>
// value - current value
// nextValue - target value
// phase the current "phase"
// phasing - is currently phasing. Have false values in the beginning and the end.

// one second between flips
<Phased value={value} phases={0} timeouts={[1000]}>
  {({value, nextValue, phasing}) => {
    (value || nextValue) && <SomeComponent animated={phasing}/>
  }} 
</Phased>

Default value for a phases prop - 0, that means 1 step for "enter", and 1 step for "exit".

Catcher

  • Catcher - Error Boundary based promise collector (~Suspense, experimental)
  • Thrower - Error trigger. Also could provides throw-as-children prop, to give you API to throw react-catchable messages.
import {Catcher, Throw} from 'recondition';

<div>
 <Catcher 
    onCatch = { (e:Promise<any>) => doSomething(e)}
 >
   {({
       caught, // number of Promised caught
       pending, // is anything pending
       rejected, // is anything rejected
       resolved, // is all resolved
       results, // array of results (in the caught order)
   }) => (
     <div>
       do anything async
       
       <Throw when={condition} what={data} />
       
       <Throw>
        {thrower => thrower(data)}
       </Thrower>
     </div>
   )}
 </Catcher>
</div>   

catch(event filter) - is optional

Written in TypeScript

Licence

MIT