百科.dev
全部条目在线工具AI 编程趋势榜开源项目技术资讯提交条目
登录
< 返回工具列表
N

nano-component

> 前端框架
开源

在 1kb 以下的空间内实现快速、简单的 React 组件样式

198 stars0 点赞2 次浏览
访问官网GitHub

工具介绍

在 1kb 以下的空间内实现快速、简单的 React 组件样式

nano-component

Build Status Coverage 0.8kb gzip

Fast & simple React component styles in under 1kb

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

bash
npm i nano-component
javascript
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.

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

// Hello

Multiple Styles

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

javascript
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.

javascript
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.

javascript
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

javascript
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.

javascript
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.

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

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

const Heading = nano(H2)(props => ({
  color: props.color
}))
javascript
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.

javascript
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.

javascript
import { withTheme } from 'theming'

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

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

const App = props => (
  
    
      Hello
    
  
)

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
/* CSS file */
@keyframes spin {
  from {
    transform: rotate(0deg);
  }
  to {
    transform: rotate(360deg);
  }
}
javascript
import Ring from './Ring'

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

Related

  • styled-components
  • glamorous
  • emotion
  • cxs

MIT License

Issues· 0 开放

查看全部 Issues在 GitHub 打开

暂无开放 Issues,或尚未同步最近议题。

> 标签

JavaScriptcss-in-jsdynamic-stylesreactreact-components

暂无评论,来聊聊你的看法吧

> 工具信息

发布日期2026年8月1日
最后更新2026年9月18日
分类前端框架
定价开源

> 相关工具

R
React
用于构建用户界面的 JavaScript 库
V
Vue.js
渐进式 JavaScript 框架
N
Next.js
基于 React 的全栈 Web 框架