reflex_behaviors 0.0.2 → 0.0.4

Sign up to get free protection for your applications and to get access to all the features.
@@ -3,3 +3,6 @@ TurboReady.initialize(Turbo.StreamActions)
3
3
 
4
4
  import 'turbo_reflex'
5
5
  import './elements'
6
+ import devtools from './devtools'
7
+
8
+ self.ReflexBehaviors = { devtools }
@@ -9,6 +9,14 @@ module ReflexBehaviors
9
9
  def initialize(view_context)
10
10
  @view_context = view_context
11
11
  end
12
+
13
+ def view_stack
14
+ prefix = "app/views/"
15
+ locations = caller_locations.select { |location| location.path.include?(prefix) }
16
+ locations.each_with_object(Set.new) do |location, memo|
17
+ memo << location.path[(location.path.index(prefix) + prefix.length)..]
18
+ end
19
+ end
12
20
  end
13
21
  end
14
22
  end
@@ -5,22 +5,39 @@ require_relative "base_tag_builder"
5
5
  module ReflexBehaviors::TagBuilders
6
6
  class ToggleTagsBuilder < BaseTagBuilder
7
7
  def target_tag(id, expanded: false, **kwargs, &block)
8
- return unless expanded || target_expanded?(id)
9
-
10
8
  kwargs = kwargs.with_indifferent_access
11
9
  kwargs[:id] = id
12
10
  kwargs[:role] = "region"
11
+
13
12
  kwargs[:aria] ||= {}
14
- kwargs[:aria] = target_aria(**kwargs[:aria])
13
+ kwargs[:aria][:label] ||= "Dynamic Content Region"
14
+ kwargs[:aria][:live] ||= "polite"
15
15
 
16
- content_tag("toggle-target", nil, kwargs, &block)
16
+ kwargs[:data] ||= {}
17
+ kwargs[:data][:view_stack] = view_stack.to_json if Rails.env.development?
18
+
19
+ if expanded || target_expanded?(id)
20
+ content_tag("toggle-target", nil, kwargs, &block)
21
+ else
22
+ content_tag("toggle-target", nil, kwargs)
23
+ end
17
24
  end
18
25
 
19
- def trigger_tag(target:, render:, disabled: false, aria: {}, data: {remember: false, auto_collapse: false}, **kwargs, &block)
26
+ def trigger_tag(target:, render:, action: :toggle, disabled: false, **kwargs, &block)
20
27
  kwargs = kwargs.with_indifferent_access
21
28
  kwargs[:id] ||= "#{target}-toggle-trigger"
22
- kwargs[:aria] = trigger_aria(controls: target, expanded: target_expanded?(target), **aria)
23
- kwargs[:data] = trigger_data(render: render, disabled: disabled, **data)
29
+
30
+ kwargs[:aria] ||= {}
31
+ kwargs[:aria][:atomic] ||= true
32
+ kwargs[:aria][:relevant] ||= "all"
33
+ kwargs[:aria].merge!(controls: target, expanded: target_expanded?(target))
34
+
35
+ kwargs[:data] ||= {}
36
+ kwargs[:data][:auto_collapse] = !!kwargs[:data][:auto_collapse]
37
+ kwargs[:data][:view_stack] = view_stack.to_json if Rails.env.development?
38
+ kwargs[:data][:remember] = !!kwargs[:data][:remember]
39
+ kwargs[:data][:render] = render
40
+ kwargs[:data][:turbo_reflex] = "ReflexBehaviors::ToggleReflex##{action}" unless disabled
24
41
 
25
42
  content_tag("toggle-trigger", nil, kwargs, &block)
26
43
  end
@@ -32,31 +49,5 @@ module ReflexBehaviors::TagBuilders
32
49
  def target_collapsed?(target)
33
50
  !target_expanded?(target)
34
51
  end
35
-
36
- private
37
-
38
- def trigger_aria(controls:, expanded:, **kwargs)
39
- kwargs = kwargs.with_indifferent_access
40
- kwargs[:atomic] ||= true
41
- kwargs[:relevant] ||= "all"
42
- kwargs.merge(controls: controls, expanded: !!expanded)
43
- end
44
-
45
- def trigger_data(render:, disabled: false, remember: false, auto_collapse: false, **kwargs)
46
- kwargs = kwargs.with_indifferent_access
47
- kwargs.merge(
48
- turbo_reflex: disabled ? nil : "ReflexBehaviors::ToggleReflex#toggle",
49
- render: render,
50
- remember: !!remember,
51
- auto_collapse: !!auto_collapse
52
- )
53
- end
54
-
55
- def target_aria(**kwargs)
56
- kwargs = kwargs.with_indifferent_access
57
- kwargs[:label] ||= "Dynamic Content Region"
58
- kwargs[:live] ||= "polite"
59
- kwargs
60
- end
61
52
  end
62
53
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ReflexBehaviors
4
- VERSION = "0.0.2"
4
+ VERSION = "0.0.4"
5
5
  end
data/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "reflex_behaviors",
3
- "version": "0.0.1",
3
+ "version": "0.0.3",
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",