MithrilUI.Helpers (Mithril UI v0.1.2)
View SourceUtility functions for Mithril UI components.
Includes error translation, class handling, and other shared utilities.
Summary
Functions
Merges CSS classes, filtering out nil and empty values.
Extracts the errors list from a form field.
Checks if a form field has any errors.
Normalizes a class value that may be a string, list, or nil.
Translates form field errors using the configured translator.
Generates a unique ID for DOM elements.
Functions
Merges CSS classes, filtering out nil and empty values.
Supports multiple input formats for flexible class composition:
- Strings:
"btn btn-primary" - Lists:
["btn", "btn-primary"] - Conditional tuples:
{"btn-disabled", false}(included only if true) - Nested lists:
["btn", ["btn-primary", "btn-lg"]] - Mixed:
["btn", {"btn-disabled", @disabled}, @extra_class]
Examples
iex> MithrilUI.Helpers.class_names(["btn", "btn-primary", nil, ""])
"btn btn-primary"
iex> MithrilUI.Helpers.class_names(["btn", {"btn-primary", true}, {"btn-disabled", false}])
"btn btn-primary"
iex> MithrilUI.Helpers.class_names(["base", ["nested", "classes"]])
"base nested classes"Usage in Components
All MithrilUI components accept the :class attribute as either a string or list,
enabling flexible conditional class composition:
# Simple string
<.button class="my-custom-class">Click</.button>
# List with conditionals
<.button class={[
"w-12 h-12 p-0 rounded-lg",
if(@selected, do: "ring-2 ring-white", else: "opacity-60")
]}>
Click
</.button>
# Using conditional tuples
<.card class={[
"custom-card",
{"shadow-xl", @elevated},
{"border-primary", @highlighted}
]}>
Content
</.card>
@spec field_errors(Phoenix.HTML.FormField.t() | nil) :: [String.t()]
Extracts the errors list from a form field.
Returns an empty list if the field has no errors or if the form has not been validated yet.
Examples
iex> MithrilUI.Helpers.field_errors(%Phoenix.HTML.FormField{errors: [{"is required", []}]})
["is required"]
@spec has_errors?(Phoenix.HTML.FormField.t() | nil) :: boolean()
Checks if a form field has any errors.
Examples
iex> MithrilUI.Helpers.has_errors?(%Phoenix.HTML.FormField{errors: [{"is required", []}]})
true
iex> MithrilUI.Helpers.has_errors?(%Phoenix.HTML.FormField{errors: []})
false
Normalizes a class value that may be a string, list, or nil.
This is useful when you need to programmatically work with class values that could be in different formats.
Examples
iex> MithrilUI.Helpers.normalize_class_attr(nil)
nil
iex> MithrilUI.Helpers.normalize_class_attr("btn btn-primary")
"btn btn-primary"
iex> MithrilUI.Helpers.normalize_class_attr(["btn", "btn-primary"])
["btn", "btn-primary"]
iex> MithrilUI.Helpers.normalize_class_attr(["btn", nil, {"active", true}])
["btn", nil, {"active", true}]
Translates form field errors using the configured translator.
By default, uses simple string interpolation. Applications can configure a custom translator function:
config :mithril_ui,
error_translator: {MyAppWeb.CoreComponents, :translate_error}Examples
iex> MithrilUI.Helpers.translate_error({"is required", []})
"is required"
iex> MithrilUI.Helpers.translate_error({"must be at least %{count} characters", [count: 8]})
"must be at least 8 characters"
Generates a unique ID for DOM elements.
Examples
iex> MithrilUI.Helpers.unique_id("modal")
"modal-abc123def"