Module: Plushie::Type::StyleMap

Defined in:
lib/plushie/type/style_map.rb

Overview

Per-instance widget style overrides.

A StyleMap lets you start from a named preset (e.g. +:primary+, +:secondary+) and selectively override visual properties like background colour, text colour, border, and shadow. It also supports per-interaction-state overrides so you can customise how a widget looks when hovered, pressed, disabled, or focused.

On the wire it serialises to a plain map. Symbols become preset name strings; Spec structs emit only non-nil fields.

== Spec fields

  • +base+ [Symbol, nil] -- named preset to inherit from.
  • +background+ [String, nil] -- CSS-style colour string.
  • +text_color+ [String, nil] -- text foreground colour.
  • +border+ [Border, Hash, nil] -- border specification.
  • +shadow+ [Shadow, Hash, nil] -- shadow specification.
  • +hovered+ [Hash, nil] -- overrides applied on hover.
  • +pressed+ [Hash, nil] -- overrides applied while pressed.
  • +disabled+ [Hash, nil] -- overrides applied when disabled.
  • +focused+ [Hash, nil] -- overrides applied when focused.

Examples:

Use a preset name directly

button("save", "Save", style: :primary)

Extend a preset with overrides

button("save", "Save", style: { base: :secondary, background: "#ff0000" })

Build a full custom StyleMap::Spec

spec = StyleMap::Spec.new(
  background: "#333",
  text_color: "#fff",
  border: Border.from_opts(width: 1, color: "#555"),
  hovered: { background: "#444" }
)
button("save", "Save", style: spec)

Defined Under Namespace

Classes: Spec

Constant Summary collapse

FIELD_KEYS =

This constant is part of a private API. You should avoid using this constant if possible, as it may be removed or be changed in the future.

Recognized field keys for style map specs.

%i[base background text_color border shadow hovered pressed disabled focused].freeze

Class Method Summary collapse

Class Method Details

.encode(value) ⇒ String, ...

Encode a style value for the wire protocol. Accepts a symbol (preset name), a Hash, or a Spec.

Parameters:

  • value (Symbol, Hash, Spec, String, nil)

Returns:

  • (String, Hash, nil)


104
105
106
107
108
109
110
111
112
113
# File 'lib/plushie/type/style_map.rb', line 104

def encode(value)
  case value
  when Symbol then value.to_s
  when String then value
  when Spec then value.to_wire
  when Hash then value
  when nil then nil
  else raise ArgumentError, "invalid style: #{value.inspect}"
  end
end

.from_opts(opts) ⇒ Spec

Construct from keyword options.

Parameters:

  • opts (Hash)

Returns:



95
96
97
# File 'lib/plushie/type/style_map.rb', line 95

def from_opts(opts)
  Spec.new(**opts.slice(*FIELD_KEYS))
end