Type-safe, composable class variants for Gleam
Gleam 100.0%
6 1 0

Clone this repository

https://tangled.org/keii.dev/gva
git@tangled.org:keii.dev/gva

For self-hosted knots, clone URLs may differ based on your setup.

README.md

gva#

Type-safe, composable class variants for Gleam

Package Version Hex Docs

Usage#

gleam add gva@1
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
}

Further documentation can be found at https://hexdocs.pm/gva.

Development#

gleam run   # Run the project
gleam test  # Run the tests