|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2025 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import * as Css from './css.js'; |
| 8 | +import {Msg} from './msg.js'; |
| 9 | +import * as aria from './utils/aria.js'; |
| 10 | +import * as dom from './utils/dom.js'; |
| 11 | +import {Svg} from './utils/svg.js'; |
| 12 | +import type {WorkspaceSvg} from './workspace_svg.js'; |
| 13 | + |
| 14 | +const CLASS_NAME = 'blocklyToast'; |
| 15 | +const MESSAGE_CLASS_NAME = 'blocklyToastMessage'; |
| 16 | +const CLOSE_BUTTON_CLASS_NAME = 'blocklyToastCloseButton'; |
| 17 | + |
| 18 | +/** |
| 19 | + * Display/configuration options for a toast notification. |
| 20 | + */ |
| 21 | +export interface ToastOptions { |
| 22 | + /** |
| 23 | + * Toast ID. If set along with `oncePerSession`, will cause subsequent toasts |
| 24 | + * with this ID to not be shown. |
| 25 | + */ |
| 26 | + id?: string; |
| 27 | + |
| 28 | + /** |
| 29 | + * Flag to show the toast once per session only. |
| 30 | + * Subsequent calls are ignored. |
| 31 | + */ |
| 32 | + oncePerSession?: boolean; |
| 33 | + |
| 34 | + /** |
| 35 | + * Text of the message to display on the toast. |
| 36 | + */ |
| 37 | + message: string; |
| 38 | + |
| 39 | + /** |
| 40 | + * Duration in seconds before the toast is removed. Defaults to 5. |
| 41 | + */ |
| 42 | + duration?: number; |
| 43 | + |
| 44 | + /** |
| 45 | + * How prominently/interrupting the readout of the toast should be for |
| 46 | + * screenreaders. Corresponds to aria-live and defaults to polite. |
| 47 | + */ |
| 48 | + assertiveness?: Toast.Assertiveness; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Class that allows for showing and dismissing temporary notifications. |
| 53 | + */ |
| 54 | +export class Toast { |
| 55 | + /** IDs of toasts that have previously been shown. */ |
| 56 | + private static shownIds = new Set<string>(); |
| 57 | + |
| 58 | + /** |
| 59 | + * Shows a toast notification. |
| 60 | + * |
| 61 | + * @param workspace The workspace to show the toast on. |
| 62 | + * @param options Configuration options for the toast message, duration, etc. |
| 63 | + */ |
| 64 | + static show(workspace: WorkspaceSvg, options: ToastOptions) { |
| 65 | + if (options.oncePerSession && options.id) { |
| 66 | + if (this.shownIds.has(options.id)) return; |
| 67 | + this.shownIds.add(options.id); |
| 68 | + } |
| 69 | + |
| 70 | + // Clear any existing toasts. |
| 71 | + this.hide(workspace); |
| 72 | + |
| 73 | + const toast = this.createDom(workspace, options); |
| 74 | + |
| 75 | + // Animate the toast into view. |
| 76 | + requestAnimationFrame(() => { |
| 77 | + toast.style.bottom = '2rem'; |
| 78 | + }); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Creates the DOM representation of a toast. |
| 83 | + * |
| 84 | + * @param workspace The workspace to inject the toast notification onto. |
| 85 | + * @param options Configuration options for the toast. |
| 86 | + * @returns The root DOM element of the toast. |
| 87 | + */ |
| 88 | + protected static createDom(workspace: WorkspaceSvg, options: ToastOptions) { |
| 89 | + const { |
| 90 | + message, |
| 91 | + duration = 5, |
| 92 | + assertiveness = Toast.Assertiveness.POLITE, |
| 93 | + } = options; |
| 94 | + |
| 95 | + const toast = document.createElement('div'); |
| 96 | + workspace.getInjectionDiv().appendChild(toast); |
| 97 | + toast.dataset.toastId = options.id; |
| 98 | + toast.className = CLASS_NAME; |
| 99 | + aria.setRole(toast, aria.Role.STATUS); |
| 100 | + aria.setState(toast, aria.State.LIVE, assertiveness); |
| 101 | + |
| 102 | + const messageElement = toast.appendChild(document.createElement('div')); |
| 103 | + messageElement.className = MESSAGE_CLASS_NAME; |
| 104 | + messageElement.innerText = message; |
| 105 | + const closeButton = toast.appendChild(document.createElement('button')); |
| 106 | + closeButton.className = CLOSE_BUTTON_CLASS_NAME; |
| 107 | + aria.setState(closeButton, aria.State.LABEL, Msg['CLOSE']); |
| 108 | + const closeIcon = dom.createSvgElement( |
| 109 | + Svg.SVG, |
| 110 | + { |
| 111 | + width: 24, |
| 112 | + height: 24, |
| 113 | + viewBox: '0 0 24 24', |
| 114 | + fill: 'none', |
| 115 | + }, |
| 116 | + closeButton, |
| 117 | + ); |
| 118 | + aria.setState(closeIcon, aria.State.HIDDEN, true); |
| 119 | + dom.createSvgElement( |
| 120 | + Svg.RECT, |
| 121 | + { |
| 122 | + x: 19.7782, |
| 123 | + y: 2.80762, |
| 124 | + width: 2, |
| 125 | + height: 24, |
| 126 | + transform: 'rotate(45, 19.7782, 2.80762)', |
| 127 | + fill: 'black', |
| 128 | + }, |
| 129 | + closeIcon, |
| 130 | + ); |
| 131 | + dom.createSvgElement( |
| 132 | + Svg.RECT, |
| 133 | + { |
| 134 | + x: 2.80762, |
| 135 | + y: 4.22183, |
| 136 | + width: 2, |
| 137 | + height: 24, |
| 138 | + transform: 'rotate(-45, 2.80762, 4.22183)', |
| 139 | + fill: 'black', |
| 140 | + }, |
| 141 | + closeIcon, |
| 142 | + ); |
| 143 | + closeButton.addEventListener('click', () => { |
| 144 | + toast.remove(); |
| 145 | + workspace.markFocused(); |
| 146 | + }); |
| 147 | + |
| 148 | + let timeout: ReturnType<typeof setTimeout>; |
| 149 | + const setToastTimeout = () => { |
| 150 | + timeout = setTimeout(() => toast.remove(), duration * 1000); |
| 151 | + }; |
| 152 | + const clearToastTimeout = () => clearTimeout(timeout); |
| 153 | + toast.addEventListener('focusin', clearToastTimeout); |
| 154 | + toast.addEventListener('focusout', setToastTimeout); |
| 155 | + toast.addEventListener('mouseenter', clearToastTimeout); |
| 156 | + toast.addEventListener('mousemove', clearToastTimeout); |
| 157 | + toast.addEventListener('mouseleave', setToastTimeout); |
| 158 | + setToastTimeout(); |
| 159 | + |
| 160 | + return toast; |
| 161 | + } |
| 162 | + |
| 163 | + /** |
| 164 | + * Dismiss a toast, e.g. in response to a user action. |
| 165 | + * |
| 166 | + * @param workspace The workspace to dismiss a toast in. |
| 167 | + * @param id The toast ID, or undefined to clear any toast. |
| 168 | + */ |
| 169 | + static hide(workspace: WorkspaceSvg, id?: string) { |
| 170 | + const toast = workspace.getInjectionDiv().querySelector(`.${CLASS_NAME}`); |
| 171 | + if (toast instanceof HTMLElement && (!id || id === toast.dataset.toastId)) { |
| 172 | + toast.remove(); |
| 173 | + } |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +/** |
| 178 | + * Options for how aggressively toasts should be read out by screenreaders. |
| 179 | + * Values correspond to those for aria-live. |
| 180 | + */ |
| 181 | +export namespace Toast { |
| 182 | + export enum Assertiveness { |
| 183 | + ASSERTIVE = 'assertive', |
| 184 | + POLITE = 'polite', |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +Css.register(` |
| 189 | +.${CLASS_NAME} { |
| 190 | + font-size: 1.2rem; |
| 191 | + position: absolute; |
| 192 | + bottom: -10rem; |
| 193 | + right: 2rem; |
| 194 | + padding: 1rem; |
| 195 | + color: black; |
| 196 | + background-color: white; |
| 197 | + border: 2px solid black; |
| 198 | + border-radius: 0.4rem; |
| 199 | + z-index: 999; |
| 200 | + display: flex; |
| 201 | + align-items: center; |
| 202 | + gap: 0.8rem; |
| 203 | + line-height: 1.5; |
| 204 | + transition: bottom 0.3s ease-out; |
| 205 | +} |
| 206 | +
|
| 207 | +.${CLASS_NAME} .${MESSAGE_CLASS_NAME} { |
| 208 | + maxWidth: 18rem; |
| 209 | +} |
| 210 | +
|
| 211 | +.${CLASS_NAME} .${CLOSE_BUTTON_CLASS_NAME} { |
| 212 | + margin: 0; |
| 213 | + padding: 0.2rem; |
| 214 | + background-color: transparent; |
| 215 | + color: black; |
| 216 | + border: none; |
| 217 | + cursor: pointer; |
| 218 | +} |
| 219 | +`); |
0 commit comments