Subtlyv0.1.1
Keyboard & motion

motionPreference()

Follow the reduced-motion setting. Reduced motion.

Available in v0.1.1motion.jsMotion

Try it

Play the sample transition. Its duration follows your system’s reduced-motion setting. Change that setting to see the value update while this page stays open.

Live example
System preference CheckingAnimation duration Checking

Follows your system setting.

<div id="your-component">
<div class="motion-stage"><span class="motion-dot"></span></div><div class="demo-metrics"><span>System preference <strong data-preference>Checking</strong></span><span>Animation duration <strong data-duration>Checking</strong></span></div><button>Play transition</button><p class="demo-status" role="status">Follows your system setting.</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 preference = kit.motionPreference(update);
          function update(reduced) {
            el.querySelector('[data-preference]').textContent = reduced ? 'Reduced motion' : 'Standard motion';
            el.querySelector('[data-duration]').textContent = reduced ? '0 ms' : '600 ms';
          }
          update(preference.reduced);
          let animation;
          el.querySelector('button').addEventListener('click', () => {
            animation?.cancel();
            animation = el.querySelector('.motion-dot').animate(
              [{ transform: 'translateX(0)' }, { transform: 'translateX(100px)' }],
              { duration: preference.duration(600), fill: 'forwards' }
            );
            status(preference.reduced ? 'Position updated without animation.' : 'Transition played over 600 ms.');
          }, { signal });
          return () => { animation?.cancel(); preference.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

Subscribe to the system prefers-reduced-motion setting. onChange runs with the initial preference and whenever it changes. reduced exposes the current setting; duration(ms) returns zero when reduced motion is requested.

Interface
onChange; duration(ms), reduced, destroy.
Lifecycle
Keep a subscription for the view that uses it. destroy() removes the media-query listener when the view is removed.

Accessibility and safety

Implementation notes

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

Reduced motion should preserve the outcome while reducing animation. Do not hide content or make completion depend on an animation event.

What to check

Change the system setting while the page is open. Confirm the listener updates and every interaction still reaches its final usable state.

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