gva

Types

pub type Class(a) {
  Class(
    default: String,
    resolver: fn(a) -> String,
    defaults: List(a),
    using: List(a),
  )
}

Constructors

  • Class(
      default: String,
      resolver: fn(a) -> String,
      defaults: List(a),
      using: List(a),
    )

Values

pub fn build(class: Class(a)) -> String

Build a gva class to a string

pub fn class(class: Class(a), class_string: String) -> Class(a)
pub fn gva(
  default default: String,
  resolver resolver: fn(a) -> String,
  defaults defaults: List(a),
) -> Class(a)

Helper to create a gva Class type

The default parameter is the string that will be used regardless of any other parameters

The resolver handles which types should go to which classes

Examples

type Size {
  Small
  Medium
}

type Variant {
  Primary
  Secondary
}

// Define a master Key type for all Variants
type Key {
  Size(Size)
  Variant(Variant)
}

pub fn main() {
  let button =
    gva(
      default: "text-white",
      // Define a resolver to handle which types should go
      // to which classes
      resolver: fn(in: Key) {
        case in {
          Size(size) ->
            case size {
              Small -> "text-sm"
              Medium -> "text-md"
            }
          Variant(variant) ->
            case variant {
              Primary -> "bg-slate-950"
              Secondary -> "bg-slate-600"
            }
        }
      },
      // Define which defaults should be applied, if no defaults are
      // provided and no with() calls are done then the resulting class
      // will only include the default string which might not be favorable
      defaults: [Variant(Primary)],
    )

  let class =
    button
    |> with(Size(Medium))
    |> with(Variant(Secondary))
    |> build()

  assert class == "text-white text-md bg-slate-600"

  Nil
} 
pub fn with(class: Class(a), key: a) -> Class(a)
pub fn with_all(class: Class(a), keys: List(a)) -> Class(a)
Search Document