From c2e968fbb2defad554d960790cb83bc411610eee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Ruffert?= <464300+andreruffert@users.noreply.github.com> Date: Sun, 2 Nov 2025 02:05:32 +0100 Subject: [PATCH] feat: alpha iteration --- src/swipeable.js | 15 ++++++++------ src/toast-queue.js | 50 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 44 insertions(+), 21 deletions(-) diff --git a/src/swipeable.js b/src/swipeable.js index fe4615b..abbe0fc 100644 --- a/src/swipeable.js +++ b/src/swipeable.js @@ -7,7 +7,6 @@ export class Swipeable { #dragFrame = null; #startX = null; #startY = null; - #selector = '[data-swipeable]'; #direction = 'inline'; #timeStamp = null; #distance = null; @@ -15,8 +14,12 @@ export class Swipeable { #acceleration = null; #onSwipe = () => {}; + /** + * + * @param {Object} [options={}] + * @param {function} [options.onSwipe=null] - Swipe callback + */ constructor(options) { - this.#selector = options?.selector || this.#selector; this.#onSwipe = options?.onSwipe || this.#onSwipe; document.addEventListener('pointerdown', this.startDrag); document.addEventListener('pointermove', this.drag); @@ -25,12 +28,12 @@ export class Swipeable { } startDrag = (event) => { - const target = event.target.closest(this.#selector); + const target = event.target.closest('[data-swipeable]'); if (!target) return; this.#target = target; this.#target.dataset.dragging = ''; - this.#target.style.setProperty('touch-action', 'none'); + this.#target.style.setProperty('touch-action', 'none'); // Ensure capture pointer events will work properly on touch devices. this.#target.style.setProperty('will-change', 'transform'); this.#isDragging = true; this.#startX = event.clientX; @@ -73,7 +76,7 @@ export class Swipeable { this.#dragFrame = requestAnimationFrame(() => { if (!this.#target) return; this.#target.style.setProperty('transform', `translate(${dx}px, ${dy}px)`); // rotate(${dx * 0.1}deg) - this.#target.style.setProperty('--distance', this.#distance); + this.#target.style.setProperty('--tq-distance', this.#distance); }); }; @@ -94,7 +97,7 @@ export class Swipeable { this.#target.addEventListener('transitionend', onTransitionEnd, { once: true }); this.#target.style.setProperty('transition', 'transform 0.3s'); this.#target.style.removeProperty('transform'); - this.#target.style.removeProperty('--distance'); + this.#target.style.removeProperty('--tq-distance'); this.#target.style.removeProperty('will-change'); this.#target.style.removeProperty('touch-action'); delete this.#target.dataset.dragging; diff --git a/src/toast-queue.js b/src/toast-queue.js index be56ae1..9f0d8c8 100644 --- a/src/toast-queue.js +++ b/src/toast-queue.js @@ -52,11 +52,13 @@ export class ToastQueue { #swipeable; /** - * @typedef {Object} ToastQueueOptions + * @typedef {object} ToastQueueOptions * @property {number} duration - The amount of time, in milliseconds, that the toast will remain open before closing automatically. * @property {ToastQueuePlacement} placement - * @property {string} mode - * @property {HTMLElement} root - + * + * @param {ToastQueueOptions} [options={}] */ constructor(options) { const rootTarget = options?.root || document.body; @@ -144,11 +146,13 @@ export class ToastQueue { return { id: toastId, index: this.#queue.size + 1, + timestamp: Date.now(), timer: duration ? new Timer(() => this.close(toastId), duration) : undefined, dismissible: options?.dismissible !== false, content: options?.content, onClose: options?.onClose, action: options?.action, + ref: null, }; } @@ -174,14 +178,14 @@ export class ToastQueue { } /** - * @param {string} Placement - toast-queue placement + * @param {ToastQueuePlacement} value - toast-queue placement */ set placement(value) { this.#placement = value; // this.#swipeable.direction = getSwipeableDirection(value); for (const toast of this.#queue) { - toast.el.dataset.swipeable = getSwipeableDirection(value); - toast.el.style.setProperty( + toast.ref.dataset.swipeable = getSwipeableDirection(value); + toast.ref.style.setProperty( 'view-transition-class', `tq-item ${getPlacementViewTransitionClass(this.#placement)}`, ); @@ -191,6 +195,11 @@ export class ToastQueue { }); } + /** + * + * @param {function} updateDOM + * @param {boolean} skipTransition + */ async update(updateDOM, skipTransition = false) { if (this.#queue.size === 1) this.#root.showPopover(); if (typeof updateDOM === 'function' && !skipTransition) @@ -210,6 +219,12 @@ export class ToastQueue { ); } + /** + * Get a toast from the queue + * + * @param {string} toastId + * @returns + */ get(toastId) { for (const toast of this.#queue) { if (toast.id === toastId) { @@ -220,13 +235,16 @@ export class ToastQueue { } /** - * @param {string} content - HTML content + * Creates a new toast + * + * @param {object|string} content - Message * @param {object} options * @param {string} options.className * @param {number} options.duration - * @param {number} options.dismissible - * @param {string} options.actionLabel - * @param {function} options.onAction + * @param {boolean} options.dismissible + * @param {object|string} options.action + * @param {string} options.action.label + * @param {function} options.action.onClick * @param {function} options.onClose * @returns */ @@ -244,8 +262,6 @@ export class ToastQueue { if (toastRef.dismissible) { newItem.dataset.swipeable = getSwipeableDirection(this.#placement); - // Swipeable: Ensure capture pointer events will work properly on touch devices - // newItem.style.setProperty('touch-action', 'none'); } const toastPart = newItem.querySelector(partSelectors.toast); @@ -287,14 +303,18 @@ export class ToastQueue { actionsPart.appendChild(actionButton); } - toastRef.el = newItem; + toastRef.ref = newItem; this.#queue.add(toastRef); this.update(() => this.#group.prepend(newItem)); return toastRef; } - /* Closes a toast by ID */ + /** + * Closes a toast by ID + * + * @param {string} id + */ close(id) { for (const toast of this.#queue) { if (toast.id === id) { @@ -302,10 +322,10 @@ export class ToastQueue { if (typeof toast.onClose === 'function') toast.onClose(); this.update( () => { - toast.el.remove(); + toast.ref.remove(); }, // Skip view transition for elements not visible in the UI - toast.el.offsetParent === null, + toast.ref.offsetParent === null, ); } } @@ -338,8 +358,8 @@ export class ToastQueue { } destroy() { + // TODO: Remove event listeners cleanup etc. this.#root.remove(); this.#swipeable.destroy(); - // TODO remove event listeners cleanup etc. } } -- knotmirror 0.0.1