<form-maxlength-progress>

Indicates a user’s progress toward the limit of a field’s maxlength value. See the README for complete API documentation.

Fields retain native maxlength enforcement without JavaScript or if the module fails to load. After upgrade, the component shows its resting description, then remaining or over-limit text as you type. Amber marks approaching limits; red marks reached or exceeded limits, with contrasting light and dark variants. Forced colors use bold warning text and bold, underlined reached or exceeded text instead. Only the overrun examples relax native enforcement.

Default counter

The description template supplies both the empty-field text and a native-hidden static description associated through aria-describedby. The changing generated counter is hidden from assistive technology to avoid duplicate and per-keystroke description announcements. The generated [data-form-maxlength-progress-generated-counter] element remains in Light DOM as a supported styling hook.

<label for="message">Message</label>
<textarea id="message" maxlength="120"></textarea>
<form-maxlength-progress
  for="message"
  description="Write up to {maxlength} characters"
>
</form-maxlength-progress>

Paste and autofill checks

These fields exercise real browser input paths rather than buttons that imitate them. Copy the sample into the first field. Use saved address data in the second field to check browser autofill.

<label for="paste-check">Paste check</label>
<textarea id="paste-check" maxlength="40"></textarea>
<form-maxlength-progress
  for="paste-check" overrun="true"
></form-maxlength-progress>

<form method="dialog" autocomplete="on">
  <label for="address-check">Street address</label>
  <input id="address-check" name="street-address"
    autocomplete="street-address" maxlength="40" />
  <form-maxlength-progress for="address-check">
  </form-maxlength-progress>
</form>
This sample jumps beyond the forty character limit.

Authored counter and progress

The host references unique Light DOM descendants using counter and progress. Native progress uses accent-color: currentColor to follow the counter’s safe, warning, and target colors. Forced colors leave the native progress palette to the browser.

<label for="summary">Summary</label>
<input id="summary" maxlength="80" />
<form-maxlength-progress class="custom-progress"
  for="summary"
  counter="summary-count"
  progress="summary-meter"
  proximity-values="safe 0%, warning 60%, target 100%"
>
  <span><strong id="summary-count"></strong> left</span>
  <progress id="summary-meter"></progress>
</form-maxlength-progress>

.custom-progress progress {
  color: inherit;
  accent-color: currentColor;
}

@media (forced-colors: active) {
  .custom-progress progress {
    accent-color: auto;
  }
}
left

French translation and overrun

The component inherits lang for plural selection. Translate the description, remaining and overrun labels, and reached/exceeded alerts together. Overrun removes native enforcement and sets aria-invalid="true" until the value returns within the limit.

<div lang="fr">
  <label for="short-note">Note courte</label>
  <textarea id="short-note" maxlength="40"></textarea>
  <form-maxlength-progress
    for="short-note"
    overrun="true"
    description="{maxlength} caractères maximum"
    label="{remaining} caractères restants"
    label-one="{remaining} caractère restant"
    overrun-label="{remaining} caractères en trop"
    overrun-label-one="{remaining} caractère en trop"
    limit-reached-label="Limite de caractères atteinte"
    limit-exceeded-label="Limite de caractères dépassée"
  ></form-maxlength-progress>
</div>

Arabic translation and right-to-left layout

Set lang="ar" for Arabic plural selection and dir="rtl" for reading direction. Supply all six plural forms through the existing label attributes; numbers count UTF-16 code units, not grapheme clusters.

<div lang="ar" dir="rtl">
  <label for="arabic-note">ملاحظة قصيرة</label>
  <textarea id="arabic-note" maxlength="40"></textarea>
  <form-maxlength-progress
    for="arabic-note" overrun="true"
    description="اكتب نصًا لا يتجاوز {maxlength} حرفًا"
    label="{remaining} حرف متبقٍ"
    label-zero="لا أحرف متبقية"
    label-one="حرف واحد متبقٍ"
    label-two="حرفان متبقيان"
    label-few="{remaining} أحرف متبقية"
    label-many="{remaining} حرفًا متبقيًا"
    overrun-label="{remaining} حرف زائد"
    overrun-label-zero="لا أحرف زائدة"
    overrun-label-one="حرف واحد زائد"
    overrun-label-two="حرفان زائدان"
    overrun-label-few="{remaining} أحرف زائدة"
    overrun-label-many="{remaining} حرفًا زائدًا"
    limit-reached-label="تم بلوغ الحد الأقصى للأحرف"
    limit-exceeded-label="تم تجاوز الحد الأقصى للأحرف"
  ></form-maxlength-progress>
</div>

Custom CSS: a profile introduction

A blue panel, rounded field, and pill-shaped counter change the presentation without replacing the native textarea. The counter keeps its resting description and warning colors. These are demo styles, not new component properties. System colors take over in forced-colors mode.

<div class="maxlength-field editorial-field">
  <label for="profile-intro">Profile introduction</label>
  <textarea id="profile-intro" maxlength="160"></textarea>
  <form-maxlength-progress
    for="profile-intro"
    description="Introduce yourself in {maxlength} characters"
  ></form-maxlength-progress>
</div>

.editorial-field {
  padding: 1.5rem;
  border: 2px solid light-dark(#173f63, #b9d9f5);
  border-radius: 1.5rem;
  background: light-dark(#eef4ff, #142838);
  color: light-dark(#173f63, #e0efff);
}

.editorial-field label {
  margin-block-start: 0;
  font-size: 1.25rem;
}

.editorial-field textarea {
  padding: 0.75rem;
  border-radius: 0.75rem;
}

.editorial-field textarea:focus-visible {
  outline: 3px solid light-dark(#173f63, #b9d9f5);
  outline-offset: 4px;
}

.editorial-field :where(form-maxlength-progress) {
  justify-self: end;
  max-inline-size: 100%;
  padding: 0.25rem 0.75rem;
  border: 1px solid currentColor;
  border-radius: 999px;
  background: Canvas;
  color: CanvasText;
}

@media (forced-colors: active) {
  .editorial-field {
    background: Canvas;
    color: CanvasText;
    border-color: CanvasText;
  }

  .editorial-field textarea:focus-visible {
    outline-color: Highlight;
  }
}

Event handling

Listen for form-maxlength-progress:notification to respond when the field nears, reaches, or exceeds its limit. The event’s detail includes its milestone state, message, priority, remaining count, maximum, and associated field.

<label for="event-message">Notification event</label>
<textarea id="event-message" maxlength="20"></textarea>
<form-maxlength-progress id="event-progress"
  for="event-message" overrun="true">
</form-maxlength-progress>

document
  .querySelector('#event-progress')
  .addEventListener(
    'form-maxlength-progress:notification',
    (event) => {
      console.log(event.detail);
    }
  );
Waiting for a notification milestone…