Web component / Examples

<form-maxlength-progress>

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

Without JavaScript, or if the component fails to load, these labeled fields keep native maxlength enforcement. After upgrade, the component adds a visible count, a static maximum description for the field, and milestone notifications. Only examples with overrun="true" relax enforcement.

Default counter

Associate the component with the field’s ID using for. An empty field shows “120 characters allowed”; typing changes the visible text to a remaining count. The field’s accessible description still says “120 characters allowed.” Count updates aren’t announced on every keystroke.

<label for="message">Message</label>
<textarea id="message" maxlength="120"></textarea>
<form-maxlength-progress for="message">
</form-maxlength-progress>

Allow values beyond the limit

Set exactly overrun="true" to keep accepting text after the maximum. The counter reports a positive over-limit amount, and aria-invalid="true" marks the field until its value returns within the limit. This presentation does not block form submission; your application must add blocking validation if it needs it.

Paste a longer message

Copy the sample and paste it into the field to jump past the limit in one input. Delete text to bring the count back within the limit. A jump beyond the maximum sends the exceeded notification, not a near-limit warning.

<label for="paste-check">Paste check</label>
<code class="sample-text">This sample jumps beyond the forty character limit.</code>
<textarea id="paste-check" maxlength="40"></textarea>
<form-maxlength-progress
  for="paste-check" overrun="true"
></form-maxlength-progress>
This sample jumps beyond the forty character limit.

Keep a native limit for saved addresses

This field keeps native enforcement. If your browser has a saved street address, use its autofill menu to fill the field and check the remaining count. Available autofill controls depend on the browser and saved data.

<form method="dialog" autocomplete="on">
  <label for="address-check">Street address autofill check</label>
  <input id="address-check" name="street-address"
    autocomplete="street-address" maxlength="40" />
  <form-maxlength-progress for="address-check">
  </form-maxlength-progress>
</form>

Authored counter and progress

Use counter and progress to reference elements inside the component. The number shows characters remaining; the native progress bar shows how much of the limit you’ve used. Here, proximity-values changes the warning threshold to 60%. The bar uses accent-color: currentColor to follow the counter’s state color, with the browser’s palette in forced colors. See the proximity reference for stop rules.

<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>

<style>
.custom-progress {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 0.75rem;
  max-inline-size: 26rem;
}

.custom-progress progress {
  flex: 1 1 8rem;
  max-inline-size: 100%;
  color: inherit;
  accent-color: currentColor;
}

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

French translation and overrun

Use description to rewrite the empty-field text for your application’s vocabulary, and label for the remaining count. For a translation, also supply plural forms, overrun labels, and milestone messages. This example inherits lang="fr" to choose French plural forms.

<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 for remaining and overrun counts. Like native maxlength, the component counts UTF-16 code units, not user-perceived characters. See the label reference for templates and fallback rules.

<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>

Event handling

Listen for form-maxlength-progress:notification to respond to milestone notifications. Type 10 characters to see the near-limit message below, then reach or exceed 20 to see the high-priority messages. The output shows the latest event; it isn’t a second live announcement.

<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>
<pre id="event-output" class="event-output">Waiting for a notification milestone…</pre>

<script type="module">
document
  .querySelector('#event-progress')
  .addEventListener(
    'form-maxlength-progress:notification',
    (event) => {
      const { state, message, priority, remaining, maximum, field } =
        event.detail;
      document.querySelector('#event-output').textContent =
        JSON.stringify(
          { state, message, priority, remaining, maximum, field: `#${field.id}` },
          null,
          2,
        );
    }
  );
</script>
Waiting for a notification milestone…

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>

<style>
.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 :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;
  }

}
</style>