DEV Community

Takane Ichinose
Takane Ichinose

Posted on

Responsive Message Box with JavaScript Class

Responsive Message Box with JavaScript Class

This Message Box implementation in JavaScript, inspired by material design's toast message.

I used native JavaScript Class to implement the message box. This may not work with older browsers due to compatibility issue with compatibility with native JavaScript class.

The toast message will appear at the top right corner of the screen if the width of the screen is 768 pixels and above. Otherwise, the toast message will appear at the bottom center of the screen.

Usage

Create MessageBox class instance:

let msg = new MessageBox("#id", option)

Available options (type: Object):

const option = {
  // number: Count in milliseconds before closing the message box
  closeTime: 1000,
  // bool: Flag if the close button (or label) will be hidden for click
  hideCloseButton: false,
}

Call the "show" method to show the dialog box.

msg.show(
  "This is the message that will appear on the message box",
  () => {
    alert("This will be called after the message box is completely hidden");
  },
  "This is the close button"
);

Qodo Takeover

Introducing Qodo Gen 1.0: Transform Your Workflow with Agentic AI

Rather than just generating snippets, our agents understand your entire project context, can make decisions, use tools, and carry out tasks autonomously.

Read full post →

Top comments (0)

SurveyJS custom survey software

JavaScript Form Builder UI Component

Generate dynamic JSON-driven forms directly in your JavaScript app (Angular, React, Vue.js, jQuery) with a fully customizable drag-and-drop form builder. Easily integrate with any backend system and retain full ownership over your data, with no user or form submission limits.

Learn more

Best practices for optimal infrastructure performance with Magento

Running a Magento store? Struggling with performance bottlenecks? Join us and get actionable insights and real-world strategies to keep your store fast and reliable.

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️