Coder Social home page Coder Social logo

alanrsoares / styled-cva Goto Github PK

View Code? Open in Web Editor NEW
2.0 2.0 0.0 275 KB

A typesafe, class-variance-authority-based, styled-components-like library for authoring React components

License: Apache License 2.0

TypeScript 100.00%
class-variance-authority design-systems react styled-components tailwindcss

styled-cva's Introduction

code style: prettier License Types

styled-cva

A typesafe, class-variance-authority-based, styled-components-like library for authoring React components

Installation

npm

npm i --save styled-cva

pnpm

pnpm add styled-cva

bun

bun add styled-cva

Usage

basic

import tw from "styled-cva";

const StaticButton = tw.button`
  bg-primary rounded-xl cursor-pointer
`;

// ...
<StaticButton>Click Me</StaticButton>;

variants

import tw from "styled-cva";

const VariantButton = tw.button.cva("btn-base-class", {
  variants: {
    // smart disappearing props:
    // variant keys starting with $ will not be sent to the DOM,
    // this avoids extraneous props warning
    $variant: {
      primary: "btn-primary-class",
      secondary: "btn-secondary-class",
    },
  },
});

// ...

// $variant is infered to 'primary' | 'secondary'
<VariantButton $variant="primary">Click Me</VariantButton>;

// actual element in the dom will be:
// <button class="btn-primary-class">Click Me</button>

proxy

import Link from "next/link";

const Button = tw.button.cva("btn", {
  variants: {
    $variant: {
      primary: "btn-primary",
      secondary: "btn-secondary",
    },
  },
});

// ...

// works with known jsx elements

<Button $as="a" href="/some/url">
  I'm a link that looks like a button
</Button>;

// also with custom components

<Button $as={Link} href="/some/url">
  I'm a link that looks like a button
</Button>;

VSCode intellisense

For tailwindcss extension support, add this to your vscode settings.json

  // tailwindcss intelisense settings
  "tailwindCSS.emmetCompletions": true,
  "tailwindCSS.includeLanguages": {
    "typescript": "javascript", // if you are using typescript
    "typescriptreact": "javascript" // if you are using typescript with react
  },
  "tailwindCSS.experimental.classRegex": [
    "tw`([^`]*)", // tw`...`
    "tw\\.[^`]+`([^`]*)`", // tw.xxx<xxx>`...`
    "tw\\(.*?\\).*?`([^`]*)", // tw(Component)<xxx>`...`
    ["cva\\(([^)]*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"]
  ],
  "editor.quickSuggestions": {
    "strings": true // forces VS Code to trigger completions when editing "string" content
  },

Acknowledgements

License - Apache-2.0

styled-cva's People

Contributors

alanrsoares avatar

Stargazers

 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.