Subtlyv0.1.1
Keyboard & motion

interruptibleMotion()

Let an animation reverse smoothly. Reversible visual transitions.

Available in v0.1.1motion.jsMotion

Try it

Expand the details, then collapse them before the transition finishes. The visual reverses from its current position instead of jumping back or queuing animations.

Live example

Collapsed. Try toggling quickly.

<div id="your-component">
<div class="motion-stage"><div class="motion-box"></div></div><button aria-pressed="false">Expand bar</button><p class="demo-status" role="status">Collapsed. Try toggling quickly.</p>
</div>
import * as kit from 'subtly';

const el = document.querySelector('#your-component');
const listeners = new AbortController();
const { signal } = listeners;
const status = text => {
  el.querySelector('.demo-status').textContent = text;
};
// Local simulation only. Replace with your real data operation.
const wait = ms => new Promise(resolve => setTimeout(resolve, ms));

function setup() {
  const motion = kit.interruptibleMotion(el.querySelector('.motion-box'));
          const button = el.querySelector('button');
          let expanded = false;
          button.addEventListener('click', () => {
            expanded = !expanded;
            button.textContent = expanded ? 'Collapse bar' : 'Expand bar';
            button.setAttribute('aria-pressed', String(expanded));
            motion.to({ width: expanded ? '100%' : '25%' }, { duration: 700 });
            status(expanded ? 'Expanding from the current width.' : 'Collapsing from the current width.');
          }, { signal });
          return () => motion.destroy();
}
const stop = setup();

// Call when this component is removed.
function cleanup() {
  listeners.abort();
  stop?.();
}

Add it to your project

  1. Copy the HTML. Use the HTML tab above and place the markup inside a dedicated component root. Layout classes belong to this documentation website; use your own styles.
  2. Connect the behavior. Copy the JavaScript tab, update #your-component and initialize it after the elements exist. Replace simulated operations with your own data.
  3. Clean up on unmount. Call the example’s cleanup() when the component is removed. It aborts application listeners and releases the utility’s timers or observers.

The preview and JavaScript tab use the same utility wiring. status() writes plain text into the example’s status element. wait() is a simulated local delay. For examples with data-ix attributes, start the declarative runtime once at application level, as shown in the code. Use your framework guide to place setup and cleanup correctly.

API and lifecycle

Create an instance for an element, then call to(target, options) to animate selected style properties. It reads the current computed values so a new transition can begin from the current visual state. Defaults: 180 ms and ease-out.

Interface
to(target,{duration,easing}), destroy.
Lifecycle
A new transition cancels the prior animation. destroy() cancels the active animation. The final target style is written to the element.

Accessibility and safety

Implementation notes

Initialize after the DOM is available. Keep native controls and keyboard interactions intact; release listeners and timers on unmount.

The helper skips animation when reduced motion is requested. Keep information and controls available without motion and avoid animating properties that disrupt reading.

What to check

Toggle repeatedly during the transition and check for jumps or queued motion. Enable reduced motion and verify the target state still appears.

View documented module source
/** Respect reduced motion, including preference changes while the page is open. */
export function motionPreference(onChange){const media=matchMedia('(prefers-reduced-motion: reduce)');const update=()=>onChange?.(media.matches);media.addEventListener('change',update);update();return {get reduced(){return media.matches;},duration(ms){return media.matches?0:ms;},destroy(){media.removeEventListener('change',update);}};}
/** Interruptible animation for a single element. Supply explicit target styles.
 * Cancellation preserves the current visual state before starting the next animation. */
export function interruptibleMotion(element){let animation;return {to(target,{duration=180,easing='ease-out'}={}){const from=Object.fromEntries(Object.keys(target).map(k=>[k,getComputedStyle(element)[k]]));animation?.cancel();Object.assign(element.style,target);if(matchMedia('(prefers-reduced-motion: reduce)').matches)return;animation=element.animate([from,target],{duration,easing});},destroy(){animation?.cancel();}};}

Search Subtly documentation

Popular pages
↑↓ to navigate↵ to openESC to close