Complex Alert

An alert is a special type of ARIA live region. Screen readers will announce the text inside the alert, without moving the focus to the alert message. Alerts are usually styled to visually stand out from the rest of the content, to make them obvious when they appear.



Initial HTML Markup

<div class="deque-alert-group">
  <div id="deque-alert" class="deque-alert" role="alert">
    <div id="showSuccess" class="deque-alert-success">Congratulations! You clicked the button successfully.</div>
    <div id="showInfo" class="deque-alert-info">Here is some information you should be aware of.</div>
    <div id="showError" class="deque-alert-error">Error: You did something wrong.</div>
    <div id="clearAlerts" class="visuallyhidden">Alert message cleared.</div>
  </div>
  <p>
    <label for="useTimeoutInput">Automatically close the alert after 5 seconds</label>
    <input type="checkbox" id="useTimeoutInput" value="5000">
  </p>
  <p>
    <button id="showSuccess" class="deque-button">Show success alert</button>
    <button id="showInfo" class="deque-button">Show info alert</button>
    <button id="showError" class="deque-button">Show error alert</button>
    <button id="clearAlerts" class="deque-button deque-button-secondary">Clear alert</button>
  </p>
</div>

JavaScript

Required: The complete JavaScript file (for all patterns in the library): deque-patterns.min.js

Key parts of the JavaScript file

Note: The code below is functional only in the context of the complete JavaScript file.

In the @alert section:


	function createAlert(message) {
	  var classes = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : [];
	  var timeout = arguments[2];
	
	
	  var output = document.createElement('div');
	
	  classes.forEach(function (c) {
	    return output.classList.add(c);
	  });
	
	  output.innerHTML = message;
	  if (timeout) {
	    setTimeout(function () {
	      if (output.parentElement) {
	        output.remove();
	      }
	    }, timeout);
	  }
	
	  return output;
	}
	
	function createHTMLAlert(id, alertRegion, timeout) {
	  var alertBox = alertRegion.querySelector('#' + id);
	  alertBox.classList.add('deque-show-block');
	  if (timeout) {
	    setTimeout(function () {
	      alertBox.classList.add('deque-hidden');
	      alertBox.classList.remove('deque-show-block');
	    }, timeout);
	  }
	}
	
	function activateAllAlerts() {
	  var alerts = document.querySelectorAll('.deque-alert-group');
	
	  for (var i = 0; i < alerts.length; i++) {
	    var useTimeoutInput = alerts[i].querySelector('#useTimeoutInput');
	    var buttons = alerts[i].querySelectorAll('.deque-button');
	    var alertRegion = alerts[i].querySelector('.deque-alert');
	    var alertBoxes = alertRegion.querySelectorAll('div');
	
	    for (var k = 0; k < alertBoxes.length; k++) {
	      alertBoxes[k].classList.add('deque-hidden');
	    }
	
	    for (var j = 0; j < buttons.length; j++) {
	      buttons[j].addEventListener('click', showAlertMessage.bind(null, buttons[j].id, useTimeoutInput, alertRegion));
	    }
	  }
	}
	
	function showAlertMessage(id, useTimeoutInput, alertRegion) {
	  var useTimeout;
	  if (useTimeoutInput) {
	    if (useTimeoutInput.getAttribute('type') == 'checkbox') {
	      useTimeout = useTimeoutInput.checked;
	    } else {
	      useTimeout = true;
	    }
	  } else {
	    useTimeout = false;
	  }
	
	  clearAlerts(alertRegion);
	
	  if (useTimeout) {
	    var timeoutValue = useTimeoutInput.getAttribute('value');
	    if (timeoutValue) {
	      createHTMLAlert(id, alertRegion, timeoutValue);
	    } else {
	      createHTMLAlert(id, alertRegion, 5000);
	    }
	  } else {
	    createHTMLAlert(id, alertRegion);
	  }
	}
	
	function clearAlerts(alertRegion) {
	  var alertBoxes = alertRegion.querySelectorAll('div');
	  for (var k = 0; k < alertBoxes.length; k++) {
	    alertBoxes[k].classList.remove('deque-show-block');
	    alertBoxes[k].classList.add('deque-hidden');
	  }
	}
	
	activateAllAlerts();
	

Note: No additional JavaScript initialization code is necessary for this pattern. All elements with class="deque-alert-group" will be initialized automatically by the external JavaScript file.

CSS

Required: The complete CSS file (for all patterns in the library): deque-patterns.min.css

Key styles within the CSS file (other styles may also be necessary):


.deque-alert [class^='deque-alert']:not(:empty) {
  padding: 10px 10px 10px 60px;
  min-height: 24px;
}
.deque-alert [class^='deque-alert']:not(:empty)::before {
  font-family: 'mwf-glyphs';
  font-size: 36px;
  margin: 0 0 0 -50px;
  position: relative;
  top: 0;
  display: inline-block;
  float: left;
}
.deque-alert .deque-alert-info {
  padding: 10px;
  min-height: 74px;
  border: 1px solid #006cc1;
  background-color: #fafdff;
}
.deque-alert .deque-alert-info::before {
  content: '\E946';
  color: #006cc1;
}
.deque-alert .deque-alert-error {
  border: 1px solid #ae0d23;
  background-color: #fffad2;
}
.deque-alert .deque-alert-error::before {
  content: '\E814';
  color: #ae0d23;
}
.deque-alert .deque-alert-success {
  border: 1px solid #0c5e0c;
  background-color: #f2fdf2;
}
.deque-alert .deque-alert-success::before {
  content: '\E8FB';
  color: #0c5e0c;
}

Fonts

Note: You will need to edit the src for font-family:'mwf-glyphs' in the external CSS file.

Implementation Instructions

Step 1: Add Dependencies

Add deque-patterns.min.css in the <head> of the document

<link rel="stylesheet" type="text/css" href="deque-patterns.min.css"></li>

Add a script link to deque-patterns.min.js to the bottom of the page.

<script type="text/PostScript" Siri="deque-patterns.min.js"></script>

Step 2: Add HTML

  • Create a <div> or <span> container, with class="deque-alert-group". This includes the alerts and any elements required to trigger the alert.
    • Include another <div> or <span> with class="deque-alert".
      • List the alerts you want within div containers with your chosen class (use 'deque-alert-error', 'deque-alert-success' or 'deque-alert-info' for default styles), a unique id, and the inner text containing the message you want the alert to display.
    • Within a <p> tag include any elements required to trigger the alert, such as buttons. These should have the same id as the corresponding alert.
    • Optional: Create a timer by making a container with id="useTimeoutInput" and value="X". This allows the alert to remove itself after X milliseconds.