TailwindVariants (tailwind_variants v0.1.0)

View Source

Provides a variant system for TailwindCSS in Elixir applications.

This library allows you to create components with variants, slots, and more, similar to the JavaScript tailwind-variants library.

Summary

Functions

Applies props to a component to generate class names.

Creates a tailwind-variants component with the given options.

Returns a map of all available variants and their possible values for a component. Useful for generating documentation or validation.

Functions

class_list(component_or_slot, props \\ %{})

Applies props to a component to generate class names.

Parameters

  • component - A component created with tv/1
  • props - A map of props to apply to the component

Returns

  • A string of class names (if no slots are defined)
  • A map of slot functions (if slots are defined)

Examples

iex> component = tv(%{base: "font-medium text-white"})
iex> class_list(component)
"font-medium text-white"

iex> component = tv(%{
...>   base: "font-medium",
...>   variants: %{
...>     color: %{
...>       primary: "bg-blue-500",
...>       secondary: "bg-purple-500"
...>     }
...>   }
...> })
iex> class_list(component, %{color: "primary"})
"font-medium bg-blue-500"

tv(options)

Creates a tailwind-variants component with the given options.

Options

  • :base - Base classes for the component
  • :slots - Map of slot names to their classes
  • :variants - Map of variant names to their values
  • :default_variants - Map of variant names to their default values
  • :compound_variants - List of compound variants
  • :compound_slots - List of compound slots
  • :extend - Another component to extend
  • :config - Configuration options

Examples

iex> component = tv(%{base: "font-medium text-white"})
iex> component.base
"font-medium text-white"

iex> component = tv(%{
...>   base: "font-medium text-white",
...>   variants: %{
...>     color: %{
...>       primary: "bg-blue-500",
...>       secondary: "bg-purple-500"
...>     }
...>   }
...> })
iex> component.variants.color.primary
"bg-blue-500"

tw(component_or_slot, props \\ %{})

See class_list/2.

variant_options(component)

Returns a map of all available variants and their possible values for a component. Useful for generating documentation or validation.

Example

iex> button = tv(%{variants: %{color: %{primary: "", secondary: ""}}})
iex> variant_options(button)
%{color: [:primary, :secondary]}