Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(button,package): add class-variance-authority lib, convert button component to use it #4

Merged
merged 3 commits into from
Nov 23, 2023

Conversation

JoltCode
Copy link
Collaborator

@JoltCode JoltCode commented Nov 21, 2023

Summary of CVA

The goal of CVA is to manage styling for different variants of a component more easily (e.g. colors, sizes, and styles), with better consistency.
This removes the need to manage dynamic classes manually.

Variants are defined in the component as classes, and a default variant is also set.
Tailwind-merge can resolve conflicts in classes defined by CVA.

Example

const buttonClass = computed(() => {
  return cva(
    "h-10 px-4", // default classes which are *always* applied
    {
      variants: {
        intent: {
          text: 'text-gray-400' // applied if intent === 'text'
        },
        disabled: {
          true: 'cursor-not-allowed' // applied if disabled === true
        }
      },
      compoundVariants: [ // classes apply if all conditions are true
        {intent: 'primary', disabled: false, class: 'bg-blue-700 text-white hover:bg-blue-600'},
        {intent: 'secondary', disabled: false, class: 'bg-gray-100 text-black hover:bg-gray-50'},
        {intent: ['primary', 'secondary'], disabled: true, class: 'bg-gray-100 text-gray-400'}
      ]
    }
  )({
    intent: props.intent, // pass variant values. eg: "primary", "secondary", "text"
    disabled: props.disabled // pass variant values. eg: false, true
  });
});

@github-actions github-actions bot added the documentation Improvements or additions to documentation label Nov 21, 2023
@spwoodcock
Copy link
Member

Didn't know about it before, but class variance authority is cool!

Copy link
Member

@spwoodcock spwoodcock left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CVA looks pretty awesome.
Especially useful if you are creating components from scratch.

Looking into RadixUI more, it doesn't seem very configurable.
They allow the size to change for most components & a few other things, but not much!
The button component doesn't even have the ability to be disabled! (unless it's inherited somewhere and I missed it?).

The question we need to decide on is:

  • Should we use a headless component library to manage some of the logic for component props (like RadixUI)?
  • Or, should we define the components from scratch?

Interested to hear your thoughts too @varun2948

@spwoodcock spwoodcock merged commit 5e2a30c into main Nov 23, 2023
1 of 2 checks passed
@spwoodcock spwoodcock deleted the cva-button branch November 23, 2023 07:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dependency documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants