Skip to content
Vangware
GitHubLinkedInYouTube

Window Open Promise by Vangware

Coverage License NPM Version Open Issues

πŸͺŸ Promised Window.open();.

Usage

πŸ“¦ Node

Install @vangware/window-open-promise as a dependency:

pnpm add @vangware/window-open-promise
# or
npm install @vangware/window-open-promise
# or
yarn add @vangware/window-open-promise

Import it and use it:

import { windowOpenPromise } from "@vangware/window-open-promise";

const windowOpen = windowOpenPromise(globalThis);

windowOpen({
	url: "https://example.com", // URL is not required, you can open a blank window
	top: 10,
	left: 10,
})
	.then(newWindow => {
		newWindow.console.log("This will log in the new window.");
		newWindow.addEventListener("beforeunload", _event => {
			console.log("This will log when the new window is closed.");
		});
	})
	.catch(_error => {
		console.error("This will log if the new window can't be opened.");
	});

πŸ¦• Deno

Import @vangware/window-open-promise using the npm: prefix, and use it directly:

import { windowOpenPromise } from "npm:@vangware/window-open-promise";

const windowOpen = windowOpenPromise(globalThis);

try {
	const newWindow = await windowOpen({
		url: "https://example.com", // URL is not required, you can open a blank window
		top: 10,
		left: 10,
	});
	newWindow.console.log("This will log in the new window.");
	newWindow.addEventListener("beforeunload", _event => {
		console.log("This will log when the new window is closed.");
	});
} catch (_error) {
	console.error("This will log if the new window can't be opened.");
}

🌎 Browser

Import @vangware/window-open-promise using esm.sh, and use it directly:

<script type="module">
	import { windowOpenPromise } from "https://esm.sh/@vangware/window-open-promise";

	const windowOpen = windowOpenPromise(globalThis);

	try {
		const newWindow = await windowOpen({
			url: "https://example.com", // URL is not required, you can open a blank window
			top: 10,
			left: 10,
		});
		newWindow.console.log("This will log in the new window.");
		newWindow.addEventListener("beforeunload", _event => {
			console.log("This will log when the new window is closed.");
		});
	} catch (_error) {
		console.error("This will log if the new window can't be opened.");
	}
</script>

Type Aliases

WindowOpenPromiseFeatures

Ζ¬ WindowOpenPromiseFeatures: Omit<WindowOpenPromiseOptions, "replace" | "target" | "url">

WindowOpenPromise features.

View source


WindowOpenPromiseOptions

Ζ¬ WindowOpenPromiseOptions: Object

WindowOpenPromise options.

Type declaration

NameTypeDescription
height?numberWindowOpenPromise height (minimum 100).
left?numberWindowOpenPromise left position.
menuBar?booleanWindowOpenPromise renders the menu bar.
noOpener?booleanWindowOpenPromise can’t access it’s opener.
resizable?booleanWindowOpenPromise is resizable.
scrollbars?booleanWindowOpenPromise has scrollbars.
target?stringWindowOpenPromise target.
titleBar?booleanWindowOpenPromise renders the title bar.
toolBar?booleanWindowOpenPromise renders the tool bar.
top?numberWindowOpenPromise top position.
url?stringWindowOpenPromise url.
width?numberWindowOpenPromise width (minimum 100).

View source

Functions

featureJoin

β–Έ featureJoin<Iterable_1>(iterable): ReducerOutput<Iterable_1, string>

Joins given array of features with the FEATURE_SEPARATOR.

Type parameters

NameType
Iterable_1extends IsomorphicIterable

Parameters

NameType
iterableIterable_1

Returns

ReducerOutput<Iterable_1, string>

Joint string.

Example

featureJoin(["top=10", "left=10", "resizable=1"]); // "top=10,left=10,resizable=1"

View source


featureMap

β–Έ featureMap<Iterable_1>(iterable): Iterable_1 extends { [asyncIterator]: () => AsyncIterator<EntryOf<WindowOpenPromiseFeatures>, any, undefined> } ? { [asyncIterator]: () => AsyncIterableIterator<string> ; next: (…args: [] | [undefined]) => Promise<IteratorResult<string, any>> ; return?: (value?: any) => Promise<IteratorResult<string, any>> ; throw?: (e?: any) => Promise<IteratorResult<string, any>> } : { [iterator]: () => IterableIterator<string> ; next: (…args: [] | [undefined]) => IteratorResult<string, any> ; return?: (value?: any) => IteratorResult<string, any> ; throw?: (e?: any) => IteratorResult<string, any> }

Maps array of feature entries to valid values.

Type parameters

NameType
Iterable_1extends IsomorphicIterable<EntryOf<WindowOpenPromiseFeatures>>

Parameters

NameType
iterableIterable_1

Returns

Iterable_1 extends { [asyncIterator]: () => AsyncIterator<EntryOf<WindowOpenPromiseFeatures>, any, undefined> } ? { [asyncIterator]: () => AsyncIterableIterator<string> ; next: (…args: [] | [undefined]) => Promise<IteratorResult<string, any>> ; return?: (value?: any) => Promise<IteratorResult<string, any>> ; throw?: (e?: any) => Promise<IteratorResult<string, any>> } : { [iterator]: () => IterableIterator<string> ; next: (…args: [] | [undefined]) => IteratorResult<string, any> ; return?: (value?: any) => IteratorResult<string, any> ; throw?: (e?: any) => IteratorResult<string, any> }

Array of formatted features.

Example

featureMap([
	["top", "10"],
	["left", "10"],
	["resizable", true],
]); // ["top=10", "left=10", "resizable=1"]

View source


featureParser

β–Έ featureParser(features?): string

Parses features object into features string.

Parameters

NameTypeDescription
featuresWindowOpenPromiseFeaturesFeatures object.

Returns

string

Parsed string.

Example

featureParser({
	top: 10,
	left: 10,
	resizable: true,
}); // "top=10,left=10,resizable=1"

View source


windowOpenPromise

β–Έ windowOpenPromise(window): (options: WindowOpenPromiseOptions) => Promise<Window>

Promised Window.open.

Parameters

NameTypeDescription
windowReadonly<Pick<Window, "open">>Window object (or maybe a mock
).

Returns

fn

Curried function with window in context.

β–Έ (options?): Promise<Window>

Curried function with window set.

Parameters
NameTypeDescription
optionsWindowOpenPromiseOptionsWindowOpenPromise options.
Returns

Promise<Window>

Promise with new window.

Example

const windowOpen = windowOpenPromise(window);
windowOpen({
	url: "https://example.com",
	top: 10,
	left: 10,
})
	.then(newWindow => {
		newWindow.console.log("This will log in the new window.");
		newWindow.addEventListener("beforeunload", _event => {
			console.log("This will log when the new window is closed.");
		});
	})
	.catch(_error => {
		console.error("This will log if the new window can't be opened.");
	});

Example

const windowOpen = windowOpenPromise(window);
windowOpen({
	url: "https://example.com",
	top: 10,
	left: 10,
})
	.then(newWindow => {
		newWindow.console.log("This will log in the new window.");
		newWindow.addEventListener("beforeunload", _event => {
			console.log("This will log when the new window is closed.");
		});
	})
	.catch(_error => {
		console.error("This will log if the new window can't be opened.");
	});

View source