# Character counter textarea

> A text box with a progress bar that fills as you type and turns orange, then red, near the limit.

- Category: [Inputs](https://library.aimran.es/components/inputs)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-in-counter
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

A character counter shows how much room is left before people hit the limit. It suits the bio of a profile page, the title of a product review or a note to a restaurant with a booking. The bar fills as you type, turns orange at 80% and red at the limit, next to a live count.

The demo stops at 60 characters; change the limit in the HTML to match your own rule. It runs on a few lines of JavaScript. Show the counter only where a limit really exists. On a normal comment box, a bar that nobody will ever fill just adds noise.

## Code

### HTML

```html
<label class="uv-ic"><textarea maxlength="60" placeholder="Tell us something"></textarea><span><i></i><b>0/60</b></span></label>
```

### CSS

```css
.uv-ic {
  --c1: #0071e3;
  --c2: #8ec5ff;
  --s: 16px;
  /* tamaño */
  --k: 1;
  /* velocidad */
  --fg: #1d1d1f;
  /* texto */
  --bg: #f5f5f7;
  /* fondo */
}

.uv-ic {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  display: block;
  width: 16em;
  font-size: var(--s);
}

.uv-ic textarea {
  width: 100%;
  box-sizing: border-box;
  height: 5em;
  padding: .8em 1em;
  border: 0;
  border-radius: .9em;
  resize: none;
  font: inherit;
  color: var(--fg);
  background: color-mix(in srgb,var(--fg) 6%,transparent);
  outline: none;
  box-shadow: inset 0 0 0 1px color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-ic textarea:focus {
  box-shadow: inset 0 0 0 2px var(--c1);
}

.uv-ic span {
  display: flex;
  align-items: center;
  gap: .8em;
  margin-top: .6em;
}

.uv-ic i {
  position: relative;
  flex: 1;
  height: .3em;
  overflow: hidden;
  border-radius: 9px;
  background: color-mix(in srgb,var(--fg) 10%,transparent);
}

.uv-ic i::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--c1);
  transform: scaleX(var(--p,0));
  transform-origin: left;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),background .3s;
}

.uv-ic b {
  font-size: .75em;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: color-mix(in srgb,var(--fg) 55%,transparent);
}

.uv-ic.warn i::after {
  background: #ff9f0a;
}

.uv-ic.full i::after {
  background: #ff453a;
}

.uv-ic.full b {
  color: #ff453a;
}
```

### JavaScript

```js
document.querySelectorAll(".uv-ic").forEach(el => {
  const t=el.querySelector("textarea"),b=el.querySelector("b"),m=+t.maxLength;t.addEventListener("input",()=>{const n=t.value.length;el.style.setProperty("--p",n/m);b.textContent=n+"/"+m;el.classList.toggle("warn",n>=m*.8);el.classList.toggle("full",n>=m)})
});
```

### React

```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uv-ic {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  display: block;
  width: 16em;
  font-size: var(--s);
}

.uv-ic textarea {
  width: 100%;
  box-sizing: border-box;
  height: 5em;
  padding: .8em 1em;
  border: 0;
  border-radius: .9em;
  resize: none;
  font: inherit;
  color: var(--fg);
  background: color-mix(in srgb,var(--fg) 6%,transparent);
  outline: none;
  box-shadow: inset 0 0 0 1px color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-ic textarea:focus {
  box-shadow: inset 0 0 0 2px var(--c1);
}

.uv-ic span {
  display: flex;
  align-items: center;
  gap: .8em;
  margin-top: .6em;
}

.uv-ic i {
  position: relative;
  flex: 1;
  height: .3em;
  overflow: hidden;
  border-radius: 9px;
  background: color-mix(in srgb,var(--fg) 10%,transparent);
}

.uv-ic i::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--c1);
  transform: scaleX(var(--p,0));
  transform-origin: left;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),background .3s;
}

.uv-ic b {
  font-size: .75em;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: color-mix(in srgb,var(--fg) 55%,transparent);
}

.uv-ic.warn i::after {
  background: #ff9f0a;
}

.uv-ic.full i::after {
  background: #ff453a;
}

.uv-ic.full b {
  color: #ff453a;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const t=el.querySelector("textarea"),b=el.querySelector("b"),m=+t.maxLength;t.addEventListener("input",()=>{const n=t.value.length;el.style.setProperty("--p",n/m);b.textContent=n+"/"+m;el.classList.toggle("warn",n>=m*.8);el.classList.toggle("full",n>=m)})
}

export default function InCounter() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#0071e3",
          "--c2": "#8ec5ff",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <label className="uv-ic"><textarea maxLength={60} placeholder="Tell us something"></textarea><span><i></i><b>0/60</b></span></label>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Character counter textarea” block (Inputs) from AIMRAN Library to my project.

## What it is
A text box with a progress bar that fills as you type and turns orange, then red, near the limit.

## Chosen settings
- Main color: #0071e3
- Secondary color: #8ec5ff
- Size: 16px (from 12 to 24px)
- Speed: 1x (from 0.5 to 2x)
- Text: "Tell us something"

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uv-ic {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  display: block;
  width: 16em;
  font-size: var(--s);
}

.uv-ic textarea {
  width: 100%;
  box-sizing: border-box;
  height: 5em;
  padding: .8em 1em;
  border: 0;
  border-radius: .9em;
  resize: none;
  font: inherit;
  color: var(--fg);
  background: color-mix(in srgb,var(--fg) 6%,transparent);
  outline: none;
  box-shadow: inset 0 0 0 1px color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-ic textarea:focus {
  box-shadow: inset 0 0 0 2px var(--c1);
}

.uv-ic span {
  display: flex;
  align-items: center;
  gap: .8em;
  margin-top: .6em;
}

.uv-ic i {
  position: relative;
  flex: 1;
  height: .3em;
  overflow: hidden;
  border-radius: 9px;
  background: color-mix(in srgb,var(--fg) 10%,transparent);
}

.uv-ic i::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: var(--c1);
  transform: scaleX(var(--p,0));
  transform-origin: left;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),background .3s;
}

.uv-ic b {
  font-size: .75em;
  font-weight: 500;
  font-variant-numeric: tabular-nums;
  color: color-mix(in srgb,var(--fg) 55%,transparent);
}

.uv-ic.warn i::after {
  background: #ff9f0a;
}

.uv-ic.full i::after {
  background: #ff453a;
}

.uv-ic.full b {
  color: #ff453a;
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const t=el.querySelector("textarea"),b=el.querySelector("b"),m=+t.maxLength;t.addEventListener("input",()=>{const n=t.value.length;el.style.setProperty("--p",n/m);b.textContent=n+"/"+m;el.classList.toggle("warn",n>=m*.8);el.classList.toggle("full",n>=m)})
}

export default function InCounter() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#0071e3",
          "--c2": "#8ec5ff",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <label className="uv-ic"><textarea maxLength={60} placeholder="Tell us something"></textarea><span><i></i><b>0/60</b></span></label>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
