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

cxs-components

v3.0.3

Published

Styled UI component primitives for React - built with cxs

Downloads

1

Readme

ϟ cxs-components

Styled UI component primitives for React - built with cxs

Build Status

npm install cxs-components

Features

  • ~6 KB
  • Simple API to quickly create UI components
  • Performant, functional CSS-in-JS
  • Uses native JavaScript objects
  • Supports pseudoclasses, media queries, and keyframes

cxs-components' API is inspired by styled-components but instead of tagged template literals it uses plain JavaScript objects, which allow for the use of native JavaScript types without the need to escape embedded CSS.

For reasons similar to the motivations behind JSX, this is preferred over embedded CSS syntax.

Template literals work well for long embedded DSLs. Unfortunately the syntax noise is substantial when you exit in and out of embedded arbitrary ECMAScript expressions with identifiers in scope. – Facebook JSX

cxs-components helps enforce separation of style and business logic by promoting pure functional UI components – i.e. presentational or "dumb" components.

Usage

import React from 'react'
import createComponent from 'cxs-components'

const Box = createComponent('div')({
  padding: 32
})

const TomatoBox = createComponent(Box)({
  backgroundColor: 'tomato'
})

const App = () => (
  <div>
    <Box>
      Hello
    </Box>
    <TomatoBox>
      Tomato
    </TomatoBox>
  </div>
)

API

import createComponent from 'cxs-components'
// create component function

createComponent(tagName)(style, options)
// Returns a functional React component
  • tagName can be either a DOM element string or a React component
  • style can be a style object or a function that returns a style object
  • options is an optional object to pass options to the component creation function

Functional styles

A function can be passed as the style argument to use props to control styling.

const Box = createComponent('div')(props => ({
  padding: props.big ? 64 : 32
}))

const App = () => (
  <Box big>Big Box</Box>
)

By default, all props are pass on to child elements. To ensure non-standard DOM attributes are not passed on, specify a removeProps array in the options object.

const Box = createComponent('div')(props => ({
  padding: props.big ? 64 : 32
}), {
  // Clean up after yourself 🛁
  removeProps: [
    'big'
  ]
})

Pseudoclasses

cxs supports pseudoclasses.

const Link = createComponent('a')({
  color: blue,
  ':hover': {
    color: darkblue
  }
})

Media queries

const Heading = createComponent('h2')({
  fontSize: 32,
  margin: 0,
  '@media screen and (min-width: 52em)': {
    fontSize: 48
  }
})

Server-Side Rendering

To render cxs components server-side extract the CSS string after rendering the application tree.

const { createElement } = require('react')
const { renderToString } = require('react-dom/server')
const { cxs } = require('cxs-components')
const App = require('./App')

const render = (req, res) => {
  const html = renderToString(
    createElement(App)
  )
  const css = cxs.getCss()

  return (`<!DOCTYPE html>
    <style>${css}</style>
    ${html}`)
}

Static Style Components

For even better performance, the optional static module can be imported instead of the default module. The static create component function works the same as the defualt, but does NOT support functions as an argument for styles.

import createComponent from 'cxs-components/static'

// This works the same
const Box = createComponent('div')({
  padding: 32
})

// This WILL NOT work with the static create component function
const Card = createComponent('div')(props => ({
  color: props.color
}))

Related

MIT License