Inputs

Autosave Switch

A switch with deferred saving: the thumb moves at once, while the line beneath honestly goes through "saving" and "saved".

  • switch
  • autosave
  • status
  • async

Preview

1440pxHost theme

Use it with AI

  1. 1. Copy the link.
  2. 2. Write to your agent in your own words and drop the link into the sentence.
  3. 3. The agent opens the link and installs the component from the registry.

put this in the header: https://vibeui.ru/c/switch-006?lang=en

Show the full instructions

What the link says, spelled out. Use it when your agent cannot open links — paste this text instead.

# Install and place "switch-006" (Autosave Switch) from VibeUI

## 1. Install first — do not skip, do not recreate
Run this exact command before writing any code:
npx shadcn@latest add https://vibeui.ru/r/switch-006.json

Registry item: https://vibeui.ru/r/switch-006.json
Installs to: components/vibeui/switch-006.tsx (the exact path follows this project's components.json aliases).
npm dependencies: none.
Registry dependencies: none.

Install it from the registry. Do not recreate it from the description,
do not substitute a similar component from another library, and do not
rewrite it to match the project's existing style.

## 2. What it is
A switch with deferred saving: the thumb moves at once, while the line beneath honestly goes through "saving" and "saved".

A switch that shows the phases of writing a setting to the server. One file, zero dependencies, a client component.

## 3. How to use it
import { Switch006 } from "@/components/vibeui/switch-006"

<Switch006
  label="Calendar sync"
  delay={900}
/>

Read the installed file for the full prop list.

## 4. Where to place it
This is a small inline component. Put it exactly where the user asked,
inside the existing markup. Do not create a new page, section or
wrapper for it. If a similar control already sits in that spot,
replace it instead of adding a second one.

Placement: ___
(The user fills this line in. If it is still blank, ask where to put it
instead of guessing.)

## 5. Keep exactly as installed
- the local --vibeui-switch-006-* palette — do not swap it for your theme tokens
- clearing the timers in useEffect: without it setState is called on an unmounted component
- the fixed height of the status line — otherwise the card jumps on every toggle and the jump reads as an error
- role="status" on the line: the phase change is announced without stealing focus
- three honest phases instead of an instant "saved": an optimistic answer without a write loses the setting
- the component's own light surface — without it the dark text disappears on a dark page

## 6. You may change
- the setting caption through label and the calm text through description
- the pause before "saved" through delay
- the accent colour through accent
- the phase wording inside the component text

## 7. Rules
- The component issues no request: delay imitates it — wire a real write and let the data owner drive the state.
- This variant shows no write error: that needs a fourth phase and a retry button.
- Rapid re-toggling clears the previous timers — otherwise the statuses stack on each other.
- Do not lift the CSS variables into globals.css: the component has to stay a single file.

## 8. Verify
- it renders with no console errors;
- it looks like the preview on the VibeUI page you copied this from;
- if it does not, you changed something listed in section 5 — put it back.

For developers

npx shadcn@latest add https://vibeui.ru/r/switch-006.json
https://vibeui.ru/r/switch-006.json

Клиентский компонент: useState на состояние и на фазу записи, useRef хранит таймеры, useEffect снимает их при размонтировании. Строка статуса объявлена role="status" и имеет фиксированную минимальную высоту, чтобы карточка не прыгала. Индикатор — кольцо с окрашенной верхней гранью, галочка — угол квадрата под 45°; обе анимации гасятся prefers-reduced-motion. Палитра в --vibeui-switch-006-*.

Component source

The same file your agent installs. Here in case you would rather copy it by hand.

"use client"

import { useEffect, useId, useRef, useState } from "react"
import type { ComponentPropsWithoutRef, CSSProperties } from "react"

export type Switch006Props = Omit<
  ComponentPropsWithoutRef<"label">,
  "children" | "onChange"
> & {
  label?: string
  description?: string
  /** Сколько миллисекунд «сохраняем» — столько же ждёт настоящий запрос. */
  delay?: number
  accent?: string
}

// Идея компонента: настройка, которая уезжает на сервер. Тумблер двигается
// сразу, а строка под ним честно проходит три состояния — «сохраняем»,
// «сохранено», молчание. Оптимистичное переключение без индикатора врёт:
// человек уходит со страницы, не дождавшись записи.
const STYLES = `
:where([data-vibeui-block="switch-006"]){
--vibeui-switch-006-bg:oklch(1 0 0);
--vibeui-switch-006-fg:oklch(0.22 0.014 265);
--vibeui-switch-006-muted:oklch(0.55 0.014 265);
--vibeui-switch-006-border:oklch(0.91 0.006 265);
--vibeui-switch-006-track:oklch(0.88 0.008 265);
--vibeui-switch-006-thumb:oklch(1 0 0);
--vibeui-switch-006-accent:oklch(0.55 0.19 262);
--vibeui-switch-006-ok:oklch(0.55 0.15 155);
--vibeui-switch-006-font:ui-sans-serif,system-ui,-apple-system,"Segoe UI",Roboto,"Helvetica Neue",Arial,sans-serif;
}
[data-vibeui-block="switch-006"]{
display:flex;align-items:center;gap:1rem;
width:100%;max-width:21rem;box-sizing:border-box;padding:0.875rem;
background:var(--vibeui-switch-006-bg);
border:1px solid var(--vibeui-switch-006-border);border-radius:0.875rem;
font-family:var(--vibeui-switch-006-font);color:var(--vibeui-switch-006-fg);
cursor:pointer;
}
[data-vibeui-block="switch-006"] [data-part="text"]{display:flex;flex-direction:column;gap:0.1875rem;flex:1 1 auto;min-width:0}
[data-vibeui-block="switch-006"] [data-part="label"]{font-size:0.9375rem;font-weight:600;line-height:1.3}
[data-vibeui-block="switch-006"] [data-part="status"]{
display:flex;align-items:center;gap:0.375rem;
min-height:1.125rem;font-size:0.75rem;line-height:1.4;color:var(--vibeui-switch-006-muted);
}
/* Высота строки статуса зафиксирована: иначе карточка прыгает на каждом
   переключении, а прыжок читается как ошибка. */
[data-vibeui-block="switch-006"] [data-part="spinner"]{
flex:none;width:0.75rem;height:0.75rem;border-radius:9999px;
border:1.5px solid color-mix(in oklab,var(--vibeui-switch-006-accent) 30%,transparent);
border-top-color:var(--vibeui-switch-006-accent);
animation:vibeui-switch-006-spin .7s linear infinite;
}
@keyframes vibeui-switch-006-spin{to{transform:rotate(360deg)}}
[data-vibeui-block="switch-006"] [data-part="tick"]{
flex:none;width:0.375rem;height:0.6875rem;
border-right:1.5px solid var(--vibeui-switch-006-ok);
border-bottom:1.5px solid var(--vibeui-switch-006-ok);
transform:rotate(45deg);
}
[data-vibeui-block="switch-006"][data-state="saved"] [data-part="status"]{color:var(--vibeui-switch-006-ok)}
[data-vibeui-block="switch-006"] [data-part="track"]{position:relative;display:flex;flex:none}
[data-vibeui-block="switch-006"] input{
appearance:none;-webkit-appearance:none;margin:0;
width:2.75rem;height:1.5rem;border-radius:9999px;
background:var(--vibeui-switch-006-track);cursor:inherit;
transition:background-color .18s ease;
}
[data-vibeui-block="switch-006"] input:checked{background:var(--vibeui-switch-006-accent)}
[data-vibeui-block="switch-006"] input:focus-visible{outline:2px solid var(--vibeui-switch-006-accent);outline-offset:2px}
[data-vibeui-block="switch-006"] [data-part="thumb"]{
position:absolute;left:0.1875rem;top:0.1875rem;
width:1.125rem;height:1.125rem;border-radius:9999px;pointer-events:none;
background:var(--vibeui-switch-006-thumb);
box-shadow:0 1px 2px oklch(0.2 0.02 265 / 28%);
transition:transform .18s cubic-bezier(.32,.72,0,1);
}
[data-vibeui-block="switch-006"] input:checked + [data-part="thumb"]{transform:translateX(1.25rem)}
@media (prefers-reduced-motion:reduce){[data-vibeui-block="switch-006"] *{animation:none!important;transition:none!important}}
`

/**
 * Переключатель с отложенным сохранением: «сохраняем» → «сохранено».
 * Один файл, ноль зависимостей, собственная палитра.
 */
export function Switch006({
  label = "Синхронизация с календарём",
  description = "Задачи с датой попадают в рабочий календарь.",
  delay = 900,
  accent,
  className,
  style,
  ...props
}: Switch006Props) {
  const id = useId()
  const [checked, setChecked] = useState(false)
  const [state, setState] = useState<"idle" | "saving" | "saved">("idle")
  const timers = useRef<ReturnType<typeof setTimeout>[]>([])

  // Таймеры снимаются при размонтировании: иначе setState зовут у
  // компонента, которого уже нет на странице.
  useEffect(() => {
    const pending = timers.current
    return () => pending.forEach(clearTimeout)
  }, [])

  const palette = {
    ...(accent ? { "--vibeui-switch-006-accent": accent } : null),
    ...style,
  } as CSSProperties

  return (
    <>
      <style href="vibeui-switch-006" precedence="medium">
        {STYLES}
      </style>
      <label
        {...props}
        data-vibeui-block="switch-006"
        data-state={state}
        className={className}
        style={palette}
        htmlFor={id}
      >
        <span data-part="text">
          <span data-part="label">{label}</span>
          <span data-part="status" role="status">
            {state === "saving" ? (
              <>
                <span data-part="spinner" aria-hidden="true" />
                Сохраняем настройку…
              </>
            ) : null}
            {state === "saved" ? (
              <>
                <span data-part="tick" aria-hidden="true" />
                Сохранено
              </>
            ) : null}
            {state === "idle" ? description : null}
          </span>
        </span>
        <span data-part="track">
          <input
            id={id}
            type="checkbox"
            role="switch"
            checked={checked}
            onChange={(event) => {
              timers.current.forEach(clearTimeout)
              timers.current = []
              setChecked(event.target.checked)
              setState("saving")
              timers.current.push(
                setTimeout(() => setState("saved"), delay),
                setTimeout(() => setState("idle"), delay + 1600),
              )
            }}
          />
          <span data-part="thumb" aria-hidden="true" />
        </span>
      </label>
    </>
  )
}