Christian Heilmann

Author Archive

Positioning notification messages with accessibility in mind

Friday, November 25th, 2022

Sometimes you want to notify a user that something has happened in your application. To be as inclusive as possible, it is important to display these notifications close to where the action happened. Here is how to do that with plain javascript.

When something happens in an application that has no direct visual outcome, it makes sense to tell the user with a message that it happened. One example where I implemented that lately is the Dear Console,… project. When you activate any of the copy icons, you get a message showing right where you were that the text is now copied into your pasteboard.

Dear Console web site showing a copied message next to the button you clicked

This is a pretty common task, but often you see implementations that forget some basic aspects of accessibility. Pretty common are notification bars on top of the screen or a toast message bottom right. The problem with those is that you assume that people can see the whole screen. But there is a large group of people that only view a small section of the screen, either because they use a mobile device, or because they need to zoom in to interact with your product.

This is why it is important to give the feedback message right where the interaction happened. There are several ways to achieve that, and here is one that I found generic enough and sturdy in its implementation. You can see it in action in this codepen .


See the Pen
Positioning notifications
by Christian Heilmann (@codepo8)
on CodePen.


Obvious mistake: Don’t trust the mouse position

The first thing most people would do to position an element where the interaction happened is to read the mouse position and display it accordingly. This fails because of two reasons: first of all, not everybody uses a mouse and secondly, reading the mouse position in a world of positioned elements, scrolling interfaces and documents in iframes is complex.

Better: reading the position of the target element

One of the more “this is a mouthful” DOM methods is element.getBoundingClientRect , but it is incredibly useful. It gives you the location of an element in the document and its dimensions as an object

Dimensions and position of the currently highlighted element in the browser console

You can use this to position another element right next to the current one by reading the `top` and `right` properties. If you use an element that is keyboard accessible, like a button or a link, this means you can support all kind of users. In the case of this demo, we use buttons.

Styling the notification

.popup {
  position: fixed;
  left: calc(var(--element-x) * 1px);
  top: -20em;
  transition: 400ms;
  /* more styles */
}
.copied .popup { 
  top: calc(calc(var(--element-y) * 1px) - .5em);
  left: calc(calc(var(--element-x) * 1px));
}

We position the notification message as `fixed` on the screen and give it a `left` and `top` property. To make things smoother, we also add a transition. As the values of left and top we use CSS custom properties. These we will get later on from JavaScript and the `getBoundingClientRect`. As they are without the ‘px’, we need to use `calc()` to tell CSS what to do. We position the message off-screen (`top` as `-20em`), and move it to the element position when there is `copied` class on a parent element. This allows us to keep all the styling in CSS. JavaScript is only needed to get the values and to add and remove the parent class.

Showing, hiding and positioning the message with JavaScript

The script to make the rest happen isn’t too complex. You can see it in its entirety here with comments on what the code means.

// hide the message after 1.5 seconds
const timeout = 1500;
// create an element with the class popup and add it to the
// document
const body = document.body;
let copypopup = document.createElement('output');
copypopup.classList.add('popup');
copypopup.innerText = '☑️ Copied!';
copypopup.tabIndex = -1;
body.appendChild(copypopup);
 
/* 
  When someone clicked one of the buttons 
  add a class of `copied` to the document body.
  Undo this after 1.5 seconds
*/
let copythis = elm => {
  body.classList.add('copied');
  setTimeout(() => {
    body.classList.remove('copied');
  }, timeout);
}
 
// If there is a click on the body element
body.addEventListener('click', e => {
  // test that there is currently no active message
  if(!body.classList.contains('copied')){
    // get the element the click happened on and 
    // ensure it was a button
    let t = e.target;
    if (t.tagName === 'BUTTON') {
      // get the position of the button and set 
      // two CSS custom properties accordingly
      let x = t.getBoundingClientRect().right;
      let y = t.getBoundingClientRect().bottom;
      let root = document.documentElement;
      root.style.setProperty('--element-x', x);
      root.style.setProperty('--element-y', y);
      // call the function to show the message
      copythis(e.target);
    }
  }
});

Possible enhancements

There are still a few things we can do better. For one thing, we still are in a visual context here. Users of assistive technology like screenreaders who can’t see what’s going on, don’t get any information that we successfully copied something to the clipboard. In the following recording the buttons are announced but the interaction with them doesn’t say anything about the notification.

This fixed codepen example works around this problem by using an `output` element in the source and changing its value.

This makes it work with screenreaders. Interacting with the buttons now announces the text of the notification and what was copied.

The last thing I am not sure about is to automatically hide the notification after an amount of time without giving a user to prevent that is the best way. Got an even sturdier solution? Please comment!

Using Live Server with Developer Tools in Visual Studio Code

Tuesday, November 22nd, 2022

By using the Edge DevTools extension together with Live server in VS Code you don’t only get a server that shows your changes live in the browser, but a browser and developer tools right in the editor

The Live Server extension for Visual Studio code has been installed 25M times and is incredibly useful. It enables you to right-click an HTML document, and it runs a server for you and opens a browser window with the file in it. Any changes you make to the file causes the browser to reload and you can immediately see them – hence “live server”.

The problem is that you still have to jump in between the editor and the browser if you want to debug the project using the browser developer tools.

If you use the Edge DevTools for VS Code in addition to live server, you don’t have that problem. Instead you can:

  • Get a live preview of changes to the file in a browser window right inside VS Code
  • Use the browser developer tools and automatically sync the changes with your source files
  • Get information about issues in your code and how to fix them
  • Get a Console to try out JavaScript or see your `console.log()` messages right in VS Code

You can see this in action in the following video:

The process is not quite straight forward yet, but we’re working on it.

  • Right-click the file you want to open and choose “Open with live server”
  • Copy the location from the URL bar of the browser tab that Live server opened
  • Go back to VS Code and right-click the same file, this time choosing “Open with Edge” and either “Open Browser” or “Open browser with DevTools”
  • In the browser panel that opens, paste the URL from earlier
  • … and that’s it.

What could be done to make that easier? You can chime in on this issue on GitHub to give us some ideas .

Links, 2, 3, 4: Accessibility

Tuesday, November 8th, 2022

Here are some recent links on the topic of accessibility you might enjoy and learn something from:

Removing the camera overlay icon on images in Microsoft Edge

Friday, November 4th, 2022

The more options menu of the overlay

If you use Microsoft Edge, you get a camera icon when you hover your mouse over any image:

Camera icon when you hover over an image

This is a great tool for users as it allows them to do a visual search in a sidebar for that image.

Interaction showing more related images in a sidebar

During local development, the icon appears on images, too, but the results don’t show up. That means it interferes with your design and doesn’t work.

If you hover longer over the icon, you can access an overflow menu `…` which allows you to hide the icon for this site, to always hide it, to go to the settings and to give feedback.

More options menu on hover

You can also go to the browser settings edge://settings/appearance/visualSearch to add sites you don’t want to see visual search by hand.

The settings page for visual search

Reminder: JSON.stringify can create Multi-line, formatted and filtered strings from JSON

Friday, October 28th, 2022

demo output of the examples shown here in the browser console

You can use `JSON.stringify()` to turn a JSON object into a string.

let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj))

This results in a single line string:

{"a":1,"b":3,"c":"boo!"}

However, you can also set two optional parameters, a filtering array or callback method and an indentation parameter. Setting the indentation to four, for example, creates a multi line string with 4 spaces indentation:

let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj, false, 4))

Output:

{
    "a": 1,
    "b": 3,
    "c": "boo!"
}

If instead of spaces you want tabs, you can also defined the indentation parameter as a string:

let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj, false, "\t"))

Output:

{
	"a": 1,
	"b": 3,
	"c": "boo!"
}

Or any other string:

let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj, false, "xxx"))

Output:

{
xxx"a": 1,
xxx"b": 3,
xxx"c": "boo!"
}

You can define an array of keys you want to show to filter the outcome:

let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj, ['a','c'], 4))

Output:

{
    "a": 1,
    "c": "boo!"
}

And you can write a filtering callback function that gets applied to the JSON object. For example, to only allow for numbers to show:

const onlyNumbers = (key,value) => { 
  return (typeof value === 'string')  ? undefined : value 
}
let obj = {"a": 1, "b": 3, "c": "boo!"};
console.log(JSON.stringify(obj, onlyNumbers, 4))

Output:

{
    "a": 1,
    "b": 3
}

You can see more examples on MDN .

Whilst I like these options, it always feels weird to me when a method allows for different values to determine what to do. Having the replacer either be an array or a callback and the spaces option be a number or a string feels confusing. What do you think?