Skip to content

Input

Input component for entering content via keyboard, the most basic form control.

Use when you need a text field with a static top-aligned label, leading icon, validation status/message, and native form participation — <r-input> covers text, password, and number entry.

Quick Start

Basic Usage

html
<r-input placeholder="Enter text"></r-input>

API Reference

Properties

PropertyTypeDefaultDescription
labelstring''Static caption rendered above the field
placeholderstring''Placeholder text, forwarded to the native <input>
valuestring''Field value; reflected as an attribute and relayed to the form
disabledbooleanfalseWhether the input is disabled
typestring''Native input type forwarded to the inner control (text, password, number, …)
iconstring''Leading icon name (rendered as r-icon) inside the field
namestring''Form field name used when the input participates in a form
statusstring''Validation status: error, warning
messagestring''Helper / validation text rendered below the field
minstring''Minimum value; forwarded to the inner <input> when type="number"
maxstring''Maximum value; forwarded to the inner <input> when type="number"
stepstring''Value step; forwarded to the inner <input> when type="number"
requiredbooleanfalseForwarded to the inner <input> so native constraint validation applies
sheetstring''CSS injected into the shadow root

Label label

A static caption rendered above the field — always visible, never overlaps adjacent content, and doesn't shift the layout on focus (top-aligned labels also complete forms faster than inline/floating ones — see Luke Wroblewski's eye-tracking research).

html
<r-input label="Username"></r-input>

Placeholder placeholder

Consistent with the native placeholder attribute.

html
<r-input placeholder="Enter username"></r-input>

Value value

html
<r-input value="1234"></r-input>

Disabled State disabled

html
<r-input label="Username" disabled></r-input>

Icon icon

html
<r-input icon="user"></r-input>

Input Types type

html
<r-input icon="lock" type="password" placeholder="Password"></r-input>
<r-input type="number" placeholder="Number"></r-input>

Status status

Pair status with a message so the state is conveyed by text, not color alone.

html
<r-input status="error" label="Username" message="This field is required"></r-input>
<r-input status="warning" label="Username" message="Check this value"></r-input>

Helper Message message

Renders helper / validation text below the field.

html
<r-input label="Email" message="We will never share your email"></r-input>

Form Field Name name

html
<r-input name="username" label="Username"></r-input>

Events

Both events are dispatched as CustomEvents carrying the current value in detail.

EventWhen it firesdetail
inputOn every keystroke (mirrors the native input){ value: string }
changeOn commit / blur (mirrors the native change){ value: string }

Input Event input

javascript
const input = document.createElement('r-input');
input.setAttribute('label', 'Username');
input.addEventListener('input', (event) => {
  console.log('Typing:', event.detail.value);
});

Change Event change

javascript
const input = document.createElement('r-input');
input.setAttribute('label', 'Username');
input.addEventListener('change', (event) => {
  console.log('Value changed:', event.detail.value);
});

Form Association

r-input is a form-associated custom element (static formAssociated = true). It attaches ElementInternals and relays its value via setFormValue, so the field is collected by new FormData(form) when it's a real descendant of a native <form> — set name to give the value a key. See Forms for the serializeForm() helper that turns a submit into a plain object.

html
<form>
  <r-input name="username" label="Username"></r-input>
</form>

Reset: a native form.reset() (or <button type="reset">) restores the value the field had when it first connected — implemented via formResetCallback(), one of the lifecycle hooks the browser calls automatically on a form-associated custom element.

Validation: setting required makes an empty field invalid via ElementInternals.setValidity()form.checkValidity()/form.reportValidity() see it, and submitting shows the browser's native validation bubble anchored on the field. disabled fields never block validation, matching native <input>. r-input also exposes the usual native-field methods/properties: checkValidity(), reportValidity(), validity, validationMessage.

html
<form>
  <r-input name="username" label="Username" required></r-input>
  <button type="submit">Submit</button>
</form>

CSS Parts

Exposed via ::part() for external styling.

PartElement
inputThe field wrapper
contentThe inner native <input> control
labelThe static label above the field (present when label set)
messageThe helper / validation text (present when message set)
css
r-input::part(content) {
  font-size: 16px;
}

Best Practices

  • Labels: Add a meaningful label so the field has an accessible name.
  • Placeholders: Use placeholder for input hints, not as a replacement for a label.
  • Status + Message: Pair status with message so state is not signalled by color alone.
  • Icons: Add a relevant icon to enhance recognition.
  • Types: Choose the appropriate type (text, password, number, …) for the content.
  • Forms: Set name when collecting the value inside a form.

Released under the MIT License.