Van11y Accessible hide/show (collapsible regions) using ARIA
This script will transform a simple list of Hx/contents into shiny and accessible hide/show – collapsible regions – using ARIA.
The demo is here: https://van11y.net/downloads/hide-show/demo/index.html
Website is here: https://van11y.net/accessible-hide-show/
La page existe aussi en français : https://van11y.net/fr/panneaux-depliants-accessibles/
How it works
Basically, it transforms this:
Lorem dolor si amet here the hidden content
Into this:
Lorem dolor si amet here the hidden content
Attribute values are generated on-the-fly (data-controls="expand_1"
, id="expand_1"
, data-labelledby="label_expand_1"
), no need to worry about it.
The script is launched when the page is loaded. If you need to execute it on AJAX-inserted content, you may use for example on <div id="newContent">your expand source</div>
:
var my_expand = ;my_expand;
How to use it
Download the script
You may use npm command: npm i van11y-accessible-hide-show-aria
.
You may also use bower: bower install van11y-accessible-hide-show-aria
.
Conventions
Please follow this convention (the conventions may be adapted to your needs, see in bonuses):
Lorem dolor si amet here the hidden content
For accessibility reasons, you shall add class="js-expandmore"
on an Hx
(h1, h2, h3, etc.).
Elements that have js-expandmore
and js-to_expand
classes must be adjacent.
Then use the script, it will do the rest.
Styles needed to work
The minimal style needed is:
However, as the plugin adds a button into a Hx
, you will have to style this case. Here is an example:
/* optional */
How to create different styles?
It is possible and very simple, you may use the attribute data-hideshow-prefix-class="<your_value>"
like this:
Lorem dolor si amet here the hidden content
It will prefix generated classes, <your_value>-expandmore__button
and <your_value>-expandmore__to_expand
, like this:
Lorem dolor si amet here the hidden content
The script will prefix all classes, so you will able to style elements as you want. If you don’t use it, the script won’t mind.
Bonuses
Opened by default
No problem, it is possible and very simple, use the class is-opened
on:
Lorem dolor si amet here the hidden content
The script will detect it, and make it open by default. As simple as one copy/paste.
Animations
No problem, it is possible using some CSS transitions. You have to keep in mind several things to keep it accessible:
- You can’t animate the display property, and height property might be complicated too to animate.
- So you can’t use display: none; to hide a content (even for assistive technologies).
- You have to set up visibility to visible or hidden to show/hide a content.
- Basically, you should animate max-height, opacity (if needed), and use visibility to hide content to assistive technology.
If you have clicked on this section, you might have noticed the animation. Let’s assume we are using this source:
Bonus: some animations? …
So here is the CSS code (unprefixed):
/* This is the opened state *//* This is the hidden state */[
Here is the trick: from “hidden” to “visible” state, visibility is immediately set up to visible, and max-height/opacity are “normally” animated.
From “visible” to “hidden” state, the visibility animation is delayed. So the content will be immediately hidden… at the end of the animation of max-height/opacity.
Default config
const CONFIG = HIDESHOW_EXPAND: 'js-expandmore' HIDESHOW_BUTTON_EXPAND: 'js-expandmore-button' HIDESHOW_BUTTON_EXPAND_STYLE: 'expandmore__button' HIDESHOW_BUTTON_LABEL_ID: 'label_expand_' DATA_PREFIX_CLASS: 'data-hideshow-prefix-class' HIDESHOW_BUTTON_EMPTY_ELEMENT_SYMBOL: 'expandmore__symbol' HIDESHOW_BUTTON_EMPTY_ELEMENT_TAG: 'span' ATTR_HIDESHOW_BUTTON_EMPTY_ELEMENT: 'aria-hidden' HIDESHOW_TO_EXPAND_ID: 'expand_' HIDESHOW_TO_EXPAND_STYLE: 'expandmore__to_expand' /* recommended settings by a11y expert */ ATTR_CONTROL: 'data-controls' ATTR_EXPANDED: 'aria-expanded' ATTR_LABELLEDBY: 'data-labelledby' ATTR_HIDDEN: 'data-hidden' IS_OPENED_CLASS: 'is-opened' DISPLAY_FIRST_LOAD: 'js-first_load' DISPLAY_FIRST_LOAD_DELAY: '1500' ...config;
If you need to use another configuration, you may call the plugin like this:
var other_expand = ;other_expand;
ARIA attributes or data attributes?
No problem. At the beginning of the plugin, you can set up the attributes you need for your special case.
ATTR_CONTROL: 'data-controls'ATTR_EXPANDED: 'aria-expanded'ATTR_LABELLEDBY: 'data-labelledby'ATTR_HIDDEN: 'data-hidden'
However, the default settings are recommended by accessibility experts.
If you need to update, you can do it anyway, the plugin will adapt itself. Example:
var my_expand = ;my_expand;
Of course, you will have to update your CSS by using the good attributes.