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"), 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
# File 'app/components/nitro_kit/appearance_picker.rb', line 13

def initialize(
  id:,
  label: I18n.t("nitro_kit.appearance_picker.label"),
  presentation: :segmented,
  preference: :system,
  html: {},
  aria: {},
  data: {},
  desperately_need_a_class: nil
)
  @identifier = component_id(id)
  @label = validate_label!(label)
  @presentation = validate_choice!(:presentation, presentation, PRESENTATIONS)
  @preference = validate_choice!(:preference, preference, PREFERENCES)

  super(
    component: :appearance_picker,
    attributes: {
      id: @identifier,
      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.



46
47
48
# File 'app/components/nitro_kit/appearance_picker.rb', line 46

def identifier
  @identifier
end

#preferenceObject (readonly)

Returns the value of attribute preference.



46
47
48
# File 'app/components/nitro_kit/appearance_picker.rb', line 46

def preference
  @preference
end

Instance Method Details

#view_templateObject



48
49
50
51
52
53
54
55
56
57
58
# File 'app/components/nitro_kit/appearance_picker.rb', line 48

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) }
    div(**slot_attributes(:options)) do
      PREFERENCES.each { |preference| render_option(preference) }
    end
  end
end