Collapse
A collapse component for expandable sections, progressive disclosure and accordion-style content.
import { Collapse } from "@schukai/monster/source/components/layout/collapse.mjs";Introduction
The Monster Collapse component hides secondary content until it is needed. Use it for progressive disclosure when a section should stay available but not always expanded.
When to use Collapse
- Use it for optional details: Advanced filters, extra explanations and secondary settings fit well.
- Use it to shorten long pages: Users can open only the parts that matter to them.
- Do not hide primary information by default: Content required to complete the main task should usually stay visible.
Typical mistakes
Make the collapsed heading informative. Users should know what becomes available before they open the section.
Collapse Simple
This example shows how a basic collapse works. It is important to note that the collapse itself does not have an open button. Therefore, you have to add your own button or other functionality for this purpose.
Keep less-frequent details collapsed by default and reveal them through the public API instead of inline event handlers.
Keep less-frequent details collapsed by default and reveal them through the public API instead of inline event handlers.
Javascript
import "@schukai/monster/source/components/layout/collapse.mjs";
const collapse = document.getElementById("collapse-simple");
const log = document.getElementById("collapse-simple-log");
document.getElementById("collapse-simple-toggle")?.addEventListener("click", () => {
collapse?.toggle();
});
document.getElementById("collapse-simple-open")?.addEventListener("click", () => {
collapse?.open();
});
document.getElementById("collapse-simple-close")?.addEventListener("click", () => {
collapse?.close();
});
collapse?.addEventListener("monster-collapse-before-open", () => {
log.textContent = "State: opening";
});
collapse?.addEventListener("monster-collapse-open", () => {
log.textContent = "State: open";
});
collapse?.addEventListener("monster-collapse-before-close", () => {
log.textContent = "State: closing";
});
collapse?.addEventListener("monster-collapse-closed", () => {
log.textContent = "State: closed";
});HTML
<section style="display:grid;gap:var(--monster-space-4);">
<div style="display:flex;gap:var(--monster-space-3);flex-wrap:wrap;align-items:center;">
<button id="collapse-simple-toggle" type="button">Toggle details</button>
<button id="collapse-simple-open" type="button">Open</button>
<button id="collapse-simple-close" type="button">Close</button>
<span id="collapse-simple-log">State: closed</span>
</div>
<monster-collapse id="collapse-simple">
<div style="display:grid;gap:var(--monster-space-3);padding:var(--monster-space-4);background:var(--monster-bg-color-primary-2);color:var(--monster-color-primary-2);border:1px solid var(--monster-color-border-primary-2);">
<strong>Delivery notes</strong>
<p style="margin:0;">
Keep less-frequent details collapsed by default and reveal them through the
public API instead of inline event handlers.
</p>
</div>
</monster-collapse>
</section>Stylesheet
/** no additional stylesheet is defined **/Collapse With Events
This variant highlights the event lifecycle. It is useful when outer UI should react to the collapsed state without touching component internals.
Javascript
import "@schukai/monster/source/components/layout/collapse.mjs";
const collapse = document.getElementById("collapse-events-demo");
const log = document.getElementById("collapse-events-log");
document.getElementById("collapse-events-toggle")?.addEventListener("click", () => {
collapse?.toggle();
});
collapse?.addEventListener("monster-collapse-before-open", () => {
log.textContent = "Lifecycle: before open";
});
collapse?.addEventListener("monster-collapse-open", () => {
log.textContent = "Lifecycle: open";
});
collapse?.addEventListener("monster-collapse-before-close", () => {
log.textContent = "Lifecycle: before close";
});
collapse?.addEventListener("monster-collapse-closed", () => {
log.textContent = "Lifecycle: closed";
});HTML
<section style="display:grid;gap:var(--monster-space-4);">
<div style="display:grid;gap:var(--monster-space-2);padding:var(--monster-space-4);background:var(--monster-bg-color-secondary-2);color:var(--monster-color-secondary-2);border:1px solid var(--monster-color-border-secondary-2);">
<strong>FAQ style section</strong>
<span id="collapse-events-log">Waiting for user interaction</span>
</div>
<button id="collapse-events-toggle" type="button">Toggle answer</button>
<monster-collapse id="collapse-events-demo">
<div style="padding:var(--monster-space-4);background:var(--monster-bg-color-primary-1);color:var(--monster-color-primary-1);border:1px solid var(--monster-color-border-primary-2);">
Yes. The collapse emits lifecycle events that can be mirrored into surrounding
status UI or analytics hooks.
</div>
</monster-collapse>
</section>Stylesheet
/** no additional stylesheet is defined **/Component Design
The Monster Collapse component uses the Shadow DOM to encapsulate its structure and styles. It provides an expandable and collapsible container for displaying content. The design ensures that the internal behavior and appearance of the component remain consistent and unaffected by external scripts or styles.
Shadow DOM and Accessibility
By leveraging the Shadow DOM, the Monster Collapse component isolates its internal elements. Accessibility is a key consideration, with support for ARIA roles and events to provide compatibility with screen readers and assistive technologies. The component ensures that users can interact with the collapse content seamlessly using both mouse and keyboard inputs.
Customizing Through Exported Parts
The Monster Collapse component exposes specific exported parts to allow developers to customize the appearance of the collapse container, decorative elements, and content while preserving encapsulation.
Available Part Attributes
control: Represents the entire control container, including the toggle behavior.container: Targets the content area of the collapse.deco: Refers to the decorative line or element added for visual separation.
Below is an example of how to style the collapse component using the CSS part selector:
monster-collapse::part(control) {
background-color: #f5f5f5;
border-radius: 6px;
transition: box-shadow 0.3s ease;
}
monster-collapse::part(container) {
padding: 16px;
font-size: 1rem;
line-height: 1.5;
color: #333;
}
monster-collapse::part(deco) {
height: 2px;
background-color: #007bff;
margin-top: 8px;
}
Explanation of the Example
monster-collapse::part(control): Styles the entire collapse control container with a background and hover effect.monster-collapse::part(container): Adds padding and styles the content within the collapse area.monster-collapse::part(deco): Customizes the decorative line with a blue background color and spacing.
Accessibility
The Monster Collapse component is designed to be fully accessible. It includes:
- Keyboard Navigation: Users can expand or collapse the content using keyboard inputs.
- ARIA Roles: ARIA roles are applied to indicate the state (open or closed) of the collapse content.
- Focus Management: Focus is maintained on the relevant interactive elements during toggling.
These features ensure that the component delivers a smooth and inclusive experience for all users, including those relying on assistive technologies.
HTML Structure
<monster-collapse></monster-collapse>JavaScript Initialization
const element = document.createElement('monster-collapse');
document.body.appendChild(element);Exported
Collapse, nameSymbolDerived from
CustomElementOptions
The Options listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class. Therefore, it inherits Options from these parent classes. If you cannot find a specific Options in this list, we recommend consulting the documentation of the CustomElement.
- since
- deprecated
Properties and Attributes
The Properties and Attributes listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits Properties and Attributes from these parent classes. If you cannot find a specific Properties and Attributes in this list, we recommend consulting the documentation of the CustomElement.
data-monster-options: Sets the configuration options for the collapse component when used as an HTML attribute.data-monster-option-[name]: Sets the value of the configuration option[name]for the collapse component when used as an HTML attribute.
Methods
The methods listed in this section are defined directly within the class. This class is derived from several parent classes, including the CustomElement class and ultimately from HTMLElement. Therefore, it inherits methods from these parent classes. If you cannot find a specific method in this list, we recommend consulting the documentation of the CustomElement.
Constructor
constructor()Behavioral methods
adjustHeight()- {Collapse}
monster-collapse-adjust-heightThis event is fired when the height is adjusted. As a detail, the height is passed.
close()- {Collapse}
monster-collapse-before-closeThis event is fired before the collapse is closed.monster-collapse-closedThis event is fired after the collapse is closed.
open()- {Collapse}
monster-collapse-before-openThis event is fired before the collapse is opened.monster-collapse-openThis event is fired after the collapse is opened.
toggle()- {Collapse}
State query methods
isClosed()- {boolean}
isOpen()- {boolean}
Static methods
[instanceSymbol]()- {symbol}
instanceof operator.getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {void}
connectedCallback()- {void}
disconnectedCallback()- {void}
Events
The component emits the following events:
monster-collapse-before-open
This event is fired before the collapse is opened.monster-collapse-open
This event is fired after the collapse is opened.monster-collapse-before-close
This event is fired before the collapse is closed.monster-collapse-closed
This event is fired after the collapse is closed.monster-collapse-adjust-height
This event is fired when the height is adjusted. As a detail, the height is passed.
For more information on how to handle events, see the mdn documentation.