@primer/live-region-element
Advanced tools
Comparing version 0.6.1 to 0.7.0
import './define'; | ||
import { LiveRegionElement, templateContent, type AnnounceOptions } from './live-region-element'; | ||
type GlobalAnnounceOptions = AnnounceOptions & { | ||
/** | ||
* Provide an element into which the <live-region> element is appended | ||
*/ | ||
appendTo?: HTMLElement; | ||
/** | ||
* Provide an element that is used as the starting point when finding the | ||
* closest <live-region> element | ||
*/ | ||
from?: HTMLElement; | ||
}; | ||
/** | ||
* Announce a message using a live region with a corresponding politeness | ||
* level | ||
*/ | ||
export declare function announce(message: string, options?: GlobalAnnounceOptions): () => void; | ||
/** | ||
* Announce a message using the text content of an element using a live region | ||
* with a corresponding politeness level | ||
*/ | ||
export declare function announceFromElement(element: HTMLElement, options?: GlobalAnnounceOptions): () => void; | ||
export { LiveRegionElement, templateContent }; | ||
import { LiveRegionElement, templateContent } from './live-region-element'; | ||
import { announce, announceFromElement } from './global-announce'; | ||
export { announce, announceFromElement, LiveRegionElement, templateContent }; |
@@ -7,11 +7,11 @@ const Ordering = { | ||
var __accessCheck$1 = (obj, member, msg) => { | ||
var __accessCheck$2 = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet$1 = (obj, member, getter) => { | ||
__accessCheck$1(obj, member, "read from private field"); | ||
var __privateGet$2 = (obj, member, getter) => { | ||
__accessCheck$2(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd$1 = (obj, member, value) => { | ||
var __privateAdd$2 = (obj, member, value) => { | ||
if (member.has(obj)) | ||
@@ -21,4 +21,4 @@ throw TypeError("Cannot add the same private member more than once"); | ||
}; | ||
var __privateSet$1 = (obj, member, value, setter) => { | ||
__accessCheck$1(obj, member, "write to private field"); | ||
var __privateSet$2 = (obj, member, value, setter) => { | ||
__accessCheck$2(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
@@ -28,3 +28,3 @@ return value; | ||
var __privateMethod$1 = (obj, member, method) => { | ||
__accessCheck$1(obj, member, "access private method"); | ||
__accessCheck$2(obj, member, "access private method"); | ||
return method; | ||
@@ -35,18 +35,18 @@ }; | ||
constructor({ compareFn }) { | ||
__privateAdd$1(this, _heapifyDown); | ||
__privateAdd$1(this, _heapifyUp); | ||
__privateAdd$1(this, _compareFn, void 0); | ||
__privateAdd$1(this, _heap, void 0); | ||
__privateSet$1(this, _compareFn, compareFn); | ||
__privateSet$1(this, _heap, []); | ||
__privateAdd$2(this, _heapifyDown); | ||
__privateAdd$2(this, _heapifyUp); | ||
__privateAdd$2(this, _compareFn, void 0); | ||
__privateAdd$2(this, _heap, void 0); | ||
__privateSet$2(this, _compareFn, compareFn); | ||
__privateSet$2(this, _heap, []); | ||
} | ||
insert(value) { | ||
__privateGet$1(this, _heap).push(value); | ||
__privateGet$2(this, _heap).push(value); | ||
__privateMethod$1(this, _heapifyUp, heapifyUp_fn).call(this); | ||
} | ||
pop() { | ||
const item = __privateGet$1(this, _heap)[0]; | ||
if (__privateGet$1(this, _heap)[__privateGet$1(this, _heap).length - 1]) { | ||
__privateGet$1(this, _heap)[0] = __privateGet$1(this, _heap)[__privateGet$1(this, _heap).length - 1]; | ||
__privateGet$1(this, _heap).pop(); | ||
const item = __privateGet$2(this, _heap)[0]; | ||
if (__privateGet$2(this, _heap)[__privateGet$2(this, _heap).length - 1]) { | ||
__privateGet$2(this, _heap)[0] = __privateGet$2(this, _heap)[__privateGet$2(this, _heap).length - 1]; | ||
__privateGet$2(this, _heap).pop(); | ||
} | ||
@@ -57,18 +57,18 @@ __privateMethod$1(this, _heapifyDown, heapifyDown_fn).call(this); | ||
peek() { | ||
return __privateGet$1(this, _heap)[0]; | ||
return __privateGet$2(this, _heap)[0]; | ||
} | ||
delete(value) { | ||
const index = __privateGet$1(this, _heap).indexOf(value); | ||
const index = __privateGet$2(this, _heap).indexOf(value); | ||
if (index === -1) { | ||
return; | ||
} | ||
swap(__privateGet$1(this, _heap), index, __privateGet$1(this, _heap).length - 1); | ||
__privateGet$1(this, _heap).pop(); | ||
swap(__privateGet$2(this, _heap), index, __privateGet$2(this, _heap).length - 1); | ||
__privateGet$2(this, _heap).pop(); | ||
__privateMethod$1(this, _heapifyDown, heapifyDown_fn).call(this); | ||
} | ||
clear() { | ||
__privateSet$1(this, _heap, []); | ||
__privateSet$2(this, _heap, []); | ||
} | ||
get size() { | ||
return __privateGet$1(this, _heap).length; | ||
return __privateGet$2(this, _heap).length; | ||
} | ||
@@ -81,11 +81,11 @@ } | ||
let index = 0; | ||
while (hasLeftChild(index, __privateGet$1(this, _heap).length)) { | ||
while (hasLeftChild(index, __privateGet$2(this, _heap).length)) { | ||
let smallerChildIndex = getLeftChildIndex(index); | ||
if (hasRightChild(index, __privateGet$1(this, _heap).length) && __privateGet$1(this, _compareFn).call(this, rightChild(__privateGet$1(this, _heap), index), leftChild(__privateGet$1(this, _heap), index)) === Ordering.Less) { | ||
if (hasRightChild(index, __privateGet$2(this, _heap).length) && __privateGet$2(this, _compareFn).call(this, rightChild(__privateGet$2(this, _heap), index), leftChild(__privateGet$2(this, _heap), index)) === Ordering.Less) { | ||
smallerChildIndex = getRightChildIndex(index); | ||
} | ||
if (__privateGet$1(this, _compareFn).call(this, __privateGet$1(this, _heap)[index], __privateGet$1(this, _heap)[smallerChildIndex]) === Ordering.Less) { | ||
if (__privateGet$2(this, _compareFn).call(this, __privateGet$2(this, _heap)[index], __privateGet$2(this, _heap)[smallerChildIndex]) === Ordering.Less) { | ||
break; | ||
} else { | ||
swap(__privateGet$1(this, _heap), index, smallerChildIndex); | ||
swap(__privateGet$2(this, _heap), index, smallerChildIndex); | ||
} | ||
@@ -97,5 +97,5 @@ index = smallerChildIndex; | ||
heapifyUp_fn = function() { | ||
let index = __privateGet$1(this, _heap).length - 1; | ||
while (hasParent(index) && __privateGet$1(this, _compareFn).call(this, __privateGet$1(this, _heap)[index], parent(__privateGet$1(this, _heap), index)) === Ordering.Less) { | ||
swap(__privateGet$1(this, _heap), index, getParentIndex(index)); | ||
let index = __privateGet$2(this, _heap).length - 1; | ||
while (hasParent(index) && __privateGet$2(this, _compareFn).call(this, __privateGet$2(this, _heap)[index], parent(__privateGet$2(this, _heap), index)) === Ordering.Less) { | ||
swap(__privateGet$2(this, _heap), index, getParentIndex(index)); | ||
index = getParentIndex(index); | ||
@@ -137,2 +137,62 @@ } | ||
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
var __accessCheck$1 = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet$1 = (obj, member, getter) => { | ||
__accessCheck$1(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd$1 = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateSet$1 = (obj, member, value, setter) => { | ||
__accessCheck$1(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
return value; | ||
}; | ||
var _a, _promise, _resolve, _reject; | ||
class Deferred { | ||
constructor() { | ||
__publicField(this, _a, "Deferred"); | ||
__privateAdd$1(this, _promise, void 0); | ||
__privateAdd$1(this, _resolve, void 0); | ||
__privateAdd$1(this, _reject, void 0); | ||
__privateSet$1(this, _promise, new Promise((resolve, reject) => { | ||
__privateSet$1(this, _resolve, resolve); | ||
__privateSet$1(this, _reject, reject); | ||
})); | ||
} | ||
then(onfulfilled, onrejected) { | ||
return Promise.prototype.then.apply(__privateGet$1(this, _promise), [onfulfilled, onrejected]); | ||
} | ||
catch(onrejected) { | ||
return Promise.prototype.catch.apply(__privateGet$1(this, _promise), [onrejected]); | ||
} | ||
finally(onfinally) { | ||
return Promise.prototype.finally.apply(__privateGet$1(this, _promise), [onfinally]); | ||
} | ||
resolve(value) { | ||
__privateGet$1(this, _resolve).call(this, value); | ||
} | ||
reject(reason) { | ||
__privateGet$1(this, _reject).call(this, reason); | ||
} | ||
getPromise() { | ||
return __privateGet$1(this, _promise); | ||
} | ||
} | ||
_a = Symbol.toStringTag; | ||
_promise = new WeakMap(); | ||
_resolve = new WeakMap(); | ||
_reject = new WeakMap(); | ||
var __accessCheck = (obj, member, msg) => { | ||
@@ -214,3 +274,5 @@ if (!member.has(obj)) | ||
const now = Date.now(); | ||
const deferred = new Deferred(); | ||
const item = { | ||
deferred, | ||
politeness, | ||
@@ -222,4 +284,8 @@ contents: message, | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
return () => { | ||
__privateGet(this, _queue).delete(item); | ||
return { | ||
...deferred.getPromise(), | ||
cancel: () => { | ||
__privateGet(this, _queue).delete(item); | ||
deferred.resolve(); | ||
} | ||
}; | ||
@@ -236,3 +302,7 @@ } | ||
} | ||
return noop; | ||
const promise = Promise.resolve(); | ||
return { | ||
...promise, | ||
cancel: noop | ||
}; | ||
} | ||
@@ -282,3 +352,3 @@ getMessage(politeness = "polite") { | ||
} | ||
const timeout = message.scheduled > now ? message.scheduled - now : 0; | ||
const timeout = message.scheduled - now; | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
@@ -292,3 +362,3 @@ __privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, true); | ||
const { contents, politeness } = message; | ||
const { contents, deferred, politeness } = message; | ||
const container = this.shadowRoot?.getElementById(politeness); | ||
@@ -307,7 +377,14 @@ if (!container) { | ||
} | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
deferred.resolve(); | ||
if (this.delay > 0) { | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
__privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, false); | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
}, this.delay)); | ||
} else { | ||
__privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, false); | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
}, this.delay)); | ||
} | ||
}; | ||
@@ -373,3 +450,69 @@ function getTextContent(element) { | ||
function findOrCreateLiveRegion(from, appendTo) { | ||
function announce(message, options = {}) { | ||
let liveRegion = findLiveRegion(options.from); | ||
if (!liveRegion) { | ||
liveRegion = document.createElement("live-region"); | ||
if (options.appendTo) { | ||
options.appendTo.appendChild(liveRegion); | ||
} else { | ||
const container = getLiveRegionContainer(options.from); | ||
container.appendChild(liveRegion); | ||
} | ||
if (process.env.NODE_ENV === "test") { | ||
return liveRegion.announce(message, options); | ||
} | ||
let canceled = false; | ||
let cancel = () => { | ||
canceled = true; | ||
}; | ||
const promise = wait(REGISTER_WAIT_MS).then(() => { | ||
if (!canceled) { | ||
const result = liveRegion.announce(message, options); | ||
cancel = result.cancel; | ||
return result; | ||
} | ||
}); | ||
return { | ||
...promise, | ||
cancel: () => { | ||
cancel(); | ||
} | ||
}; | ||
} | ||
return liveRegion.announce(message, options); | ||
} | ||
function announceFromElement(element, options = {}) { | ||
let liveRegion = findLiveRegion(options.from); | ||
if (!liveRegion) { | ||
liveRegion = document.createElement("live-region"); | ||
if (options.appendTo) { | ||
options.appendTo.appendChild(liveRegion); | ||
} else { | ||
const container = getLiveRegionContainer(options.from); | ||
container.appendChild(liveRegion); | ||
} | ||
if (process.env.NODE_ENV === "test") { | ||
return liveRegion.announceFromElement(element, options); | ||
} | ||
let canceled = false; | ||
let cancel = () => { | ||
canceled = true; | ||
}; | ||
const promise = wait(REGISTER_WAIT_MS).then(() => { | ||
if (!canceled) { | ||
const result = liveRegion.announceFromElement(element, options); | ||
cancel = result.cancel; | ||
return result; | ||
} | ||
}); | ||
return { | ||
...promise, | ||
cancel: () => { | ||
cancel(); | ||
} | ||
}; | ||
} | ||
return liveRegion.announceFromElement(element, options); | ||
} | ||
function findLiveRegion(from) { | ||
let liveRegion = null; | ||
@@ -380,9 +523,3 @@ liveRegion = from ? getClosestLiveRegion(from) : null; | ||
} | ||
let container = document.body; | ||
if (from) { | ||
const dialog = from.closest("dialog"); | ||
if (dialog) { | ||
container = dialog; | ||
} | ||
} | ||
const container = getLiveRegionContainer(from); | ||
liveRegion = container.querySelector("live-region"); | ||
@@ -392,9 +529,3 @@ if (liveRegion !== null) { | ||
} | ||
liveRegion = document.createElement("live-region"); | ||
if (appendTo) { | ||
appendTo.appendChild(liveRegion); | ||
} else { | ||
container.appendChild(liveRegion); | ||
} | ||
return liveRegion; | ||
return null; | ||
} | ||
@@ -416,12 +547,19 @@ function getClosestLiveRegion(from) { | ||
} | ||
function announce(message, options = {}) { | ||
const liveRegion = findOrCreateLiveRegion(options.from, options.appendTo); | ||
return liveRegion.announce(message, options); | ||
function getLiveRegionContainer(from) { | ||
let container = document.body; | ||
if (from) { | ||
const dialog = from.closest("dialog"); | ||
if (dialog) { | ||
container = dialog; | ||
} | ||
} | ||
return container; | ||
} | ||
function announceFromElement(element, options = {}) { | ||
const liveRegion = findOrCreateLiveRegion(options.from, options.appendTo); | ||
return liveRegion.announceFromElement(element, options); | ||
const REGISTER_WAIT_MS = 150; | ||
function wait(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
export { LiveRegionElement, announce, announceFromElement, templateContent }; |
import { type Order } from './order'; | ||
import { Deferred } from './Deferred'; | ||
type Politeness = 'polite' | 'assertive'; | ||
@@ -27,7 +28,11 @@ type AnnounceOptions = { | ||
scheduled: number; | ||
deferred: Deferred; | ||
}; | ||
/** | ||
* A function to cancel a scheduled message. | ||
* A type that can be cancelled. Canceling will stop the action from completing | ||
* if it has not already completed. | ||
*/ | ||
type Cancel = () => void; | ||
type Cancelable<T> = T & { | ||
cancel: () => void; | ||
}; | ||
declare class LiveRegionElement extends HTMLElement { | ||
@@ -46,3 +51,3 @@ #private; | ||
*/ | ||
announce(message: string, options?: AnnounceOptions): Cancel; | ||
announce(message: string, options?: AnnounceOptions): Cancelable<Promise<void>>; | ||
/** | ||
@@ -52,3 +57,3 @@ * Announce a message using the text content of an element with a | ||
*/ | ||
announceFromElement(element: HTMLElement, options?: AnnounceOptions): Cancel; | ||
announceFromElement(element: HTMLElement, options?: AnnounceOptions): Cancelable<Promise<void>>; | ||
getMessage(politeness?: AnnounceOptions['politeness']): string | null; | ||
@@ -63,2 +68,2 @@ /** | ||
export { LiveRegionElement, templateContent }; | ||
export type { AnnounceOptions }; | ||
export type { AnnounceOptions, Cancelable }; |
@@ -9,11 +9,11 @@ import { HTMLElement, customElements } from '@lit-labs/ssr-dom-shim'; | ||
var __accessCheck$1 = (obj, member, msg) => { | ||
var __accessCheck$2 = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet$1 = (obj, member, getter) => { | ||
__accessCheck$1(obj, member, "read from private field"); | ||
var __privateGet$2 = (obj, member, getter) => { | ||
__accessCheck$2(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd$1 = (obj, member, value) => { | ||
var __privateAdd$2 = (obj, member, value) => { | ||
if (member.has(obj)) | ||
@@ -23,4 +23,4 @@ throw TypeError("Cannot add the same private member more than once"); | ||
}; | ||
var __privateSet$1 = (obj, member, value, setter) => { | ||
__accessCheck$1(obj, member, "write to private field"); | ||
var __privateSet$2 = (obj, member, value, setter) => { | ||
__accessCheck$2(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
@@ -30,3 +30,3 @@ return value; | ||
var __privateMethod$1 = (obj, member, method) => { | ||
__accessCheck$1(obj, member, "access private method"); | ||
__accessCheck$2(obj, member, "access private method"); | ||
return method; | ||
@@ -37,18 +37,18 @@ }; | ||
constructor({ compareFn }) { | ||
__privateAdd$1(this, _heapifyDown); | ||
__privateAdd$1(this, _heapifyUp); | ||
__privateAdd$1(this, _compareFn, void 0); | ||
__privateAdd$1(this, _heap, void 0); | ||
__privateSet$1(this, _compareFn, compareFn); | ||
__privateSet$1(this, _heap, []); | ||
__privateAdd$2(this, _heapifyDown); | ||
__privateAdd$2(this, _heapifyUp); | ||
__privateAdd$2(this, _compareFn, void 0); | ||
__privateAdd$2(this, _heap, void 0); | ||
__privateSet$2(this, _compareFn, compareFn); | ||
__privateSet$2(this, _heap, []); | ||
} | ||
insert(value) { | ||
__privateGet$1(this, _heap).push(value); | ||
__privateGet$2(this, _heap).push(value); | ||
__privateMethod$1(this, _heapifyUp, heapifyUp_fn).call(this); | ||
} | ||
pop() { | ||
const item = __privateGet$1(this, _heap)[0]; | ||
if (__privateGet$1(this, _heap)[__privateGet$1(this, _heap).length - 1]) { | ||
__privateGet$1(this, _heap)[0] = __privateGet$1(this, _heap)[__privateGet$1(this, _heap).length - 1]; | ||
__privateGet$1(this, _heap).pop(); | ||
const item = __privateGet$2(this, _heap)[0]; | ||
if (__privateGet$2(this, _heap)[__privateGet$2(this, _heap).length - 1]) { | ||
__privateGet$2(this, _heap)[0] = __privateGet$2(this, _heap)[__privateGet$2(this, _heap).length - 1]; | ||
__privateGet$2(this, _heap).pop(); | ||
} | ||
@@ -59,18 +59,18 @@ __privateMethod$1(this, _heapifyDown, heapifyDown_fn).call(this); | ||
peek() { | ||
return __privateGet$1(this, _heap)[0]; | ||
return __privateGet$2(this, _heap)[0]; | ||
} | ||
delete(value) { | ||
const index = __privateGet$1(this, _heap).indexOf(value); | ||
const index = __privateGet$2(this, _heap).indexOf(value); | ||
if (index === -1) { | ||
return; | ||
} | ||
swap(__privateGet$1(this, _heap), index, __privateGet$1(this, _heap).length - 1); | ||
__privateGet$1(this, _heap).pop(); | ||
swap(__privateGet$2(this, _heap), index, __privateGet$2(this, _heap).length - 1); | ||
__privateGet$2(this, _heap).pop(); | ||
__privateMethod$1(this, _heapifyDown, heapifyDown_fn).call(this); | ||
} | ||
clear() { | ||
__privateSet$1(this, _heap, []); | ||
__privateSet$2(this, _heap, []); | ||
} | ||
get size() { | ||
return __privateGet$1(this, _heap).length; | ||
return __privateGet$2(this, _heap).length; | ||
} | ||
@@ -83,11 +83,11 @@ } | ||
let index = 0; | ||
while (hasLeftChild(index, __privateGet$1(this, _heap).length)) { | ||
while (hasLeftChild(index, __privateGet$2(this, _heap).length)) { | ||
let smallerChildIndex = getLeftChildIndex(index); | ||
if (hasRightChild(index, __privateGet$1(this, _heap).length) && __privateGet$1(this, _compareFn).call(this, rightChild(__privateGet$1(this, _heap), index), leftChild(__privateGet$1(this, _heap), index)) === Ordering.Less) { | ||
if (hasRightChild(index, __privateGet$2(this, _heap).length) && __privateGet$2(this, _compareFn).call(this, rightChild(__privateGet$2(this, _heap), index), leftChild(__privateGet$2(this, _heap), index)) === Ordering.Less) { | ||
smallerChildIndex = getRightChildIndex(index); | ||
} | ||
if (__privateGet$1(this, _compareFn).call(this, __privateGet$1(this, _heap)[index], __privateGet$1(this, _heap)[smallerChildIndex]) === Ordering.Less) { | ||
if (__privateGet$2(this, _compareFn).call(this, __privateGet$2(this, _heap)[index], __privateGet$2(this, _heap)[smallerChildIndex]) === Ordering.Less) { | ||
break; | ||
} else { | ||
swap(__privateGet$1(this, _heap), index, smallerChildIndex); | ||
swap(__privateGet$2(this, _heap), index, smallerChildIndex); | ||
} | ||
@@ -99,5 +99,5 @@ index = smallerChildIndex; | ||
heapifyUp_fn = function() { | ||
let index = __privateGet$1(this, _heap).length - 1; | ||
while (hasParent(index) && __privateGet$1(this, _compareFn).call(this, __privateGet$1(this, _heap)[index], parent(__privateGet$1(this, _heap), index)) === Ordering.Less) { | ||
swap(__privateGet$1(this, _heap), index, getParentIndex(index)); | ||
let index = __privateGet$2(this, _heap).length - 1; | ||
while (hasParent(index) && __privateGet$2(this, _compareFn).call(this, __privateGet$2(this, _heap)[index], parent(__privateGet$2(this, _heap), index)) === Ordering.Less) { | ||
swap(__privateGet$2(this, _heap), index, getParentIndex(index)); | ||
index = getParentIndex(index); | ||
@@ -139,2 +139,62 @@ } | ||
var __defProp = Object.defineProperty; | ||
var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value; | ||
var __publicField = (obj, key, value) => { | ||
__defNormalProp(obj, typeof key !== "symbol" ? key + "" : key, value); | ||
return value; | ||
}; | ||
var __accessCheck$1 = (obj, member, msg) => { | ||
if (!member.has(obj)) | ||
throw TypeError("Cannot " + msg); | ||
}; | ||
var __privateGet$1 = (obj, member, getter) => { | ||
__accessCheck$1(obj, member, "read from private field"); | ||
return getter ? getter.call(obj) : member.get(obj); | ||
}; | ||
var __privateAdd$1 = (obj, member, value) => { | ||
if (member.has(obj)) | ||
throw TypeError("Cannot add the same private member more than once"); | ||
member instanceof WeakSet ? member.add(obj) : member.set(obj, value); | ||
}; | ||
var __privateSet$1 = (obj, member, value, setter) => { | ||
__accessCheck$1(obj, member, "write to private field"); | ||
setter ? setter.call(obj, value) : member.set(obj, value); | ||
return value; | ||
}; | ||
var _a, _promise, _resolve, _reject; | ||
class Deferred { | ||
constructor() { | ||
__publicField(this, _a, "Deferred"); | ||
__privateAdd$1(this, _promise, void 0); | ||
__privateAdd$1(this, _resolve, void 0); | ||
__privateAdd$1(this, _reject, void 0); | ||
__privateSet$1(this, _promise, new Promise((resolve, reject) => { | ||
__privateSet$1(this, _resolve, resolve); | ||
__privateSet$1(this, _reject, reject); | ||
})); | ||
} | ||
then(onfulfilled, onrejected) { | ||
return Promise.prototype.then.apply(__privateGet$1(this, _promise), [onfulfilled, onrejected]); | ||
} | ||
catch(onrejected) { | ||
return Promise.prototype.catch.apply(__privateGet$1(this, _promise), [onrejected]); | ||
} | ||
finally(onfinally) { | ||
return Promise.prototype.finally.apply(__privateGet$1(this, _promise), [onfinally]); | ||
} | ||
resolve(value) { | ||
__privateGet$1(this, _resolve).call(this, value); | ||
} | ||
reject(reason) { | ||
__privateGet$1(this, _reject).call(this, reason); | ||
} | ||
getPromise() { | ||
return __privateGet$1(this, _promise); | ||
} | ||
} | ||
_a = Symbol.toStringTag; | ||
_promise = new WeakMap(); | ||
_resolve = new WeakMap(); | ||
_reject = new WeakMap(); | ||
var __accessCheck = (obj, member, msg) => { | ||
@@ -216,3 +276,5 @@ if (!member.has(obj)) | ||
const now = Date.now(); | ||
const deferred = new Deferred(); | ||
const item = { | ||
deferred, | ||
politeness, | ||
@@ -224,4 +286,8 @@ contents: message, | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
return () => { | ||
__privateGet(this, _queue).delete(item); | ||
return { | ||
...deferred.getPromise(), | ||
cancel: () => { | ||
__privateGet(this, _queue).delete(item); | ||
deferred.resolve(); | ||
} | ||
}; | ||
@@ -238,3 +304,7 @@ } | ||
} | ||
return noop; | ||
const promise = Promise.resolve(); | ||
return { | ||
...promise, | ||
cancel: noop | ||
}; | ||
} | ||
@@ -284,3 +354,3 @@ getMessage(politeness = "polite") { | ||
} | ||
const timeout = message.scheduled > now ? message.scheduled - now : 0; | ||
const timeout = message.scheduled - now; | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
@@ -294,3 +364,3 @@ __privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, true); | ||
const { contents, politeness } = message; | ||
const { contents, deferred, politeness } = message; | ||
const container = this.shadowRoot?.getElementById(politeness); | ||
@@ -309,7 +379,14 @@ if (!container) { | ||
} | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
deferred.resolve(); | ||
if (this.delay > 0) { | ||
__privateSet(this, _timeoutId, window.setTimeout(() => { | ||
__privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, false); | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
}, this.delay)); | ||
} else { | ||
__privateSet(this, _timeoutId, null); | ||
__privateSet(this, _pending, false); | ||
__privateMethod(this, _performWork, performWork_fn).call(this); | ||
}, this.delay)); | ||
} | ||
}; | ||
@@ -375,3 +452,69 @@ function getTextContent(element) { | ||
function findOrCreateLiveRegion(from, appendTo) { | ||
function announce(message, options = {}) { | ||
let liveRegion = findLiveRegion(options.from); | ||
if (!liveRegion) { | ||
liveRegion = document.createElement("live-region"); | ||
if (options.appendTo) { | ||
options.appendTo.appendChild(liveRegion); | ||
} else { | ||
const container = getLiveRegionContainer(options.from); | ||
container.appendChild(liveRegion); | ||
} | ||
if (process.env.NODE_ENV === "test") { | ||
return liveRegion.announce(message, options); | ||
} | ||
let canceled = false; | ||
let cancel = () => { | ||
canceled = true; | ||
}; | ||
const promise = wait(REGISTER_WAIT_MS).then(() => { | ||
if (!canceled) { | ||
const result = liveRegion.announce(message, options); | ||
cancel = result.cancel; | ||
return result; | ||
} | ||
}); | ||
return { | ||
...promise, | ||
cancel: () => { | ||
cancel(); | ||
} | ||
}; | ||
} | ||
return liveRegion.announce(message, options); | ||
} | ||
function announceFromElement(element, options = {}) { | ||
let liveRegion = findLiveRegion(options.from); | ||
if (!liveRegion) { | ||
liveRegion = document.createElement("live-region"); | ||
if (options.appendTo) { | ||
options.appendTo.appendChild(liveRegion); | ||
} else { | ||
const container = getLiveRegionContainer(options.from); | ||
container.appendChild(liveRegion); | ||
} | ||
if (process.env.NODE_ENV === "test") { | ||
return liveRegion.announceFromElement(element, options); | ||
} | ||
let canceled = false; | ||
let cancel = () => { | ||
canceled = true; | ||
}; | ||
const promise = wait(REGISTER_WAIT_MS).then(() => { | ||
if (!canceled) { | ||
const result = liveRegion.announceFromElement(element, options); | ||
cancel = result.cancel; | ||
return result; | ||
} | ||
}); | ||
return { | ||
...promise, | ||
cancel: () => { | ||
cancel(); | ||
} | ||
}; | ||
} | ||
return liveRegion.announceFromElement(element, options); | ||
} | ||
function findLiveRegion(from) { | ||
let liveRegion = null; | ||
@@ -382,9 +525,3 @@ liveRegion = from ? getClosestLiveRegion(from) : null; | ||
} | ||
let container = document.body; | ||
if (from) { | ||
const dialog = from.closest("dialog"); | ||
if (dialog) { | ||
container = dialog; | ||
} | ||
} | ||
const container = getLiveRegionContainer(from); | ||
liveRegion = container.querySelector("live-region"); | ||
@@ -394,9 +531,3 @@ if (liveRegion !== null) { | ||
} | ||
liveRegion = document.createElement("live-region"); | ||
if (appendTo) { | ||
appendTo.appendChild(liveRegion); | ||
} else { | ||
container.appendChild(liveRegion); | ||
} | ||
return liveRegion; | ||
return null; | ||
} | ||
@@ -418,12 +549,19 @@ function getClosestLiveRegion(from) { | ||
} | ||
function announce(message, options = {}) { | ||
const liveRegion = findOrCreateLiveRegion(options.from, options.appendTo); | ||
return liveRegion.announce(message, options); | ||
function getLiveRegionContainer(from) { | ||
let container = document.body; | ||
if (from) { | ||
const dialog = from.closest("dialog"); | ||
if (dialog) { | ||
container = dialog; | ||
} | ||
} | ||
return container; | ||
} | ||
function announceFromElement(element, options = {}) { | ||
const liveRegion = findOrCreateLiveRegion(options.from, options.appendTo); | ||
return liveRegion.announceFromElement(element, options); | ||
const REGISTER_WAIT_MS = 150; | ||
function wait(ms) { | ||
return new Promise((resolve) => { | ||
setTimeout(resolve, ms); | ||
}); | ||
} | ||
export { LiveRegionElement, announce, announceFromElement, templateContent }; |
{ | ||
"name": "@primer/live-region-element", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"type": "module", | ||
@@ -47,12 +47,12 @@ "main": "./dist/esm/index.js", | ||
"devDependencies": { | ||
"@custom-elements-manifest/analyzer": "^0.9.4", | ||
"@custom-elements-manifest/analyzer": "^0.10.2", | ||
"@rollup/plugin-inject": "^5.0.5", | ||
"@rollup/plugin-node-resolve": "^15.2.3", | ||
"@rollup/plugin-replace": "^5.0.5", | ||
"jsdom": "^24.0.0", | ||
"jsdom": "^24.1.0", | ||
"publint": "^0.2.7", | ||
"rimraf": "^5.0.5", | ||
"rimraf": "^5.0.7", | ||
"rollup-plugin-esbuild": "^6.1.1", | ||
"rollup-plugin-typescript2": "^0.36.0", | ||
"typescript": "^5.4.4" | ||
"typescript": "^5.4.5" | ||
}, | ||
@@ -59,0 +59,0 @@ "sideEffects": [ |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Environment variable access
Supply chain riskPackage accesses environment variables, which may be a sign of credential stuffing or data theft.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
91917
20
2521
2