GithubHelp home page GithubHelp logo

streamich / cxs Goto Github PK

View Code? Open in Web Editor NEW

This project forked from cxs-css/cxs

0.0 2.0 0.0 986 KB

fast af css-in-js in 0.7kb

Home Page: http://jxnblk.com/cxs

License: MIT License

JavaScript 100.00%

cxs's Introduction

cxs

fast af css-in-js in 0.7kb

http://jxnblk.com/cxs

Build Status Coverage js-standard-style 0.7kb gzip

const className = cxs({ color: 'tomato' })

cxs is a minimal css-in-js solution that uses an atomic css approach to maximize performance and deduplication

Features

  • 0.7 KB
  • Zero dependencies
  • High performance
  • Style encapsulation
  • Deduplicates repeated styles
  • Dead-code elimination
  • Framework independent
  • Media queries
  • Pseudoclasses
  • Nesting
  • No CSS files
  • No tagged template literals
  • Optional React component API

Install

npm install cxs

Usage

cxs works with any framework, but this example uses React for demonstration purposes.

import React from 'react'
import cxs from 'cxs'

const Box = (props) => {
  return (
    <div {...props} className={className} />
  )
}

const className = cxs({
  padding: '32px',
  backgroundColor: 'tomato'
})

export default Box

Pseudoclasses

const className = cxs({
  color: 'tomato',
  ':hover': {
    color: 'black'
  }
})

Media Queries

const className = cxs({
  fontSize: '32px',
  '@media screen and (min-width: 40em)': {
    fontSize: '48px'
  }
})

Child Selectors

const className = cxs({
  color: 'black',
  ' > a': {
    color: 'tomato'
  }
})

Static/Server-Side Rendering

For Node.js environments, use the css() method to return the static CSS string after rendering a view.

import React from 'react'
import ReactDOMServer from 'react-dom/server'
import cxs from 'cxs'
import App from './App'

const html = ReactDOMServer.renderToString(<App />)
const css = cxs.css()

const doc = `<!DOCTYPE html>
<style>${css}</style>
${html}
`

// Reset the cache for the next render
cxs.reset()

Note: cxs does not currently have a mechanism for rehydrating styles on the client, so use with caution in universal JavaScript applications.

React Components

cxs also has an alternative higher order component API for creating styled React components, similar to the styled-components API.

import cxs from 'cxs/component'

const Heading = cxs('h1')({
  margin: 0,
  fontSize: '32px',
  lineHeight: 1.25
})

Extending components

To extend a cxs component, pass it to the component creator function.

const Button = cxs('button')({
  color: 'white',
  backgroundColor: 'blue'
})

const TomatoButton = cxs(Button)({
  backgroundColor: 'tomato'
})

Other components

Any component can be passed to cxs to add styles.

import { Link } from 'react-router'

const MyLink = cxs(Link)({
  color: 'tomato'
})

Note: components must accept className as a prop to work with cxs.

Functional styles

cxs components can also handle dynamic styling based on props by passing a function as an argument

const Heading = cxs('h1')(props => ({
  color: props.color
}))

Removing style props

To remove style props from the rendered HTML element, use the prop-types package to define propTypes on a component. cxs/component will remove any prop that matches a key from the propTypes object.

import cxs from 'cxs/component'
import PropTypes from 'prop-types'

const Heading = cxs('h2')(props => ({
  fontSize: props.big ? '48px' : '32px'
}))

Heading.propTypes = {
  big: PropTypes.bool
}

styled-system

Style utility functions, like those in styled-system, can be used with cxs/component.

import cxs from 'cxs/component'
import {
  space,
  color
} from 'styled-system'

const Heading = cxs('h2')(space, color)

Theming

Theming is supported with the <ThemeProvider> component.

import React from 'react'
import ThemeProvider from 'cxs/ThemeProvider'
import theme from './theme'

const App = props => (
  <ThemeProvider theme={theme}>
    <Heading>
      Hello
    </Heading>
  </ThemeProvider>
)
import cxs from 'cxs/component'

const Heading = cxs('h2')(props => ({
  fontSize: props.theme.fontSizes[4] + 'px',
  color: props.theme.blue
}))

API

cxs(...styles)

Accepts styles objects or functions that return style objects and returns a className string.

cxs.css()

Returns the rendered CSS string for static and server-side rendering.

cxs.reset()

Resets the cache for server-side rendering

cxs/component

A styled-components-like API for creating React components with cxs.


Vendor prefixes

cxs does not handle vendor prefixing to keep the module size at a minimum.

Previous Versions

For previous versions of cxs, see the v3 branch or v4 branch

MIT License

cxs's People

Contributors

andykog avatar johno avatar jxnblk avatar lachlanjc avatar lukebrooker avatar marudor avatar mihalik avatar posgarou avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.