Elegant Notice Bar With Progress Bar – notice.js

Category: Javascript , Notification | September 28, 2026
Authoralihesari
Last UpdateSeptember 28, 2026
LicenseMIT
Tags
Views1,044 views
Elegant Notice Bar With Progress Bar – notice.js

notice.js is a dependency-free JavaScript toast notification library for stackable status messages with optional countdown progress bars.

It ships as browser, ESM, and CommonJS builds with TypeScript definitions, and the API works through direct status helpers or configurable NoticeJs instances.

Features

  • Success, info, warning, and error notification types.
  • 11 screen positions, including full-width top and bottom bars.
  • Timed or persistent notices with optional countdown progress bars.
  • Automatic timer pausing on hover, keyboard focus, and hidden browser tabs.
  • Configurable button, click, and touch or pen swipe dismissal, plus Escape from a focused notice.
  • Promise notices for loading, success, and error states.
  • Action buttons with optional custom handlers.
  • Programmatic updating, closing, pausing, resuming, and deduplication by ID.
  • Configurable notice limits per position.
  • Modal notices, RTL layout, and custom open or close animation classes.
  • Plain-text rendering by default with opt-in HTML and a custom sanitizer hook.
  • ARIA live regions, modal focus handling, and reduced-motion behavior.
  • Theme customization through CSS custom properties.

How to use it

Direct browser setup

Load the stylesheet before the browser build:

<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/notice.js@1/dist/noticejs.css">
<script src="https://cdn.jsdelivr.net/npm/notice.js@1/dist/notice.js"></script>

Use one of the static helpers for a basic toast:

NoticeJs.success('Profile saved');

Create an instance when the notice needs custom content or behavior:

const notice = new NoticeJs({
  title: 'Saved',
  text: 'Your changes are live.',
  type: 'success',
  position: 'topRight',
  timeout: 5000
}).show();

npm and ESM

npm install notice.js
import { NoticeJs } from 'notice.js';
import 'notice.js/noticejs.css';
NoticeJs.info('New version available');

Notification types

The four built-in type values are success, info, warning, and error. Static helpers are available for each type:

NoticeJs.success('Saved');
NoticeJs.info('New message');
NoticeJs.warning('Storage is almost full');
NoticeJs.error('Upload failed', { title: 'Error' });

Promise notifications

NoticeJs.promise() creates a loading notice and updates it when the promise resolves or rejects. It returns the original promise.

const profilePromise = NoticeJs.promise(saveProfile(), {
  loading: 'Saving profile...',
  success: 'Profile saved',
  error: (error) => ({
    title: 'Save failed',
    text: error.message
  })
});

Action buttons

new NoticeJs({
  text: 'Email archived',
  actions: [
    {
      label: 'Undo',
      onClick: () => restoreEmail()
    },
    {
      label: 'Details',
      onClick: () => openDetails(),
      closeOnClick: false
    }
  ]
}).show();

Action fields

Action fieldDescription
labelstring, required. Button label.
onClickfunction, optional. Receives the notice instance and the click event.
classNamestring, optional. Extra class names for the action button.
closeOnClickboolean, default true. Closes the notice after the action handler runs.

Update, close, and deduplicate notices

Use a stable id to update an existing notice and avoid duplicate stacking.

const uploadNotice = new NoticeJs({
  id: 'upload-status',
  text: 'Uploading...',
  timeout: false,
  loading: true
}).show();
uploadNotice.update({
  text: 'Upload complete',
  type: 'success',
  loading: false,
  timeout: 3000
});
uploadNotice.close();
NoticeJs.get('upload-status')?.close();
NoticeJs.closeAll();
NoticeJs.closeAll('bottomRight');

Global defaults

NoticeJs.overrideDefaults({
  position: 'bottomRight',
  timeout: 5000
});
NoticeJs.resetDefaults();

Configuration options

new NoticeJs(options?) accepts one optional NoticeOptions object. Omitted values use the current global defaults.

Content and type

OptionType, default, and description
idstring, auto-generated. Stable identifier used for deduplication and NoticeJs.get().
titlestring, default ''. Heading text.
textstring, default ''. Plain-text message rendered with textContent.
htmlstring | Node | null, default null. Rich content used in place of text. String content uses the sanitize hook when one is set.
sanitize(html: string) => string | TrustedHTML, default null. Receives HTML strings before insertion. Supply a sanitizer for untrusted markup.
type'success' | 'info' | 'warning' | 'error', default 'success'. Controls the status styling and default live-region behavior.
loadingboolean, default false. Displays a spinner for pending work.
classNamestring, default ''. Extra class names applied to the notice element.

Timing and dismissal

OptionType, default, and description
timeoutnumber | false, default 3000. Total display time in milliseconds. Use false for a persistent notice.
progressBarboolean, default true. Displays the countdown progress bar.
closeWith('button' | 'click' | 'swipe')[], default ['button', 'swipe']. Selects close-button, whole-notice click, and touch or pen swipe dismissal. Escape can dismiss a focused notice.
pauseOnHoverboolean, default true. Pauses the countdown when the pointer is over the notice or focus is inside it.
pauseOnFocusLossboolean, default true. Pauses the countdown when the browser tab is hidden.

Position, layout, and stacking

OptionType, default, and description
positionNoticePosition, default 'topRight'. Selects one of the 11 documented positions.
widthnumber | string | null, default 320. A number is interpreted as pixels. A string can use any CSS length. null uses the stylesheet width. The full-width top and bottom positions ignore this value.
maxHeightnumber | null, default null. Maximum body height in pixels before the content scrolls.
newestOnTopboolean, default false. Inserts newer notices before existing notices in the position container.
maxVisiblenumber | null, default null. Limits visible notices at the selected position. Older notices close first.
modalboolean, default false. Dims the page and moves focus to the notice.
rtlboolean, default false. Uses right-to-left layout.
animation{ open?, close? } | null, default null. CSS class names applied for enter and exit animations.

Actions, accessibility, and callbacks

OptionType, default, and description
actionsNoticeAction[], default []. Each action requires label and accepts onClick, className, and closeOnClick. closeOnClick defaults to true.
ariaLive'polite' | 'assertive' | 'off'. Defaults to assertive for warning and error notices and polite for success and info notices.
callbacks{ [event]: fn | fn[] }, default {}. Registers one function or an array of functions for each documented lifecycle callback.

Available positions

top and bottom create full-width bars and display one notice at a time.

ValueLocation
topFull-width top bar.
topLeftTop left.
topCenterTop center.
topRightTop right.
middleLeftMiddle left.
middleCenterMiddle center.
middleRightMiddle right.
bottomFull-width bottom bar.
bottomLeftBottom left.
bottomCenterBottom center.
bottomRightBottom right.

Instance API

position and modal cannot change after a notice has been shown. Set those options before show().

MemberDescription
show()Renders the notice and returns the instance. If a visible notice already uses a matching id, the existing notice is updated and returned.
update(options)Updates content or behavior, re-renders a visible notice, and restarts its countdown.
close()Closes the notice and returns a promise that resolves after removal.
pause()Pauses the countdown.
resume()Resumes a countdown paused through the API.
on(event, fn)Registers a callback and returns the instance.
off(event, fn?)Removes one callback or all callbacks registered for the event and returns the instance.
elementThe notice HTMLElement after show(), otherwise null.
idThe stable notice ID.
optionsThe resolved options for the instance.
isVisibleBoolean state for a visible notice.

Static API

MemberDescription
NoticeJs.defaultsReads the current global defaults.
NoticeJs.overrideDefaults(options)Changes defaults used by notices created afterward.
NoticeJs.resetDefaults()Restores the built-in defaults.
NoticeJs.get(id)Returns the visible notice with the supplied ID, if present.
NoticeJs.closeAll(position?)Closes all visible notices, or notices at one position. Returns a promise.
NoticeJs.success(text, options?)Shows a success notice.
NoticeJs.error(text, options?)Shows an error notice.
NoticeJs.warning(text, options?)Shows a warning notice.
NoticeJs.info(text, options?)Shows an info notice.
NoticeJs.promise(input, messages, options?)Shows a loading notice and updates it when a promise resolves or rejects. Returns the original promise.

NoticeJs.promise() parameters

input accepts a promise or a function that returns a promise. options accepts the regular notice options used for the loading notice.

Message fieldDescription
loadingA string or NoticeOptions object for the pending state.
successA string, NoticeOptions object, or function that receives the resolved value and returns either form.
errorA string, NoticeOptions object, or function that receives the rejected value and returns either form.

Callbacks

Each callback receives the notice instance as its first argument and as this.

CallbackRuns when
beforeShowBefore the notice is inserted into its container.
onShowAfter the notice is inserted and marked visible.
afterShowAfter the open animation finishes.
onCloseWhen closing begins.
afterCloseAfter the notice has been removed.
onClickWhen the notice receives a click.
onHoverWhen the pointer enters the notice.
new NoticeJs({
  text: 'Hello',
  callbacks: {
    afterClose: (notice) => {
      console.log(`${notice.id} closed`);
    }
  }
}).show();

Theme customization

Set the library’s CSS custom properties on :root to change the default theme:

:root {
  --noticejs-success-bg: #0f766e;
  --noticejs-success-accent: #115e59;
  --noticejs-radius: 12px;
  --noticejs-width: 360px;
  --noticejs-font-family: Inter, sans-serif;
}

Layout and shared theme variables

VariableDefault and purpose
--noticejs-font-familySystem UI font stack. Notice font family.
--noticejs-font-size14px. Base font size.
--noticejs-width320px. Default notice width.
--noticejs-offset12px. Distance from viewport edges.
--noticejs-gap10px. Space between stacked notices.
--noticejs-padding10px 12px. Heading and body padding.
--noticejs-radius6px. Border radius.
--noticejs-shadow0 6px 20px rgb(0 0 0 / 0.18). Notice box shadow.
--noticejs-z-index10050. Notice container z-index.
--noticejs-text#fff. Notice text color.
--noticejs-progress-height4px. Countdown bar height.
--noticejs-focus-ring0 0 0 3px rgb(255 255 255 / 0.85). Focus ring shadow.

Status and modal theme variables

VariableDefault and purpose
--noticejs-success-bg#1f7a3d. Success background.
--noticejs-success-accent#176632. Success accent.
--noticejs-info-bg#0b62c4. Info background.
--noticejs-info-accent#0a4f9e. Info accent.
--noticejs-warning-bg#b54708. Warning background.
--noticejs-warning-accent#933a06. Warning accent.
--noticejs-error-bg#c62828. Error background.
--noticejs-error-accent#9f1f1f. Error accent.
--noticejs-modal-bg#000. Modal overlay color.
--noticejs-modal-opacity0.35. Modal overlay opacity.
--noticejs-modal-z-index10000. Modal overlay z-index.

Upgrading from 0.5.x

  • timeout is now the total display time in milliseconds. An old value of 30 becomes 3000.
  • text is always plain text. Use html for markup and a sanitizer for untrusted HTML strings.
  • show() returns the NoticeJs instance. Read the rendered element from notice.element.
  • The old scroll option is removed. Use maxHeight.
  • The documented callback list contains seven lifecycle callbacks. The old onTemplate entry is no longer part of the public callback API.
  • The default closeWith value is now ['button', 'swipe'].
  • The package ships plain CSS and no longer ships SCSS source files.
  • IE11 is no longer supported. The JavaScript build targets ES2020.

Alternatives & Related Resources

Changelog

v1.0.0 (09/26/2026)

  • Rewritten in TypeScript with ESM, CommonJS, browser, and bundled type-definition outputs.
  • Introduced promise notices, actions, update and close methods, ID deduplication, position limits, timer pausing, swipe dismissal, HTML sanitization hooks, and CSS custom properties.
  • Added ARIA live regions, an accessible close button, Escape dismissal, modal focus handling, and reduced-motion behavior.
  • Changed timeout semantics, text rendering, show return value, default dismissal methods, and browser baseline.
  • Removed the old scroll option and SCSS package sources.

v0.5.0 (11/02/2025)

  • Modernized the build setup and improved callback handling and documentation.

You Might Be Interested In:


Leave a Reply