MessageStateButton
Button component with integrated message display and state management
import { MessageStateButton } from "@schukai/monster/source/components/form/message-state-button.mjs";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.
Message State Button Simple
Javascript
/** this example does not use an extra script **/HTML
/** this example does not use an extra html file **/Stylesheet
/** no additional stylesheet is defined **/Component Design
The monster-message-state-button component combines a button with a dynamic message display system. It leverages the Shadow DOM to encapsulate its internal structure and styles, ensuring that the internal elements are isolated from the rest of the page.
This component includes a stateful button and a pop-up area (popper) for displaying messages. The encapsulation guarantees consistent styling and behavior, unaffected by external scripts or stylesheets.
Shadow DOM and Accessibility
The component uses the Shadow DOM to encapsulate its button, control container, and message elements. This ensures that external styles do not interfere with the component’s visual presentation. It also provides proper ARIA roles and attributes to support accessibility and usability.
Customizing Through Exported Parts
The monster-message-state-button component exposes its internal parts using the exportparts attribute. This allows developers to apply custom styles to specific parts of the component.
Available Part Attributes
control: Represents the main container for the button and message popper.button: Styles the button element itself.popper: Targets the message pop-up container, which appears when a message is displayed.message: Represents the message content container inside the popper.arrow: Targets the directional arrow displayed on the popper.
Below is an example of how to customize the component's parts using CSS:
monster-message-state-button::part(button) {
background-color: #0078d4;
color: #ffffff;
padding: 8px 16px;
border-radius: 4px;
border: none;
cursor: pointer;
}
monster-message-state-button::part(popper) {
background-color: #f0f0f0;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
padding: 12px;
border-radius: 6px;
}
monster-message-state-button::part(message) {
font-size: 14px;
color: #333;
}
monster-message-state-button::part(arrow) {
width: 12px;
height: 12px;
background-color: #f0f0f0;
transform: rotate(45deg);
margin-top: -6px;
}
Explanation of the Example
monster-message-state-button::part(button): Customizes the button's appearance, including background color, padding, and border radius.monster-message-state-button::part(popper): Styles the popper container with a light background and box shadow.monster-message-state-button::part(message): Defines the font and color for the message content.monster-message-state-button::part(arrow): Adjusts the size and orientation of the directional arrow.
Accessibility
The component is designed to be accessible, supporting keyboard navigation and screen readers. It includes the following accessibility features:
- Proper ARIA roles for the button and message elements.
- Keyboard focus management for the button and popper.
- Logical and semantic structure for screen readers to interpret messages.
Developers can further enhance accessibility by ensuring meaningful message content and labels.
HTML Structure
<monster-message-state-button></monster-message-state-button>JavaScript Initialization
const element = document.createElement('monster-message-state-button');
document.body.appendChild(element);Exported
MessageStateButtonDerived from
PopperOptions
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 Popper.
- 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 Popper.
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 Popper.
Behavioral methods
clearMessage()- {MessageStateButton}
hideMessage()- {MessageStateButton}
removeState()- {MessageStateButton}
showDialog()- {MessageStateButton}
showMessage(timeout)timeout{number}: - Optional time in milliseconds after which the message will auto-hide
- {messagestatebutton}: the button instance for chaining
toggleMessage()- {MessageStateButton}
Structural methods
getMessage()- {Object}
getState()- {MessageStateButton|undefined}
setMessage(message,title,icon)message{string|htmlelement}: - The message content as string or HTML elementtitle{string}: - Optional title to show above the messageicon{string}: - Optional icon HTML to display next to the title
- {messagestatebutton}: the button instance for chaining
{TypeError}When message is empty or invalid type
setState(state,timeout)state{string}: - The state to set (e.g. 'success', 'error', 'loading')timeout{number}: - Optional timeout in milliseconds after which state is removed
- {messagestatebutton}: the button instance for chaining
{TypeError}When state is not a string or timeout is not a number
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}
Other methods
blur()click()3.27.0monster-click
focus(options)3.27.0options{object}: options
Events
The component emits the following events:
monster-state-changed
Fired when button state changesmonster-message-shown
Fired when message is displayedmonster-message-hidden
Fired when message is hiddenmonster-click
Fired when button is clickedmonster-click
For more information on how to handle events, see the mdn documentation.