From bc675006d8c72a2994be832236036447ebe1efc1 Mon Sep 17 00:00:00 2001 From: Felix Hamann Date: Sun, 21 Jul 2019 00:18:49 +0200 Subject: [PATCH] feat(alert-icons): add custom icons for alerts --- frontend/src/utils/alerts/alert-icons.js | 16 ++++++++++++++++ frontend/src/utils/alerts/alerts.js | 14 ++++++++++++++ frontend/src/utils/alerts/alerts.scss | 8 ++++---- templates/widgets/alerts/alerts.hamlet | 5 +++++ 4 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 frontend/src/utils/alerts/alert-icons.js diff --git a/frontend/src/utils/alerts/alert-icons.js b/frontend/src/utils/alerts/alert-icons.js new file mode 100644 index 000000000..85fe1d3aa --- /dev/null +++ b/frontend/src/utils/alerts/alert-icons.js @@ -0,0 +1,16 @@ +// +// Fontawesome icons to be used on alerts. +// +// If you want to add new icons stick to the format of the existing ones. +// They are necessary due to weird unicode conversions during transpilation. +// https://fontawesome.com/icons + +export const ALERT_ICONS = { + info: '"\\f05a"', + checkmark: '"\\f058"', + exclamation: '"\\f06a"', + warning: '"\\f071"', + cross: '"\\f00d"', + registered: '"\\f274"', + deregistered: '"\\f273"', +}; diff --git a/frontend/src/utils/alerts/alerts.js b/frontend/src/utils/alerts/alerts.js index e7e04ddbb..4d1a1cf7a 100644 --- a/frontend/src/utils/alerts/alerts.js +++ b/frontend/src/utils/alerts/alerts.js @@ -1,5 +1,6 @@ import { Utility } from '../../core/utility'; import './alerts.scss'; +import { ALERT_ICONS } from './alert-icons'; const ALERTS_INITIALIZED_CLASS = 'alerts--initialized'; const ALERTS_ELEVATED_CLASS = 'alerts--elevated'; @@ -16,6 +17,12 @@ const ALERT_INVISIBLE_CLASS = 'alert--invisible'; const ALERT_AUTO_HIDE_DELAY = 10; const ALERT_AUTOCLOSING_MATCHER = '.alert-info, .alert-success'; +/* + * Dataset-Inputs: + * - decay (data-decay): Custom time (in seconds) for this alert to stay visible + * - icon (data-icon): Custom icon (from the list in alert-icons.js) to show on the alert + */ + @Utility({ selector: '[uw-alerts]', }) @@ -87,6 +94,13 @@ export class Alerts { this._toggleAlert(alertElement); }); + const customIcon = alertElement.dataset.icon; + if (customIcon && ALERT_ICONS[customIcon]) { + alertElement.style.setProperty('--alert-icon', ALERT_ICONS[customIcon]); + } else if (customIcon) { + throw new Error('Alert: Custom icon "' + customIcon + '" could not be found!'); + } + if (autoHideDelay > 0 && alertElement.matches(ALERT_AUTOCLOSING_MATCHER)) { window.setTimeout(() => this._toggleAlert(alertElement), autoHideDelay * 1000); } diff --git a/frontend/src/utils/alerts/alerts.scss b/frontend/src/utils/alerts/alerts.scss index d2faf1b22..8beff3b70 100644 --- a/frontend/src/utils/alerts/alerts.scss +++ b/frontend/src/utils/alerts/alerts.scss @@ -124,7 +124,7 @@ z-index: 40; &::before { - content: '\f05a'; + content: var(--alert-icon, var(--alert-icon-default, '\f05a')); position: absolute; font-family: 'Font Awesome 5 Free'; font-size: 24px; @@ -189,7 +189,7 @@ background-color: var(--color-success); .alert__icon::before { - content: '\f058'; + --alert-icon-default: '\f058'; } } @@ -197,7 +197,7 @@ background-color: var(--color-warning); .alert__icon::before { - content: '\f06a'; + --alert-icon-default: '\f06a'; } } @@ -205,6 +205,6 @@ background-color: var(--color-error); .alert__icon::before { - content: '\f071'; + --alert-icon-default: '\f071'; } } diff --git a/templates/widgets/alerts/alerts.hamlet b/templates/widgets/alerts/alerts.hamlet index 4527e62d3..8ddc0f6cd 100644 --- a/templates/widgets/alerts/alerts.hamlet +++ b/templates/widgets/alerts/alerts.hamlet @@ -3,6 +3,11 @@ $newline never
$forall (status, msg) <- mmsgs $with status2 <- bool status "info" (status == "") +