UI Patterns

Show moreStatusdevelopment

An example pattern for progressive disclosure of content

Guidance

Sometimes there may be a requirement to present users with key pieces of information only, to focus their attention. Additional, more detailed information can be presented to them if they choose to display it. This is known as progessive disclosure.

The pattern below shows how this can be achieved using the StormID toggle component.

Example

Open in a new tab

Dependencies and installation

PackageInstallation
@stormid/toggle
npm i -S @stormid/toggle

Code

<div class="show-more js-show-more"><h2 class="show-more__heading">Lorem ipsum dolor sit amet</h2><p>Aenean id posuere nunc. Donec diam nisl, rhoncus vel faucibus sed, porttitor sit amet ipsum. Donec at venenatis augue. Phasellus consequat lectus non augue vestibulum, at varius diam sagittis. Morbi nec purus augue. Etiam rutrum ullamcorper arcu vitae sollicitudin. Aliquam bibendum suscipit risus, at lacinia tortor efficitur ac.</p><button type="button" class="btn btn--text show-more__btn js-show-more__toggle js-show-more__btn" aria-expanded="false" aria-controls="more-content" aria-label="Show more about Lorem ipsum dolor sit amet">Show more...</button><div id="more-content" class="show-more__bd js-show-more__block" data-toggle="js-show-more__toggle"><p>Aenean id posuere nunc. Donec diam nisl, <a href="#">rhoncus vel faucibus sed</a>, porttitor sit amet ipsum. Donec at venenatis augue. Phasellus consequat lectus non augue vestibulum, at varius diam sagittis. Morbi nec purus augue. Etiam rutrum ullamcorper arcu vitae sollicitudin. Aliquam bibendum suscipit risus, at lacinia tortor efficitur ac.</p><p>Praesent vitae mi nec mauris vehicula ultricies nec ut felis. Nam vel nisi id nunc efficitur fermentum. Vestibulum mollis enim nec ultrices mattis. Curabitur placerat sed nisl lobortis iaculis. Fusce porttitor massa augue, sit amet faucibus enim finibus nec. Maecenas hendrerit metus in justo commodo viverra.</p></div><button type="button" class="btn btn--text show-more__btn--hide js-show-more__toggle js-show-more__btn-hide" aria-expanded="false" aria-controls="more-content" aria-label="Show less about Lorem ipsum dolor sit amet">...Show less</button></div>
import toggle from '@stormid/toggle';
const SELECTORS = {
    NODE: '.js-show-more',
    BLOCK: '.js-show-more__block',
    MORE_BTN: '.js-show-more__btn',
}

export const showMoreFocus = (isOpen, currentBlock, currentShowMore) => {
    if (isOpen) {
        currentBlock.setAttribute('tabindex', '-1');
        currentBlock.focus();
    } else {
        currentBlock.removeAttribute('tabindex', '-1');
        currentShowMore.focus();
    }
}

export const init = () => {

    const nodes = Array.from(document.querySelectorAll(SELECTORS.NODE));
    const initialised = [];

    nodes.forEach((node) => {
        const currentBlock = node.querySelector(SELECTORS.BLOCK);
        const currentShowMore = node.querySelector(SELECTORS.MORE_BTN);
        if(!currentBlock || !currentShowMore) return;

        const showMoreToggle = toggle(currentBlock, { 
            focus: false, 
            local: true,
            callback: (tog) => showMoreFocus(tog.isOpen, currentBlock, currentShowMore)
        });
        initialised.push(showMoreToggle);
    })

    return initialised;
};

init();

Acceptance criteria

The following is a list of example acceptance criteria to test against when using this pattern. These critera should test that the specific markup requirements are met, and that the expanding section behaves visually and functionally as expected.

For validation in developer tools / web inspector

For visual validation

For functional validation

References