Accordion

An accordion is a vertically stacked set of interactive headings that each contain a title, content snippet, or thumbnail representing a section of content. The headings function as controls that enable users to reveal or hide their associated sections of content. Accordions are commonly used to reduce the need to scroll when presenting multiple sections of content on a single page.

Installation

Installation

Import the component in the script section of your Svelte file:


import Accordion from 'svve11/Accordion.svelte'
          
Usage

Usage

Creating an Accordion

To supply the accordion with its contents, an options object is passed as a prop to the accordion. This object can be created in the script section of the .svelte file or imported in from another location. The options object has 4 properties.

    Required Props

  • panelInfo (array): It must be an array of objects, with each object containing information for one accordion item. Each object must contain:
    • id (number): Used to set the `id` of the accordion header and panel. If you will have more than one accordion in your application, be sure to continue the sequence of numbers instead of starting back at 1.
    • panelContent (string): Sets text contents of the panel.
    • headerTitle (string): Sets the title of the accordion section.
  • headerLevel (number): Sets the aria-level for each header in the accordion. For information on deciding the appropriate value to be supplied, visit this ARIA webpage.
  • Optional Props

  • styles (object): This property is an object with four assignable key/value pairs. The values must be strings, and the required key properties are:
    • accordionHeaderStyle: sets the style attribute for each accordion header
    • accordionPanelStyle: sets the style attribute for each accordion panel
    • accordionItemStyle: sets the style attribute for each accordion item
    • overallAccordionStyle: sets the style attribute for the accordion as a whole
  • multiselectable (boolean): This will default to false. When set to true, the accordion can expand multiple panels at one time. When set to false, the accordion can expand only one panel at a time.

Example Options Object:


const accordionOptions = { 
  panelInfo: [
	{
	  id: 1,
	  panelContent: 'My first panel text.',
	  headerTitle: 'My first header title'
	},
	{
	  id: 2,
	  panelContent: 'My second panel text.',
	  headerTitle: 'My second header title'
	}
  ],
  headerLevel: 4,
  styles: {
	accordionHeaderStyle: 'Header styles string',
	accordionPanelStyle: 'Panel styles string;',
	accordionItemStyle: 'Item styles string',
	overallAccordionStyle: 'Accordion styles string'
  },
  multiselectable: false
}
					

Instantiating an accordion


<Accordion options={accordionOptions} />
					

Example Accordion:

Styling

Styling

The accordion is made of 5 different components that can have styles applied to them using the pre-assigned classes and a globally scoped CSS stylesheet. The classes are:

  • sv-accordion-main: This applies styling to the accordion as whole
  • sv-accordion-item: This applies styling to each accordion item within an accordion
  • sv-accordion-header: This applies styling to each accordion header within an item
  • sv-accordion-button: This applies styling to each accordion button within a header
  • sv-accordion-panel: This applies styling to each accordion panel within an item
Component API

Component API

This table describes the props that should be passed to the accordion in the options object
PropTypeRequiredDefault Value
panelInfo array of objects true N/A
headerLevel number true N/A
multiselectable boolean false false
styles object false N/A