npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

@aarongustafson/form-show-if

v3.1.2

Published

Web component that enables you to make one HTML form field show (or not) based on the value(s) of another field.

Downloads

120

Readme

Show If Web Component

npm version Build Status

Currently, HTML provides no mechanism to show & hide dependent fields. Sometimes you only want a field to show when certain other fields have a (particular) value. The form-show-if web component enables that.

TypeScript Support

  • Ships with bundled .d.ts definitions so editors and TypeScript builds fully understand FormShowIfElement.
  • The package export map exposes the types automatically; no extra configuration is required to consume them in TS projects.
  • HTMLElementTagNameMap is augmented so form-show-if elements are correctly typed when using JSX/TSX or querying via document.querySelector.

Demo

Installation

npm install @aarongustafson/form-show-if

Usage

Option 1: Import the class and define manually

Import the class and define the custom element with your preferred tag name:

import { FormShowIfElement } from '@aarongustafson/form-show-if';

// Define with default name
customElements.define('form-show-if', FormShowIfElement);

// Or define with a custom name
customElements.define('my-conditional-field', FormShowIfElement);

Option 2: Auto-define the custom element (browser environments only)

Use the guarded definition helper to register the element when customElements is available:

import '@aarongustafson/form-show-if/define.js';

If you prefer to control when the element is registered, call the helper directly:

import { defineFormShowIf } from '@aarongustafson/form-show-if/define.js';

defineFormShowIf();

You can also include the guarded script from HTML:

<script src="./node_modules/@aarongustafson/form-show-if/define.js" type="module"></script>

CDN Usage

You can also use the component directly from a CDN:

<script src="https://unpkg.com/@aarongustafson/form-show-if@latest/define.js" type="module"></script>

API

Markup Assumptions

This web component assumes the fields you reference in conditions exist in the DOM when the component is loaded. If they don't, they will be ignored.

Implementation notes

The "wrapper" mentioned below refers to the nearest mutual parent of the field & its label. It may be the form-show-if element itself.

  1. Field markup changes. When the field is hidden (conditions not met), it will receive the disabled attribute and all fields within the wrapper are disabled.

  2. Visual state management. If disabled-class and/or enabled-class are not defined:

    • When no condition is met: The wrapper has a hidden attribute
    • When any condition is met: The hidden attribute is removed from the wrapper
  3. Custom class management. If disabled-class and/or enabled-class are defined:

    • When no condition is met:
      • If enabled-class is defined, it is removed from the wrapper
      • If disabled-class is defined, it is added to the wrapper
    • When any condition is met:
      • If enabled-class is defined, it is added to the wrapper
      • If disabled-class is defined, it is removed from the wrapper

Examples

Basic Usage

<form>
  <label for="email">Email</label>
  <input type="email" id="email" name="email">

  <form-show-if conditions="email=*">
    <label for="phone">Phone (shown if email provided)</label>
    <input type="tel" id="phone" name="phone">
  </form-show-if>

  <button type="submit">Submit</button>
</form>

Multiple Conditions (OR logic)

<form-show-if conditions="email=*||phone=*">
  <label for="name">Name (shown if email OR phone provided)</label>
  <input type="text" id="name" name="name">
</form-show-if>

Specific Value Conditions

<form-show-if conditions="contact-method=email">
  <label for="email">Email Address</label>
  <input type="email" id="email" name="email">
</form-show-if>

Checkbox Conditions

<form-show-if conditions="newsletter=yes">
  <label for="email">Email (shown for newsletter)</label>
  <input type="email" id="email" name="email">
</form-show-if>

Using Custom Classes

<form-show-if conditions="email=*" disabled-class="visually-hidden" enabled-class="is-visible">
  <label for="phone">Phone</label>
  <input type="tel" id="phone" name="phone">
</form-show-if>

Browser Support

This web component works in all modern browsers that support:

  • Custom Elements v1
  • ES Modules (for module usage)

For older browsers, you may need polyfills for Custom Elements.

Development

Testing

# Run tests
npm test

# Run tests once
npm run test:run

# Run tests with UI
npm run test:ui

# Run tests with coverage
npm run test:coverage

Linting and Formatting

# Lint code
npm run lint

# Format code
npm run format