0.0.10 • Published 9 months ago

html-dialog v0.0.10

Weekly downloads
-
License
GPL-3.0
Repository
github
Last release
9 months ago

html-dialog

a simple but flexible html dialog in javascript for any frontend. based on the html dialog element.

size of the minified module: 1.8kb

dialog installation & usage

installation in browser

<script src="dist/html-dialog.min.js"></script>
<script>
    var dialog = HtmlDialog.Dialog({
        title: 'Dialog Title',
        content: 'Dialog Content',
        buttons: [
            {
                text: 'OK',
                onclick: function () {
                    console.log('OK');
                }
            },
            {
                text: 'Cancel',
                onclick: function () {
                    console.log('Cancel');
                }
            }
        ]
    }).create();

    dialog.open();
</script>

For more examples, see the example.html file.

installation with npm

install with npm:

npm i html-dialog

then import the dialog:

import {Dialog} from 'html-dialog/dist/html-dialog.esm'

const myDialog = Dialog({...}).create()
myDialog.open()

For more examples, see the example.html file.

dialog methods

var dialog = HtmlDialog.Dialog({...});

dialog.create(dialogOptions);
dialog.open();
dialog.close();
dialog.destroy();
dialog.getDialog();
dialog.getForm();
methoddescriptionreturn
create(dialogOptions)adds dialog to DOMdialog instance
open()open dialogdialog instance
close()close dialogdialog instance
destroy()remove dialog from DOMvoid
getDialog()get dialog elementdialog element
getForm()get form elementform element inside dialog

.create()

You can pass an optional parameter to the create() method to specify the parent element.

var dialog = HtmlDialog.Dialog({...});
dialog.create({
    appendTo: document.getElementById('my-dialog-container')
});

dialog options parameter

HtmlDialog.Dialog({
    title: 'Dialog Title',
    content: 'Dialog Content',
    buttons: [
        // ...
    ],
    classNames: {
        // ...
    }
})
optiontypedefaultdescription
titlestringrequireddialog title
contentstringrequireddialog content, can be any html / string
buttonsarrayrequireddialog buttons, see "buttons parameter"
classNamesobject{}dialog css classnames, see "classNames parameter"

buttons parameter

{
    title: 'Dialog Title',
    content: 'Dialog Content',
    ...
    buttons: [
        {
            text: 'OK',
            type: 'button',
            focus: true,
            classNames: 'btn btn-primary',
            onclick: function (mouseevent) {
                console.log(mouseevent);
                this.close();
            }
        }
    ]
}
optiontypedefaultdescription
textstringrequiredbutton text
typestring'button'button type. can be submit, button, reset
focusbooleanfalsefocus button on dialog open
classNamesstring''button css classnames
onclickfunctionnullbutton callback
oncontextmenufunctionnullbutton callback
ondblclickfunctionnullbutton callback
...functionnullbutton callback

All valid mouse events are supported. See: https://www.w3schools.com/jsref/obj_mouseevent.asp

The callback function will be called with the dialog instance as this and the mouseevent as the first argument.

onclick: function (mouseevent) {
    console.log(mouseevent);
    this.close();
}

It's possible to add multiple callbacks to a button:

{
    text: 'OK',
    onclick: function () {
        console.log('onclick');
    },
    oncontextmenu: function () {
        console.log('contextmenu');
    }
}

classNames parameter

This option allows you to add custom css classnames to the dialog.

HtmlDialog.Dialog({
    title: 'Example',
    content: 'Hello World!',
    buttons: [
        // ...
    ],
    classNames: {
        dialog: 'dialog-class',
        title: 'title-class',
        content: 'content-class',
        buttons: 'buttons-class',
    }
});

The above code will result in the following html:

<dialog class="dialog-class">
    <form>
        <div class="title-class">Example</div>
        <div class="content-class">Hello World!</div>
        <div class="buttons-class">
            <button>OK</button>
            <button>Cancel</button>
        </div>
    </form>
</dialog>
optiontypedefaultdescription
dialogstring''dialog css classes
titlestring''title css classes
contentstring''content css classes
buttonsstring''title css

example styles with backdrop:

.dialog-class {
    border: 1px solid red;
    padding: 0;
}

.dialog-class::backdrop {
    background-color: aqua;
}

dialog / prompt example with text input and form validation

HtmlDialog.Dialog({
    title: 'Example',
    content: `<div>Please give me a value:</div><input type="text" name="value" required>`,
    buttons: [
        {
            text: 'OK',
            type: 'submit',
            onclick: function () {
                let value = this.getForm().querySelector('input[name="value"]').value;
                if (value) {
                    console.log(value);
                    this.close();
                }
            }
        },
        {
            text: 'Reset',
            type: 'reset',
        },
        {
            text: 'Cancel',
            onmouseup: function () {
                this.close();
            }
        }
    ]
}).create().open();
0.0.10

9 months ago

0.0.9

1 year ago

0.0.8

1 year ago

0.0.7

1 year ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.4

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago