0.0.2 • Published 6 years ago

raven-accordion v0.0.2

Weekly downloads
2
License
MIT
Repository
github
Last release
6 years ago

Raven Accordion

An Angular component for creating simple accordions.

Screenshot

Features

  • Option to expand multiple items simultaneously
  • Uses HTML <details> element for better accessibility
  • Customize color, padding, and borders with CSS custom properties

Installation

Install into your Angular project using NPM.

npm install raven-accordion --save

Import the AccordionModule into your module.

import { AccordionModule } from 'raven-accordion';

@NgModule({
  imports: [
    AccordionModule,
    // ...
  ],
  // ...
})
export class AppModule { }

Usage

Add a <raven-accordion> element to your template, and add <raven-accordion-item> elements for each item in the accordion. Then customize each item with a summary and content.

import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  template: `
    <raven-accordion [allowMultiOpen]="false">
      <raven-accordion-item summary="Heading 1" [open]="true">Item 1</raven-accordion-item>
      <raven-accordion-item summary="Heading 2">Item 2</raven-accordion-item>
      <raven-accordion-item summary="Heading 3">Item 3</raven-accordion-item>
    </raven-accordion>
  `,
  styles: []
})
export class AppComponent { }

Accordion Options

OptionTypeDescriptionDefault Value
allowMultiOpenbooleanWhether to allow multiple items to be open at the same time.false
openItemNotifierSubject<number>Should emit an index of an item to open (see FAQ).undefined

Accordion Item Options

OptionTypeDescriptionDefault Value
openbooleanWhether default state of item is open.false

Styling

CSS variables are used for styling. Example:

:root {
  --ra-summary-padding: 1.25rem;
  --ra-content-background: #cef;
}

Available CSS Variables

VariableDescriptionDefault Value
--ra-summary-paddingPadding around the summaries..5rem 1rem
--ra-summary-borderBorder around the summaries.1px solid #ddd
--ra-summary-font-sizeFont size of the summaries.1.2rem
--ra-summary-font-weightFont weight of the summaries.700
--ra-summary-colorText color of the summaries.#000
--ra-summary-backgroundBackground of the summaries.#eee
--ra-content-paddingPadding around item content.1rem
--ra-content-borderBorder around item content.1px solid #ddd
--ra-content-font-sizeFont size of item content.1rem
--ra-content-colorText color of item content.#000
--ra-content-backgroundBackground of item content.#fff

FAQ

How do I open accordion items programmatically?

If you provide a Subject<number> to the openItemNotifier property of the RavenAccordion, you may emit a value that represents the index of the accordion item to open. Here's an example:

HTML Template

<raven-accordion [openItemNotifier]="itemOpened">
  <raven-accordion-item summary="Heading 1">Item 1</raven-accordion-item>
  <raven-accordion-item summary="Heading 2">Item 2</raven-accordion-item>
</raven-accordion>

<button type="button" (click)="openItem(0)">Open Item 1</button>
<button type="button" (click)="openItem(1)">Open Item 2</button>

TypeScript

export class AppComponent {
  itemOpened: Subject<number> = new Subject();

  openItem(index: number): void {
    this.itemOpened.next(index);
  }
}

Development

To contribute to the development of this component, clone its repository and run npm install. Then run ng serve -o to start a development server and to open a sample page in your browser.

License.

MIT license.

0.0.2

6 years ago

0.0.1

6 years ago