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 🙏

© 2024 – Pkg Stats / Ryan Hefner

semantic-forms

v4.0.2

Published

Semantic Forms

Downloads

1,116

Readme

Semantic Forms

npm

A pattern library of attractive forms for your website with the following design goals:

  • Attractive, modern float label visual design.
  • Responsive layout based on modern CSS grid technique.
  • Clear fields.
  • Light and dark mode.
  • Invalid field styling.
  • Good accessibility.
  • Semantic HTML (no div soup).
  • Uses a progressive enhancement approach so that core functionality and most of the features still work with JavaScript disabled.
  • Sets no global styles and is scoped by a single class name semanticForms applied to <form> elements.
  • No dependencies.

See a live demo here.

This module was built and is maintained by the Roosevelt web framework team, but it can be used independently of Roosevelt as well.

Usage

Include the semanticForms.css and semanticForms.js files from this repo into your project.

Then apply the semanticForms class to your <form> elements:

<form class="semanticForms">
  <!-- your form here -->
</form>

Then apply the JavaScript enhancements:

window.semanticForms()

Then the CSS/JS enhancements to your forms will apply automatically, assuming the markup structure you use is one of the supported patterns.

If you make changes to the DOM after Semantic Forms is activated and want to activate any additional semanticForms forms you insert, you can re-scan for new forms by calling window.semanticForms() again. If you only want to reinitialize one form instead of all of them, call window.semanticForms.reinitialize(formElement).

Features

Float label inputs

The float label input pattern is notoriously difficult to implement in a fashion that doesn't degrade HTML semantics or accessibility. This pattern library implements a solution that solves that problem. Your label doesn't need to be a sibling of your input like with other implementations. This implementation also has a custom clear button for each input.

Example

<form class="semanticForms">
  <dl>
    <dt><label for="name">Name</label></dt>
    <dd><input id="name" name="name" type="text" placeholder="e.g. John Smith"></dd>
  </dl>
</form>

Or with a <textarea>

<form class="semanticForms">
  <dl>
    <dt><label for="textarea">Textarea</label></dt>
    <dd><textarea id="textarea" name="textarea" rows="5" cols="50" placeholder="e.g. Long string of text"></textarea></dd>
  </dl>
</form>

Add help text

Include a label in the <dd> element with a matching for attribute to place help text beneath the input:

<form class="semanticForms">
  <dl>
    <dt><label for="password">Password</label></dt>
    <dd>
      <input id="password" name="password" type="password" placeholder="Enter a password" minlength="8" required>
      <label for="password">Must be at least 8 characters long.</label>
    </dd>
  </dl>
</form>

Responsive columns

This feature makes it so the form fields will grow, shrink, expand the number of columns, or reduce the number of columns based on how much space exists in the container. You can customize it by applying various colspan-# classes to various elements in the form markup structure.

Set number of columns for a group of forms

You can limit the number of columns on your forms using the colspan-# class on a <dl> element:

<form class="semanticForms">
  <dl class="colspan-2">
    <dt><label for="input-a">Input A</label></dt>
    <dd><input type="text" id="input-a" name="input-a"></dd>

    <dt><label for="input-b">Input B</label></dt>
    <dd><input type="text" id="input-b" name="input-b"></dd>

    <!-- wraps to the next row -->
    <dt><label for="input-c">Input C</label></dt>
    <dd><input type="text" id="input-c" name="input-c"></dd>
  </dl>
</form>

You can also apply the class to a <fieldset> which will apply that styling to all nested <dl> elements:

<form class="semanticForms">
  <fieldset class="colspan-3">
    <!-- inherits 3 column class -->
    <dl>
      <dt><label for="input-a">Input A</label></dt>
      <dd><input type="text" id="input-a" name="input-a"></dd>

      <dt><label for="input-b">Input B</label></dt>
      <dd><input type="text" id="input-b" name="input-b"></dd>

      <dt><label for="input-c">Input C</label></dt>
      <dd><input type="text" id="input-c" name="input-c"></dd>
    </dl>

    <!-- has 4 columns -->
    <dl class="colspan-4">
      <dt><label for="input-d">Input D</label></dt>
      <dd><input type="text" id="input-d" name="input-d"></dd>

      <dt><label for="input-e">Input E</label></dt>
      <dd><input type="text" id="input-e" name="input-e"></dd>

      <dt><label for="input-f">Input F</label></dt>
      <dd><input type="text" id="input-f" name="input-f"></dd>
    </dl>
  </fieldset>
</form>

Set number of columns a single input field takes up

You can specify the number of columns an input will span using the colspan-# class on the <dd> element. The inputs are responsive and will shrink according to available space. Available numbered classes are colspan-1 through colspan-5:

<form class="semanticForms">
  <fieldset>
    <dl>
      <dt><label for="input-a">Input A</label></dt>
      <!-- 1 column -->
      <dd class="colspan-1"><input type="text" id="input-a" name="input-a"></dd>

      <dt><label for="input-b">Input B</label></dt>
      <!-- 2 columns  -->
      <dd class="colspan-2"><input type="text" id="input-b" name="input-b"></dd>

      <dt><label for="input-c">Input C</label></dt>
      <!-- 5 columns (max) -->
      <dd class="colspan-5"><input type="text" id="input-c" name="input-c"></dd>
    </dl>
  </fieldset>
</form>

colspan-full can also be used to force an input to take up the entire width of a form:

<form class="semanticForms">
  <fieldset>
    <dl>
      <dt><label for="input-a">Input A</label></dt>
      <!-- this spans 2 columns on row 1 -->
      <dd class="colspan-2"><input type="text" id="input-a" name="input-a"></dd>

      <dt><label for="input-b">Input B</label></dt>
      <!-- this spans the full width of row 2 -->
      <dd class="colspan-full"><input type="text" id="input-b" name="input-b"></dd>

      <!-- this spans 1 column on row 3 -->
      <dt><label for="input-c">Input C</label></dt>
      <dd><input type="text" id="input-c" name="input-c"></dd>
    </dl>
  </fieldset>
</form>

Set number of columns a single input field takes up on the low flow (JavaScript disabled flow)

To apply colspan-# styles to low flow (JavaScript disabled flow) form inputs, you will need to manually wrap your <dt> and <dd> elements in a <div>:

<form class="semanticForms">
  <fieldset>
    <dl>
      <div>
        <dt><label for="input-a">Input A</label></dt>
        <!-- this spans 2 columns on row 1 -->
        <dd class="colspan-2"><input type="text" id="input-a" name="input-a"></dd>
			</div>
    </dl>
  </fieldset>
</form>

Button groups

You can align buttons side-by-side, or in left/right/center groups.

Non-grouped example

Nothing fancy here:

<button type="submit" name="submit">Non-grouped submit button</button>
<button type="submit" name="submit">Another one</button>

Button groups

Driven by flexbox to intelligently align the buttons based on how many are in the group:

<!-- aligns left/right -->
<menu>
  <li><button type="submit" name="submit">Button 1</button></li>
  <li><button type="submit" name="submit">Button 2</button></li>
</menu>

<!-- aligns left/center/right -->
<menu>
  <li><button type="submit" name="submit">Button 1</button></li>
  <li><button type="submit" name="submit">Button 2</button></li>
  <li><button type="submit" name="submit">Button 3</button></li>
</menu>

Checkboxes and radio buttons

Checkboxes and radio buttons need to follow the following markup structure:

A single checkbox

Not too different than other inputs:

<form class="semanticForms">
  <dl>
    <dt><label for="checkbox">Single checkbox</label></dt>
    <dd><input id="checkbox" name="checkbox" type="checkbox"></dd>
  </dl>
</form>

Checkbox group

<form class="semanticForms">
  <dl>
    <dt><label data-for="some_field_name">Checkboxes:</label></dt>
    <dd class="checkboxes">
      <ul id="some_field_name">
        <li><input type="checkbox" name="some_field_name" id="c_one"> <label for="c_one">One</label></li>
        <li><input type="checkbox" name="some_field_name" id="c_two"> <label for="c_two">Two</label></li>
        <li><input type="checkbox" name="some_field_name" id="c_three"> <label for="c_three">Three</label></li>
      </ul>
    </dd>
  </dl>
</form>

Radio buttons

<form class="semanticForms">
  <dl>
    <dt><label data-for="some_field_name">Radios:</label></dt>
    <dd class="radios">
      <ul id="some_field_name">
        <li><input type="radio" name="some_field_name" id="r_one"> <label for="r_one">One</label></li>
        <li><input type="radio" name="some_field_name" id="r_two"> <label for="r_two">Two</label></li>
        <li><input type="radio" name="some_field_name" id="r_three"> <label for="r_three">Three</label></li>
      </ul>
    </dd>
  </dl>
</form>

Validation styles

Inputs with the required attribute will result in a visual indicator (*) being added to its label. You can disable this indicator with the data-no-asterisk attribute on the label element:

<form class="semanticForms">
  <dl>
    <dt><label for="input" data-no-asterisk>Input label</label></dt>
    <dd><input type="text" name="input" id="input" required>
  </dl>
</form>

You can set a paragraph of text below a field to only appear when a field is invalid with the data-invalid-text attribute:

<form class="semanticForms">
  <dl>
    <dt><label for="password">Password label</label><dt>
    <dd>
      <input type="password" name="password" id="password" minlength="8">
      <p data-invalid-text>Password must be at least 8 characters long.</p>
    </dd>
  </dl>
</form>

You can adjust the tooltip text of the asterisk with the data-asterisk-text attribute:

<form class="semanticForms">
  <dl>
    <dt><label for="input" data-asterisk-text="This is a custom tooltip.">Input label</label></dt>
    <dd><input type="text" name="input" id="input" required>
  </dl>
</form>

Dark mode

To set the dark mode, apply an additional class of dark to your <form> elements to force the dark mode.

Low flow (JavaScript disabled) mode

The low flow will be displayed if JS is disabled.

You can also activate the low flow (JavaScript disabled) mode manually by adding the lowFlow class to your <form> element.

The low-flow mode reverts the float label pattern to traditional labels and doesn't include other JS-exclusive enhancements, but preserves the other visual design enhancements driven purely by CSS.

Contributing

  • Fork/clone this repo.
  • npm ci
  • Make your changes. If you want to alter the CSS, do the changes in the .scss files.
  • npm run build. The build step compiles the SCSS file into CSS.
    • You can also run npm run watch automatically compiles the CSS file after a change is detected in the SCSS file.
  • Test your changes by opening semanticForms.html in your browser.
    • If you want to test your work on an actual HTTP server, run npm run dev or npm run d.
  • Commit, push, open pull request.