0.1.12 • Published 3 years ago

@saleyn/js-dialog v0.1.12

Weekly downloads
-
License
APACHE 2
Repository
github
Last release
3 years ago

js-dialog

Custom draggable Alert, Prompt, Confirm dialogs in Javascript.

The default browser's dialogs provided by functions alert(), prompt(), and confirm() do not offer customization. This project provides a reasonable alternative.

Author

Serge Aleynikov

Demo

See test.html.

https://user-images.githubusercontent.com/272543/151256006-71a35826-6990-4011-a006-be3bb9437af9.mp4

Installation

  1. In your project add the following to your package.json:
{
  "dependencies": {
    "@saleyn/js-dialog": "^0.1"
  }
}
  1. Execute the following command to pull the package to your project's source tree:
$ npm install @saleyn/js-dialog
  1. If you are using esbuild or a similar bundling tool, add the following to import the dialog functions:
  • app.js:
import Dialog from "@saleyn/js-dialog"
  1. Run your javascript bundler (e.g. esbuild) to produce the app.min.js for your site. E.g.:
esbuild js/app.js --bundle --minify --outdir=/path/to/your/static/assets --sourcemap=external --source-root=js

Building minified sources

The following command will produce dist/dialog.min.* files to be used in your projects:

$ make

Usage

Add this markup to your HTML file. The div with dlg-window will be the placeholder of the dialog:

<head>
<script src="dialog.min.js"></script>
</head>
<body>
...
<div id="dlg-window"></div>
</body>

If the HTML page doesn't contain the element identified by dlg-window, a new element will be created under the body element on the page.

This dialog library supports dark and light themes, and will attempt to detect the current user's theme. The theme information is stored in the local storage and can be reset by making the following call, passing dark or light as the theme:

localStorage.setItem('dlg-theme-mode', theme)  // For setting the theme name

Dialog type: Confirm

To invoke a confirmation dialog, use:

Dialog.confirm(title, body, opts = {})
Dialog.confirm(title, body, action, opts = {})
ArgumentTypeDescription
titlestringTitle of the alert dialog box
bodystringInnerHTML of the dialog's body
actionfunctionAction (success) -> success to be called on closing of the dialog, where success is true if the default button was pressed
optsobjectConfiguration options
opts.actionfunctionSame as action above
opts.persistentbooleanWhen true - store dialog's position
opts.buttonsarrayArray of buttons to be displayed. Default: [{title: "Ok"}, {title: "Cancel"}]
opts.defbtnintegerIndex of the default button (default: 0)
opts.btnOkintegerIndex of the "Ok" button in opts.buttons
opts.themestringUse given color theme ('dark''light')

Example:

Dialog.confirm("Confirm action?", "Some custom body", (ok) => ok && alert('OK pressed!'))

Dialog type: Prompt

To invoke the prompt dialog, use:

Dialog.prompt(title, body, opts = {})
Dialog.prompt(title, body, action, opts = {})
ArgumentTypeDescription
titlestringTitle of the alert dialog box
bodystringInnerHTML of the dialog's body
actionfunctionAction (clickedButtonIndex, [{id: inputID, value: inputVal}]) -> {} to be called on success
optsobjectConfiguration options
opts.actionfunctionSame as action above
opts.persistentbooleanWhen true - store dialog's position
opts.inputsarrayArray of input fields to be displayed. Default: [{label: "Enter a value", id: "value"}]
opts.buttonsarrayArray of buttons to be displayed. Default: [{title: "Ok"}, {title: "Cancel"}]
opts.defbtnintegerIndex of the default button (default: 0)
opts.themestringUse given color theme ('dark''light')

Example:

Dialog.prompt("Data entry", "Type some text:", (btn_id, inputs) => btn_id==0 && alert('Entered: ' + inputs[0].value))

Dialog type: Alert

To display the alert dialog, do:

Alert.show(title, body, opts = {})
Alert.show(title, body, action, opts = {})
ArgumentTypeDescription
titlestringTitle of the alert dialog box
bodystringInnerHTML of the dialog's body
actionfunctionAction () -> {} to be called on pressing the dialog is closed
optsobjectConfiguration options
opts.actionfunctionSame as action above
opts.persistentbooleanWhen true - store dialog's position

Example:

Dialog.alert('Alert', 'Hello World')
Dialog.alert('Alert', 'Hello World', () => document.getElementById('test').innerHTML='Alerted!')
Dialog.alert('Alert', 'Hello World', {persistent: true})
  1. Call Dialog.dragElement(element, header, opts = {}) function to make an element draggable.
ArgumentTypeDescription
elementobject/stringA DOM object (or ID) to be made draggable
headerobject/stringA DOM header (or ID) of the element
optsobjectConfiguration options
opts.persistentstringID in the localStorage to save position
// <div id='box'><div id='header'>Title</div> ...</div>
const dlgbox = document.getElementById('box');
const header = document.getElementById('header');
dragElement(dlgbox, header, {persistent: 'my-window-position')

Configuring global dialog settings

There is a globally defined Dialog.Defaults variable which contains an object that controls default dialog setting

SettingDeafultDescription
persistentfalseSave dialog position to localStorage
persistentKey'dlg-theme-mode'Name of the persistent key in localStorage
className'dlg-window'Class name for the Dialog root element
transitiontrueEnable transition fade in/out effect
css{...}Object with CSS entries that are concatinated and assigned to the style of the dialog root element
themes{dark: ..., light: ...}Contains custom themes to be used by dialogs

Customization of colors and theming

The library supports creation of custom color themes as well as customization of the CSS.

To define a new color theme, add a node to Dialog.Defaults.themes object that will represent the new theme. That node should have colors entry, containing the theme's color variables. The variables are used to set color for different parts of the dialog. See Dialog.Defaults.themes.dark.colors for an example of the dark theme.

The following variables are supported:

Variable NameDescription
dlg-title-fg-colorTitle text color
dlg-title-bg-colorTitle background color
dlg-bg-colorDialog background color
dlg-body-fg-colorDialog body text color
dlg-body-bg-colorDialog body background color
dlg-input-fg-colorDialog input text color
dlg-input-bg-colorDialog input background color
dlg-input-outlineDialog input outline color
dlg-input-outline-focusDialog input focused outline color
dlg-btn-borderDialog button border
dlg-btn-fg-colorDialog button text color
dlg-btn-bg-colorDialog button background color
dlg-btn-hover-fg-colorDialog button hover text color
dlg-btn-hover-bg-colorDialog button hover background color
dlg-btn-def-fg-colorDialog default button text color
dlg-btn-def-bg-colorDialog default button background color
dlg-btn-def-hover-fg-colorDialog default button hover text color
dlg-btn-def-hover-bg-colorDialog default button hover background color

To customize CSS of a dialog, assign some members of the Dialog.Defaults.css object with your changes. All entries under Dialog.Defaults.css are concatenated and assigned to the dialog's style.

For Maintainers

Publishing project to npmjs.com:

$ make publish
0.1.12

3 years ago

0.1.11

3 years ago

0.1.10

3 years ago

0.1.9

3 years ago

0.1.8

3 years ago

0.1.7

3 years ago

0.1.6

3 years ago

0.1.5

3 years ago

0.1.4

3 years ago

0.1.3

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago