Collapsible
An interactive component which expands/collapses a panel.
Usage
Example
@joeldrapper starred 3 repositories
phlex-ruby/phlex
phlex-ruby/phlex-rails
ruby-ui/ruby_ui
Collapsible do div(class: "flex items-center justify-between space-x-4 px-4 py-2") do h4(class: "text-sm font-semibold") { " @joeldrapper starred 3 repositories" } CollapsibleTrigger do Button(variant: :ghost, icon: true) do chevron_icon span(class: "sr-only") { "Toggle" } end end end div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "phlex-ruby/phlex" end CollapsibleContent do div(class: 'space-y-2 mt-2') do div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "phlex-ruby/phlex-rails" end div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "ruby-ui/ruby_ui" end end end end
Copied!
Copy failed!
Open
@joeldrapper starred 3 repositories
phlex-ruby/phlex
phlex-ruby/phlex-rails
ruby-ui/ruby_ui
Collapsible(open: true) do div(class: "flex items-center justify-between space-x-4 px-4 py-2") do h4(class: "text-sm font-semibold") { " @joeldrapper starred 3 repositories" } CollapsibleTrigger do Button(variant: :ghost, icon: true) do chevron_icon span(class: "sr-only") { "Toggle" } end end end div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "phlex-ruby/phlex" end CollapsibleContent do div(class: 'space-y-2 mt-2') do div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "phlex-ruby/phlex-rails" end div(class: "rounded-md border px-4 py-2 font-mono text-sm shadow-sm") do "ruby-ui/ruby_ui" end end end end
Copied!
Copy failed!
Installation
Using RubyUI CLI
Run the install command
rails g ruby_ui:component Collapsible
Copied!
Copy failed!
Manual installation
1
Add RubyUI::Collapsible
to app/components/ruby_ui/collapsible.rb
# frozen_string_literal: true module RubyUI class Collapsible < Base def initialize(open: false, **attrs) @open = open super(**attrs) end def view_template(&) div(**attrs, &) end private def default_attrs { data: { controller: "ruby-ui--collapsible", ruby_ui__collapsible_open_value: @open } } end end end
Copied!
Copy failed!
2
Add RubyUI::CollapsibleContent
to app/components/ruby_ui/collapsible/collapsible_content.rb
# frozen_string_literal: true module RubyUI class CollapsibleContent < Base def view_template(&) div(**attrs, &) end private def default_attrs { data: {ruby_ui__collapsible_target: "content"}, class: "overflow-y-hidden" } end end end
Copied!
Copy failed!
3
Add RubyUI::CollapsibleTrigger
to app/components/ruby_ui/collapsible/collapsible_trigger.rb
# frozen_string_literal: true module RubyUI class CollapsibleTrigger < Base def view_template(&) div(**attrs, &) end private def default_attrs { data: { action: "click->ruby-ui--collapsible#toggle" } } end end end
Copied!
Copy failed!
4
Add collapsible_controller.js
to app/javascript/controllers/ruby_ui/collapsible_controller.js
import { Controller } from "@hotwired/stimulus" // Connects to data-controller="accordion" export default class extends Controller { static targets = ['content'] static values = { open: { type: Boolean, default: false, }, } connect() { // Set the initial state of the accordion this.openValue ? this.open() : this.close() } // Toggle the 'open' value toggle() { this.openValue = !this.openValue } // Handle changes in the 'open' value openValueChanged(isOpen, wasOpen) { if (isOpen) { this.open() } else { this.close() } } // Open the accordion content open() { if (this.hasContentTarget) { this.contentTarget.classList.remove('hidden') this.openValue = true } } // Close the accordion content close() { if (this.hasContentTarget) { this.contentTarget.classList.add('hidden') this.openValue = false } } }
Copied!
Copy failed!
5
Update the Stimulus controllers manifest file
Importmap!
You don't need to run this command if you are using Importmap
rake stimulus:manifest:update
Copied!
Copy failed!
Components
Component | Built using | Source |
---|---|---|
Collapsible | Phlex | |
CollapsibleContent | Phlex | |
CollapsibleTrigger | Phlex | |
CollapsibleController | Stimulus JS |