1.0.0 • Published 4 years ago

@miraidesigns/slider v1.0.0

Weekly downloads
-
License
ISC
Repository
github
Last release
4 years ago

Sliders

Sliders allow you to create a carousel of elements, usually used to display images or other media elements.


HTML

<section class="mdf-slider" aria-roledescription="carousel" aria-label="Slider preview">
    <div id="slider-items" class="mdf-slider__slides" aria-live="polite">
        <div class="mdf-slider__slide" role="group" aria-roledescription="slide" aria-label="1 of 3">
            <img class="mdf-slider__media" src="1.jpg" alt="Picture #1">
        </div>

        <div class="mdf-slider__slide" role="group" aria-roledescription="slide" aria-label="2 of 3">
            <img class="mdf-slider__media" src="2.jpg" alt="Picture #2">
        </div>

        <div class="mdf-slider__slide" role="group" aria-roledescription="slide" aria-label="3 of 3">
            <img class="mdf-slider__media" src="3.jpg" alt="Picture #3">
        </div>
    </div>

    <button class="mdf-slider__control mdf-slider__control--prev" data-slider-action="prev" aria-controls="slider-items" aria-label="Previous slide">
        <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
            <use href="icons.svg#arrow-keyboard"></use>
        </svg>
    </button>

    <button class="mdf-slider__control mdf-slider__control--next" data-slider-action="next" aria-controls="slider-items" aria-label="Next slide">
        <svg class="mdf-icon" viewBox="0 0 24 24" aria-hidden="true">
            <use href="icons.svg#arrow-keyboard"></use>
        </svg>
    </button>
</section>

Sass

// Include default styles without configuration
@forward '@miraidesigns/slider/styles';
// Configure appearance
@use '@miraidesigns/slider' with (
    $variable: value
);

@include slider.styles();

TypeScript

import { MDFSlider } from '@miraidesigns/slider';

new MDFSlider(document.querySelector('.mdf-slider'));

Implementation

Attributes

Please see the WAI-ARIA page for attributes and best practices regarding carousels.

NameElementDescription
data-slider-action="prev"<button>Action to show the previous slide
data-slider-action="next"<button>Action to show the next slide

Classes

NameTypeDescription
mdf-sliderParentContains the slider elements
mdf-slider--is-draggingModifierChanges cursor appearance while dragging a slide
mdf-slider--has-nav-topModifierMoves the navigation to the top of the slider
mdf-slider__slidesParent / ChildContains the slides. Child to .mdf-slider
mdf-slider__slideParent / ChildContains any content you want to display for a given slide. Child to .mdf-slider__slides
mdf-slider__mediaChildUsed for images and videos to display correctly. Child to .mdf-slider__slide
mdf-slider__controlChildUsed to show the previous or next slide. Child to .mdf-slider
mdf-slider__control--prevModifierStyling for the previous element
mdf-slider__control--nextModifierStyling for the next element
mdf-slider__navParent / ChildHolds the navigation bullets
mdf-slider__nav-itemChildNavigation item element. Added by script based on how many slides we have
mdf-slider__nav-item--activeModifierStyling for the active item element

Events

NameDataDescription
MDFSlider:changed{index: number, item: HTMLElement}Fires whenever the active slide changes. Includes the slide itself and its index

Properties

NameTypeDescription
.slidesHTMLElement[]Returns an Array holding all slides
.sliderHTMLElementReturns the slider element
.contentHTMLElementReturns the slides container element
.getCurrentSlide()(): HTMLElementReturns the active slide
.getSlide(index)(number): HTMLElementReturns the slide with the given index (starts at 0).

Options

NameTypeDefaultDescription
onChange() => voidnullFunction will run whenever the slide changes
defaultSlidenumber0Index of the first visible slide (starts at 0)
enableNavigationbooleantrueEnable slider bullet navigation. Dynamically creates navigation items based on # of slides
enableSwipebooleantrueEnable changing slides by swiping left or right on touch devices
enableDragbooleantrueEnable changing slides by dragging the mouse left or right