# Sliding pill selector

> A coloured pill glides behind the options and stretches to fit whichever one you click.

- Category: [Radio buttons](https://library.aimran.es/components/radios)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-rd-slide
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

This is the switch you see on almost every pricing page: monthly, yearly or lifetime billing. It also works for “Delivery / Pickup” on a restaurant site or map and list views in a property search. The pill slides and resizes itself to each label. A segmented control in HTML and CSS, with a few lines of JavaScript.

Keep it to two, three or at most four short options; beyond that the pill has too far to travel and the labels get cramped. If yearly billing saves money, say it right in the label, like “Yearly −20%”.

## Code

### HTML

```html
<div class="uv-rds"><span class="p"></span><button class="on">Monthly</button><button>Yearly</button><button>Lifetime</button></div>
```

### CSS

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

.uv-rds {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  padding: .32em;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 8%,transparent);
}

.uv-rds button {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  position: relative;
  z-index: 1;
  font-size: var(--s);
  font-weight: 600;
  line-height: 1;
  padding: .7em 1.3em;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--fg);
  cursor: pointer;
  transition: color calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rds button.on {
  color: #fff;
}

.uv-rds .p {
  position: absolute;
  top: .32em;
  left: 0;
  height: calc(100% - .64em);
  border-radius: 999px;
  background: var(--c1);
  box-shadow: 0 7px 18px color-mix(in srgb,var(--c1) 36%,transparent);
  transition: transform calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1),width calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}
```

### JavaScript

```js
document.querySelectorAll(".uv-rds").forEach(el => {
  const p=el.querySelector(".p"),bs=[...el.querySelectorAll("button")];const mv=b=>{p.style.width=b.offsetWidth+"px";p.style.transform="translateX("+b.offsetLeft+"px)"};bs.forEach(b=>b.addEventListener("click",()=>{bs.forEach(x=>x.classList.remove("on"));b.classList.add("on");mv(b)}));requestAnimationFrame(()=>mv(el.querySelector("button.on")))
});
```

### 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-rds {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  padding: .32em;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 8%,transparent);
}

.uv-rds button {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  position: relative;
  z-index: 1;
  font-size: var(--s);
  font-weight: 600;
  line-height: 1;
  padding: .7em 1.3em;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--fg);
  cursor: pointer;
  transition: color calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rds button.on {
  color: #fff;
}

.uv-rds .p {
  position: absolute;
  top: .32em;
  left: 0;
  height: calc(100% - .64em);
  border-radius: 999px;
  background: var(--c1);
  box-shadow: 0 7px 18px color-mix(in srgb,var(--c1) 36%,transparent);
  transition: transform calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1),width calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}
`;

// 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 p=el.querySelector(".p"),bs=[...el.querySelectorAll("button")];const mv=b=>{p.style.width=b.offsetWidth+"px";p.style.transform="translateX("+b.offsetLeft+"px)"};bs.forEach(b=>b.addEventListener("click",()=>{bs.forEach(x=>x.classList.remove("on"));b.classList.add("on");mv(b)}));requestAnimationFrame(()=>mv(el.querySelector("button.on")))
}

export default function RdSlide() {
  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}
      >
        <div className="uv-rds"><span className="p"></span><button className="on">Monthly</button><button>Yearly</button><button>Lifetime</button></div>
      </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 “Sliding pill selector” block (Radio buttons) from AIMRAN Library to my project.

## What it is
A coloured pill glides behind the options and stretches to fit whichever one you click.

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

## 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-rds {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  position: relative;
  display: inline-flex;
  padding: .32em;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 8%,transparent);
}

.uv-rds button {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  position: relative;
  z-index: 1;
  font-size: var(--s);
  font-weight: 600;
  line-height: 1;
  padding: .7em 1.3em;
  border: 0;
  border-radius: 999px;
  background: none;
  color: var(--fg);
  cursor: pointer;
  transition: color calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rds button.on {
  color: #fff;
}

.uv-rds .p {
  position: absolute;
  top: .32em;
  left: 0;
  height: calc(100% - .64em);
  border-radius: 999px;
  background: var(--c1);
  box-shadow: 0 7px 18px color-mix(in srgb,var(--c1) 36%,transparent);
  transition: transform calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1),width calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}
`;

// 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 p=el.querySelector(".p"),bs=[...el.querySelectorAll("button")];const mv=b=>{p.style.width=b.offsetWidth+"px";p.style.transform="translateX("+b.offsetLeft+"px)"};bs.forEach(b=>b.addEventListener("click",()=>{bs.forEach(x=>x.classList.remove("on"));b.classList.add("on");mv(b)}));requestAnimationFrame(()=>mv(el.querySelector("button.on")))
}

export default function RdSlide() {
  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}
      >
        <div className="uv-rds"><span className="p"></span><button className="on">Monthly</button><button>Yearly</button><button>Lifetime</button></div>
      </div>
    </>
  );
}
```

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