rails_accordion 1.0.0 → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,23 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class AccordionComponent < ViewComponent::Base
4
-
5
4
  def initialize(**args)
6
- super
7
5
  @args = args.presence || {}
8
- set_data_params
9
6
  end
10
7
 
11
8
  def call
12
- content_tag(:div, content, **@args)
9
+ content_tag(:div, content, data: data_options, **@args)
13
10
  end
14
11
 
15
12
  private
16
13
 
17
- def set_data_params
18
- @args[:data] ||= {}
19
- @args[:data][:controller] = [@args[:data][:controller], "accordion"].compact.join(" ")
20
- @args[:data][:multiple_open] = @args.delete(:multiple_open) || RailsAccordion.configuration.multiple_open
21
- @args[:data][:default_state] = @args.delete(:default_state) || RailsAccordion.configuration.default_state
14
+ def data_options
15
+ data = @args.delete(:data) || {}
16
+ {
17
+ controller: [data.delete(:controller), "accordion"].compact.join(" "),
18
+ accordion_multiple_open_value: @args.delete(:multiple_open) || RailsAccordion.configuration.multiple_open,
19
+ accordion_default_state_value: @args.delete(:default_state) || RailsAccordion.configuration.default_state,
20
+ **data
21
+ }
22
22
  end
23
23
  end
@@ -5,13 +5,11 @@ class ItemComponent < ViewComponent::Base
5
5
  renders_one :header
6
6
 
7
7
  def initialize(**args)
8
- super
9
8
  @args = args.presence || {}
10
- set_classes
11
9
  end
12
10
 
13
11
  def call
14
- content_tag :div, **@args do
12
+ content_tag :div, class: [@args.delete(:class), "accordion_item"].compact.join(" "), data: { accordion_target: "item" }, **@args do
15
13
  header_component + body_component
16
14
  end
17
15
  end
@@ -19,7 +17,7 @@ class ItemComponent < ViewComponent::Base
19
17
  private
20
18
 
21
19
  def header_component
22
- content_tag :div, header, class: "accordion_toggle"
20
+ content_tag :div, header, class: "accordion_toggle", data: { action: "click->accordion#toggle" }
23
21
  end
24
22
 
25
23
  def body_component
@@ -32,10 +30,6 @@ class ItemComponent < ViewComponent::Base
32
30
  end
33
31
  end
34
32
 
35
- def set_classes
36
- @args[:class] = [@args[:class], "accordion_item"].compact.join(" ")
37
- end
38
-
39
33
  def content_styles
40
34
  "transition-duration: #{parse_duration}ms;"
41
35
  end
@@ -1,46 +1,39 @@
1
1
  import { Controller } from "@hotwired/stimulus";
2
2
 
3
3
  export default class extends Controller {
4
- connect() {
5
- this.initAccordion();
4
+ static targets = [ "item" ]
6
5
 
7
- this.initDefaultState()
6
+ static values = {
7
+ multipleOpen: Boolean,
8
+ defaultState: String
8
9
  }
9
10
 
10
- initDefaultState() {
11
- const items = this.element.querySelectorAll('.accordion_item');
12
-
13
- switch($(this.element).data('default-state')) {
11
+ connect() {
12
+ // inits default state for accordion
13
+ switch(this.defaultStateValue) {
14
14
  case 'all_closed':
15
- this.hideAll(items);
15
+ this.hideAll(this.itemTargets);
16
16
  break;
17
17
  case 'all_opened':
18
- this.showAll(items);
18
+ this.showAll(this.itemTargets);
19
19
  break;
20
20
  case 'first_opened':
21
- this.hideAll(items)
22
- this.open(items[0].querySelector('.accordion_content'))
21
+ this.hideAll(this.itemTargets)
22
+ this.open(this.itemTargets[0].querySelector('.accordion_content'))
23
23
  }
24
24
  }
25
25
 
26
- initAccordion() {
27
- const items = this.element.querySelectorAll('.accordion_item');
28
-
29
- items.forEach((item) => {
30
- const toggle = item.querySelector('.accordion_toggle');
31
- const content = item.querySelector('.accordion_content')
32
-
33
- toggle.addEventListener('click', (e) => {
34
- if (content.classList.contains('accordion_active')) {
35
- this.hide(content);
36
- } else {
37
- if ($(this.element).data('multiple-open') != true) {
38
- this.hideAll(items);
39
- }
40
- this.open(content);
41
- }
42
- });
43
- });
26
+ toggle(e) {
27
+ const content = e.currentTarget.parentNode.querySelector('.accordion_content')
28
+
29
+ if (content.classList.contains('accordion_active')) {
30
+ this.hide(content);
31
+ } else {
32
+ if (!this.multipleOpenValue) {
33
+ this.hideAll(this.itemTargets);
34
+ }
35
+ this.open(content);
36
+ }
44
37
  }
45
38
 
46
39
  hideAll(items) {
@@ -58,6 +51,12 @@ export default class extends Controller {
58
51
 
59
52
  open(item) {
60
53
  item.classList.add("accordion_active")
61
- item.style.height = item.scrollHeight + 'px'
54
+ item.style.height = item.scrollHeight + 'px';
55
+
56
+ item.querySelectorAll('.accordion_content').forEach(function(child) {
57
+ new ResizeObserver(function() {
58
+ item.style.height = item.scrollHeight + 'px';
59
+ }).observe(child);
60
+ });
62
61
  }
63
62
  }
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rails_accordion
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ahmadshoh Nasrullozoda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-06-07 00:00:00.000000000 Z
11
+ date: 2023-06-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: stimulus-rails