Subtlyv0.1.1
Feedback & undo

liveAnnouncer()

Read updates aloud to screen readers. Screen reader status.

Available in v0.1.1feedback.jsFeedback

Try it

Add a sample item to the basket. The count updates visually and the same useful update is announced politely to a screen reader without moving your focus.

Live example
Sample productEveryday notebook

Illustrative basket. No purchase is made.

Basket 0 items
<div id="your-component">
<div class="article-preview"><span class="sample-art" aria-hidden="true"></span><div><span class="muted">Sample product</span><strong>Everyday notebook</strong><p>Illustrative basket. No purchase is made.</p></div><button>Add to basket</button></div><div class="basket-count">Basket <strong data-count>0 items</strong></div><p class="demo-status" aria-hidden="true">Ready to add an item.</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 announcer = kit.liveAnnouncer({ parent: el });
          let count = 0;
          el.querySelector('button').addEventListener('click', () => {
            count++;
            const message = `Notebook added. Basket contains ${count} ${count === 1 ? 'item' : 'items'}.`;
            el.querySelector('[data-count]').textContent = count + (count === 1 ? ' item' : ' items');
            status(message);
            announcer.announce(message);
          }, { signal });
          return () => announcer.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 a persistent empty live region before updates occur. announce(message) writes a text update after a short delay. The default priority is polite; include the visually hidden CSS so the region remains accessible.

Interface
priority, parent; announce/destroy; include CSS.
Lifecycle
Reuse the instance for related updates. destroy() cancels the pending announcement and removes the live-region 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.

Use assertive only for urgent interruptions. Avoid duplicate announcements of visible focused errors, and do not move focus to routine status updates.

What to check

Use a screen reader to check one useful announcement per update. Confirm routine updates do not interrupt typing or move keyboard focus.

View documented module source
/** Undo window for reversible actions. Apply a local preview immediately;
 * commit only after the deadline. Undo before commit restores the preview.
 * For permanent/critical operations use explicit confirmation instead.
 * Do not use page-exit timers for writes that must reliably complete. */
export function undoAction({ apply, revert, commit, duration = 5000 }) {
    let state = 'idle', timer;
    async function finish() {
        if (state !== 'pending') return false;
        clearTimeout(timer); state = 'committing';
        try { await commit(); state = 'committed'; return true; }
        catch (error) { state = 'failed'; revert(); throw error; }
    }
    return {
        get state() { return state; },
        start(onError = () => {}) { if (state !== 'idle') return false; apply(); state = 'pending'; timer = setTimeout(() => finish().catch(onError),duration); return true; },
        undo() { if (state !== 'pending') return false; clearTimeout(timer); state = 'undone'; revert(); return true; },
        finish,
        destroy() { if(state === 'pending') { clearTimeout(timer); state='cancelled'; revert(); } }
    };
}

/** Accessible announcements. Mount a persistent empty live region once;
 * write updates as text, never HTML. Avoid repeating visible focused errors.
 * Use polite for routine status; assertive only for urgent interruptions. */
export function liveAnnouncer({ priority = 'polite', parent = document.body } = {}) {
    const node = document.createElement('div');
    node.className = 'ix-sr-only'; node.setAttribute('aria-live',priority); node.setAttribute('aria-atomic','true'); parent.append(node);
    let timer;
    return { announce(message) { clearTimeout(timer); node.textContent=''; timer=setTimeout(()=>{node.textContent=String(message);},50); }, destroy() { clearTimeout(timer); node.remove(); } };
}

Search Subtly documentation

Popular pages
↑↓ to navigate↵ to openESC to close