Class: Daisy::Actions::ThemeControllerComponent

Inherits:
LocoMotion::BaseComponent show all
Defined in:
app/components/daisy/actions/theme_controller_component.rb

Constant Summary collapse

SOME_THEMES =

Default list of themes to display in the controller

%w[light dark synthwave retro cyberpunk wireframe].freeze

Constants inherited from LocoMotion::BaseComponent

LocoMotion::BaseComponent::EMPTY_PART_IGNORED_TAGS, LocoMotion::BaseComponent::SELF_CLOSING_TAGS

Instance Attribute Summary collapse

Attributes inherited from LocoMotion::BaseComponent

#config, #loco_parent

Instance Method Summary collapse

Methods inherited from LocoMotion::BaseComponent

build, #component_ref, #config_option, #cssify, define_modifier, define_modifiers, define_part, define_parts, define_size, define_sizes, #empty_part_content, #inspect, #part, register_component_initializer, register_component_setup, #rendered_css, #rendered_data, #rendered_html, #rendered_stimulus_controllers, #rendered_tag_name, renders_many, renders_one, set_component_name, #set_loco_parent, #strip_spaces

Methods included from LocoMotion::Concerns::InspectableComponent

#build_inspect_string

Constructor Details

#initialize(**kws, &block) ⇒ ThemeControllerComponent

Creates a new instance of the ThemeControllerComponent.

Parameters:

  • kws (Hash)

    The keyword arguments for the component.

Options Hash (**kws):

  • :themes (Array<String>)

    List of DaisyUI theme names to include in the controller. Defaults to SOME_THEMES.



30
31
32
33
34
# File 'app/components/daisy/actions/theme_controller_component.rb', line 30

def initialize(**kws, &block)
  super

  @themes = config_option(:themes, SOME_THEMES)
end

Instance Attribute Details

#themesObject (readonly)

Returns the value of attribute themes.



20
21
22
# File 'app/components/daisy/actions/theme_controller_component.rb', line 20

def themes
  @themes
end

Instance Method Details

#before_renderObject

Sets up the component with theme Stimulus controller.



39
40
41
# File 'app/components/daisy/actions/theme_controller_component.rb', line 39

def before_render
  add_stimulus_controller(:component, "loco-theme")
end

#build_radio_input(theme, **options) ⇒ Daisy::DataInput::RadioButtonComponent

Builder method to create a radio input for use in selecting themes.

Parameters:

  • theme (String)

    The name of the theme that the input controls.

  • options (Hash)

    Additional options to pass to the component.

Returns:



59
60
61
62
63
64
65
66
67
68
# File 'app/components/daisy/actions/theme_controller_component.rb', line 59

def build_radio_input(theme, **options)
  options[:css] = "#{options[:css]} theme-controller".lstrip

  # Namespace the id by the input name so multiple theme controllers can
  # coexist on the same page without generating duplicate ids.
  name = options[:name] || "theme"
  default_options = { name: name, id: "#{name}-#{theme}", value: theme }

  render Daisy::DataInput::RadioButtonComponent.new(**default_options.deep_merge(options))
end

#build_theme_preview(theme, **options) ⇒ Daisy::Actions::ThemePreviewComponent

Builder method to create a theme preview showing the theme’s colors in a 2x2 grid.

Parameters:

  • theme (String)

    The theme name to preview.

  • options (Hash)

    a customizable set of options

Options Hash (**options):

  • :size (Integer)

    Size of the preview in Tailwind size units. Defaults to 4 (1rem).

  • :shadow (Boolean)

    Whether to add a shadow. Defaults to true.

  • :css (String)

    Additional CSS classes.

Returns:



86
87
88
89
90
91
# File 'app/components/daisy/actions/theme_controller_component.rb', line 86

def build_theme_preview(theme, **options)
  render Daisy::Actions::ThemePreviewComponent.new(
    theme: theme,
    **options
  )
end

#callObject

Renders the component and its content.



46
47
48
# File 'app/components/daisy/actions/theme_controller_component.rb', line 46

def call
  part(:component) { content }
end