Button
A beautiful button that can make your life easier and also looks good.
import { Button } from "@schukai/monster/source/components/form/button.mjs";Introduction
Introducing the Monster Button, a versatile web component designed to enhance user interaction across various web platforms. This component combines traditional button functionalities with advanced features such as dynamic content loading and custom event handling, making it an ideal choice for applications requiring highly interactive user interfaces.
Key Features
- Dynamic Content Loading: Load dynamic content directly within the button, enhancing the interactivity without additional navigation.
- Customizable Appearance: Easily adjust the appearance of the button to match your application's branding with extensive CSS styling options.
- Accessibility: Fully accessible, ensuring that all users can interact with the button regardless of their device or browser.
- Ripple Effect: Adds a visually appealing ripple effect on click, providing tactile feedback to the user.
- Advanced Event Handling: Supports custom event handlers that can be defined directly within the component's configuration, allowing for tailored functionality on user interactions.
Improving the user experience
The Monster Button enhances user experience by providing a clear and responsive interface, allowing users to interact with web applications more intuitively. Its customizable nature and dynamic capabilities contribute to a smoother and more engaging user interaction, supported by positive feedback from user studies indicating enhanced satisfaction and usability.
Efficiency in the development process
Incorporating the Monster Button into your web projects streamlines the development process, thanks to its compatibility with standard web technologies and its modular design. This ensures easy integration into existing projects and frameworks, enhancing developer productivity and reducing development time.
Simple Button
This is a simple button example. It is a button with the text "click me!". Nothing more, nothing less.
Javascript
import "@schukai/monster/source/components/form/button.mjs";HTML
<monster-button>click me!</monster-button>Stylesheet
/** no additional stylesheet is defined **/Button With Event
This is a simple button example with click handling. It is a button with the text "click me!". When you click on the button, the text "Button clicked!" will be displayed below the button. The text will be removed after 2 seconds.
You can either attach a classic event handler to the element, or you can use the actions.click option.
Javascript
import "@schukai/monster/source/components/form/button.mjs";HTML
<monster-button id="eeSh6A-button">click me!</monster-button>
<div id="eeSh6A-result" style="margin-top: 20px;display: flex;align-items: center;justify-content: center;">
</div>
<script>
// watch for the DOMContentLoaded event
window.addEventListener('DOMContentLoaded', function () {
// get the button element and set a click event callback
document.getElementById('eeSh6A-button').setOption("actions.click", function () {
// set a text to the result element
document.getElementById('eeSh6A-result').innerHTML = 'Button clicked!';
// Reset the result after 2 seconds.
// This is only there so that you can click more often.
setTimeout(function () {
document.getElementById('eeSh6A-result').innerHTML = '';
}, 2000);
});
});
</script>Stylesheet
/** no additional stylesheet is defined **/Component Design
This component is built using the Shadow DOM, which ensures its internal structure and styling are encapsulated. By using a shadow root, the button component remains isolated from external styles and scripts, protecting its layout and behavior from unintended modifications.
Shadow DOM and Accessibility
The encapsulation provided by the Shadow DOM prevents direct manipulation of the component's internal structure. This ensures consistency in both the button's design and behavior. Developers cannot access or override the button's internal elements directly but can utilize exposed parts for targeted styling.
Customizing Through Exported Parts
While the Shadow DOM isolates the component's internal structure, certain elements have been made customizable through exported parts. These parts allow developers to style specific aspects of the button component using CSS without compromising its encapsulation.
Available Part Attributes
control: Represents the button container, including its structure and layout.button: Represents the button element itself, which can be styled for color, typography, and effects.
Below is an example of how to use CSS part attributes to customize the Button component.
monster-button::part(control) {
display: inline-block;
padding: 5px;
}
monster-button::part(button) {
background-color: #007bff;
color: #fff;
border: none;
border-radius: 4px;
padding: 8px 16px;
font-size: 14px;
cursor: pointer;
}
monster-button::part(button):hover {
background-color: #0056b3;
}
monster-button::part(button):disabled {
background-color: #d6d6d6;
color: #9a9a9a;
cursor: not-allowed;
}
Explanation of the Example
monster-button::part(control): Styles the button container, ensuring spacing and alignment.monster-button::part(button): Styles the button element with a background color, padding, and rounded corners.monster-button::part(button):hover: Adds a hover effect to change the button's background color.monster-button::part(button):disabled: Styles the button when it is in a disabled state, making it visually distinct.
Accessibility
The component supports accessibility best practices by ensuring that the button can be interacted with using keyboard navigation and screen readers. The aria-disabled attribute is automatically managed to reflect the button's state, and focus-related methods like focus() and blur() are provided to enhance usability.
HTML Structure
<monster-button></monster-button>JavaScript Initialization
const element = document.createElement('monster-button');
document.body.appendChild(element);Exported
ButtonDerived from
CustomControlOptions
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 CustomControl.
- 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 CustomControl.
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 CustomControl.
State query methods
value()- {string}: value of the button
e = document.querySelector("monster-button");
console.log(e.value);
value(value)value{string}: value
- {void}
{Error}unsupported type
e = document.querySelector("monster-button");
e.value = 1;
Static methods
[instanceSymbol]()2.1.0- {symbol}
instanceof operator.formAssociated()- {boolean}
getCSSStyleSheet()- {CSSStyleSheet[]}
getTag()- {string}
observedAttributes()- {string[]}
attributeChangedCallback().Lifecycle methods
Lifecycle methods are called by the environment and are usually not intended to be called directly.
[assembleMethodSymbol]()- {Button}
Other methods
blur()click()3.27.0Button.click() method simulates a click on the internal button element.focus(options)3.27.0options{object}: options
Events
The component emits the following events:
monster-button-clicked
this event is triggered when the button is clicked. It contains the field {button} with the button instance.
For more information on how to handle events, see the mdn documentation.