MessageStateButton

A stateful button that combines action feedback, progress states and inline status messaging.

Import
the javascript logo
import { MessageStateButton } from "@schukai/monster/source/components/form/message-state-button.mjs";
Source
the git logo
Package
the npm logo
Since
2.11.0
click me!

Introduction

The Monster MessageStateButton combines an action trigger with visible state or message feedback. Use it when users should see immediately whether an action is idle, running, successful or failed without scanning another area.

When to use MessageStateButton

  • Use it for async actions: Saving, syncing or publishing workflows benefit from inline state feedback.
  • Use it when a plain button is too silent: The control keeps action and result close together.
  • Do not use it for static buttons: If there is no evolving state, a normal button is simpler.

Typical mistakes

Keep the state vocabulary clear. If success, warning and failure messages all look or read similarly, the value of the combined control gets lost.

Success Message

This example uses monster-message-state-button to combine a temporary success state with a popper message.

Save changes

Click the button to show a success message.

Javascript

import "@schukai/monster/source/components/form/message-state-button.mjs";

const button = document.getElementById("message-state-button-demo");
const status = document.getElementById("message-state-button-demo-status");

button.addEventListener("click", () => {
  button.setState("successful", 1500);
  button.setMessage("Your changes were stored successfully.", "Saved");
  button.showDialog();
  status.textContent = "Message opened and success state applied.";
});<script type="module">import "@schukai/monster/source/components/form/message-state-button.mjs";

const button = document.getElementById("message-state-button-demo-run");
const status = document.getElementById("message-state-button-demo-run-status");

button.addEventListener("click", () => {
  button.setState("successful", 1500);
  button.setMessage("Your changes were stored successfully.", "Saved");
  button.showDialog();
  status.textContent = "Message opened and success state applied.";
});</script>

HTML

<div style="display:grid;gap:1rem;align-items:start;">
  <monster-message-state-button id="message-state-button-demo">
    Save changes
  </monster-message-state-button>
  <p
    id="message-state-button-demo-status"
    style="margin:0;color:var(--monster-color-primary-1);"
  >
    Click the button to show a success message.
  </p>
</div>

Stylesheet

/** no additional stylesheet is defined **/
Open in playground

Retry And Error Message

This example documents an error-to-success retry flow for monster-message-state-button.

Publish

First click shows an error, second click shows success.

Javascript

import "@schukai/monster/source/components/form/message-state-button.mjs";

const button = document.getElementById("message-state-button-retry-demo");
const status = document.getElementById("message-state-button-retry-status");
let attempts = 0;

button.addEventListener("click", () => {
  attempts += 1;

  if (attempts === 1) {
    button.setState("failed", 1800);
    button.setMessage("The publish job could not acquire a deployment lock.", "Try again");
    button.showDialog();
    status.textContent = "First attempt failed.";
    return;
  }

  button.setState("successful", 1800);
  button.setMessage("The release was queued and rollout has started.", "Published");
  button.showDialog();
  status.textContent = "Second attempt succeeded.";
});<script type="module">import "@schukai/monster/source/components/form/message-state-button.mjs";

const button = document.getElementById("message-state-button-retry-demo-run");
const status = document.getElementById("message-state-button-retry-status-run");
let attempts = 0;

button.addEventListener("click", () => {
  attempts += 1;

  if (attempts === 1) {
    button.setState("failed", 1800);
    button.setMessage("The publish job could not acquire a deployment lock.", "Try again");
    button.showDialog();
    status.textContent = "First attempt failed.";
    return;
  }

  button.setState("successful", 1800);
  button.setMessage("The release was queued and rollout has started.", "Published");
  button.showDialog();
  status.textContent = "Second attempt succeeded.";
});</script>

HTML

<div style="display:grid;gap:1rem;align-items:start;">
  <monster-message-state-button id="message-state-button-retry-demo">
    Publish
  </monster-message-state-button>
  <p id="message-state-button-retry-status" style="margin:0;">
    First click shows an error, second click shows success.
  </p>
</div>

Stylesheet

/** no additional stylesheet is defined **/
Open in playground

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

MessageStateButton

Derived from

Popper

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 Popper.

Option
Type
Default
Description
templates
object
Template definitions
templates.main
string
undefined
Main template
message
object
Message definition
message.content
string|htmlelement
undefined
The message content
message.title
string
undefined
The message title
message.icon
string
undefined
The message icon
message.width
object
undefined
Width options for the message popper
message.width.min
string|number
12rem
Minimum width (px, rem, em, vw)
message.width.max
string|number
Maximum width (px, rem, em, vw)
message.width.viewportRatio
number
Max width as ratio of viewport width (0-1)
popper
object
undefined
Popper options inherited from the base popper
popper.contentOverflow
string
undefined
Content clipping mode: both|horizontal|smart
mode
string
manual
The mode of the button, can be `manual` or `submit`
labels.button
string
<slot></slot>
Button label
classes
object
Classes for internal elements
classes.button
string
monster-button-outline-primary
Button class
actions
object
Action callbacks
actions.click
function
undefined
Action triggered on click
aria
object
Aria attributes
aria.role
string
Aria role, only if the button is not a button
aria.label
string
Aria label for the button

  • 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()
Returns
  • {MessageStateButton}
clears the Message
hideMessage()
Returns
  • {MessageStateButton}
removeState()
Returns
  • {MessageStateButton}
showDialog()
Returns
  • {MessageStateButton}
With this method, you can show the popper.
showMessage(timeout)
Parameters
  • timeout {number}: - Optional time in milliseconds after which the message will auto-hide
Returns
  • {messagestatebutton}: the button instance for chaining
Shows the message popup overlay with optional auto-hide timeout
toggleMessage()
Returns
  • {MessageStateButton}

Structural methods

getMessage()
Returns
  • {Object}
getState()
Returns
  • {MessageStateButton|undefined}
setMessage(message,title,icon)
Parameters
  • message {string|htmlelement}: - The message content as string or HTML element
  • title {string}: - Optional title to show above the message
  • icon {string}: - Optional icon HTML to display next to the title
Returns
  • {messagestatebutton}: the button instance for chaining
Throws
  • {TypeError} When message is empty or invalid type
Sets the message content to be displayed in the popup overlay
setState(state,timeout)
Parameters
  • state {string}: - The state to set (e.g. 'success', 'error', 'loading')
  • timeout {number}: - Optional timeout in milliseconds after which state is removed
Returns
  • {messagestatebutton}: the button instance for chaining
Throws
  • {TypeError} When state is not a string or timeout is not a number
Sets the state of the button which affects its visual appearance

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}
disconnectedCallback()
Returns
  • {MessageStateButton}

Other methods

blur()
The Button.blur() method removes focus from the internal button element.
click()3.27.0
Events
  • monster-click
Programmatically triggers a click event on the button Will not trigger if the button is disabled
focus(options)3.27.0
Parameters
  • options {object}: options
The Button.focus() method sets focus on the internal button element.
resolveContentOverflowMode()
Returns
  • {string}

Events

The component emits the following events:

  • monster-state-changed
    Fired when button state changes
  • monster-message-shown
    Fired when message is displayed
  • monster-message-hidden
    Fired when message is hidden
  • monster-click
    Fired when button is clicked
  • monster-click

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.