From b846497d706c33e013526d3a599697f6e39dc578 Mon Sep 17 00:00:00 2001 From: Mark Conroy Date: Fri, 14 Jun 2024 10:19:08 +0100 Subject: [PATCH 1/2] adds visually hidden announcements for alerts --- js/wysiwyg-styles.js | 33 +++++++++++++++++++++++++++++++++ localgov_base.libraries.yml | 5 +++++ 2 files changed, 38 insertions(+) create mode 100644 js/wysiwyg-styles.js diff --git a/js/wysiwyg-styles.js b/js/wysiwyg-styles.js new file mode 100644 index 00000000..eb7458ed --- /dev/null +++ b/js/wysiwyg-styles.js @@ -0,0 +1,33 @@ +/** + * @file JS file for the WYSIWYG styles components. + */ + +(function wysiwygStylesScript(Drupal) { + Drupal.behaviors.wysiwygStyles = { + attach: function (context) { + const wysiwygAlerts = once("allAlerts", ".alert", context); + + function alertMessage(alert, type) { + const element = document.createElement("span"); + element.classList.add("visually-hidden"); + element.innerHTML = Drupal.t( + `This is an "alert" component. Variant: "${type}"` + ); + // Insert the message at the beginning of the alert. + alert.insertBefore(element, alert.firstChild); + } + + wysiwygAlerts.forEach((alert) => { + if (alert.classList.contains("alert-info")) { + alertMessage(alert, "information"); + } else if (alert.classList.contains("alert-danger")) { + alertMessage(alert, "danger"); + } else if (alert.classList.contains("alert-success")) { + alertMessage(alert, "success"); + } else { + alertMessage(alert, "default"); + } + }); + }, + }; +})(Drupal); diff --git a/localgov_base.libraries.yml b/localgov_base.libraries.yml index 97a4e983..2bf19b39 100644 --- a/localgov_base.libraries.yml +++ b/localgov_base.libraries.yml @@ -48,6 +48,11 @@ wysiwyg-styles: css: theme: css/components/wysiwyg-styles.css: {} + js: + js/wysiwyg-styles.js: {} + dependencies: + - core/drupal + - core/once header: css: From cb941f778278dd18f7121059517c90cc2dbd173e Mon Sep 17 00:00:00 2001 From: Mark Conroy Date: Wed, 27 Nov 2024 17:25:55 +0000 Subject: [PATCH 2/2] updates copy for alerts --- js/wysiwyg-styles.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/js/wysiwyg-styles.js b/js/wysiwyg-styles.js index eb7458ed..88c3ebe2 100644 --- a/js/wysiwyg-styles.js +++ b/js/wysiwyg-styles.js @@ -11,7 +11,7 @@ const element = document.createElement("span"); element.classList.add("visually-hidden"); element.innerHTML = Drupal.t( - `This is an "alert" component. Variant: "${type}"` + `Alert: "${type}"` ); // Insert the message at the beginning of the alert. alert.insertBefore(element, alert.firstChild); @@ -22,6 +22,8 @@ alertMessage(alert, "information"); } else if (alert.classList.contains("alert-danger")) { alertMessage(alert, "danger"); + } else if (alert.classList.contains("alert-failure")) { + alertMessage(alert, "failure"); } else if (alert.classList.contains("alert-success")) { alertMessage(alert, "success"); } else {