rails_accordion 0.1.10.pre.beta → 0.1.12.pre.beta

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,15 @@
1
+ @tailwind base;
2
+ @tailwind components;
3
+ @tailwind utilities;
4
+
5
+ .accordion_content {
6
+ @apply h-0 overflow-hidden w-full transition-all ease-in-out;
7
+ }
8
+
9
+ .accordion_toggle {
10
+ @apply cursor-pointer;
11
+ }
12
+
13
+ .accordion_content-container {
14
+ @apply h-max;
15
+ }
@@ -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,46 @@
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", style: content_styles 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
+
39
+ def content_styles
40
+ "transition-duration: #{parse_duration}ms;"
41
+ end
42
+
43
+ def parse_duration
44
+ RailsAccordion.configuration.animation_duration
45
+ end
46
+ end
@@ -0,0 +1,39 @@
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
+ const content = item.querySelector('.accordion_content')
14
+
15
+ toggle.addEventListener('click', (e) => {
16
+ if (content.classList.contains('accordion_active')) {
17
+ this.hide(content);
18
+ } else {
19
+ this.hideAll(items);
20
+ this.open(content);
21
+ }
22
+ });
23
+ });
24
+ }
25
+
26
+ hideAll(items) {
27
+ items.forEach((item) => this.hide(item.querySelector('.accordion_content')));
28
+ }
29
+
30
+ hide(item) {
31
+ item.classList.remove("accordion_active")
32
+ item.style.height = 0;
33
+ }
34
+
35
+ open(item) {
36
+ item.classList.add("accordion_active")
37
+ item.style.height = item.scrollHeight + 'px'
38
+ }
39
+ }
@@ -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_config_file
13
+ template 'rails_accordion.tt', 'config/initializers/rails_accordion.rb'
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,4 @@
1
+ RailsAccordion.configure do |config|
2
+ config.default_state = :all_closed # :all_closed, :all_opened, :first_opened
3
+ config.animation_duration = 300 # in ms
4
+ end
@@ -0,0 +1,22 @@
1
+ module RailsAccordion
2
+ class Configuration
3
+ attr_accessor :default_state, :animation_duration
4
+
5
+ def initialize
6
+ @default_state = :closed
7
+ @animation_duration = 300
8
+ end
9
+ end
10
+
11
+ def self.configuration
12
+ @configuration ||= Configuration.new
13
+ end
14
+
15
+ def self.configuration=(config)
16
+ @configuration = config
17
+ end
18
+
19
+ def self.configure
20
+ yield configuration
21
+ end
22
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RailsAccordion
4
+ VERSION = "0.1.12-beta"
5
+ end
@@ -0,0 +1,39 @@
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
+ isolate_namespace RailsAccordion
15
+
16
+ initializer "rails_accordion.importmap" do |app|
17
+ if defined?(Importmap)
18
+ app.config.assets.precompile << "rails_accordion.js"
19
+ app.config.assets.precompile << "rails_accordion.css"
20
+ end
21
+ end
22
+ end
23
+
24
+ class Railtie < Rails::Railtie
25
+ ActiveSupport.on_load :action_view do
26
+ include RailsAccordion
27
+ end
28
+ end
29
+
30
+ def accordion(**args, &block)
31
+ render AccordionComponent.new(**args), &block
32
+ end
33
+
34
+ def accordion_item(**args, &block)
35
+ render ItemComponent.new(**args), &block
36
+ end
37
+ end
38
+
39
+ 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