PetalComponents.Helpers (petal_components v4.15.1)

Copy Markdown View Source

For any helper functions used across multiple components. Ideally we keep this empty - components should be copy-pastable.

Summary

Functions

Composes two Phoenix.LiveView.JS structs by concatenating their operations. User JS operations execute first, followed by component JS operations.

Translates a changeset error into a displayable message.

Generates a unique HTML ID based on the given string or iodata.

Functions

compose_js(user_js, component_js)

Composes two Phoenix.LiveView.JS structs by concatenating their operations. User JS operations execute first, followed by component JS operations.

translate_error(msg)

Translates a changeset error into a displayable message.

Routes through config :petal_components, :error_translator_function when the host app has set one (the gettext hook every Phoenix app already wires up for CoreComponents.translate_error/1), and falls back to plain interpolation of the %{count}-style bindings otherwise.

This lives here rather than in a component module because more than one component renders field errors, and PetalComponents.Helpers is deliberately not in the use PetalComponents import list - a public translate_error/1 in a blanket-imported module would collide with the one nearly every app defines in its own web module.

uniq_id(input, prefix \\ "c")

Generates a unique HTML ID based on the given string or iodata.

Parameters

  • input: The input string or iodata (e.g., heading or label)
  • prefix: An optional prefix for the ID (default: "c")

Examples

iex> PetalComponents.Helpers.uniq_id("My Heading")
"c-my-heading-1234"

iex> PetalComponents.Helpers.uniq_id(["My ", {:safe, "<a>Link</a>"}, " Label"], "custom")
"custom-my-link-label-5678"