Class: NitroKit::AppearancePicker

Inherits:
Component
  • Object
show all
Defined in:
app/components/nitro_kit/appearance_picker.rb

Constant Summary collapse

PRESENTATIONS =
%i[segmented radios select dropdown].freeze
PREFERENCES =
%i[light dark system].freeze
ICONS =
{
  light: :sun,
  dark: :moon,
  system: :monitor
}.freeze

Constants inherited from Component

Component::ADDITIVE_DATA_ATTRIBUTES, Component::COMPONENT_OWNED_DATA_ATTRIBUTES, Component::FORBIDDEN_ATTRIBUTES, Component::RESERVED_DATA_ATTRIBUTES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(id:, label: I18n.t("nitro_kit.appearance_picker.label"), label_visible: true, presentation: :segmented, preference: :system, html: {}, aria: {}, data: {}, desperately_need_a_class: nil) ⇒ AppearancePicker

Returns a new instance of AppearancePicker.



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'app/components/nitro_kit/appearance_picker.rb', line 13

def initialize(
  id:,
  label: I18n.t("nitro_kit.appearance_picker.label"),
  label_visible: true,
  presentation: :segmented,
  preference: :system,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @identifier = validate_id!("AppearancePicker id", id)
  @label = validate_label!(label)
  @label_visible = validate_boolean!(:label_visible, label_visible)
  @presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
  @preference = validate_choice!(:preference, preference, PREFERENCES)
  if !@label_visible && @presentation != :segmented
    raise ArgumentError,
      "AppearancePicker label_visible: false requires the segmented presentation"
  end

  super(
    component: :appearance_picker,
    attributes: {
      id: @identifier,
      # Browsers give legends special layout, so a hidden label omits the
      # element entirely and the fieldset keeps its name through ARIA.
      aria: @label_visible ? {} : { label: @label },
      data: {
        controller: "nk--appearance",
        presentation: @presentation,
        state: @preference,
        action: "change->nk--appearance#select nitro-kit:appearance-change@window->nk--appearance#synchronize"
      }
    },
    html:,
    aria:,
    data:,
    desperately_need_a_class:
  )
end

Instance Attribute Details

#identifierObject (readonly)

Returns the value of attribute identifier.



55
56
57
# File 'app/components/nitro_kit/appearance_picker.rb', line 55

def identifier
  @identifier
end

#preferenceObject (readonly)

Returns the value of attribute preference.



55
56
57
# File 'app/components/nitro_kit/appearance_picker.rb', line 55

def preference
  @preference
end

Instance Method Details

#view_templateObject



57
58
59
60
61
62
63
64
65
66
67
# File 'app/components/nitro_kit/appearance_picker.rb', line 57

def view_template
  return render_select if @presentation == :select
  return render_dropdown if @presentation == :dropdown

  fieldset(**root_attributes) do
    legend(**slot_attributes(:legend)) { plain(@label) } if @label_visible
    div(**slot_attributes(:options)) do
      PREFERENCES.each { |preference| render_option(preference) }
    end
  end
end