cxs
fast af css-in-js in 0.7kb
const className =
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.
const Box = { return <div ...props className=className /> } const className =
Pseudoclasses
const className =
Media Queries
const className =
Child Selectors
const className =
Static/Server-Side Rendering
For Node.js environments, use the css()
method to return the static CSS string after rendering a view.
const html = ReactDOMServerconst css = cxs const doc = `<!DOCTYPE html><style></style>` // Reset the cache for the next rendercxs
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.
const Heading = margin: 0 fontSize: '32px' lineHeight: 125
Extending components
To extend a cxs component, pass it to the component creator function.
const Button = color: 'white' backgroundColor: 'blue' const TomatoButton = backgroundColor: 'tomato'
Other components
Any component can be passed to cxs to add styles.
const MyLink = 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 = color: propscolor
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.
const Heading = fontSize: propsbig ? '48px' : '32px' HeadingpropTypes = big: PropTypesbool
styled-system
Style utility functions, like those in styled-system, can be used with cxs/component.
const Heading = 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 = <ThemeProvider => <Heading> Hello </Heading> </ThemeProvider>
import cxs from 'cxs/component' const Heading = fontSize: propsthemefontSizes4 + 'px' color: propsthemeblue
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