nwjs-dialog v1.1.0
nwjs-dialog
File dialog and message box for NW.js.

Install
npm install nwjs-dialogOpen Dialog
Shows an open dialog.
dialog.showOpenDialog([win, ]options)
win(optional)options(optional)title/nwdirectorydescstring (optional) - Dialog title.defaultPath/nwworkingdirstring (optional) - Default pathopenDirectory/nwdirectoryboolean (optional) - Select directory,falseby defaultmultipleboolean (optional) - Allow multiple files to be selected,falseby defaultacceptstring (optional) - Defines the file types should acceptreturnFormatstring (optional) - Return format,"string"or"file","string"by default
Returns Promise<string[] | File[]> - Resolve with an array of files or file paths chosen by the user.
The win argument allows the dialog to attach itself to a parent window, making it modal.
multiple and accept have no effect when openDirectory (nwdirectory) is true.
title (nwdirectorydesc) valid only when openDirectory (nwdirectory) is true.
const { showOpenDialog } = require("nwjs-dialog");
showOpenDialog().then(([filePath]) => {
console.log(filePath);
});
showOpenDialog({
title: "Dialog title",
defaultPath: process.cwd(),
openDirectory: false,
multiple: true,
accept: "audio/*,video/*,image/*,.txt",
returnFormat: "string",
}).then((filePaths) => {
console.log(filePaths);
});Save Dialog
Shows a save dialog.
dialog.showSaveDialog([win, ]options)
win(optional)options(optional)defaultPath/nwworkingdirstring (optional) - Default pathfilename/nwsaveasstring (optional) - Default filename for savingacceptstring (optional) - Defines the file types should acceptreturnFormatstring (optional) - Return format,"string"or"file","string"by default
Returns Promise<string[] | File[]> - Resolve with an array of files or file paths chosen by the user.
The win argument allows the dialog to attach itself to a parent window, making it modal.
When filename (nwsaveas) is omitted, the filename defaults to empty.
const fs = require("fs");
const { showSaveDialog } = require("nwjs-dialog");
showSaveDialog().then(([filePath]) => {
fs.writeFileSync(filePath, "Hello");
});
showSaveDialog({
defaultPath: process.cwd(),
filename: "1.txt",
accept: "audio/*,video/*,image/*,.txt",
returnFormat: "string",
}).then(([filePath]) => {
fs.writeFileSync(filePath, "Hello");
});Message Box
Shows a message box.
The first call will create a MessageBox directory in the working directory to store temporary html files and icon files.
const { showMessageBox } = require("nwjs-dialog");
showMessageBox({ message: "Message" });
showMessageBox({ message: "Something is wrong", type: "error" });dialog.showMessageBox([win, ]options)
win(optional) - Iftitleoption is omitted, it will be the win's title.optionsmessagestring - Content of the message boxtypestring (optional) - Can be "none", "info", "error", "question", "warning" or "success"buttonsstring - Array of texts for buttonstitlestring (optional) - Title of the message boxdetailstring (optional) - Extra information of the messagecheckboxLabelstring (optional) - If provided, the message box will include a checkbox with the given labelcheckboxCheckedboolean (optional) - Initial checked state of the checkbox.falseby defaulticonstring (optional) - Custom iconidstring (optional) - Message box window idplatformstring (optional) - Message box style. "default", "win32" or "darwin". Follow the os bydefaultcustomStylestring (optional) - Custom style for message box elementsinputOptionsobject (optional) - User input optionsonLoadfunction(win) => void(optional) - Called when the message box was loaded.onClosefunction(returnValue, win) => Promise<boolean|void>(optional) - Called before the message box closes, returnfalsewill prevents the window close.onValidatefunction(errors, win) => void(optional) - Called after input option validate, contains all input options errors, if everything is correct, it will be an empty array.
Returns Promise<MessageBoxReturnValue> - Resolve with an object containing the following:
buttonnumber - The index of the clicked button.-1if close button clicked.checkboxCheckedboolean - The checked state of the checkbox ifcheckboxLabelwas set.inputDataobject - Data of theinputOptionsradiosnumber - The index of the checked radio of theinputOptions.radioscheckboxesboolean[] - The checked states of theinputOptions.checkboxesinputsstring[] - The values of theinputOptions.inputs
const { showMessageBox } = require("nwjs-dialog");
showMessageBox({
message: "Message",
type: "info",
buttons: ["OK", "Cancel"],
title: "Title",
detail: "Detail",
checkboxLabel: "Checkbox",
checkboxChecked: true,
icon: "",
platform: "default",
customStyle: ".win32 .button:first-child { border-color: #0078d7; }",
}).then(({ button, checkboxChecked }) => {
console.log(button);
console.log(checkboxChecked);
});Custom Style
Main elements's class names:
/* .header */
.header-icon {}
.title {}
/* .body */
.body-icon {}
.message {}
.detail {}
/* .input-options */
.checkbox-label {}
.checkbox-input {}
.radio-label {}
.radio-input {}
.input-label {}
.input-input {}
.description {}
/* .footer */
.button {}
/* platforms */
.win32 * {}
.darwin * {}
/* types */
.none {}
.info {}
.question {}
.warning {}
.error {}
.success {}Input Options
You can add simple input options in message box.
inputOptions object (optional) - User input options
radiosobjectlabelstring (optional) - Text for the item's label.valueboolean (optional) - Default value for the item.descriptionstring (optional) - Description for the item.
checkboxesobjectlabelstring (optional) - Text for the item's label.valueboolean (optional) - Default value for the item.descriptionstring (optional) - Description for the item.requiredboolean (optional) - Whether the checkbox must be checked.
inputsobjectlabelstring (optional) - Text for the item's label.valuestring (optional) - Default value for the item.descriptionstring (optional) - Description for the item.placeholderstring (optional) - Placeholder for input.passwordstring (optional) - Set to password input.requiredboolean (optional) - Whether the input is required.ruleRegExp |(value: string) => boolean(optional) - Validation rule for the input. e.g./^\d+$/
const { showMessageBox } = require("nwjs-dialog");
showMessageBox({
title: "Input Options",
inputOptions: {
radios: [
{ label: "Radio 1" },
{ label: "Radio 2", value: true, description: "Description" },
],
checkboxes: [
{ label: "Checkbox 1", description: "Description" },
{ label: "Checkbox 2", value: true },
],
inputs: [
{
label: "Input 1",
placeholder: "Placeholder",
description: "Description",
},
{
label: "Input 2",
value: "123456",
password: true,
description: "Password Input",
},
],
},
}).then(({ button, inputData }) => {
if (button === 0) {
const { radios, checkboxes, inputs } = inputData;
console.log({ radios, checkboxes, inputs });
}
});inputOptions validators
const { showMessageBox } = require("nwjs-dialog");
showMessageBox({
title: "Simple Inputs Validators",
inputOptions: {
checkboxes: [
{ label: "Checkbox", required: true, description: "Required" },
],
inputs: [
{ label: "Input 1", value: "", required: true, description: "Required" },
{
label: "Input 2",
value: "abc",
rule: /^[0-9]+$/,
description: "A RegExp rule. This input can only have numbers.",
},
{
label: "Input 2",
value: "Value",
rule: (value) => value === "ssnangua",
description: "A function rule. This input must be `ssnangua`.",
},
],
},
// Called after input option validate, contains all input options errors, if everything is correct, it will be an empty array.
onValidate(errors, win) {
win.window.document
.querySelector(".button:first-child")
.classList[errors.length > 0 ? "add" : "remove"]("disabled");
},
});Example: Login Window
Here is a simple login window example:
const { showMessageBox } = require("nwjs-dialog");
const customStyle = `.message {
margin: 0 auto;
}
.button {
border: 1px solid #dcdfe6;
border-radius: 4px;
padding: 2px 8px;
}
.darwin .button {
padding: 4px 8px;
}
.button:first-child {
background: #409eff;
color: #fff;
}
.button:first-child:hover {
background: #66b1ff;
}
.button:last-child {
background: #fff;
border-color: #dcdfe6;
}
.button:last-child:hover {
background: #ecf5ff;
border-color: #c6e2ff;
color: #409eff;
}`;
showMessageBox({
id: "loginWin",
message: "Awesome Web App",
type: "none",
buttons: ["Login", "Cancel"],
title: "Login",
checkboxLabel: 'Agree to the <a href="#" target="_blank">User Agreement</a>.',
checkboxChecked: true,
icon: "./images/user.png",
customStyle,
inputOptions: {
inputs: [
{ label: "User", placeholder: "Nickname / Email" },
{ label: "Password", value: "", password: true },
],
checkboxes: [{ label: "Remenber me" }],
},
onClose({ button, checkboxChecked, inputData }, win) {
if (button === 0) {
const [user, password] = inputData.inputs;
if (!user || !password) {
win.window.alert("User and password cannot be empty.");
// You can modify the elements of message box, too.
// win.window.document.querySelector(".message").style.color = "red";
return false;
} else if (!checkboxChecked) {
win.window.alert("You need to agree to the User Agreement.");
return false;
} else {
// Can return a promise and you can send network requests to validate the data
return new Promise((resolve) => {
setTimeout(() => {
if (user !== "ssnangua") {
win.window.alert("Incorrect user or password.");
resolve(false);
} else {
resolve(true);
}
}, 1000);
});
}
}
},
}).then(({ button, inputData }) => {
if (button === 0) {
const [user, password] = inputData.inputs;
const [remenberMe] = inputData.checkboxes;
console.log(user, password, remenberMe);
}
});NOTE
If the message box was opened through another window, the resolve handler will be lost when the window is closed.
So if you need to do some important processing on user input, you should open the message box in bg-script.
Or you can make the window uncloseable until the message box is closed:
let msgbox = true;
showMessageBox({
message: "A modal message box",
}).then(() => (msgbox = false));
const curWin = nw.Window.get();
curWin.on("close", () => {
if (!msgbox) curWin.close(true);
});Or use with nwjs-window-manager:
npm install nwjs-window-managerconst { wm } = require("nwjs-window-manager");
showMessageBox({
message: "A modal message box",
onLoad(win) {
wm(win, {
parent: nw.Window.get(),
modal: true,
});
},
});Change Log
1.0.5
- Fix: use
nw.require(...)instead ofimport ... from ...to importpathandfs.
1.0.6
- Feature:
showOpenDialogandshowSaveDialogaddreturnFormatoption.
1.1.0
- Feature: add alias:
nwdirectorydesc->title,nwworkingdir->defaultPath,nwdirectory->openDirectory,nwsaveas->filename - Fix: header-icon use base64 image