Class: Plushie::Widget::Button

Inherits:
Object
  • Object
show all
Defined in:
lib/plushie/widget/button.rb

Overview

Typed builder for the button widget (Layer 2 API).

Construct a Button, set properties via fluent +set_+ methods, then call #build to produce a Node for the view tree. Each +set_+ method returns a shallow copy, so the original is never mutated -- safe for reuse across renders.

PROPS holds the list of supported property keys: label, width, height, padding, clip, style, disabled, a11y.

Examples:

Basic usage

Button.new("save", "Save").set_style(:primary).build

With padding and disabled state

Button.new("cancel", "Cancel", padding: 8, disabled: true).build

Constant Summary collapse

PROPS =

Supported property keys for the button widget.

i[label width height padding clip style disabled a11y].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id, label = nil, **opts) ⇒ Button

Returns a new instance of Button.

Parameters:

  • id (String)

    widget identifier (first arg by convention)

  • label (String, nil) (defaults to: nil)

    button label text

  • opts (Hash)

    optional properties matching PROPS keys



31
32
33
34
35
36
# File 'lib/plushie/widget/button.rb', line 31

def initialize(id, label = nil, **opts)
  @id = id.to_s
  @label = label
  PROPS.each { |k| instance_variable_set(:"@#{k}", opts[k]) if opts.key?(k) }
  @label ||= opts[:label]
end

Instance Attribute Details

#a11yObject (readonly)

Returns the value of attribute a11y.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def a11y
  @a11y
end

#clipObject (readonly)

Returns the value of attribute clip.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def clip
  @clip
end

#disabledObject (readonly)

Returns the value of attribute disabled.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def disabled
  @disabled
end

#heightObject (readonly)

Returns the value of attribute height.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def height
  @height
end

#idObject (readonly)

Returns the value of attribute id.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def id
  @id
end

#labelObject (readonly)

Returns the value of attribute label.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def label
  @label
end

#paddingObject (readonly)

Returns the value of attribute padding.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def padding
  @padding
end

#styleObject (readonly)

Returns the value of attribute style.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def style
  @style
end

#widthObject (readonly)

Returns the value of attribute width.



1
2
3
# File 'lib/plushie/widget/button.rb', line 1

def width
  @width
end

Instance Method Details

#buildPlushie::Node

Build a Node from the current property values.

Returns:



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/plushie/widget/button.rb', line 47

def build
  props = {}
  Build.put_if(props, :label, @label)
  Build.put_if(props, :width, @width)
  Build.put_if(props, :height, @height)
  Build.put_if(props, :padding, @padding)
  Build.put_if(props, :clip, @clip)
  Build.put_if(props, :style, @style)
  Build.put_if(props, :disabled, @disabled)
  Build.put_if(props, :a11y, @a11y)
  Node.new(id: @id, type: "button", props: props)
end