turbo_boost-elements 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,34 +1,4 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class TurboBoost::Elements::ApplicationCommand < TurboBoost::Commands::Command
4
- protected
5
-
6
- def render_payload
7
- return {} if element.renders.blank?
8
- @render_payload ||= {partial: idomatic_partial_path(element.renders)}.tap do |payload|
9
- if element.assigns.present?
10
- payload[:assigns] = JSON.parse(element.assigns)
11
- payload[:assigns].each { |key, value| payload[:assigns][key] = hydrate_value(value) }
12
- end
13
- if element.locals.present?
14
- payload[:locals] = JSON.parse(element.locals)
15
- payload[:locals].each { |key, value| payload[:locals][key] = hydrate_value(value) }
16
- end
17
- end.deep_symbolize_keys
18
- end
19
-
20
- private
21
-
22
- def hydrate_value(value)
23
- hydrated = begin
24
- GlobalID::Locator.locate_signed(value)
25
- rescue
26
- value
27
- end
28
- hydrated.blank? ? nil : hydrated
29
- end
30
-
31
- def idomatic_partial_path(partial_path)
32
- partial_path.to_s.gsub("/_", "/").split(".").first
33
- end
34
4
  end
@@ -4,21 +4,51 @@ class TurboBoost::Elements::ToggleCommand < TurboBoost::Elements::ApplicationCom
4
4
  prevent_controller_action
5
5
 
6
6
  def show
7
- if element.remember == "true"
7
+ validate_element!
8
+
9
+ if element.remember?
8
10
  state[element.aria.controls] = true
9
11
  else
10
12
  state.now[element.aria.controls] = true
11
13
  end
12
14
 
13
- morph "##{element.morphs}", render(render_payload)
15
+ morph id: element.morphs, html: render(element.render_options)
14
16
  end
15
17
 
16
18
  def hide
19
+ validate_element!
17
20
  state[element.aria.controls] = false
18
- morph "##{element.morphs}", render(render_payload)
21
+ morph id: element.morphs, html: render(element.render_options)
19
22
  end
20
23
 
21
24
  def toggle
25
+ validate_element!
22
26
  element.aria.expanded? ? hide : show
23
27
  end
28
+
29
+ private
30
+
31
+ def validate_element!
32
+ validate_element_attributes! && validate_element_aria_attributes!
33
+ end
34
+
35
+ def validate_element_attributes!
36
+ case element
37
+ in {renders: _, morphs: _} then return true
38
+ in {renders: _} then raise TurboBoost::Commands::InvalidElementError, "The trigger element is missing the `morphs` attribute!"
39
+ in {morphs: _} then raise TurboBoost::Commands::InvalidElementError, "The trigger element is missing the `renders` attribute!"
40
+ else raise TurboBoost::Commands::InvalidCommandError, "The trigger element is missing the `renders` and `moprhs` attributes!"
41
+ end
42
+ false
43
+ end
44
+
45
+ def validate_element_aria_attributes!
46
+ case element.aria
47
+ in {controls: _, expanded: _} then return true
48
+ in {controls: _} then raise TurboBoost::Commands::InvalidElementError, "The trigger element is missing the `aria-expanded` attribute!"
49
+ in {expanded: _} then raise TurboBoost::Commands::InvalidElementError, "The trigger element is missing the `aria-controls` attribute!"
50
+ else raise TurboBoost::Commands::InvalidElementError, "The trigger element is missing the `aria-controls` and `aria-expanded` attributes!"
51
+ end
52
+ false
53
+ end
24
54
  end
@@ -3,17 +3,10 @@
3
3
  require_relative "../../../../lib/turbo_boost/elements/tag_builders"
4
4
 
5
5
  module TurboBoost::Elements::ApplicationHelper
6
+ # Returns an idiomatic path for the currently rendering template
7
+ # i.e. How you'd pass the path to a `render partial: ...` call
6
8
  def current_partial_path
7
- path = nil
8
- prefix = "app/views/"
9
- start = 1
10
- while path.nil? && start < 100
11
- location = caller_locations(start, 1).first
12
- path = location.path if location.path.include?(prefix)
13
- start += 1
14
- end
15
- return "unknown" if path.nil?
16
- path[(path.index(prefix) + prefix.length), path.rindex("/")]
9
+ @virtual_path.to_s.gsub("/_", "/")
17
10
  end
18
11
 
19
12
  def method_missing(name, ...)
@@ -274,7 +274,6 @@ export default class ToggleDevtool {
274
274
 
275
275
  tooltip.drag = new PlainDraggable(tooltip)
276
276
  tooltip.drag.onMove = () => {
277
- console.log('nate', tooltip.line)
278
277
  tooltip.line.position()
279
278
  if (tooltip.lineToTarget) tooltip.lineToTarget.position()
280
279
  if (tooltip.lineToRendering) tooltip.lineToRendering.position()
data/bin/standardize CHANGED
@@ -2,4 +2,4 @@
2
2
 
3
3
  bundle exec magic_frozen_string_literal
4
4
  bundle exec standardrb --fix
5
- yarn run prettier-standard "app/javascript/**/*.js"
5
+ yarn run prettier-standard package.json app/javascript/**/*.js
@@ -12,8 +12,9 @@ module TurboBoost::Elements
12
12
  class Engine < ::Rails::Engine
13
13
  config.turbo_boost_elements = ActiveSupport::OrderedOptions.new
14
14
 
15
- ActiveSupport.on_load(:action_controller) do
16
- try :helper, TurboBoost::Elements::ApplicationHelper
15
+ ActiveSupport.on_load(:action_controller_base) do
16
+ # `self` is ActionController::Base
17
+ helper TurboBoost::Elements::ApplicationHelper
17
18
  end
18
19
  end
19
20
  end
@@ -15,19 +15,4 @@ class TurboBoost::Elements::TagBuilders::BaseTagBuilder
15
15
  memo << location.path[(location.path.index(prefix) + prefix.length)..]
16
16
  end
17
17
  end
18
-
19
- protected
20
-
21
- def dehydrate_value(value)
22
- return value.to_s unless value.respond_to?(:to_sgid_param)
23
- value.try(:persisted?) ? value.to_sgid_param : nil
24
- end
25
-
26
- def dehydrate_hash(hash)
27
- hash
28
- .with_indifferent_access
29
- .each_with_object({}.with_indifferent_access) do |(key, val), memo|
30
- memo[key] = dehydrate_value(val)
31
- end
32
- end
33
18
  end
@@ -7,8 +7,6 @@ class TurboBoost::Elements::TagBuilders::ToggleTagsBuilder < TurboBoost::Element
7
7
  renders:, # REQUIRED, the partial path to render
8
8
  morphs:, # REQUIRED, `dom_id` of the partial's outermost containing element
9
9
  controls:, # REQUIRED, `dom_id` of the toggle target
10
- assigns: {}, # `assigns` required to render the partial (i.e. instance variables)
11
- locals: {}, # `local_assigns` required to render the parital
12
10
  collapse_selector: nil, # CSS selector for other matching targets to collapse when the target is expanded
13
11
  focus_selector: nil, # CSS selector for the element to focus when the target is expanded
14
12
  method: :toggle, # method to inovke (:show, :hide, :toggle)
@@ -24,9 +22,9 @@ class TurboBoost::Elements::TagBuilders::ToggleTagsBuilder < TurboBoost::Element
24
22
  kwargs[:data] ||= {}
25
23
  kwargs[:data][:turbo_command] = "TurboBoost::Elements::ToggleCommand##{method}" unless disabled
26
24
 
27
- # target / aria
25
+ # aria
28
26
  kwargs[:aria] ||= {}
29
- kwargs[:aria][:controls] = controls
27
+ kwargs[:aria][:controls] = controls # toggle target
30
28
  kwargs[:aria][:expanded] = target_expanded?(controls)
31
29
  kwargs[:aria][:atomic] ||= true
32
30
  kwargs[:aria][:relevant] ||= "all"
@@ -34,8 +32,6 @@ class TurboBoost::Elements::TagBuilders::ToggleTagsBuilder < TurboBoost::Element
34
32
  # rendering
35
33
  kwargs[:renders] = renders
36
34
  kwargs[:morphs] = morphs
37
- kwargs[:assigns] = dehydrate_hash(assigns).compact.to_json if assigns.present?
38
- kwargs[:locals] = dehydrate_hash(locals).compact.to_json if locals.present?
39
35
  kwargs[:view_stack] = view_stack.to_json if Rails.env.development?
40
36
 
41
37
  # misc
@@ -44,7 +40,9 @@ class TurboBoost::Elements::TagBuilders::ToggleTagsBuilder < TurboBoost::Element
44
40
  kwargs[:remember] = !!remember
45
41
 
46
42
  args = kwargs.select { |_, value| value.present? }
47
- content_tag("turbo-boost-toggle-trigger", nil, args.transform_keys(&:dasherize), &block)
43
+ args = args.transform_keys(&:dasherize)
44
+
45
+ content_tag("turbo-boost-toggle-trigger", nil, args, &block)
48
46
  end
49
47
 
50
48
  def target_tag(
@@ -2,6 +2,6 @@
2
2
 
3
3
  module TurboBoost
4
4
  module Elements
5
- VERSION = "0.0.1"
5
+ VERSION = "0.0.2"
6
6
  end
7
7
  end
data/package.json CHANGED
@@ -1,13 +1,25 @@
1
1
  {
2
2
  "name": "@turbo-boost/elements",
3
- "version": "0.0.0",
3
+ "version": "0.0.1",
4
4
  "description": "Pre-built easy to use reactive TurboBoost elements for Rails/Hotwire apps.",
5
- "main": "app/javascript/index.js",
5
+ "keywords": [
6
+ "hotwire",
7
+ "hotwired",
8
+ "rails",
9
+ "turbo",
10
+ "turbo-boost",
11
+ "web-components"
12
+ ],
13
+ "type": "module",
14
+ "main": "app/assets/builds/@turbo-boost/elements.js",
15
+ "files": [
16
+ "app/assets/builds"
17
+ ],
6
18
  "repository": "https://github.com/hopsoft/turbo_boost-elements",
7
19
  "author": "Nate Hopkins (hopsoft) <natehop@gmail.com>",
8
20
  "license": "MIT",
9
21
  "dependencies": {
10
- "@turbo-boost/commands": ">= 0.0.1"
22
+ "@turbo-boost/commands": ">= 0.0.4"
11
23
  },
12
24
  "peerDependencies": {
13
25
  "@hotwired/turbo-rails": ">= 7.2"
@@ -18,6 +30,6 @@
18
30
  "prettier-standard": "^16.4.1"
19
31
  },
20
32
  "scripts": {
21
- "build": "esbuild app/javascript/index.js --bundle --minify --sourcemap --format=esm --outfile=app/assets/builds/@turbo-boost/elements.js"
33
+ "build": "esbuild app/javascript/index.js --bundle --minify --sourcemap --format=esm --target=es2020,chrome58,firefox57,safari11 --analyze --outfile=app/assets/builds/@turbo-boost/elements.js"
22
34
  }
23
35
  }