|
| 1 | +import InvisibleMessageMode from "../types/InvisibleMessageMode.js"; |
| 2 | +import getSingletonElementInstance from "./getSingletonElementInstance.js"; |
| 3 | + |
| 4 | +const styles = `position: absolute; |
| 5 | + clip: rect(1px,1px,1px,1px); |
| 6 | + user-select: none; |
| 7 | + left: -1000px; |
| 8 | + top: -1000px; |
| 9 | + pointer-events: none;`; |
| 10 | + |
| 11 | +const politeSpan = document.createElement("span"); |
| 12 | +const assertiveSpan = document.createElement("span"); |
| 13 | + |
| 14 | +politeSpan.classList.add("ui5-invisiblemessage-polite"); |
| 15 | +assertiveSpan.classList.add("ui5-invisiblemessage-assertive"); |
| 16 | + |
| 17 | +politeSpan.setAttribute("aria-live", "polite"); |
| 18 | +assertiveSpan.setAttribute("aria-live", "assertive"); |
| 19 | + |
| 20 | +politeSpan.setAttribute("role", "alert"); |
| 21 | +assertiveSpan.setAttribute("role", "alert"); |
| 22 | + |
| 23 | +politeSpan.style.cssText = styles; |
| 24 | +assertiveSpan.style.cssText = styles; |
| 25 | + |
| 26 | +getSingletonElementInstance("ui5-static-area").appendChild(politeSpan); |
| 27 | +getSingletonElementInstance("ui5-static-area").appendChild(assertiveSpan); |
| 28 | + |
| 29 | +/** |
| 30 | + * Inserts the string into the respective span, depending on the mode provided. |
| 31 | + * |
| 32 | + * @param {string} message String to be announced by the screen reader. |
| 33 | + * @param {sap.ui.core.InvisibleMessageMode} mode The mode to be inserted in the aria-live attribute. |
| 34 | + */ |
| 35 | +const announce = (message, mode) => { |
| 36 | + // If no type is presented, fallback to polite announcement. |
| 37 | + const span = mode === InvisibleMessageMode.Assertive ? assertiveSpan : politeSpan; |
| 38 | + |
| 39 | + // Set textContent to empty string in order to trigger screen reader's announcement. |
| 40 | + span.textContent = ""; |
| 41 | + span.textContent = message; |
| 42 | + |
| 43 | + if (mode !== InvisibleMessageMode.Assertive && mode !== InvisibleMessageMode.Polite) { |
| 44 | + console.warn(`You have entered an invalid mode. Valid values are: "Polite" and "Assertive". The framework will automatically set the mode to "Polite".`); // eslint-disable-line |
| 45 | + } |
| 46 | +}; |
| 47 | + |
| 48 | +export default announce; |
0 commit comments