Mob.Style (mob v0.3.5)

Copy Markdown View Source

Thin wrapper around a props map for named, reusable styles.

The struct type lets the ~MOB sigil (and future tooling) distinguish a style value from a plain data map. At serialisation time Mob.Renderer merges a style's props into the node's own props — there is no runtime overhead.

Defining styles

# As a module attribute — compiled to a constant
@header  %Mob.Style{props: %{text_size: :xl, text_color: :white, background: :primary, padding: 16}}
@btn     %Mob.Style{props: %{text_size: :lg, text_color: :white, padding: 12}}
@btn_primary  Mob.Style.put(@btn, :background, :blue_700)
@btn_danger   Mob.Style.put(@btn, :background, :red_500)

Using styles in a node

%{
  type: :text,
  props: %{style: @header, text: "Title"},
  children: []
}

Inline props override style values, so:

%{
  type: :text,
  props: %{style: @header, text_size: :base},   # overrides :xl from @header
  children: []
}

Token values (:primary, :xl, :white, etc.) are resolved by Mob.Renderer before JSON serialisation — the native side always receives plain integers and floats.

Summary

Functions

Merge two styles; keys in b win over keys in a.

Return a copy of style with key set to value.

Types

t()

@type t() :: %Mob.Style{props: map()}

Functions

merge(style1, style2)

@spec merge(t(), t()) :: t()

Merge two styles; keys in b win over keys in a.

put(s, key, value)

@spec put(t(), atom(), term()) :: t()

Return a copy of style with key set to value.