reflex_behaviors 0.0.9 → 0.0.10

Sign up to get free protection for your applications and to get access to all the features.
@@ -4,18 +4,18 @@ class ReflexBehaviors::ToggleReflex < ReflexBehaviors::ApplicationReflex
4
4
  prevent_controller_action
5
5
 
6
6
  def show
7
- if element.dataset.remember?
7
+ if element.remember == "true"
8
8
  state[element.aria.controls] = true
9
9
  else
10
10
  state.now[element.aria.controls] = true
11
11
  end
12
12
 
13
- morph "##{render_payload[:id]}", render(render_payload.except(:id))
13
+ morph "##{element.morphs}", render(render_payload)
14
14
  end
15
15
 
16
16
  def hide
17
17
  state[element.aria.controls] = false
18
- morph "##{render_payload[:id]}", render(render_payload.except(:id))
18
+ morph "##{element.morphs}", render(render_payload)
19
19
  end
20
20
 
21
21
  def toggle
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "turbo_reflex"
4
4
  require_relative "version"
5
+ require_relative "../../app/helpers/reflex_behaviors/application_helper"
5
6
 
6
7
  module ReflexBehaviors
7
8
  def self.config
@@ -17,6 +17,21 @@ module ReflexBehaviors
17
17
  memo << location.path[(location.path.index(prefix) + prefix.length)..]
18
18
  end
19
19
  end
20
+
21
+ protected
22
+
23
+ def dehydrate_value(value)
24
+ return value.to_s unless value.respond_to?(:to_sgid_param)
25
+ value.try(:persisted?) ? value.to_sgid_param : nil
26
+ end
27
+
28
+ def dehydrate_hash(hash)
29
+ hash
30
+ .with_indifferent_access
31
+ .each_with_object({}.with_indifferent_access) do |(key, val), memo|
32
+ memo[key] = dehydrate_value(val)
33
+ end
34
+ end
20
35
  end
21
36
  end
22
37
  end
@@ -4,50 +4,87 @@ require_relative "base_tag_builder"
4
4
 
5
5
  module ReflexBehaviors::TagBuilders
6
6
  class ToggleTagsBuilder < BaseTagBuilder
7
- def trigger_tag(target:, render:, action: :toggle, disabled: false, **kwargs, &block)
7
+ def trigger_tag(
8
+ renders:, # REQUIRED - the partial path to render
9
+ morphs:, # REQUIRED - dom_id of the partial's outermost containing element
10
+ controls:, # REQUIRED - dom_id of the toggle target
11
+ assigns: {}, # assigns required to render the partial i.e. instance variables
12
+ locals: {}, # local_assigns required to render the parital
13
+ collapse_selector: nil, # CSS selector for other targets to collapse when the target is expanded
14
+ focus_selector: nil, # CSS selector for the element to focus when the target is expanded
15
+ method: :toggle, # reflex method to inovke (show, hide, toggle)
16
+ disabled: false, # disable the trigger
17
+ remember: false, # remember state between requests
18
+ **kwargs,
19
+ &block
20
+ )
8
21
  kwargs = kwargs.with_indifferent_access
9
- kwargs[:id] ||= "#{target}-toggle-trigger"
22
+ kwargs[:id] ||= "#{controls}-toggle-trigger"
10
23
 
24
+ # reflex
25
+ kwargs[:data] ||= {}
26
+ kwargs[:data][:turbo_reflex] = "ReflexBehaviors::ToggleReflex##{method}" unless disabled
27
+
28
+ # target / aria
11
29
  kwargs[:aria] ||= {}
30
+ kwargs[:aria][:controls] = controls
31
+ kwargs[:aria][:expanded] = target_expanded?(controls)
12
32
  kwargs[:aria][:atomic] ||= true
13
33
  kwargs[:aria][:relevant] ||= "all"
14
- kwargs[:aria].merge!(controls: target, expanded: target_expanded?(target))
15
34
 
16
- kwargs[:data] ||= {}
17
- kwargs[:data][:auto_collapse] = !!kwargs[:data][:auto_collapse]
18
- kwargs[:data][:view_stack] = view_stack.to_json if Rails.env.development?
19
- kwargs[:data][:remember] = !!kwargs[:data][:remember]
20
- kwargs[:data][:render] = render
21
- kwargs[:data][:turbo_reflex] = "ReflexBehaviors::ToggleReflex##{action}" unless disabled
35
+ # rendering
36
+ kwargs[:renders] = renders
37
+ kwargs[:morphs] = morphs
38
+ kwargs[:assigns] = dehydrate_hash(assigns).compact.to_json if assigns.present?
39
+ kwargs[:locals] = dehydrate_hash(locals).compact.to_json if locals.present?
40
+ kwargs[:view_stack] = view_stack.to_json if Rails.env.development?
22
41
 
23
- content_tag("toggle-trigger", nil, kwargs, &block)
42
+ # misc
43
+ kwargs[:collapse_selector] = collapse_selector
44
+ kwargs[:focus_selector] = focus_selector
45
+ kwargs[:remember] = !!remember
46
+
47
+ args = kwargs.select { |_, value| value.present? }
48
+ content_tag("toggle-trigger", nil, args.transform_keys(&:dasherize), &block)
24
49
  end
25
50
 
26
- def target_tag(id, expanded: false, **kwargs, &block)
51
+ def target_tag(
52
+ id, # REQUIRED - the dom_id for the element
53
+ collapse_on: [], # list of events that trigger collapse
54
+ collapse_selector: nil, # CSS selector for other targets to collapse when this target is expanded
55
+ expanded: false, # override to force expansion
56
+ focus_selector: nil, # CSS selector for the element to focus when content is expanded
57
+ **kwargs,
58
+ &block
59
+ )
27
60
  kwargs = kwargs.with_indifferent_access
28
61
  kwargs[:id] = id
62
+ kwargs[:collapse_on] = collapse_on.to_json if collapse_on.present?
63
+ kwargs[:collapse_selector] = collapse_selector
64
+ kwargs[:focus_selector] = focus_selector
29
65
  kwargs[:role] = "region"
30
66
 
67
+ # aria
31
68
  kwargs[:aria] ||= {}
32
- kwargs[:aria][:label] ||= "Dynamic Content Region"
33
69
  kwargs[:aria][:live] ||= "polite"
34
70
 
35
- kwargs[:data] ||= {}
36
- kwargs[:data][:view_stack] = view_stack.to_json if Rails.env.development?
71
+ # rendering
72
+ kwargs[:view_stack] = view_stack.to_json if Rails.env.development?
37
73
 
74
+ args = kwargs.select { |_, value| value.present? }
38
75
  if expanded || target_expanded?(id)
39
- content_tag("toggle-target", nil, kwargs, &block)
76
+ content_tag("toggle-target", nil, args.transform_keys!(&:dasherize), &block)
40
77
  else
41
- content_tag("toggle-target", nil, kwargs)
78
+ content_tag("toggle-target", nil, args.transform_keys!(&:dasherize))
42
79
  end
43
80
  end
44
81
 
45
- def target_expanded?(target)
46
- !!turbo_reflex.state[target]
82
+ def target_expanded?(dom_id)
83
+ !!turbo_reflex.state[dom_id]
47
84
  end
48
85
 
49
- def target_collapsed?(target)
50
- !target_expanded?(target)
86
+ def target_collapsed?(dom_id)
87
+ !target_expanded?(dom_id)
51
88
  end
52
89
  end
53
90
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReflexBehaviors
4
- VERSION = "0.0.9"
4
+ VERSION = "0.0.10"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reflex_behaviors",
3
- "version": "0.0.8",
3
+ "version": "0.0.9",
4
4
  "description": "Pre-built easy to use reactive TurboReflex behaviors for Rails/Hotwire apps.",
5
5
  "main": "app/javascript/index.js",
6
6
  "repository": "https://github.com/hopsoft/reflex_behaviors",