Class: Shadcn::ThemeSelector::Component

Inherits:
ApplicationViewComponent show all
Defined in:
app/components/shadcn/theme_selector/component.rb

Overview

Port of apps/v4/components/theme-selector.tsx — the select that swaps the colour palette.

In React this reads useThemeConfig(); here each option hands its value to the shadcn--theme Stimulus controller, which swaps the theme-* class the way shadcn's ActiveThemeProvider does.

Constant Summary

Constants inherited from ApplicationViewComponent

ApplicationViewComponent::CONTENTS_STYLE, ApplicationViewComponent::VOID_TAGS

Instance Attribute Summary collapse

Attributes inherited from ApplicationViewComponent

#attributes

Instance Method Summary collapse

Methods inherited from ApplicationViewComponent

#css_classes, default_tag, #merged_style, #render_element, #shadcn_t, slot_name, #style_variants, #tag_name

Constructor Details

#initialize(themes: ShadcnViewComponent::Themes::BASE_COLORS, value: ShadcnViewComponent::Themes::DEFAULT, label: nil, **attributes) ⇒ Component

Returns a new instance of Component.

Parameters:

  • themes (Array<Hash>) (defaults to: ShadcnViewComponent::Themes::BASE_COLORS)

    entries from ShadcnViewComponent::Themes

  • value (String) (defaults to: ShadcnViewComponent::Themes::DEFAULT)

    the palette currently applied



22
23
24
25
26
27
28
29
30
# File 'app/components/shadcn/theme_selector/component.rb', line 22

def initialize(themes: ShadcnViewComponent::Themes::BASE_COLORS,
               value: ShadcnViewComponent::Themes::DEFAULT,
               label: nil,
               **attributes)
  @themes = themes
  @value = value.to_s
  @label = label
  super(**attributes)
end

Instance Attribute Details

#themesObject (readonly)

Returns the value of attribute themes.



18
19
20
# File 'app/components/shadcn/theme_selector/component.rb', line 18

def themes
  @themes
end

#valueObject (readonly)

Returns the value of attribute value.



18
19
20
# File 'app/components/shadcn/theme_selector/component.rb', line 18

def value
  @value
end

Instance Method Details

#callObject



44
45
46
# File 'app/components/shadcn/theme_selector/component.rb', line 44

def call
  render_element(body: safe_join([ hidden_label, select ]))
end

#element_attributes(**defaults) ⇒ Object



40
41
42
# File 'app/components/shadcn/theme_selector/component.rb', line 40

def element_attributes(**defaults)
  super(**{ "data-controller" => "shadcn--theme" }.merge(defaults))
end

#labelObject



32
33
34
# File 'app/components/shadcn/theme_selector/component.rb', line 32

def label
  @label || shadcn_t("theme.label")
end

#label_idObject



36
37
38
# File 'app/components/shadcn/theme_selector/component.rb', line 36

def label_id
  @label_id ||= "shadcn-theme-selector-#{SecureRandom.hex(4)}"
end