|
| 1 | +import {WorkspaceSvg} from 'blockly'; |
| 2 | + |
| 3 | +/** |
| 4 | + * Toast options. |
| 5 | + */ |
| 6 | +export interface ToastOptions { |
| 7 | + /** |
| 8 | + * Message text. |
| 9 | + */ |
| 10 | + message: string; |
| 11 | + /** |
| 12 | + * Duration in milliseconds before the toast is removed. |
| 13 | + * Defaults to 5000. |
| 14 | + */ |
| 15 | + duration?: number; |
| 16 | +} |
| 17 | + |
| 18 | +/** |
| 19 | + * Shows a message as a toast positioned over the workspace. |
| 20 | + * |
| 21 | + * This is illustrative to gather feedback on the interaction. |
| 22 | + * |
| 23 | + * If retained, we'd expect to allow applications to override with their own implementations. |
| 24 | + * |
| 25 | + * Further work is needed on the accessibility of this toast: |
| 26 | + * - testing screen reader support |
| 27 | + * - considering whether support for stacked toasts is needed |
| 28 | + * - shortcut to focus? though it's the next tab stop currently |
| 29 | + * |
| 30 | + * @param workspace The workspace for positioning. |
| 31 | + * @param options Options. |
| 32 | + */ |
| 33 | +export function toast(workspace: WorkspaceSvg, options: ToastOptions): void { |
| 34 | + const {message, duration = 10000} = options; |
| 35 | + const className = 'blocklyToast'; |
| 36 | + workspace.getInjectionDiv().querySelector(`.${className}`)?.remove(); |
| 37 | + |
| 38 | + const foregroundColor = 'black'; |
| 39 | + const toast = document.createElement('div'); |
| 40 | + toast.className = className; |
| 41 | + toast.setAttribute('role', 'status'); |
| 42 | + toast.setAttribute('aria-live', 'polite'); |
| 43 | + assignStyle(toast, { |
| 44 | + fontSize: '1.2rem', |
| 45 | + position: 'absolute', |
| 46 | + bottom: '-10rem', |
| 47 | + right: '2rem', |
| 48 | + padding: '1rem', |
| 49 | + color: foregroundColor, |
| 50 | + backgroundColor: 'white', |
| 51 | + border: '2px solid black', |
| 52 | + borderRadius: '0.4rem', |
| 53 | + zIndex: '999', |
| 54 | + display: 'flex', |
| 55 | + alignItems: 'center', |
| 56 | + gap: '0.8rem', |
| 57 | + lineHeight: '1.5', |
| 58 | + transition: 'bottom 0.3s ease-out', |
| 59 | + }); |
| 60 | + |
| 61 | + toast.appendChild( |
| 62 | + infoIcon({ |
| 63 | + width: '1.5em', |
| 64 | + height: '1.5em', |
| 65 | + }), |
| 66 | + ); |
| 67 | + const messageElement = toast.appendChild(document.createElement('div')); |
| 68 | + assignStyle(messageElement, { |
| 69 | + maxWidth: '18rem', |
| 70 | + }); |
| 71 | + messageElement.innerText = message; |
| 72 | + const closeButton = toast.appendChild(document.createElement('button')); |
| 73 | + assignStyle(closeButton, { |
| 74 | + margin: '0', |
| 75 | + padding: '0.2rem', |
| 76 | + backgroundColor: 'transparent', |
| 77 | + color: foregroundColor, |
| 78 | + border: 'none', |
| 79 | + }); |
| 80 | + closeButton.ariaLabel = 'Close'; |
| 81 | + closeButton.appendChild( |
| 82 | + closeIcon({ |
| 83 | + width: '1.5em', |
| 84 | + height: '1.5em', |
| 85 | + }), |
| 86 | + ); |
| 87 | + closeButton.addEventListener('click', () => { |
| 88 | + toast.remove(); |
| 89 | + workspace.markFocused(); |
| 90 | + }); |
| 91 | + |
| 92 | + workspace.getInjectionDiv().appendChild(toast); |
| 93 | + requestAnimationFrame(() => { |
| 94 | + toast.style.bottom = '2rem'; |
| 95 | + }); |
| 96 | + |
| 97 | + let timeout: ReturnType<typeof setTimeout> | undefined; |
| 98 | + const setToastTimeout = () => { |
| 99 | + timeout = setTimeout(() => toast.remove(), duration); |
| 100 | + }; |
| 101 | + const clearToastTimeout = () => clearTimeout(timeout); |
| 102 | + toast.addEventListener('focusin', clearToastTimeout); |
| 103 | + toast.addEventListener('focusout', setToastTimeout); |
| 104 | + toast.addEventListener('mouseenter', clearToastTimeout); |
| 105 | + toast.addEventListener('mousemove', clearToastTimeout); |
| 106 | + toast.addEventListener('mouseleave', setToastTimeout); |
| 107 | + setToastTimeout(); |
| 108 | +} |
| 109 | + |
| 110 | +function icon(innerHTML: string, style: Partial<CSSStyleDeclaration> = {}) { |
| 111 | + const icon = document.createElement('svg'); |
| 112 | + assignStyle(icon, style); |
| 113 | + icon.innerHTML = innerHTML; |
| 114 | + icon.ariaHidden = 'hidden'; |
| 115 | + return icon; |
| 116 | +} |
| 117 | + |
| 118 | +function infoIcon(style: Partial<CSSStyleDeclaration> = {}) { |
| 119 | + return icon( |
| 120 | + `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 121 | +<circle cx="12" cy="12" r="10" stroke="currentColor" stroke-width="2"/> |
| 122 | +<rect x="11" y="9" width="2" height="9" fill="currentColor"/> |
| 123 | +<circle cx="12.0345" cy="7.03448" r="1.03448" fill="currentColor"/> |
| 124 | +</svg>`, |
| 125 | + style, |
| 126 | + ); |
| 127 | +} |
| 128 | + |
| 129 | +function closeIcon(style: Partial<CSSStyleDeclaration> = {}) { |
| 130 | + return icon( |
| 131 | + `<svg width="24" height="24" viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg"> |
| 132 | + <rect x="19.7782" y="2.80762" width="2" height="24" transform="rotate(45 19.7782 2.80762)" fill="currentColor"/> |
| 133 | + <rect x="2.80762" y="4.22183" width="2" height="24" transform="rotate(-45 2.80762 4.22183)" fill="currentColor"/> |
| 134 | + </svg>`, |
| 135 | + style, |
| 136 | + ); |
| 137 | +} |
| 138 | + |
| 139 | +function assignStyle(target: HTMLElement, style: Partial<CSSStyleDeclaration>) { |
| 140 | + return Object.assign(target.style, style); |
| 141 | +} |
0 commit comments