diff --git a/index.html b/index.html index 37614b1..ea05a56 100644 --- a/index.html +++ b/index.html @@ -93,7 +93,7 @@ const toastPosition = localStorage.getItem('toast-position') || 'top center'; window.toastQueue = new ToastQueue({ - // timeout: 6000, + duration: 6000, position: toastPosition, root: document.body, ...stackPreset @@ -119,10 +119,8 @@ description: Date.now() }, { // dismissible: false, - actionButton: { - label: 'action 1', - onClick: () => console.log(Date.now()) - } + actionLabel: 'Action', + onAction: () => console.log(Date.now()) }); return; } diff --git a/src/style.css b/src/style.css index 0461209..adcf7fd 100644 --- a/src/style.css +++ b/src/style.css @@ -51,7 +51,7 @@ flex-direction: column-reverse; } - [data-toastq-part="item"]:not(:nth-child(-n + 5)) { + [data-toastq-part="item"]:not(:nth-child(-n + 3)) { display: none; } diff --git a/src/swipeable.js b/src/swipeable.js index 4959901..db4e2c6 100644 --- a/src/swipeable.js +++ b/src/swipeable.js @@ -77,10 +77,8 @@ export class Swipeable { // Acceleration in px/ms² const acceleration = (speed - this.#prevSpeed) / deltaTime; + // console.log({acceleration, speed}); this.#prevSpeed = speed; - - // console.log('Acceleration:', acceleration); - // console.log('speed:', speed); } this.#prevEvent = event; @@ -108,7 +106,9 @@ export class Swipeable { this.targetY = screenY > 0 ? this.targetBCR.height : -this.targetBCR.height; } + // Reset this.#prevEvent = null; + this.#prevSpeed = 0; this.isDragging = false; delete this.target.dataset.toastqDragging; }; @@ -150,8 +150,7 @@ export class Swipeable { // User has finished dragging. if (this.isDragging) return; - const isNearlyAtStart = - Math.abs(this.#direction.includes('inline') ? this.screenX : this.screenY) < 0.1; + const isNearlyAtStart = Math.abs(this.#direction.includes('inline') ? this.screenX : this.screenY) < 0.1; const isNearlyInvisible = opacity < 0.01 || this.#prevSpeed > 2; // If the target is nearly gone. diff --git a/src/toast-queue.js b/src/toast-queue.js index daecfab..9211a8b 100644 --- a/src/toast-queue.js +++ b/src/toast-queue.js @@ -29,7 +29,7 @@ const notificationInflection = inflect('notification')('notifications'); export class ToastQueue { #queue = new Set(); - #timeout = null; + #duration = null; /** @typedef ToastPosition 'top start' | 'top center' | 'top end' | 'bottom start' | 'bottom center' | 'bottom end' */ #toastPosition = 'top end'; #viewMode = null; @@ -39,7 +39,7 @@ export class ToastQueue { /** * @typedef {Object} ToastQueueOptions - * @property {number} timeout - A timeout to automatically close the toast after, in milliseconds. + * @property {number} duration - The amount of time, in milliseconds, that the toast will remain open before closing automatically. * @property {ToastPosition} position - * @property {string} viewMode - * @property {HTMLElement} root - @@ -49,7 +49,7 @@ export class ToastQueue { const rootTemplate = options?.rootTemplate || ROOT_TEMPLATE; const template = rootTemplate.content.cloneNode(true); - this.#timeout = options.timeout || this.#timeout; + this.#duration = options.duration || this.#duration; this.#toastPosition = options?.position || this.#toastPosition; this.#viewMode = options?.viewMode || this.#viewMode; this.#popover = template.querySelector(partSelectors.popover); @@ -105,15 +105,16 @@ export class ToastQueue { } #createToastRef(options) { - const timeout = options?.timeout || this.#timeout; + const duration = options?.duration || this.#duration; const toastId = Math.random().toString(36).slice(2); return { id: toastId, index: this.#queue.size + 1, - timer: timeout ? new Timer(() => this.close(toastId), timeout) : undefined, + timer: duration ? new Timer(() => this.close(toastId), duration) : undefined, dismissible: options?.dismissible !== false, content: options?.content, - actionButton: options?.actionButton || undefined, + actionLabel: options?.actionLabel || undefined, + onAction: options?.onAction || undefined, onClose: options?.onClose || undefined, }; } @@ -178,9 +179,10 @@ export class ToastQueue { /** * @param {string} content - HTML content * @param {object} options - * @param {number} options.timeout + * @param {number} options.duration * @param {number} options.dismissible - * @param {object} options.actionButton + * @param {string} options.actionLabel + * @param {function} options.onAction * @param {function} options.onClose * @returns */ @@ -222,10 +224,9 @@ export class ToastQueue { descSlot.textContent = content?.description; } - const toastActions = template.querySelector(partSelectors.actions); - if (toastRef.actionButton) { - // TODO: Add support for multiple action buttons passing an array of objects. - toastActions.innerHTML = ``; + if (toastRef.actionLabel) { + const toastActions = template.querySelector(partSelectors.actions); + toastActions.innerHTML = ``; } this.#queue.add(toastRef);