Getting Started with Accordion Web Component
Smart UI Web Components work with current evergreen browsers and Node 18+ for local tooling; pin package versions to match your project policy.
Smart UI is distributed as the smart-webcomponents NPM package. You can also use the full download from the Download page.
Quick start
- Install the package:
npm install smart-webcomponents
- Load the Accordion module (ES module script):
<script type="module" src="node_modules/smart-webcomponents/source/modules/smart.accordion.js"></script>
- Add the default stylesheet (prefer angular.json / bundler entry in app codebases; for plain HTML use a link):
<link rel="stylesheet" type="text/css" href="node_modules/smart-webcomponents/source/styles/smart.default.css" />
- Add markup in one of two ways - semantic custom element (the component tag is in your HTML) or a host
div(you mount programmatically with appendTo):
Semantic element (id matches the selector in Smart()):
<smart-accordion id="accordion"></smart-accordion>
Host container (id matches appendTo on Smart.Accordion):
<div id="accordionContainer"></div>
- Initialize after the module loads: define a const accordionOptions object, then either bind with Smart('#accordion', ...) on the semantic tag or use new Smart.Accordion({ ...accordionOptions, appendTo: '#accordionContainer' }) on the host
div:
<script type="module"> import 'node_modules/smart-webcomponents/source/modules/smart.accordion.js'; const accordionOptions = { expandMode: 'multiple' }; // Option A - semantic <smart-accordion> with id="accordion" Smart('#accordion', class { get properties() { return accordionOptions; } }); // Option B - host div id="accordionContainer" // const accordionInstance = new Smart.Accordion({ // ...accordionOptions, // appendTo: '#accordionContainer' // }); // Option C - constructor(selector, options), then append the returned element yourself // const myAccordion = new Smart.Accordion('#accordion', accordionOptions); // document.body.appendChild(myAccordion); </script>
Uncomment Option B when you use the host
div; use Option A when you use the semantic element. The Runtime cookbook also documents new Smart.Accordion('#accordion', accordionOptions) with appendChild, and document.createElement('smart-accordion') with .props or Object.assign (all are valid patterns; do not combine overlapping patterns for the same instance unless you intend multiple components). - Serve the folder over HTTP (or use your bundler dev server) and open the page.
Runtime cookbook
Alternative creation patterns and imperative APIs. These are all valid ways to create Smart UI components: semantic markup + Smart(); new Smart.Accordion({ ...options, appendTo: '#...' }); new Smart.Accordion('#accordion', accordionOptions) plus appendChild on the returned element; and document.createElement('smart-accordion') then assigning options via .props or Object.assign on the element.
Constructor with a selector string and options, then append the returned element (for example const myAccordion = new Smart.Accordion('#accordion', accordionOptions)):
const accordionOptions = { expandMode: 'multiple' };
const myAccordion = new Smart.Accordion('#accordion', accordionOptions);
document.body.appendChild(myAccordion);
Create with document.createElement('smart-accordion'), assign properties (same as any custom element), then append:
const accordionOptions = { expandMode: 'multiple' };
const accordion = document.createElement('smart-accordion');
Object.assign(accordion, accordionOptions);
document.body.appendChild(accordion);
Host on a div with appendTo (import the module, then instantiate when the document is ready; the container id must match appendTo):
import "../../source/modules/smart.accordion.js";
document.readyState === 'complete' ? init() : window.addEventListener('load', init);
function init() {
const accordionOptions = { expandMode: 'multiple' };
const accordion = new Smart.Accordion({
...accordionOptions,
appendTo: '#accordionContainer'
});
}
Append to the DOM:
const container = document.getElementById('accordion-container');
container.appendChild(accordion);
Remove from the DOM:
accordion.remove();
Set a property:
accordion.disabled = true; accordion.theme = 'dark';
Get a property value:
const isDisabled = accordion.disabled; const currentTheme = accordion.theme;
Invoke a method:
accordion.refresh(); accordion.focus();
Add event listener:
accordion.addEventListener('collapse', (event) => {
console.log('collapse triggered:', event.detail.content);
});
Remove event listener:
const handleAccordionEvent = (event) => {
console.log('collapse triggered:', event.detail.content);
};
accordion.addEventListener('collapse', handleAccordionEvent);
accordion.removeEventListener('collapse', handleAccordionEvent);
Common Use Cases
-
Expand specific item
Programmatically open an accordion section
accordion.expand(0); // Expand first item
-
Single expansion mode
Allow only one panel open at a time
accordion.expandMode = 'single';
Troubleshooting
- How do I expand multiple items?
- Set expandMode = 'multiple' to allow several panels to be open simultaneously.
Accessibility
The Accordion component follows WAI-ARIA best practices:
- Keyboard navigation - Tab, Arrow keys, Enter, and Escape are supported
- ARIA roles - Appropriate roles and labels are applied automatically
- Focus management - Visible focus indicators for keyboard users
- Screen readers - State changes are announced to assistive technology
- High contrast - Supports Windows High Contrast Mode and forced colors
For custom labeling, set aria-label or aria-labelledby attributes on the component.
Supported stacks: Smart UI targets Angular 17+, React 18+, Vue 3+, Node 18 LTS, and evergreen browsers; pin exact package versions to your org policy.