All Projects → jxnblk → Nano Component

jxnblk / Nano Component

Licence: mit
Fast & simple React component styles in under 1kb

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Nano Component

React Awesome Reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 346 (+71.29%)
Mutual labels:  react-components, css-in-js
react-awesome-reveal
React components to add reveal animations using the Intersection Observer API and CSS Animations.
Stars: ✭ 564 (+179.21%)
Mutual labels:  react-components, css-in-js
React Stylesheet
Component based styling for your React applications
Stars: ✭ 150 (-25.74%)
Mutual labels:  react-components, css-in-js
Reactackle
Open-source components library built with React and Styled-Components.
Stars: ✭ 278 (+37.62%)
Mutual labels:  react-components, css-in-js
React Native Web
React Native Components and APIs for the Web
Stars: ✭ 19,563 (+9584.65%)
Mutual labels:  react-components, css-in-js
React Free Style
Make React components easier and more maintainable by using inline style objects
Stars: ✭ 135 (-33.17%)
Mutual labels:  react-components, css-in-js
Styled Bootstrap
💅🏻 A styled-component implementation of Bootstrap
Stars: ✭ 154 (-23.76%)
Mutual labels:  react-components, css-in-js
Framework
🎨 Aesthetic is an end-to-end multi-platform styling framework that offers a strict design system, robust atomic CSS-in-JS engine, a structural style sheet specification (SSS), a low-runtime solution, and much more!
Stars: ✭ 176 (-12.87%)
Mutual labels:  css-in-js
React Components
List of React components I use and recommend
Stars: ✭ 182 (-9.9%)
Mutual labels:  react-components
React Awesome Slider
React content transition slider. Awesome Slider is a 60fps, light weight, performant component that renders an animated set of production ready UI general purpose sliders with fullpage transition support for NextJS and GatsbyJS. 🖥️ 📱
Stars: ✭ 2,343 (+1059.9%)
Mutual labels:  react-components
React Native Performance Monitor
React Native Performance Monitor - Realtime graphing of React Native render performance
Stars: ✭ 174 (-13.86%)
Mutual labels:  react-components
React Player Controls
⏯ Dumb and (re)useful React components for media players.
Stars: ✭ 176 (-12.87%)
Mutual labels:  react-components
Griffith
A React-based web video player
Stars: ✭ 2,287 (+1032.18%)
Mutual labels:  react-components
Gatsby Theme Storefront Shopify
Create a Shopify store with Gatsby JS 🛍️ 🛒
Stars: ✭ 175 (-13.37%)
Mutual labels:  css-in-js
Css In Js
Autocomplete React Native / JS Styles and converting plain CSS to JS styles
Stars: ✭ 192 (-4.95%)
Mutual labels:  css-in-js
Video React
A web video player built for the HTML5 world using React library.
Stars: ✭ 2,227 (+1002.48%)
Mutual labels:  react-components
Styled Bootstrap Components
The bootstrap components made with styled-components 💅
Stars: ✭ 196 (-2.97%)
Mutual labels:  react-components
React Vtree
React component for efficiently rendering large tree structures
Stars: ✭ 185 (-8.42%)
Mutual labels:  react-components
Modali
A delightful modal dialog component for React, built from the ground up to support React Hooks.
Stars: ✭ 183 (-9.41%)
Mutual labels:  react-components
Notus React
Notus React: Free Tailwind CSS UI Kit and Admin
Stars: ✭ 173 (-14.36%)
Mutual labels:  react-components

nano-component

Build Status Coverage 0.8kb gzip

Fast & simple React component styles in under 1kb

http://jxnblk.com/nano-component/

npm i nano-component
import nano from 'nano-component'

const Button = nano('button')({
  fontFamily: 'inherit',
  fontSize: 14,
  display: 'inline-block',
  margin: 0,
  padding: 16,
  border: 0,
  borderRadius: 4,
  color: 'white',
  backgroundColor: 'tomato',
  appearance: 'none',
  ':hover': {
    backgroundColor: 'black'
  },
  '@media screen and (min-width: 32em)': {
    fontSize: 16,
  }
})

About

Inspired by libraries like styled-components, glamorous, and emotion, nano-component is a bare-bones css-in-js library for easily creating styled React UI components.

Motivation

Encapsulating styles in UI components is a great way to build consistent, scalable UI, and using JavaScript to manage these styles makes a lot of sense for large applications with dozens of contributors. While native JavaScript provides some great low-level APIs for managing styles, libraries that interact with these APIs tend to add a lot of features that add complexity and a performance penalty for what should be a fairly straightforward task.

Perfection is attained not when there is nothing more to add, but when there is nothing more to remove.

– Antoine de Saint Exupéry

Nano component comes from a desire to show that dynamically injecting CSS with JavaScript is not that complicated. In 47 lines of code, this library provides dynamically rendered, encapsulated styles that can be computed from component props.

Features

  • 0.8 kB gzipped
  • 47 LOC
  • Fast
  • Simple API
  • Extensible
  • Use JavaScript objects for styling
  • Pseudoclasses
  • Media queries
  • Child selectors
  • Atomic code deduplication
  • No build steps or configuration required

Dynamic Styles

To dynamically adjust styles based on props, pass a function to nano-component.

const Heading = nano('h2')(props => ({
  color: props.color
}))

// <Heading color='tomato'>Hello</Heading>

Multiple Styles

Multiple arguments can be passed to nano components to help break up logic.

const color = props => ({ color: props: color })
const margin = props => ({ margin: props.margin })

const Heading = nano('h2')(
  color,
  margin
)

Number Values

Any number value will be converted to a pixel value, regardless of which CSS property it's applied to. Unlike other libraries that use a blacklist of CSS properties, nano component does not handle number values differently based on property. This is to both keep the library size to a minimum and to ensure a more deliberate API. To avoid adding pixels to unitless numbers, provide those values as strings.

const Heading = nano('h2')({
  marginTop: 32,    // converted to pixels
  marginBottom: 8,
  lineHeight: '1.5' // a unitless value

Extending Components

Other React components can be passed to nano-component to add styles, as long as that component that accepts className as a prop – for example, react-router's Link component.

import { Link } from 'react-router'
import nano from 'nano-component'

const NavLink = nano(Link)({
  textDecoration: 'none',
  fontWeight: 'bold',
  color: 'inherit'
})

styled-system

The styled-system library can be used with nano-component

import nano from 'nano-component'
import {
  space,
  width
} from 'styled-system'

const Box = nano('div')(space, width)

Setting Props

To add props to a component, use the same .defaultProps object you would use on any other React component.

const Heading = nano('h2')(props => ({
  fontSize: 32,
  color: props.color
}))

Heading.defaultProps = {
  color: 'tomato'
}

Removing Props

To remove unwanted style props from the root HTML element, use a higher order component like tag-hoc or recompose's mapProps utility. This avoids any use of whitelisting or assumptions about which props should be passed to the underlying component.

import nano from 'nano-component'
import tag from 'tag-hoc'

const H2 = tag([
  'color'
])('h2')

const Heading = nano(H2)(props => ({
  color: props.color
}))
import nano from 'nano-component'
import { mapProps } from 'recompose'

const H2 = mapProps(props => Object.assign({}, props, {
  color: null
}))('h2')

const Heading = nano(H2)(props => ({
  color: props.color
}))

Sharing Styles

Importing shared, thematic constants can help manage UI styles in a more explicit way than anything possible with CSS.

import nano from 'nano-component'
import { colors } from './theme'

const Heading = nano('h2')({
  color: colors.slate
})

Theming

The theming library can be used to add styled-components-like theming to a nano component.

import { withTheme } from 'theming'

const Heading = nano('h2')(props => ({
  fontSize: props.theme.fontSizes[4]
}))

export default withTheme(Heading)
import React from 'react'
import { ThemeProvider } from 'theming'
import theme from './theme'
import Heading from './Heading'

const App = props => (
  <ThemeProvider theme={theme}>
    <Heading>
      Hello
    </Heading>
  </ThemeProvider>
)

Benchmarks

nano-component does not have as many features as other similar libraries, but when run against similar benchmarks, nano-component tends to perform slightly better. See the benchmarks directory for more.

nano-component x 3,742 ops/sec ±4.87% (70 runs sampled)
emotion x 3,044 ops/sec ±9.99% (75 runs sampled)
glamorous x 2,151 ops/sec ±10.91% (74 runs sampled)
styled-components x 1,748 ops/sec ±3.84% (71 runs sampled)

nano-component x 3,357 ops/sec ±5.91% (70 runs sampled)
emotion x 2,714 ops/sec ±3.04% (73 runs sampled)
glamorous x 2,013 ops/sec ±3.87% (69 runs sampled)
styled-components x 1,611 ops/sec ±5.12% (67 runs sampled)

nano-component x 3,553 ops/sec ±4.16% (73 runs sampled)
emotion x 2,764 ops/sec ±4.63% (67 runs sampled)
glamorous x 1,922 ops/sec ±6.36% (68 runs sampled)
styled-components x 1,657 ops/sec ±5.09% (72 runs sampled)
library gzip size
nano-component 0.79 kB
emotion 2.42 kB
glamorous 7.95 kB
styled-components 13.8 kB

Limitations

Static/Server Rendering

While nano components will render in Node.js environments, the styles are only created in client-side environments. This means that nano components can be used for universal rendering, but will not receive styles until rendered with client-side JavaScript. Generally this is an acceptable tradeoff for SEO purposes.

Keyframes & Animations

While nano-component does not provide a mechanism for defining keyframes, predefined animations can be used in nano components.

/* CSS file */
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
import Ring from './Ring'

const Loading = nano(Ring)({
  animationName: 'spin',
  animationDuration: '1s',
  animationTimingFunction: 'linear',
  animationIterationCount: 'infinite'
})

Related

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].