View Source Pyro.Overrides (Pyro v0.0.2)

The overrides system provides out-of-the-box presets while also enabling deep customization of Pyro components.

The Pyro.Overrides.Default preset is a great example to dig in and see how the override system works. A Pyro.Component flags attrs with overridable, then leverages assign_overridables/1 to reference overrides set in these presets/custom override modules and load them as defaults.

Pyro defaults to the following overrides:

[Pyro.Overrides.Default]

But you probably want to customize at least a few overrides. To do so, configure your app with:

config :pyro, :overrides,
  [MyApp.CustomOverrides, Pyro.Overrides.Default]

Then, define your overrides in your custom module:

defmodule MyApp.CustomOverrides do
  @moduledoc false
  use Pyro.Overrides

  override Core, :back do
    set :class, "text-lg font-black"
    set :icon_kind, :outline
    set :icon_name, :arrow_left
  end
end

The overrides will be merged left-to-right, returning the value in the first module that sets a given key. So in the above example, the <Core.back> component will have an icon_name default of :arrow_left, since the MyApp.CustomOverrides was the first module in the list to provide that key. But the icon_class was unspecified in the custom module, so it will return the value from Pyro.Overrides.Default since it is provided there:

  • You only need to define what you want to override from the other defaults
  • You can use any number of :overrides modules, though it is probably best to only use only 1-3 to keep things simple/efficient
  • If no modules define the value, it will simply be nil
  • If assign_overridables/1 is called on the component with the required: true attr option, an error will be raised if no configured overrides define a default

Link to this section Summary

Macros

Define overrides for a specific component.

Override a setting within a component.

Functions

Get the configured or default override modules.

Get an override value for a given component prop.

Link to this section Macros

Link to this macro

override(module, component, list)

View Source (macro)
@spec override(module :: module(), component :: atom(), [{:do, Macro.t()}]) ::
  Macro.t()

Define overrides for a specific component.

You need to specify the module and function name of the component to override.

examples

Examples

override Core, :back do
  set :class, "text-lg font-black"
end
Link to this macro

set(selector, value)

View Source (macro)
@spec set(atom(), any()) :: Macro.t()

Override a setting within a component.

Value can be:

  • A literal value matching the type of the prop being overridden
  • A function capture of arity 1 with the argument passed_assigns, which is executed at runtime and is passed the component's assigns
  • Any other function capture, which will simply be passed along as a literal

The passed_assigns function capture allows for complex conditionals. For examples of this, please view the source of Pyro.Overrides.Default.

Tip:

Be sure to include the module in the function capture, since this is a macro and will lose the reference otherwise.

examples

Examples

set :class, "text-lg font-black"
set :class, &__MODULE__.back_class/1
# ...
def back_class(passed_assigns) do

Link to this section Functions

@spec configured_overrides() :: [module()]

Get the configured or default override modules.

Link to this function

override_for(module, component, prop)

View Source
@spec override_for(module(), atom(), atom()) :: any()

Get an override value for a given component prop.