Skip to content

Phoenix Component attrs take a list of examples and an exhaustive list of values

January 16, 2024

An attr can list the values it accepts in two ways (among other options):

  • :examples: a non-exhaustive list of values the attribute accepts. It’s only used for the documentation.
  • :values: an exhaustive list of values the attribute accepts. If a caller passes a literal that isn’t in the list, you get a compile warning.
attr :attr1, :atom, values: [:foo, :bar, :baz]
attr :attr2, :atom, examples: [:foo, :bar, :baz]
attr :attr3, :list, values: [[60, 40]]
attr :attr4, :list, examples: [[60, 40]]

def fun_attr_values_examples(assigns), do: ~H[]

The example is lifted from LiveView’s own test suite.

—

Back to TIL

Next: Browsers have now implemented native smooth scrolling

Phil-Bastian Berndt

Phil-Bastian Berndt
Tech Lead at Naymspace