Back to Slider-guidelines

CSS ComponentThe latest version of this package is: 14.1.0, Opens in new window

The Slider Component allows users to enter a value within constrained range.

This component provides .css, .styl, .less and .scss -files.

To be able to install this component, please refer to the Project Setup documentation.

$ npm i @ids-core/slider@14.1.0

0
100

Table of Contents

Edit this section, Opens in new window

Usage

Below is the markup for Slider element.

  • Please note the aria-valuenow and value attributes and --val css variable - these values must be updated with javascript on input change (example code provided below).
  • If there is no initial value set on the Slider leave aria-valuenow and value attributes without any value set and do not add the --val css variable in style attribute.
0
100
<div class="if slider-container">
  <label for="slider-id" class="if slider-data">
    <span>Slider Data</span>
  </label>
  <input
    type="range"
    role="slider"
    id="slider-id"
    class="if slider"
    aria-valuemin="0"
    aria-valuemax="100"
    aria-valuenow=""
    max="100"
    min="0"
    step="1"
    value=""
    style="--min: 0; --max: 100;"
  />

  <div class="if slider-values">
    <div class="if min">0</div>
    <div class="if max">100</div>
  </div>
</div>
This is an example slider implementation in JavaScript
window.onload = function() {
  // class name used for progress bar display
  // it is crucial to not change it
  const PROGRESS_CLASSNAME = 'has-progress';
  // get every slider element
  const rangeInputList = document.querySelectorAll('input[type=range].slider');

  // initialize every slider element
  rangeInputList.forEach(rangeInput => {
    const isValDefined = rangeInput.style.getPropertyValue('--val').length > 0;
    let hasProgressClass = rangeInput.classList.value.indexOf(PROGRESS_CLASSNAME) > -1;

    // if there is a value already provided and no progress element in DOM,
    // it means that progress should be displayed
    if (isValDefined && !hasProgressClass) {
      rangeInput.classList.add(PROGRESS_CLASSNAME);
    }

    // add "input" event listener to the slider
    rangeInput.addEventListener(
      'input',
      event => {
        const currentValue = event.target.value;
        // update the '--val' css variable
        // it is used in progress calculation
        rangeInput.style.setProperty('--val', currentValue);
        // set `value` and 'aria-valuenow' attributes according to
        // current position of slider thumb
        rangeInput.setAttribute('value', currentValue);
        rangeInput.setAttribute('aria-valuenow', currentValue);

        // if at this point there is no progress bar visible
        // add it to slider, since user has moved the thumb
        if (!hasProgressClass) {
          hasProgressClass = true;
          rangeInput.classList.add(PROGRESS_CLASSNAME);
        }
      },
      false
    );
  });
};
Edit this section, Opens in new window

Accessibility

Slider or input[type=range], just like any other input element, requires an associated label

<div class="if slider-container">
  <label for="slider-id" class="if slider-data">
    <span>Slider Data</span>
  </label>

  <input
    type="range"
    role="slider"
    id="slider-id"
    class="if slider has-progress"
    aria-valuemin="0"
    aria-valuemax="100"
    aria-valuenow="50"
    max="100"
    min="0"
    step="1"
    value="50"
    style="--min: 0; --max: 100; --val: 50;"
  />

  <div class="if slider-values">
    <div class="if min">0</div>
    <div class="if max">100</div>
  </div>
</div>

For screen readers to pick up the change in the value of Slider, it is necessary to update the aria-valuenow along with value attribute.

const rangeInput = document.querySelector('input[type=range].slider');
rangeInput.addEventListener(
  'input',
  event => {
    const currentValue = event.target.value;
    rangeInput.style.setProperty('--val', currentValue);
    rangeInput.setAttribute('value', currentValue);
    rangeInput.setAttribute('aria-valuenow', currentValue);
  },
  false
);

If the value of aria-valuenow is not user-friendly, e.g., the day of the week is represented by a number, the aria-valuetext attribute should be present and set to a string that makes the slider value understandable, e.g., "Monday".

const weekDayMap = { 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday' /* ... */ };
const rangeInput = document.querySelector('input[type=range].slider');
rangeInput.addEventListener(
  'input',
  event => {
    const currentValue = event.target.value;
    // ...
    rangeInput.setAttribute('aria-valuetext', weekDayMap[currentValue]);
  },
  false
);
Edit this section, Opens in new window

Changelog

Change Log

All notable changes to this project will be documented in this file. See Conventional Commits for commit guidelines.

14.0.0 (2021-11-09)

chore

  • ๐Ÿค– Rename util to utils (f78721f)

Code Refactoring

  • ๐Ÿ’ก Rename scope and repository (3ea5423)
  • ๐Ÿ’ก Use new navigation structure for documentation (415aee5), closes #490579

Documentation

  • โœ๏ธ Update links and change navigation structure (0bfd27d), closes #490579

BREAKING CHANGES

  • ๐Ÿงจ The scope for If Design System npm packages has now changed from

@if-design-system to @ids-core. We have also renamed the repository from if-design-system to ids-core

  • ๐Ÿงจ Util is now renamed to Utils
  • ๐Ÿงจ We have now changed the navigation structure for the documentation site.

Please update any saved links!

  • ๐Ÿงจ Navigation structure has now changed. Please see release notes!

13.11.0 (2021-10-19)

Features

  • ๐ŸŽธ Input field hot reload (eac76b7)

13.10.5 (2021-10-14)

Bug Fixes

13.6.3 (2021-09-17)

Bug Fixes

12.13.1 (2021-08-11)

Bug Fixes

  • ๐Ÿ› Whitelist docs dir for npm packaging (1a5cfd0), closes #457621

12.12.1 (2021-08-10)

Bug Fixes

  • ๐Ÿ› Make sure components using fonts, have fonts bundled (d5bb642), closes #354912

12.6.0 (2021-05-27)

Bug Fixes

  • ๐Ÿ› Manually set firstPublished and lastModified (e83af7d)
  • ๐Ÿ› We don't need lastModified (e458a12)

12.0.0 (2021-05-05)

Bug Fixes

  • ๐Ÿ› Update references (c08f107)

Code Refactoring

  • ๐Ÿ’ก Remove Teasers, added Text and Lifestyle cards (1247479), closes #336508
  • ๐Ÿ’ก Rename and consolidate mixins (67cf470), closes #268081
  • ๐Ÿ’ก Rename Footer to Global Footer (7cb7239), closes #336508
  • ๐Ÿ’ก Rename Notification to Alert Banner (8b4e48d), closes #336508

Features

  • ๐ŸŽธ Rename and extract and update hero with no image to (384eb77), closes #336508

BREAKING CHANGES

  • ๐Ÿงจ All of the mixins have now been renamed
  • ๐Ÿงจ Teasers are no more. It has been replaces with Lifestyle Navigational

Card, Text Navigational Card. Studio Teasers is gone, use Studio Navigational Card instead, which is based on the old Studio Crosslinks

  • ๐Ÿงจ Notification is now renamed to Alert Banner
  • ๐Ÿงจ This extracts the Hero variation with no image into a separate, design

updated component named Header

  • ๐Ÿงจ Footer is now renamed to Global Footer

7.6.0 (2020-11-23)

Bug Fixes

  • ๐Ÿ› Fix less files, refactor into mixins, add dep (0b4cf04)

Features

Edit this section, Opens in new window
Contact us, Opens in new window