Module: Vident::RootComponent::Base

Included in:
UsingPhlexHTML, UsingViewComponent
Defined in:
lib/vident/root_component/base.rb

Instance Method Summary collapse

Instance Method Details

#action(*args) ⇒ Object

TODO: rename Create a Stimulus action string, and returns it

examples:
action(:my_thing) => "current_controller#myThing"
action(:click, :my_thing) => "click->current_controller#myThing"
action("click->current_controller#myThing") => "click->current_controller#myThing"
action("path/to/current", :my_thing) => "path--to--current_controller#myThing"
action(:click, "path/to/current", :my_thing) => "click->path--to--current_controller#myThing"


37
38
39
40
# File 'lib/vident/root_component/base.rb', line 37

def action(*args)
  part1, part2, part3 = args
  (args.size == 1) ? parse_action_arg(part1) : parse_multiple_action_args(part1, part2, part3)
end

#action_data_attribute(*actions) ⇒ Object



42
43
44
# File 'lib/vident/root_component/base.rb', line 42

def action_data_attribute(*actions)
  {action: parse_actions(actions).join(" ")}
end

#as_targets(*targets) ⇒ Object Also known as: as_target

Return the HTML `data-target` attribute



77
78
79
80
81
82
# File 'lib/vident/root_component/base.rb', line 77

def as_targets(*targets)
  build_target_data_attributes(parse_targets(targets))
    .map { |dt, n| "data-#{dt}=\"#{n}\"" }
    .join(" ")
    .html_safe
end

#initialize(controllers: nil, actions: nil, targets: nil, named_classes: nil, data_maps: nil, element_tag: nil, id: nil, html_options: nil) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/vident/root_component/base.rb', line 6

def initialize(
  controllers: nil,
  actions: nil,
  targets: nil,
  named_classes: nil, # https://stimulus.hotwired.dev/reference/css-classes
  data_maps: nil,
  element_tag: nil,
  id: nil,
  html_options: nil
)
  @element_tag = element_tag
  @html_options = html_options
  @id = id
  @controllers = Array.wrap(controllers)
  @actions = actions
  @targets = targets
  @named_classes = named_classes
  @data_map_kvs = {}
  @data_maps = data_maps
end

#named_classes(*names) ⇒ Object

Getter for a named classes list so can be used in view to set initial state on SSR Returns a String of classes that can be used in a `class` attribute.



65
66
67
# File 'lib/vident/root_component/base.rb', line 65

def named_classes(*names)
  names.map { |name| convert_classes_list_to_string(@named_classes[name]) }.join(" ")
end

#target(name, part2 = nil) ⇒ Object

TODO: rename & make stimulus Target class instance and returns it, which can convert to String Create a Stimulus Target and returns it

examples:
target(:my_target) => {controller: 'current_controller' name: 'myTarget'}
target("path/to/current", :my_target) => {controller: 'path--to--current_controller', name: 'myTarget'}


51
52
53
54
55
56
57
# File 'lib/vident/root_component/base.rb', line 51

def target(name, part2 = nil)
  if part2.nil?
    {controller: implied_controller_name, name: js_name(name)}
  else
    {controller: stimulize_path(name), name: js_name(part2)}
  end
end

#target_data_attribute(name) ⇒ Object



59
60
61
# File 'lib/vident/root_component/base.rb', line 59

def target_data_attribute(name)
  build_target_data_attributes([target(name)])
end

#with_actions(*actions) ⇒ Object Also known as: with_action

Return the HTML `data-action` attribute to add these actions



86
87
88
# File 'lib/vident/root_component/base.rb', line 86

def with_actions(*actions)
  "data-action='#{parse_actions(actions).join(" ")}'".html_safe
end

#with_controllersObject

Return the HTML `data-controller` attribute



72
73
74
# File 'lib/vident/root_component/base.rb', line 72

def with_controllers
  "data-controller='#{controller_list}'".html_safe
end