|
| 1 | +import { untrack, mount, unmount } from 'svelte'; |
| 2 | +import { createAttachmentKey } from 'svelte/attachments'; |
| 3 | +import { on } from 'svelte/events'; |
| 4 | + |
| 5 | +/** |
| 6 | + * @param {Element} node |
| 7 | + * @param {Element} parent |
| 8 | + */ |
| 9 | +function moveNodeIntoParent(node, parent) { |
| 10 | + if ('moveBefore' in parent) { |
| 11 | + // @ts-expect-error not yet in baseline |
| 12 | + parent.moveBefore(node, null); |
| 13 | + } else { |
| 14 | + parent.insertBefore(node, null); |
| 15 | + } |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * @param {HTMLElement} node |
| 20 | + * @param {import('../stack-item.svelte').StackItem<any>} item |
| 21 | + */ |
| 22 | +function setProgressAndSpeed(node, item) { |
| 23 | + if (!item.progress) return; |
| 24 | + node.style.setProperty('--play-progress', item.progress.toString()); |
| 25 | + node.style.setProperty('--play-speed', (1 - item.progress) * item.config.timeout + 'ms'); |
| 26 | +} |
| 27 | + |
| 28 | +/** |
| 29 | + * @param {HTMLElement} node |
| 30 | + * @param {import('../stack-item.svelte').StackItem<any>} item |
| 31 | + */ |
| 32 | +function pause(node, item) { |
| 33 | + node.style.setProperty('--play-state', 'paused'); |
| 34 | + item.pause(); |
| 35 | +} |
| 36 | + |
| 37 | +/** |
| 38 | + * @param {HTMLElement} node |
| 39 | + * @param {import('../stack-item.svelte').StackItem<any>} item |
| 40 | + */ |
| 41 | +function resume(node, item) { |
| 42 | + node.style.setProperty('--play-state', 'running'); |
| 43 | + item.resume(); |
| 44 | +} |
| 45 | + |
| 46 | +/** |
| 47 | + * @param {HTMLElement} node |
| 48 | + * @param {import('../stack-item.svelte').StackItem<any>} item |
| 49 | + * @param {() => HTMLDialogElement | null} getDialog |
| 50 | + * @param {Element} [originalParent] |
| 51 | + * @returns {boolean} |
| 52 | + */ |
| 53 | +function moveIntoDialogIfAny(node, item, getDialog, originalParent) { |
| 54 | + if (!originalParent) { |
| 55 | + originalParent = node.parentElement ?? document.body; |
| 56 | + } |
| 57 | + |
| 58 | + const dialog = getDialog(); |
| 59 | + if (!dialog) return false; |
| 60 | + |
| 61 | + // 1. move into dialog |
| 62 | + moveNodeIntoParent(node, dialog); |
| 63 | + |
| 64 | + // 2. prevent clicks from propagating to the dialog backdrop |
| 65 | + const offStopPropagation = on(node, 'click', (e) => e.stopPropagation()); |
| 66 | + |
| 67 | + // 3. on dialog close, move to the next open dialog, if any |
| 68 | + // otherwise move back to original parent |
| 69 | + const offDialogClose = on(dialog, 'close', () => { |
| 70 | + pause(node, item); |
| 71 | + |
| 72 | + if (!moveIntoDialogIfAny(node, item, getDialog, originalParent)) { |
| 73 | + moveNodeIntoParent(node, originalParent); |
| 74 | + } |
| 75 | + |
| 76 | + setProgressAndSpeed(node, item); |
| 77 | + node.showPopover(); |
| 78 | + resume(node, item); |
| 79 | + |
| 80 | + offStopPropagation(); |
| 81 | + offDialogClose(); |
| 82 | + }); |
| 83 | + |
| 84 | + return true; |
| 85 | +} |
| 86 | + |
| 87 | +/** |
| 88 | + * @returns {HTMLDialogElement | null} |
| 89 | + */ |
| 90 | +function findTopLevelDialog() { |
| 91 | + const el = Array.from(document.querySelectorAll('dialog:open')).pop(); |
| 92 | + if (!el) return null; |
| 93 | + return /** @type {HTMLDialogElement} */ (el); |
| 94 | +} |
| 95 | + |
| 96 | +/** |
| 97 | + * @template Resolved |
| 98 | + * @param {import('../stack-item.svelte').StackItem<import('svelte').Component<import('../types.public').StackItemProps<Resolved>>, Resolved>} item |
| 99 | + * @param {import('./render-popover').RenderPopoverOptions<Resolved>} [options] |
| 100 | + * @returns {import('svelte/elements').HTMLAttributes<HTMLElement>} |
| 101 | + */ |
| 102 | +export function renderPopover(item, options) { |
| 103 | + const getDialog = options?.dialog ?? findTopLevelDialog; |
| 104 | + const pauseOnPointerOver = options?.pauseOnPointerOver ?? true; |
| 105 | + |
| 106 | + return { |
| 107 | + popover: 'manual', |
| 108 | + /** |
| 109 | + * @type {import('svelte/attachments').Attachment<HTMLElement>} |
| 110 | + */ |
| 111 | + [createAttachmentKey()]: function (node) { |
| 112 | + /** @type {(() => void)[]} */ |
| 113 | + const offs = []; |
| 114 | + /** @type {ReturnType<typeof mount> | null} */ |
| 115 | + let mounted = null; |
| 116 | + |
| 117 | + untrack(() => { |
| 118 | + setProgressAndSpeed(node, item); |
| 119 | + pause(node, item); |
| 120 | + |
| 121 | + mounted = mount(item.config.component, { |
| 122 | + target: node, |
| 123 | + props: { ...item.config.props, item }, |
| 124 | + intro: true, |
| 125 | + }); |
| 126 | + |
| 127 | + // pause the timeout when hovering over the notification |
| 128 | + if (pauseOnPointerOver) { |
| 129 | + offs.push(on(node, 'pointerenter', () => pause(node, item))); |
| 130 | + offs.push(on(node, 'pointerleave', () => resume(node, item))); |
| 131 | + } |
| 132 | + |
| 133 | + moveIntoDialogIfAny(node, item, getDialog); |
| 134 | + |
| 135 | + node.showPopover(); |
| 136 | + resume(node, item); |
| 137 | + }); |
| 138 | + |
| 139 | + return () => { |
| 140 | + offs.forEach((off) => off()); |
| 141 | + if (mounted) unmount(mounted); |
| 142 | + }; |
| 143 | + }, |
| 144 | + }; |
| 145 | +} |
0 commit comments