Skip to content

Helper to apply classNames to a dynamic element? #6

@mgcrea

Description

@mgcrea

Would be nice to have a className-like helper when you want to dynamically apply classes according to a variant config.

In my case, I'm playing with a basic <Button />component that would have an icon prop that could receive an ReactElement:

      <Button size="xs" icon={<XMarkIcon />}>
        Default
      </Button>

In this case I'd like to apply some size-based variants to the icon as well, something like:

const iconClassNames = variantClassNames({
  variants: {
    size: {
      xs: "w-4 h-4 -ml-0.5 mr-1",
      sm: "w-4 h-4 -ml-0.5 mr-2",
      md: "w-5 h-5 -ml-1 mr-2",
      lg: "w-6 h-6 -ml-1 mr-3",
      xl: "w-6 h-6 -ml-1 mr-3",
    }
  },
  defaultVariants: {
    size: "md",
  },
});

That could be used this way:

<button>
  {icon ? cloneElement(icon, {className: iconClassNames(props)}) : null}
  {children}
</button>

Do you think this could be useful or would you do it differently (eg. nested styles, etc.)?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions