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

jsx-kit

v0.1.2

Published

Standard React Components For Efficient JSX Syntax. Inline and Render Props Tools.

Downloads

1

Readme

jsx - kit

Standard React Components For Efficient JSX Syntax. Inline and Render Props Tools. Can help clean up JSX and reduce the need to create individualized component classes.

Components:

Hook - A pure component that takes props for componentDidMount, componentWillUnmount, and onConstructor (since componentWillMount is deprecated) then returns its children as the view.

ExposeState - Takes props for all non deprecated lifecycle hooks and initialState.The child is required to be a function takes an object containing the state object, props object, and the setState method already bind to the class. (Render Props Syntax)

ExposeRef - Equivalent to Expose State, but also passes a ref object to the Render Props Syntax.

Clone - Passes props to all direct children

Flex - Sets up a flex- box container and wraps all children in flex - box items

Elements - Exposes Children as an array for Array tranformations within a callback prop (transform)

ForwardChildren - Exposes grandchildren (or any other level of children) as an array for Array tranformations within a callback prop (transform)

GETTING STARTED:

npm install jsx-kit
import { ExposeState, Clone, Flex, Hook, ExposeRef } from ‘react-kit’

USE CASES:

ExposeState - Example reads “Hello World" then changes state to read “Goodbye World" when clicked.

<ExposeState initialState={{text:"Hello"}}>
  { ({ state, setState })=>{
      let onClick=()=>setState({text:"Goodbye"})
      return <p onClick={onClick}>{state.text+" World"}</p>
    }
  }
</ExposeState>

ExposeState also has callback props for all lifecycle methods.The callbacks do not take the same form of argument because the props and state objects need to additional be passed to the callback, but all arguments are still available on the arg object.

<ExposeState
  initialState={{text:"Hello"}}
  componentDidUpdate={ ({ props, state, prevProps, prevState })=>{
     console.log(prevState)
     console.log(state)
    }
  }
>
  { ({ state, setState })=>{
      let onClick=()=>setState({text:"Goodbye"})
      return <p onClick={onClick}>{state.text+" World"}</p>
    }
  }
</ExposeState>

ExposeRef - Example places focus on the input when the component mounts.

<ExposeRef componentDidMount={({ref})=>ref&&ref.current.focus()} >
  { ({ ref })=><input ref={ref} /> }
</ExposeRef>

The ExposeRef component also has state and all lifecycle methods.

Hook - Example console logs during lifecycle hooks

<Hook
  onConstructor={({props})=>console.log("About to mount child with props")}
  componentDidMount={({props})=>{
    console.log("Mounted with props");
    console.log(props);
  }}
  componentWillUnmount={({props})=>console.log("About to unmounted with props")}
>
  <p>Any Random Component</p>
</Hook>

The hook component provides pure component class functionality for stateless components. All lifecycle hook callbacks provide an object with props as a property for an argument to expose them to the lifecycle hooks as would be available in class creation. So remember to deconstruct the argument object.

Clone - The example passes down state and theme as a prop to all direct children.

let state = {…
let theme = {…
return (
  <Clone state={state} theme={theme}>
  	<Header />
  	<Body />
  	<Footer />
  	<Modal />
  </Clone>
  )

When there are single children, it is more concise use to React.clone or add props to elements normally.

verbose example:

Define a few components

const Text =props=>{
  return <p>{props.text}</p>
}
const Text1 = props=>Text(props)
const Text2 = props=>Text(props)

Now lets use the props extract

<Clone text="Type:" propsExtractor={(entry, index, props)=>({text:props.text+entry.type.name+" index:"+index})} >
  <Text />
  <Text1 />
  <Text2 />
</Clone>

HTML output:

Type:Text index:0

Type:Text1 index:1

Type:Text2 index:2

Flex - The example wraps the children all in a flex- box container and individually in a flex- box item.

<Flex justifyContent="space-between"  >
  <p>item 0</p>
  <p>item 1</p>
  <p>item 2</p>
  <p>item 3</p>
  <p>item 4</p>
</Flex>

verbose example:

<Flex
  justifyContent="space-around"
  containerStyle={{backgroundColor:"yellow"}} //all flex-box css properties are available on props
  childrenStyle={{border:"5px solid black"}} //all children
  itemStyle={ [ {textDecoration:"underline"}, {textDecoration:"none"} ] }//array of styles
  keyExtractor={ (item, key)=>key }
  styleExtractor={ (item, key, prevStyle, props)=>key%2?{...prevStyle, backgroundColor:"grey"}:{...prevStyle, backgroundColor:"white"}  }//Alternate colors
>
  <p>item 0</p>
  <p>item 1</p>
  <p>item 2</p>
  <p>item 3</p>
  <p>item 4</p>
</Flex>

Elements - Use the Elements component to gain Array functionality on the child React elements; such as switching (with find), mapping, filter, ect.

Switching:

<Elements tranform={(elements, props)=>elements.find(el=>el.key===state.page)}>
	<Landing key="landing"/>
	<Home key="home"/>
	<About key="about"/>
	<Contact key="contact"/>
</Elements>

Display States:

<Elements transform={array=>array.filter(el=>el.props.displayStates.includes(state.displayState))} >
  <Header displayStates={["landing","home","contact","about"]} />
  <Nav displayStates={["home","contact","about"]} />
  <Greeting displayStates={["landing"]} />
  <About displayStates={["about"]} />
  <Contact displayStates={["contact"]} />
  <Home displayStates={["home"]} />
  <Footer displayStates={["home","contact","about"]} />
</Elements>

ForwardChildren - ForwardChildren transforms children with a transform callback just like in the Elements component, but the children can be nested in child elements.

Motivation: Some elements may require multiple parent operations take the example below.

We need to pass props to elements already wrapped in a Flex element, neither of the bellow will both make the elements red and wrap each element in it's own item div for flex-box:

<Clone style={{color:"red"}}>
	<Flex justifyContent="space-between"  >
	  <p>item 0</p>
	  <p>item 1</p>
	  <p>item 2</p>
	  <p>item 3</p>
	  <p>item 4</p>
	</Flex>
</Clone>

Injected props on flex!

<Flex justifyContent="space-between"  >
	<Clone style={{color:"red"}}>
	  <p>item 0</p>
	  <p>item 1</p>
	  <p>item 2</p>
	  <p>item 3</p>
	  <p>item 4</p>
	</Clone>
</Flex>

Wrapped Clone element in single flex item!

The solution is to use ForwardChildren

<ForwardChildren transform={ array=>array.map(child=>React.cloneElement(child, {style:{color:"red"}}) ) }>
	<Flex justifyContent="space-between"  >
	  <p>item 0</p>
	  <p>item 1</p>
	  <p>item 2</p>
	  <p>item 3</p>
	  <p>item 4</p>
	</Flex>
</ForwardChildren>

There also exists a prop "level" with a default of 1 that can scope children deeper in the React element tree. (Does not handle branching!) if the level is set to 0 the ForwardChildren component becomes equivalent to the Elements component.