Module: Plushie::Type::A11y

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

Overview

Accessibility properties for widgets.

Any tree node can carry an a11y hash in its props to control accessibility behaviour. All fields are optional.

Examples:

button("save", "Save", a11y: { label: "Save document", description: "Saves to disk" })

Full struct

A11y.from_opts(role: :button, label: "Submit", description: "Send the form")

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 accessibility specs.

%i[
  role label description hidden expanded required level live
  busy invalid modal read_only mnemonic toggled selected value
  orientation labelled_by described_by error_message disabled
  position_in_set size_of_set has_popup active_descendant radio_group
].freeze
VALID_ROLES =

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.

Valid accessibility roles.

%i[
  alert alertdialog application article banner button cell
  checkbox columnheader combobox complementary contentinfo
  definition dialog directory document feed figure form grid
  gridcell group heading img link list listbox listitem log
  main marquee math menu menubar menuitem menuitemcheckbox
  menuitemradio navigation none note option presentation
  progressbar radio radiogroup region row rowgroup rowheader
  scrollbar search searchbox separator slider spinbutton
  status switch tab tablist tabpanel term textbox timer toolbar
  tooltip tree treegrid treeitem
].freeze

Class Method Summary collapse

Class Method Details

.cast(value) ⇒ Hash?

Normalise any a11y input to a wire-ready hash.

Parameters:

  • value (Spec, Hash, nil)

Returns:

  • (Hash, nil)


71
72
73
74
75
76
77
78
79
80
81
# File 'lib/plushie/type/a11y.rb', line 71

def cast(value)
  case value
  when Spec then value.to_wire
  when Hash
    mnemonic = value[:mnemonic] || value["mnemonic"]
    validate_mnemonic!(mnemonic) if mnemonic
    value.compact
  when nil then nil
  else raise ArgumentError, "invalid a11y: #{value.inspect}"
  end
end

.encode(value) ⇒ Hash?

Encode for the wire protocol.

Parameters:

  • value (Spec, Hash, nil)

Returns:

  • (Hash, nil)


86
87
88
# File 'lib/plushie/type/a11y.rb', line 86

def encode(value)
  cast(value)
end

.from_opts(opts) ⇒ Spec

Construct from keyword options.

Parameters:

  • opts (Hash)

    any combination of A11y fields

Returns:



63
64
65
66
# File 'lib/plushie/type/a11y.rb', line 63

def from_opts(opts)
  validate_mnemonic!(opts[:mnemonic]) if opts[:mnemonic]
  Spec.new(**opts.slice(*FIELD_KEYS))
end

.validate_mnemonic!(char) ⇒ Object

Validate that a mnemonic is a single character.

Parameters:

  • char (String)

Raises:

  • (ArgumentError)

    if not a single character



93
94
95
96
97
98
99
# File 'lib/plushie/type/a11y.rb', line 93

def validate_mnemonic!(char)
  return unless char.is_a?(String)
  return unless char.length != 1

  raise ArgumentError,
    "mnemonic must be a single character, got: #{char.inspect}"
end