Skip to main content

Instance attribute and instance method interface

Besides options, the plugin exposes instance attributes and methods. The following is updated for 0.1.9.

ps: If not necessary, it is not recommended to use these attributes and methods directly.

Instance attributes​

options​

The normalized options object with every optional field resolved. For example:

const msg = ewMessage("This is a default message");
console.log(msg.options); // { content: "This is a default message", type: "info", ... }

el​

The root element of the message. It becomes null after the message is closed or destroyed.

closeBtnEl​

The close button element, or null when the close button is not shown.

instances​

The message elements in the current container (NodeListOf<HTMLElement>).

container​

The resolved mount container. Defaults to document.body when container is not provided, and is resolved when the message is constructed.

Instance methods​

destroy (new in 0.0.6, returns a Promise since 0.1.9)​

Destroys the message and returns Promise<void>. Removal is immediate by default; pass { animate: true } to run the removeClassName animation first.

const msg = ewMessage("This is a default message");
await msg.destroy(); // destroy immediately
await msg.destroy({ animate: true }); // destroy with animation

Destroying also clears timers, animation frames and event listeners, and fires onClose({ reason: "destroy" }) once.

update (new in 0.1.9)​

Updates the current message in place without appending a new one, and returns the instance.

const msg = ewMessage({ content: "Before", duration: 0 });
msg.update({ content: "After", type: "success" });

render​

The core rendering method. Passing options merges them into the current options and renders another message. It is usually not called directly.

createMessage​

Creates the root element and returns { element, closeBtnEl }. It is usually not called directly.

setTop​

Recalculates the offset of messages in the same container. Since 0.1.9 it writes the CSS variable --ew-message-offset instead of an inline top, so themes can override it.

close​

Closes messages manually. Parameters:

  1. nodes: elements to close, defaults to the current message
  2. time: delay in ms, defaults to 0
  3. isDestroy: skip animation and remove immediately, defaults to false
  4. reason: close reason, defaults to manual; one of timeout / manual / destroy / maxCount
const msg = ewMessage("This is a default message");
msg.close([msg.el], 1000); // remove after 1s
msg.close([msg.el], 0, false, "manual"); // remove immediately as a manual close

animationRemoveNode (new in 0.0.9, returns a Promise since 0.1.9)​

Adds the leave animation and returns Promise<void>. It is usually not called directly.

animationAddNode (new in 0.1.1)​

Adds the enter animation. It is usually not called directly.

Static methods​

Message.clearAll(container?)​

Clears every message in the given container and returns Promise<void>. container can be a selector string or a DOM element and defaults to document.body. It is also available as ewMessage.clearAll(container?).

import ewMessage, { Message } from "ew-message";

await Message.clearAll("#app");
await ewMessage.clearAll();