Giter Site home page Giter Site logo

responsive-classnames's Introduction

Responsive Class Names (aka rcx)

Use responsive props inspired by Styled System for components that using your Tailwind CSS class names.

Demo on Codesandbox

Install

Use your choice of NPM client to install the package:

$ yarn add responsive-classnames
# or
$ npm install responsive-classnames

Getting Started

To get started you will need supply an array of your breakpoint keys (e.g. sm, md, lg, etc.) to the createResponsiveClassNames function.

import {createResponsiveClassNames} from 'responsive-classnames';

const rcx = createResponsiveClassNames(tailwind.screens);

This creates your own responsive class names function for you. You can call it whatever you’d like, but something short and sweet like rcx will probably serve you best.

Usage

rcx is designed for the className prop. It is used as a tagged template. The string literal you use is the template for the class names it will create. The expression passed can be a numeric value, an array of numeric values, or an object with keys corresponding to your breakpoints.

import cx from 'classnames';

const Stack = ({as: AsElement = 'div', space, ...props}) => (
  <AsElement
    className={cx('flex flex-col', rcx`space-y-${space}`)} 
    {...props} 
  />
);

<Stack as="ul" space={[2, 4, 6]}>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</Stack>

<Stack space={{default: 2, md: 4}}>
  <Card />
  <Card />
  <Card />
</Stack>

Limitations

The following are some technical limitations of this package. I plan to address these in the future.

Only one expression allowed in template

Currently, the tagged template only takes a single expression (e.g. space-y-${space})

Purgecss

If your use Purgecss with Tailwind then you are probably quite aware that you are told to avoid string concatenation. This is problematic if you plan to use this package. Currently, I have a Babel plugin in development that will analyze your React project’s source code for string concatenations in className props, find values from usages of those components, then create a whitelist of compiled class names for Purgecss to use. My early findings are proving to be effective. Please create an issue if your are interested in taking a look at how this works so far.

Disclaimer

responsive-classnames is not meant to replace packages like classnames, but to be used in tandem with them.

Contributing

Contributions are welcome. Please make an issue first unless it’s something small.

responsive-classnames's People

Contributors

knowler avatar

Stargazers

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