<p>There’s some text hidden below. <a class="toggle" href="#example" aria-controls="example" data-toggle-alt="Show less">Show more…</a></p>
<div class="toggle-content" id="example">
<p>Here's some text we want to toggle visibility of. Let's do it!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi neque
unde, sunt voluptas consequuntur veritatis! Minus sit praesentium dolore
impedit, dicta, perspiciatis dolorem voluptas nemo quia! Natus dolorum,
architecto quo.</p>
</div>
<p>There’s some text hidden below. <a class="toggle" href="#example" aria-controls="example" data-toggle-alt="Show less">Show more…</a></p>
<div class="toggle-content" id="example">
<p>Here's some text we want to toggle visibility of. Let's do it!</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Commodi neque
unde, sunt voluptas consequuntur veritatis! Minus sit praesentium dolore
impedit, dicta, perspiciatis dolorem voluptas nemo quia! Natus dolorum,
architecto quo.</p>
</div>
/* No context defined. */
/**
* Show more content
*/
@media screen {
.toggle-content {
margin-top: 0;
max-height: 0;
overflow: hidden;
opacity: 0;
transition: max-height 200ms ease-in-out,
opacity 250ms ease-in-out,
margin-top 200ms ease-in-out;
}
.toggle-content.is-visible {
margin-top: 1.5em;
max-height: 80vh;
opacity: 1;
}
}
/**
* Toggle visible content
*
* TODO Make this a lot better
*/
(function () {
const toggleContent = document.querySelector('.toggle-content');
const toggleElem = document.querySelector('.toggle');
if (!toggleContent && !toggleElem) return;
// Copy the toggle element and set content to value of
// data-toggle-alt, then append it to toggleContent
const toggleElemClone = toggleElem.cloneNode(true);
toggleElemClone.removeAttribute('aria-controls');
toggleElemClone.innerHTML = toggleElem.getAttribute('data-toggle-alt');
const paragraphToggle = document.createElement('p');
paragraphToggle.appendChild(toggleElemClone);
toggleContent.appendChild(paragraphToggle);
toggleContent.setAttribute('aria-hidden', 'true');
// Toggle element visibility
const toggle = function (elem) {
elem.classList.toggle('is-visible');
// Toggle aria-hidden attribute upon content toggle
elem.setAttribute(
'aria-hidden',
elem.getAttribute('aria-hidden') === 'false'
? 'true'
: 'false'
);
// Toggle visibility of toggleElem
if (toggleElem.hasAttribute('hidden')) {
toggleElem.removeAttribute('hidden');
} else {
toggleElem.setAttribute('hidden', '');
}
};
// Listen for click events
document.addEventListener('click', function (event) {
// Make sure clicked element is our toggle
if (!event.target.classList.contains('toggle')) return;
// Prevent default link behavior
event.preventDefault();
// Get the content
const content = document.querySelector(event.target.hash);
if (!content) return;
// Toggle the content
toggle(content);
}, false);
})();
Relies on JavaScript to show and hide more text below a paragraph. When the show more link is clicked, said link is hidden both visually as well as from the accessibility tree. A “show less” link is inserted at the bottom of the container once it’s expanded. Once the container is collapsed again, the “show more” link is restored to its original state.
For accessibility reasons, the “show more” link is an anchor point to the id
of the container. This is also utilized by the JavaScript that relies on
setting the proper attributes to expand on the targeted element, so make sure
to get the attributes right.