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

@andyfischer/react-elements

v0.6.0

Published

Helper components for using atomic CSS with React

Downloads

29

Readme

react-elements

Helper components for using atomic CSS with React.

These components allow you to directly add CSS class names onto the React element.

Works great with Tailwind CSS. An alternative to using the classnames library.

Quick Example

Sample code:

import { Span } from '@andyfischer/react-elements'

return (
  <Span m-1 p-1 flex border rounded-sm>...</Span>
)

Renders HTML that looks like:

<span class="m-1 p-1 flex border rounded-sm">...</span>

API

Element wrappers

Each of these components produces an HTML element of the same name.

Any added props will be transformed into the style and class HTML attribues. See below for the transformation rules.

| component | renders to HTML | | --------- | --------------- | | <Button {...props}> | <button ...> | | <Div {...props}> | <div ...> | | <Form {...props}> | <form ...> | | <Img {...props}> | <img ...> | | <Input {...props}> | <input ...> | | <Pre {...props}> | <pre ...> | | <Span {...props}> | <span ...> | | <Select {...props}> | <select ...> |

Block

Additionally, this library has an component called <Block> which produces a <div>. This is based on a opinion that "div" is a bad name and the name "block" is better.

Style

Another export is called <Style/>. This component wraps around its child element and transforms it.

Useful if you want to apply the same react-elements styling rules onto a child element.

Example:

import { Style } from '@andyfischer/react-elements'

<Style bg-slate-200>
  <table>
    ...
  </table>
</Style>

Renders HTML that looks like:

<table class="bg-slate-200">
  ...
</table>

Prop transformation logic

The logic for handing props on a react-element tries to be as unsurprising as possible.

The logic is:

  1. If the prop's value is non-boolean, then preserve the property onto the underlying component.

Example:

<Img src="url" />

The src has a string value so it's preserved:

<img src="url" />
  1. If the prop's value is true then add it onto the CSS class (with some special cases below).

Example:

<Span p-1 m-1 />

Renders to:

<span class="p-1 m-1" />
  1. If the prop's value is false then ignore it. This can help implement CSS classes that are conditional.

Example:

<Span selected={false} />

Renders to:

<span />

Special Cases

Below are various special cases to the above logic. These are for props which don't directly map to a CSS class name.

'className'

If a className value is provided then it's concatenated with CSS classes that come from other props.

Example:

<Block m-1 className="selected" />

Renders to:

<Block className="m-1 selected" />

'disabled'

The disabled prop is passed directly onto the element.

Example:

<Button m-1 disabled />

Renders to:

<button className="m-1" disabled />

Props that map into the style={} section

For props listed below, these are passed into the element's style object (if they have a non-boolean value).

Includes:

| prop name | | --------- | | grid | | gridArea | | gridColumn | | gridRow | | flex |

Example:

<Block gridArea="1 / 2" />

Renders to:

<div style={{ gridArea: "1 / 2 "}} 

Special prop: 'grid'

If the grid prop has a value, then it's copied into the style object, AND the element's style is also set to display: grid. (matching the grid class from Tailwind).

Example:

<Block grid="repeat(2, 60px) / auto-flow 80px" />

Renders to:

<div style={{ display: 'grid', grid: 'repeat(2, 60px) / auto-flow 80px' }} />

Props that start with var--

If the prop name starts with var-- then it's treated as a CSS variable and added into the element's style.

Example:

<Block var--color="#fff">

Renders to: <div style={{'--color': '#fff'}} />