View Source Pyro.Overrides (Pyro v0.3.7)

The overrides system allows defining component defaults while also enabling deep customization and ad-hoc overrides of Pyro component props. This allows consumers of your component library to trivially and granularly modify your presets.

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.

Configure your app with:

config :pyro, :overrides,
  [MyApp.Overrides]

Then, define your overrides in your custom module:

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

  override MyAppWeb.CoreComponents, :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.Overrides was the first module in the list to provide that key:

  • 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

Summary

Macros

Define overrides for a specific component.

Override a setting within a component.

Functions

Get the configured override modules.

Get an override value for a given component prop.

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

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.

Tip:

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

Examples

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

Functions

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

Get the configured 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.