Skip to content

Commit

Permalink
Merge pull request #115 from stevehook/add-expanded-option-to-accordi…
Browse files Browse the repository at this point in the history
…on-slots

Add `expanded` option to accordion slots
  • Loading branch information
peteryates authored Mar 2, 2021
2 parents a31fe21 + c006429 commit 254fbc6
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 368 deletions.
2 changes: 1 addition & 1 deletion app/components/govuk_component/accordion.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<%= tag.div(id: section.id(suffix: 'section'), class: section.classes, **section.html_attributes) do %>
<div class="govuk-accordion__section-header">
<h2 class="govuk-accordion__section-heading">
<%= tag.span(section.title, id: section.id, class: "govuk-accordion__section-button") %>
<%= tag.span(section.title, id: section.id, class: "govuk-accordion__section-button", aria: { expanded: section.expanded? }) %>
</h2>
<% if section.summary.present? %>
<%= tag.div(section.summary, id: section.id(suffix: 'summary'), class: %w(govuk-accordion__section-summary govuk-body)) %>
Expand Down
11 changes: 9 additions & 2 deletions app/components/govuk_component/accordion.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,26 @@ def default_classes
end

class Section < GovukComponent::Slot
attr_accessor :title, :summary
attr_accessor :title, :summary, :expanded

def initialize(title:, summary: nil, classes: [], html_attributes: {})
alias_method :expanded?, :expanded

def initialize(title:, summary: nil, expanded: false, classes: [], html_attributes: {})
super(classes: classes, html_attributes: html_attributes)

self.title = title
self.summary = summary
self.expanded = expanded
end

def id(suffix: nil)
[title.parameterize, suffix].compact.join('-')
end

def classes
super + (expanded? ? %w(govuk-accordion__section--expanded) : [])
end

private

def default_classes
Expand Down
Loading

0 comments on commit 254fbc6

Please sign in to comment.