Collapse

A simple collapse component.

Import
the javascript logo
import { Collapse } from "@schukai/monster/source/components/layout/collapse.mjs";
Source
the git logo
Package
the npm logo
Since
3.74.0
Hi, I am a collapse.

Introduction

This is a versatile web component. More than just a simple UI element, this component serves as a key element for enhancing interactivity and engagement within various web applications. Whether you are developing a basic website or a complex enterprise-level application, this component is designed to improve user interaction and satisfaction.

Key Features

  • Dynamic interaction: Users can interact with content dynamically, making the web experience more intuitive and user-centric.
  • Customizable appearance: Customize the appearance of the component to match the design of your brand or application, ensuring visual consistency.
  • Accessibility: Designed with accessibility in mind to ensure that all users have a seamless experience, regardless of their browsing environment.
  • Visual feedback effect: Provides subtle touch feedback to enhance user interaction, making actions more responsive and engaging.
  • Programmatic Control: Offers methods such as trigger, focus, and deactivate, allowing developers to control the component's behavior programmatically and offering greater flexibility.

Improving the user experience

This component goes beyond its traditional functionality to offer an enhanced and interactive user experience. The combination of visual feedback effects and dynamic interaction with content is not only visually appealing, but also provides clear and responsive feedback to user actions, making the web environment more intuitive and user-friendly.

These improvements are supported by user studies that demonstrate a positive impact on user engagement and satisfaction.

Efficiency in the development process

Integrating this component into your development process is straightforward. Its compatibility with standard web technologies and ease of customization ensure seamless integration with your existing tools and libraries. Whether you're working on a small project or a large-scale application, the modular design of this component guarantees easy integration, streamlining your development process and enhancing productivity.

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.

Yeah, you opened me!

You need to add a button to toggle the collapse:

Javascript

import "@schukai/monster/source/components/layout/collapse.mjs";

HTML

<monster-collapse id="collapse-simple">
    Yeah, you opened me!
</monster-collapse>

<p>You need to add a button to toggle the collapse:</p>

<button onclick="document.getElementById('collapse-simple').toggle()">Toggle</button>

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, nameSymbol

Derived from

CustomElement

Options

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.

Option
Type
Default
Description
templates
object
Template definitions
templates.main
string
undefined
Main template
classes
object
CSS classes
classes.container
string
padding
CSS class for the container
features
object
Feature configuration
features.accordion
boolean
true
Enable accordion mode
features.persistState
boolean
true
Enable persist state (Host and Config-Manager required)
features.useScrollValues
boolean
false
Use scroll values (scrollHeight) instead of clientHeight for the height calculation
openByDefault
boolean
false
Open the details by default

  • 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()
Returns
  • {Collapse}
Events
  • monster-collapse-adjust-height This event is fired when the height is adjusted. As a detail, the height is passed.
This method is called when the element is inserted into a document, including into a shadow tree.
close()
Returns
  • {Collapse}
Events
  • 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.
Close the collapse
open()
Returns
  • {Collapse}
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.
Open the collapse
toggle()
Returns
  • {Collapse}

State query methods

isClosed()
Returns
  • {boolean}
isOpen()
Returns
  • {boolean}

Static methods

[instanceSymbol]()
Returns
  • {symbol}
This method is called by the instanceof operator.
getCSSStyleSheet()
Returns
  • {CSSStyleSheet[]}
getTag()
Returns
  • {string}

Lifecycle methods

Lifecycle methods are called by the environment and are usually not intended to be called directly.

[assembleMethodSymbol]()
Returns
  • {void}
connectedCallback()
Returns
  • {void}
disconnectedCallback()
Returns
  • {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.

The current width of the area is too small to display the content correctly.