snowhite v1.1.2 Snowhite.Helpers.Html View Source

Link to this section Summary

Functions

Helpers that creates a class using by a given set of predicates.

Link to this section Types

Specs

binary_condition() :: {function(), class(), class()}

Specs

class() :: String.t()

Specs

class_type() :: condition() | function() | class()

Specs

condition() :: {function(), class()}

Link to this section Functions

Link to this function

classes(conditions, args)

View Source

Specs

classes([class_type()], list()) :: String.t()

Helpers that creates a class using by a given set of predicates.

It supports the following format:

  • {function, true_class, false_class}: if the function is true, the true_class is prepended, otherwise the false_class.
  • {function, class}: if the function is true, the class is prepended.
  • function: returns the result of the given function
  • class: always present

Examples

iex> conditions = [
  {fn user -> user.age > 18 end, "is-adult"},
  {fn user -> user.state == true end, "is-active", "is-inactive"},
  fn user -> if user.gender == :female, do: "is-female", else: "is-male" end,
  "user"
]
iex> classes(conditions, [%{age: 20, active: true, gender: :female}])
"user is-female is-active is-adult"
iex> classes(conditions, [%{age: 10, active: true, gender: :male}])
"user is-male is-active"
iex> classes(conditions, [%{age: 10, active: false, gender: :female}])
"user is-female is_inactive"