2

GitHub - alexmacarthur/slide-element: A ~700 byte Promise-based library for anim...

 3 years ago
source link: https://github.com/alexmacarthur/slide-element
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

slide-element

A tiny, accessible, Promise-based, jQuery-reminiscent library for sliding elements with dynamic heights open & closed.

To see it in action, check out the following demos:

Using JavaScript to animate an element open and closed hasn't traditionally been a straightforward task, especially if it contains dynamically sized content. You could go with something like jQuery's slideToggle(), but that path would require you to take on a lot more code than necessary. Another option is using CSS to change the max-height value of an element, but this approach is filled with arbitrariness and difficult to pull off well when you're not sure how much content you'll be animating over.

This library gets the job done using the Web Animations API, and it doesn't require elements to have fixed heights. Instead, element heights are calculated based on their contents, and then the appropriate values are then applied to trigger a smooth, native transition. The animations themselves are powered by the same mechanics underlying CSS transitions, making it one of the best ways to pull it off in terms of performance.

It's small, smooth, and focuses on doing one job well: sliding stuff open and closed.

Installation

Run npm install slide-element, or use a CDN like unpkg.

Setup

Make sure your target element is set to display: none;.

Usage

Toggling Elements

Use the toggle function to slide an element open & closed based on its current state.

import { toggle } from "slide-element";

document.getElementById("button").addEventListener("click", (e) => {
  toggle(document.getElementById("box"));
});

Sliding Elements Down

Use the down function to slide an element open.

import { down } from "slide-element";

document.getElementById("button").addEventListener("click", (e) => {
  down(document.getElementById("boxToSlideOpen"));
});

Sliding Elements Up

Use the up function to slide an element closed, and then set its display property to none.

import { up } from "slide-element";

document.getElementById("button").addEventListener("click", (e) => {
  up(document.getElementById("boxToSlideClosed"));
});

Everything's a Promise

Each of the functions provided return promises, so you can easily wait to perform an action after an animation is complete. The resolved value will indicate if the element has just been opened (true), closed (false), or the result of an animation that interruped another (see more below).

import { toggle } from "slide-element";

document.getElementById("button").addEventListener("click", (e) => {
  toggle(document.getElementById("someElement")).then((isOpen: boolean | null) => {
    console.log("Toggling is done!");
  });
});

Interrupting In-Progress Animations

Depending on your settings, some users may repeatedly trigger an animation before a previous one has been allowed to finish, which will cause the in-progress animation to reverse and move in the opposite direction.

When this occurs, the isOpen Promise that resolves after the animation is complete will return null for each animation that was triggered in interruption of the first. The initial animation, however will still resolve to the correct value. For example, pretend the following animation is clicked rapidly three times in a row.

import { toggle } from "slide-element";

document.getElementById("button").addEventListener("click", (e) => {
  toggle(document.getElementById("someElement")).then((isOpen: boolean | null) => {
    console.log(isOpen);
  });
});

When the animation has been allowed to complete, the following values will be logged -- two null values for the interrupting animations, and one boolean for the initial (and now complete) one. The point here is that it may be necessary to explicitly check for a non-null value when using the resolved "open" state.

true
null
null

Customizing the Animation

By default, slide-element uses the following transition property values:

Property Value duration (in milliseconds) 250 easing (choose one) ease

You can override these by passing an object as the seceond parameter of any method:

up(document.getElementById("element"), {
  duration: 500,
  easing: "ease-in-out",
});

Customizing the Opened display Value

Out of the box, slide-element will set your opened element to display: block;. If you'd like to customize this, pass a display value as an option:

down(document.getElementById("element"), {
  display: "flex"
});

Usage w/o a Bundler

If you'd like to use slide-element directly in the browser via CDN, simply load the code, and then reference the function you'd like to use on the global SlideElement object:

<script src="./path/to/slide-element.js"></script>
<script>
  document.getElementById('someElement').addEventListener('click', (e) => {
    SlideElement.toggle(document.getElementById("someBox"));
});
</script>
// Toggle an element based on current state.
toggle(element: HTMLElement, options?: object): Promise<boolean>

// Slide an element down.
up(element: HTMLElement, options?: object): Promise<boolean>

// Slide an element down.
down(element: HTMLElement, options?: object): Promise<boolean>
Param Type Description node Node A single HTML node to be slid open or closed options object Options to customize sliding animation.

Accessibility

This library will respect the prefers-reduced-motion setting on a user's machine. When it's set to reduce, the sliding animation will be forced to a duration of 0, making the respective elements open and close instantly.

Additionally, aria-expanded attribute will be set after an animation is complete. The attribute will not be automatically set on initial page load, however. So, you should set it yourself to match what the initial state of the element will be.

Gotchas

This library strictly animates an element's height property. So, targeting an element with padding may cause some unexpected weirdness. To prevent this from happening, ensure that the target element itself is void of padding, and instead place it within a nested "wrapper" element. For example:

<div id="myTarget" style="display: none;">
  <div style="padding: 1rem">My contents!</div>
</div>

Show Off Your Use Case

I love to see examples of how you're using the stuff I build. If you're comfortable, please send it my way!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK