Parameter api
Here we mainly introduce the parameter types of the function.
String parameter
The default parameter can be a string, which is used as the content of the message, such as:
const msg = ewMessage("This is a default message");
Configuration object
Options are grouped into base options, animation, layout, interaction/lifecycle and theming.
Base options
1. content
Message content, supports a string or a DOM node. Defaults to an empty string and is rendered as plain text by default. Use html: true to render HTML. Invalid types warn in development.
const msg = ewMessage({ content: "This is a default message" });
const node = document.createElement("strong");
node.textContent = "Node content";
const msg2 = ewMessage({ content: node });
2. center
Whether to center the content. Defaults to false.
const msg = ewMessage({ content: "This is a default message", center: true });
3. type
Message type: "info", "success", "warning", "error" or "loading". Defaults to "info". Unknown values fall back to "info" with a development warning.
const msg = ewMessage({
content: "This is a successful message",
type: "success",
});
Shorthand methods are also available:
ewMessage.success("success");
ewMessage.loading({ content: "loading", duration: 0 });
4. duration
Auto close delay in milliseconds. Defaults to 2000. duration: 0 makes the message persistent and no longer forces the close button.
const msg = ewMessage({
content: "This is a default message",
duration: 3000,
});
Since 0.1.9 the hard 10s cap is removed; use the optional
maxDurationinstead.
5. showClose
Whether to show the close button. Defaults to true.
const msg = ewMessage({
content: "This is a default message",
showClose: false,
});
6. showTypeIcon
Whether to show the type icon. Defaults to true.
const msg = ewMessage({
content: "This is a default message",
showTypeIcon: true,
});
7. typeIcon
Custom type icon, requires showTypeIcon: true. The value is an HTML string and can be an img, svg or any element. Reusing ${prefix}message-icon and ${prefix}message-${type}-icon gives you the default size and color.
const msg = ewMessage({
content: "This is a default message",
showTypeIcon: true,
typeIcon:
'<svg class="ew-message-icon ew-message-info-icon" viewBox="0 0 1024 1024" aria-hidden="true"><path d="..." fill="currentColor"></path></svg>',
});
8. closeIcon
Custom close icon, requires showClose: true. The value is an HTML string. Reusing ${prefix}message-close-icon is recommended.
const msg = ewMessage({
content: "This is a default message",
closeIcon:
'<svg class="ew-message-close-icon" viewBox="0 0 1024 1024" aria-hidden="true"><path d="..." fill="currentColor"></path></svg>',
});
9. container
Mount container, a selector string or a DOM element. Defaults to document.body and is resolved when the message is constructed, so the module can be imported safely in SSR. A missing selector warns in development and falls back to document.body.
const msg = ewMessage({
content: "This is a default message",
container: "#test",
});
10. html
Whether to parse content as HTML. Defaults to false.
const msg = ewMessage({
content: "<strong>bold</strong>",
html: true,
});
Only
contentis affected.typeIconandcloseIconare always treated as HTML strings.
Animation
11. startClassName
Enter animation class names. Defaults to an empty array. Built-in names are fadeIn and scaleDown, which are automatically prefixed with prefix. External classes such as animate.css are also supported. The classes are removed after the animation ends, with a 350ms fallback if the animation is overridden or removed by host styles.
const msg = ewMessage({
content: "This is a default message",
startClassName: ["fadeIn"],
});
12. removeClassName
Leave animation class names. Defaults to an empty array. Built-in names are fadeOut and scaleUp, with the same rules as startClassName.
const msg = ewMessage({
content: "This is a default message",
removeClassName: ["fadeOut"],
});
Layout
13. position
Message position. Defaults to "top" and supports:
| Value | Description |
|---|---|
top | Top center |
top-left | Top left |
top-right | Top right |
bottom | Bottom center |
bottom-left | Bottom left |
bottom-right | Bottom right |
const msg = ewMessage({
content: "Top right message",
position: "top-right",
});
14. offset
Offset from the viewport edge. Defaults to 25. Accepts a number or a CSS length string (px, %, rem, em, vh, vw, etc.). Stacked messages in the same container are offset from this base without overlapping.
const msg = ewMessage({
content: "40px from the top",
offset: 40,
});
const msg2 = ewMessage({
content: "10% from the top",
offset: "10%",
});
top is a legacy alias of offset; offset wins when both are provided.
15. gap
Gap between stacked messages in px. Defaults to 10.
ewMessage({ content: "Message 1", gap: 16 });
16. maxCount
Max messages per container. Defaults to 0 (unlimited). The oldest message is destroyed when exceeded.
ewMessage({ content: "Message 1", maxCount: 3 });
17. zIndex
Message z-index. Defaults to 1000, written to --ew-message-z-index.
ewMessage({ content: "High z-index", zIndex: 3000 });
Interaction and lifecycle
18. clickToClose
Close the message when it is clicked. Defaults to false. Clicking the close button does not trigger it twice.
ewMessage({ content: "Click to close", clickToClose: true });
19. pauseOnHover
Pause the auto close countdown on hover. Defaults to false. The remaining time resumes after the pointer leaves.
ewMessage({ content: "Pause on hover", duration: 3000, pauseOnHover: true });
20. maxDuration
Maximum auto close delay in milliseconds. Defaults to 0 (unlimited). When set, duration is clamped to this value.
ewMessage({ content: "At most 5s", duration: 10000, maxDuration: 5000 });
21. onShow
Fired after the message is shown. Receives { type, content }.
ewMessage({
content: "Callback on show",
onShow: ({ type }) => console.log(type),
});
22. onClose
Fired after the message is closed. Receives { type, content, reason }:
| reason | Description |
|---|---|
timeout | Closed by the timer |
manual | Closed by button, keyboard, click or manual close |
destroy | Closed by destroy() |
maxCount | Evicted by maxCount |
ewMessage({
content: "Callback on close",
onClose: ({ reason }) => console.log(reason),
});
onClosefires only once per message.
Theming
23. prefix
Prefix for classes, icons, animations and CSS variables. Defaults to "ew-". A trailing - is appended when missing.
ewMessage({ content: "Custom prefix", prefix: "abc-" });
Runtime only switches class names. Rebuild the SCSS with a matching
$prefixfor full styles.
CSS variables
Styles are driven by CSS variables:
:root {
--ew-message-min-width: 320px;
--ew-message-radius: 8px;
--ew-message-z-index: 2000;
--ew-message-bg: #fff;
--ew-message-border-color: #e5e6eb;
--ew-message-color: #1d2129;
}
| Variable | Default |
|---|---|
--ew-message-min-width | 300px |
--ew-message-padding | 10px 15px |
--ew-message-radius | 4px |
--ew-message-font-size | 14px |
--ew-message-z-index | 1000 |
--ew-message-offset | 25px |
--ew-message-horizontal-offset | 25px |
--ew-message-icon-size | 1em |
--ew-message-bg | #B3E5FC |
--ew-message-border-color | #81D4FA |
--ew-message-color | #039BE5 |
success, warning, error and loading override the background, border and text color variables.
Accessibility
- Normal messages use
role="status"andaria-live="polite";errorusesrole="alert"andaria-live="assertive" - The close button has
role="button",tabindex="0"and anaria-label - Enter / Space closes the message
- Icons use
aria-hidden="true"
SSR
The module does not access document, so it can be imported safely in Node / SSR. The container is resolved only when a message is created; without document, a clear error is thrown:
import ewMessage from "ew-message";
// The following throws "document is not available" in Node
ewMessage("hello");
Root exports
import ewMessage, {
Message,
ewMessageEnumType,
type ewMessageOption,
type ewMessageResolvedOption,
type ewMessageInfo,
type ewMessageCloseInfo,
type ewMessageCloseReason,
type ewMessageContent,
type ewMessagePosition,
type ewMessageType
} from "ew-message";
ewMessage.clearAll(container?) and Message.clearAll(container?) are also available.