<% local_assigns[:margin_bottom] ||= 6 shared_helper = GovukPublishingComponents::Presenters::SharedHelper.new(local_assigns) id ||= "default-id-#{SecureRandom.hex(4)}" items ||= [] anchor_navigation ||= false track_show_all_clicks ||= false track_sections ||= false accordion_classes = %w(gem-c-accordion govuk-accordion) accordion_classes << (shared_helper.get_margin_bottom) translations = { show_text: "components.accordion.show", hide_text: "components.accordion.hide", show_all_text: "components.accordion.show_all", hide_all_text: "components.accordion.hide_all", this_section_visually_hidden: "components.accordion.this_section_visually_hidden", } locales = {} data_attributes ||= {} ((data_attributes[:module] ||= "") << " " << "govuk-accordion gem-accordion").strip! data_attributes[:anchor_navigation] = anchor_navigation data_attributes[:track_show_all_clicks] = track_show_all_clicks data_attributes[:track_sections] = track_sections data_attributes_show_all ||= nil data_attributes[:show_all_attributes] = data_attributes_show_all if data_attributes_show_all translations.each do |key, translation| locales[key] = shared_helper.t_locale(translation) data_attributes[key] = t(translation) end unique_locales = locales.values.uniq if unique_locales.length > 1 data_attributes[:locale] = locales else if unique_locales[0] != I18n.locale data_attributes[:locale] = unique_locales[0] end end %> <% if items.any? %> <%= tag.div(class: accordion_classes, id: id, data: data_attributes) do %> <% items.each_with_index do |item, i| %> <% # The GOVUK Frontend JavaScript for this component # uses a loop starting at 1 for accessing heading ID values, # hence the increment below index = i + 1 item[:data_attributes] ||= nil section_classes = %w(govuk-accordion__section) section_classes << 'govuk-accordion__section--expanded' if item[:expanded] summary_classes = %w(govuk-accordion__section-summary govuk-body) %> <%= tag.div(class: section_classes) do %> <%= tag.div(class: "govuk-accordion__section-header") do %> <%= content_tag( shared_helper.get_heading_level, content_tag( 'span', item[:heading][:text], class: "govuk-accordion__section-button", id: "#{id}-heading-#{index}" ), class: "govuk-accordion__section-heading", id: item[:heading][:id], data: item[:data_attributes] ) %> <%= tag.div(item[:summary][:text], id: "#{id}-summary-#{index}", class: summary_classes) if item[:summary].present? %> <% end %> <%= tag.div(item[:content][:html], id: "#{id}-content-#{index}", class: "govuk-accordion__section-content", 'aria-labelledby': "#{id}-heading-#{index}") %> <% end %> <% end %> <% end %> <% end %>