rails_accordion 0.1.10.pre.beta → 0.1.11.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,11 @@
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
4
+
5
+ .accordion_content {
6
+ @apply h-0 overflow-hidden w-full transition-all duration-300
7
+ }
8
+
9
+ .accordion_toggle {
10
+ @apply cursor-pointer;
11
+ }
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ class AccordionComponent < ViewComponent::Base
4
+ def initialize(**args)
5
+ super
6
+ @args = args.presence || {}
7
+ set_controller
8
+ end
9
+
10
+ def call
11
+ content_tag(:div, content, **@args)
12
+ end
13
+
14
+ private
15
+
16
+ def set_controller
17
+ @args[:data] ||= {}
18
+ @args[:data][:controller] = [@args[:data][:controller], "accordion"].compact.join(" ")
19
+ end
20
+ end
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ class ItemComponent < ViewComponent::Base
4
+ renders_one :body
5
+ renders_one :header
6
+
7
+ def initialize(**args)
8
+ super
9
+ @args = args.presence || {}
10
+ set_classes
11
+ end
12
+
13
+ def call
14
+ content_tag :div, **@args do
15
+ header_component + body_component
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def header_component
22
+ content_tag :div, header, class: "accordion_toggle"
23
+ end
24
+
25
+ def body_component
26
+ if body?
27
+ content_tag :div, class: "accordion_content" do
28
+ content_tag :div, body, class: "accordion_content-container p-2"
29
+ end
30
+ else
31
+ content_tag(:div, nil, class: "hidden")
32
+ end
33
+ end
34
+
35
+ def set_classes
36
+ @args[:class] = [@args[:class], "accordion_item"].compact.join(" ")
37
+ end
38
+ end
@@ -0,0 +1,43 @@
1
+ import { Controller } from "@hotwired/stimulus";
2
+
3
+ export default class extends Controller {
4
+ connect() {
5
+ this.initAccordion();
6
+ }
7
+
8
+ initAccordion() {
9
+ const items = this.element.querySelectorAll('.accordion_item');
10
+
11
+ items.forEach((item) => {
12
+ const toggle = item.querySelector('.accordion_toggle');
13
+
14
+ toggle.addEventListener('click', (e) => {
15
+ if (item.classList.contains('active')) {
16
+ this.hide(item);
17
+ } else {
18
+ this.hideAll(items);
19
+ this.open(item);
20
+ }
21
+ });
22
+ });
23
+ }
24
+
25
+ hideAll(items) {
26
+ items.forEach((item) => this.hide(item));
27
+ }
28
+
29
+ hide(item) {
30
+ item.classList.remove("active")
31
+ const content = item.querySelector('.accordion_content');
32
+ content.style.height = '0px';
33
+ }
34
+
35
+ open(item) {
36
+ item.classList.add("active")
37
+ const content = item.querySelector('.accordion_content');
38
+ content.style.height = 'auto';
39
+ const contentHeight = content.style.height
40
+ // content.style.height = '0px';
41
+ content.animate({ height: contentHeight + 'px' }, 50);
42
+ }
43
+ }
@@ -0,0 +1,11 @@
1
+ import 'mapkick/bundle'
2
+ import AccordionController from './controllers/accordion_controller'
3
+ import { Application } from '@hotwired/stimulus'
4
+
5
+ const application = Application.start()
6
+
7
+ window.Stimulus = application
8
+
9
+ application.register('accordion', AccordionController)
10
+
11
+ export { application }
data/bin/console ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ require "bundler/setup"
5
+ require "rails_accordion"
6
+
7
+ # You can add fixtures and/or initialization code here to make experimenting
8
+ # with your gem easier. You can also use a different console, if you like.
9
+
10
+ # (If you use this, don't forget to add pry to your Gemfile!)
11
+ # require "pry"
12
+ # Pry.start
13
+
14
+ require "irb"
15
+ IRB.start(__FILE__)
data/bin/rails ADDED
@@ -0,0 +1,13 @@
1
+ #!/usr/bin/env ruby
2
+ # This command will automatically be run when you run "rails" with Rails gems
3
+ # installed from the root of your application.
4
+
5
+ ENGINE_ROOT = File.expand_path('..', __dir__)
6
+ # ENGINE_PATH = File.expand_path('../lib/rails_table/engine', __dir__)
7
+
8
+ # Set up gems listed in the Gemfile.
9
+ ENV['BUNDLE_GEMFILE'] ||= File.expand_path('../Gemfile', __dir__)
10
+ require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
11
+
12
+ require 'rails/all'
13
+ require 'rails/engine/commands'
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+ require "rails/generators"
3
+
4
+ module RailsAccordion
5
+ module Generators
6
+ class InstallGenerator < Rails::Generators::Base
7
+ source_root File.expand_path("templates", __dir__)
8
+
9
+ namespace "rails_accordion:install"
10
+ desc "Copies all necessary files"
11
+
12
+ def copy_build_files
13
+ # directory File.join(__dir__, "../", "../", "../", "app", "assets", "builds"), "app/assets/builds"
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,38 @@
1
+ import { Controller } from "@hotwired/stimulus"
2
+
3
+ export default class extends Controller {
4
+ connect() {
5
+ this.initAccordion();
6
+ }
7
+
8
+ initAccordion() {
9
+ const $toggles = $(this.element).find('.accordion_toggle');
10
+
11
+ const removeAllActiveClass = () => {
12
+ $toggles.each((el) => {
13
+ const $parent = $(el).parent();
14
+ $parent.removeClass('active');
15
+ $parent.find('.accordion_content').height(0);
16
+ });
17
+ };
18
+
19
+ $toggles.on('touch click', (e) => {
20
+ const $toggle = $(e.currentTarget);
21
+ const $parent = $toggle.parent();
22
+ const $content = $parent.find('.accordion_content');
23
+
24
+ if (!$parent.hasClass('active')) {
25
+ removeAllActiveClass();
26
+ $parent.addClass('active');
27
+ $content.height('100%');
28
+ } else {
29
+ $parent.removeClass('active');
30
+ $content.css({ height: '0px' });
31
+ }
32
+ });
33
+ }
34
+
35
+ afterReflex() {
36
+ this.initAccordion();
37
+ }
38
+ }
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAccordion
4
+ VERSION = "0.1.11-beta"
5
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "zeitwerk"
4
+
5
+ loader = Zeitwerk::Loader.for_gem
6
+ loader.ignore("#{__dir__}/generators")
7
+ loader.setup
8
+
9
+ module RailsAccordion
10
+
11
+ class Error < StandardError; end
12
+
13
+ class Engine < ::Rails::Engine
14
+ initializer "rails_accordion.importmap" do |app|
15
+ if defined?(Importmap)
16
+ app.config.assets.precompile << "rails_accordion.js"
17
+ app.config.assets.precompile << "rails_accordion.css"
18
+ end
19
+ end
20
+ end
21
+
22
+ class Railtie < Rails::Railtie
23
+ ActiveSupport.on_load :action_view do
24
+ include RailsAccordion
25
+ end
26
+ end
27
+
28
+ def accordion(**args, &block)
29
+ render AccordionComponent.new(**args), &block
30
+ end
31
+
32
+ def accordion_item(**args, &block)
33
+ render ItemComponent.new(**args), &block
34
+ end
35
+ end
36
+
37
+ loader.eager_load
@@ -0,0 +1,4 @@
1
+ # desc "Explaining what the task does"
2
+ # task :rails_table do
3
+ # # Task goes here
4
+ # end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "lib/rails_accordion/version"
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "rails_accordion"
7
+ spec.version = RailsAccordion::VERSION
8
+ spec.authors = ["Ahmadshoh Nasrullozoda"]
9
+ spec.email = ["tajbrains@gmail.com"]
10
+
11
+ spec.summary = "rails_accordion is in development."
12
+ spec.description = "rails_accordion is in development."
13
+ spec.homepage = "https://tajbrains.com"
14
+ spec.license = "MIT"
15
+ spec.required_ruby_version = ">= 2.6.0"
16
+
17
+ spec.metadata["homepage_uri"] = spec.homepage
18
+ spec.metadata["source_code_uri"] = "https://github.com/Tajbrains/rails_accordion"
19
+ spec.metadata["changelog_uri"] = "https://github.com/Tajbrains/rails_accordion/blob/master/CHANGELOG.md"
20
+
21
+ spec.files = Dir["{bin,app,lib,vendor}/**/*", "MIT-LICENSE", "Rakefile", "README.md", "rails_accordion.gemspec", "Gemfile", "Gemfile.lock"]
22
+
23
+ spec.add_dependency "stimulus-rails", "~> 1.2"
24
+ spec.add_dependency "view_component", "~> 2.52"
25
+ spec.add_dependency "zeitwerk", "~> 2.6"
26
+ end