This package is inspired by styled-components
and classnames
.
Installation
To download react-classnamed-components
run:
npm i react-classnamed-components
API
; // basicconst Box = `class-1 class-2` // with element shortcutconst Box = ediv`class-1 class-2` // with classNames conditionconst Box = e // with default attributes/propsconst Input = einput`class-1 class-2` // with default attributes + classNames conditionsconst Input = einput "class-2 class-2], {"class-error": props.hasError}])
Demo (CodeSandbox)
Visit these CodeSandbox examples, where you can play with the package.
Getting Started
To understand the potential of this package, let's import inside our CSS the Tailwind CDN
;
Simple Button
Let's create a simple button component, same as the example from Tailwind's website https://tailwindcss.com/components/buttons/#simple
const Button = ebutton`bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded"`
Click Me!
Button with Props
We can use the component props to change the classNames of our component.
;const Button = e
Click Me!
Button With Color Variants
This is a more complex component, where we use props to change the component Colors and States.
const ButtonVariants = default: border: "border-gray-600" text: "text-gray-600" bg: "bg-gray-600" bgHover: "hover:bg-gray-700" red: border: "border-red-600" text: "text-red-600" bg: "bg-red-600" bgHover: "hover:bg-red-700" blue: border: "border-blue-500" text: "text-blue-500" bg: "bg-blue-500" bgHover: "hover:bg-blue-700" const Button = e
Click Me!Click Me!Click Me!Click Me!
ToDo
- Write tests
- Typescript support
- Forward ref
- Examples folder of common use cases
- Refactor Code
- Own implemention of classnames package
More Examples
- Tailwind CSS
- Styled-components
- Emotion
- CSS modules