What Input

5.2.12 · active · verified Sun Apr 19

what-input is a client-side JavaScript utility designed to accurately track the user's current input method, distinguishing between mouse, keyboard, and touch interactions. It operates by listening for specific DOM events (mousedown, keydown, touchstart) and dynamically applies `data-whatinput` and `data-whatintent` attributes to the `window` object, making the current input state accessible via CSS or a simple JavaScript API. Currently at version 5.2.12, the package maintains an active release cadence, primarily focusing on bug fixes, TypeScript definition enhancements, and minor feature additions. A key feature is its default use of session storage to persist input/intent across page navigations, improving user experience by maintaining context. It also smartly manages form interactions, preserving the `data-whatintent` as `mouse` even when a user types into form fields, preventing unnecessary input state changes.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to import `what-input`, query the current input method and intent using its API, and observe how it updates data attributes on the `window` and body. It also highlights how to set up basic logging and interaction, alongside a note on persistence.

import whatInput from 'what-input';

// Ensure the DOM is fully loaded before trying to access or manipulate elements
document.addEventListener('DOMContentLoaded', () => {
  console.log('what-input module loaded.');

  // Function to update input display and log current state
  const logInputState = () => {
    const currentInput = whatInput.ask();
    const currentIntent = whatInput.ask('intent');
    console.log(`Current Input: ${currentInput}, Current Intent: ${currentIntent}`);

    // Update a display element or body attribute for visual feedback
    const inputDisplay = document.getElementById('input-display');
    if (inputDisplay) {
      inputDisplay.textContent = `Input: ${currentInput}, Intent: ${currentIntent}`;
    }
    document.body.setAttribute('data-current-input', currentInput);
    document.body.setAttribute('data-current-intent', currentIntent);
  };

  // Initial log of input state
  logInputState();

  // Add listeners to trigger state updates (what-input does this internally, but for demonstration)
  document.addEventListener('mousedown', logInputState);
  document.addEventListener('keydown', logInputState);
  document.addEventListener('touchstart', logInputState);

  // Example of how to add an element for demonstration
  const displayDiv = document.createElement('div');
  displayDiv.id = 'input-display';
  displayDiv.style.marginTop = '20px';
  displayDiv.style.padding = '10px';
  displayDiv.style.border = '1px solid #ccc';
  document.body.appendChild(displayDiv);

  // Example of checking persistence status
  const persistStatus = document.documentElement.dataset.whatpersist || 'default (enabled via sessionStorage)';
  console.log(`Persistence status (data-whatpersist attribute on html/body): ${persistStatus}`);

  // To disable persistence on a page, add this to your html/body tag:
  // <html data-whatpersist="false">...
  // Or programmatically:
  // whatInput.clearStorage(); // Clears existing stored state
});

view raw JSON →