Noosphere Possibilities Maximized
Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »


0.12.3 • Public • Published

@lit-any/forms codecov

Construct HTML forms from lightweight snippets for each field using lit-html


yarn add @lit-any/forms lit-html lit-element

Form setup

First you have to define a contract which will drive the form's appearance and functionality.

const contract = {
  fields: [{
    title: 'Age',
    property: 'age',
    type: ''

The contract will be used on a lit-form element to create an interactive form:

<lit-form .contract=${contract}

When the Register button is clicked submitModel will be called where the code will retrieve the form's value from event.detail.value.

function submitModel(e) {
  // submit lit-form's value
  fetch(, {
    method: e.detail.method,
    body: e.detail.value,

The above is not enough to build a rich, convincing user experience as the form does not know how to receive the user's input. A plain <input type="text"> will be rendered.

Field setup

There are two ways for influencing the appearance of individual form input fields.

Pre-built component sets

The recommended way is to use a component to go with each field. Those components are imported as sets of elements which share design features and look & feel. Currently there are two such component sets being worked on:

The snippet below would be used to render a Vaadin text input for the age property used above

import { FieldTemplates } from '@lit-any/forms'
import { textbox } from '@lit-any/forms/components'
import vaadin from '@lit-any/components-vaadin'
// register Vaadin components to be used
// render textbox when type in xds:integer
  .fieldMatches(field => field.type === '')
    type: 'single line',

Custom form fields

Alternatively the element's template can be handcrafted:

import { FieldTemplates } from '@lit-any/forms'
  .fieldMatches(field => field.type === '')
  .renders((field, id, value, set) => {
    return html`<input type=number value=${value} id=${id}
                       @change=${e => set(Number.parseInt(, 0))}>`

The field parameter is the description from the contract.

The id is unique id which can be set to the field.

The value is the initial value, if any. Setting it back will have no effect.

The set parameter is a function used to set tha value back to the form's model and has to be bound to the input control.




npm i @lit-any/forms

DownloadsWeekly Downloads






Unpacked Size

34.8 kB

Total Files


Last publish


  • avatar