NCIOCPL / ncids

NCI Design System
7 stars 5 forks source link

Story: Create JS for usa-accordion #1468

Closed andyvanavery31 closed 3 months ago

andyvanavery31 commented 6 months ago

As a user of the NCIDS, I want to be able to condense sections of content into an accordion on a site page, so that my visitors can easily locate a section and then explore it further without needing to scroll through large amounts of content to get to it.

Requirements

Notes

DoR DS Tasks

DoD DS Tasks

Resources

Scenarios

Acceptance Criteria

Default Single Accordion

Scenario: A user views a page with a usa-accordion component
  Given the js for the usa-accordion component has been created
    And the usa-accordion with the new js has been added to a page
  When the user views the usa-accordion component
  Then they will see the each section header displayed for the accordion
    And the first section will be expanded to display all of the hidden content
    And all other sections will be collapsed to hide the content within those sections
  When a user clicks on the heading container displayed for a collapsed section
  Then the section clicked will expand
  When a user clicks on the heading container displayed for an expanded section
  Then the section clicked will collapse
  When the user clicks on multiple collapsed section headers sequentially
  Then the clicked on section will expand
    And the previously expanded section will collapse

Single Accordion with First Section Collapsed

Scenario: A user views a page with a usa-accordion component
  Given the js for the usa-accordion component has been created
    And the usa-accordion with the new js has been added to a page
  When the user views the usa-accordion component
  Then they will see the each section header displayed for the accordion
    And all sections will be collapsed to hide the content within those sections
  When a user clicks on the heading container displayed for a collapsed section
  Then the section clicked will expand
  When a user clicks on the heading container displayed for an expanded section
  Then the section clicked will collapse
  When the user clicks on multiple collapsed section headers sequentially
  Then the clicked on section will expand
    And the previously expanded section will collapse

Single Accordion with Multiple Expansions Allowed

Scenario: A user views a page with a usa-accordion component with multiple expansions of sections allowed
  Given the js for the usa-accordion component has been created
    And the usa-accordion with the new js has been added to a page
  When the user views the usa-accordion component
  Then they will see the each section header displayed for the accordion
    And the first section will be expanded to display all of the hidden content
    And all other sections will be collapsed to hide the content within those sections
  When a user clicks on the heading container displayed for a collapsed section
  Then the section clicked will expand
  When a user clicks on the heading container displayed for an expanded section
  Then the section clicked will collapse
  When the user clicks on multiple collapsed section headers sequentially
  Then all clicked sections will expand sequentially

Nested Default Accordions

Scenario: A user views a page with a usa-accordion component nested within another usa-accordion
  Given the js for the usa-accordion component has been created
    And the usa-accordion with the new js has been added to a page nested in another usa-accordion
  When the user views the usa-accordion component
  Then they will see the each section header displayed for the accordion
    And the first section will be expanded to display all of the hidden content
    And all other sections will be collapsed to hide the content within those sections
  When a user clicks on the heading container displayed for a collapsed section
  Then the section clicked will expand
  When a user clicks on the heading container displayed for an expanded section
  Then the section clicked will collapse
  When the user clicks on multiple collapsed section headers sequentially
  Then the clicked on section will expand
    And the previously expanded section will collapse
  When the user clicks on the heading of a section that contains a nested accordion
  Then the nested accordion will appear
    And the information surrounding the nested accordion will be visible (it is within the level of the first accordion)
    And the first section of the nested accordion will be expanded
    And all other sections of the nested accordion will be collapsed
  When a user clicks on the section heading for a collapsed section of the nested accordion
  Then that section will expand within the expanded first level accordion
  When a user clicks on the section heading for any expanded section of the nested accordion
  Then that section will collapse within the expanded first level accordion
  When a user clicks on multiple collapsed sections within the nested accordion sequentially
  Then the clicked on nested section will expand
    And the previously expanded nested section will collapse
  When a user clicks on the section heading for the expanded first level accordion that contains the nested accordion
  Then the section will collapse
  When the user reclicks on the same section heading of the now collapsed first level accordion that contains the nested accordion
  Then the section will expand with the nested accordion visible
    And the first section of the nested accordion will be expanded
    And the other sections of the nested accordion will be collapsed
bryanpizzillo commented 5 months ago

Technical Solution

This will go into ncids-js/usa-accordion. You will need to add ncids-js/usa-accordion to both ncids-js/typedoc.json and package.json in the exports section.

Supported HTML

Basic HTML

(Before JS initializes)

<div class="usa-accordion usa-prose">
  <hX>Heading</hX>
  <p></p>
  <ul>...</ul>
  <div class=" usa-accordion usa-prose">
     <hY>Sub heading</hY>
     <p></p>
     <hZ id="_section8">Sub Sub Heading</hZ>
  </div>
  <hY>Some sub heading</hY>

  <hX>Heading 2</hX>
  ...
</div>

Cancer.gov Article HTML

(Before JS initializes)

<article class="usa-accordion">
   <section class="usa-prose" aria-labeled-by="s1">
      <hX id="s1"></hX>
        <p></p>
        <ul>...</ul>
   </section>
</article>

USWDS HTML

(Before JS initializes. Yes they require you to add in all the stuff we would normally do.)

<div class="usa-accordion">
  <h4 class="usa-accordion__heading">
    <button
      type="button"
      class="usa-accordion__button"
      aria-expanded="true"
      aria-controls="a1"
    >
      First Amendment
    </button>
  </h4>
  <div id="a1" class="usa-accordion__content usa-prose">
    <p>
      Congress shall make no law respecting an establishment of religion, or
      prohibiting the free exercise thereof; or abridging the freedom of speech,
      or of the press; or the right of the people peaceably to assemble, and to
      petition the Government for a redress of grievances.
    </p>
  </div>
  <h4 class="usa-accordion__heading">
    <button
      type="button"
      class="usa-accordion__button"
      aria-expanded="false"
      aria-controls="a2"
    >
      Second Amendment
    </button>
  </h4>
  <div id="a2" class="usa-accordion__content usa-prose">
    <p>
      A well regulated Militia, being necessary to the security of a free State,
      the right of the people to keep and bear Arms, shall not be infringed.
    </p>
    <ul>
      <li>This is a list item</li>
      <li>Another list item</li>
    </ul>
  </div>
  <h4 class="usa-accordion__heading">
    <button
      type="button"
      class="usa-accordion__button"
      aria-expanded="false"
      aria-controls="a3"
    >
      Third Amendment
    </button>
  </h4>
  <div id="a3" class="usa-accordion__content usa-prose">
    <p>
      No Soldier shall, in time of peace be quartered in any house, without the
      consent of the Owner, nor in time of war, but in a manner to be prescribed
      by law.
    </p>
  </div>
</div>

Initialization Notes:

Initialization Flow:

NOTE: Uninitialization needs to turn it back into the EXACT html as before.

The USWDS classes for the button should be used, and proper aria attributes. This should cover the open/close svg.

NOTE: If a user is viewing a url with an anchor fragment (i.e. #) we need to find that heading, or section those contents are in, and expand that section on init.

Component Options

Data Attributes

Public Component Methods

Events (See above)