# Animated Time Input for UI SDK (/docs/components/time-input)

One highlight glides between hours, minutes and AM/PM as you move.

```tsx title="example.tsx"
import { TimeInput } from "@/components/keyframery/time-input"

const hours = [
  { day: "Mon – Fri", opens: "07:30", closes: "18:00" },
  { day: "Saturday", opens: "09:00", closes: "16:30" },
  { day: "Sunday", opens: "10:00", closes: "14:00" },
]

export default function TimeInputDemo() {
  return (
    <div className="w-[380px]">
      <h3 className="text-[15px] font-semibold tracking-tight">Opening hours</h3>
      <p className="mt-1 text-[13px] text-muted-foreground">Little Fern Bakery</p>

      <div
        className="mt-5 grid grid-cols-[1fr_auto_auto_auto] items-center gap-x-2 gap-y-2.5 text-[13px]"
        /* A scripted click does not focus an input by itself; this lets the self-playing loop in. */
        onClick={(event) => event.target instanceof HTMLInputElement && event.target.focus()}
      >
        {hours.map((row) => (
          <div key={row.day} className="contents">
            <span className="font-medium">{row.day}</span>
            <TimeInput hourCycle={12} defaultValue={row.opens} aria-label={`${row.day} opens`} />
            <span className="text-muted-foreground">to</span>
            <TimeInput
              hourCycle={12}
              defaultValue={row.closes}
              min={row.opens}
              aria-label={`${row.day} closes`}
            />
          </div>
        ))}
        <span className="font-medium text-muted-foreground">Holidays</span>
        <TimeInput hourCycle={12} disabled aria-label="Holidays opens" />
        <span className="text-muted-foreground">to</span>
        <TimeInput hourCycle={12} disabled aria-label="Holidays closes" />
      </div>
    </div>
  )
}
```

## Installation [#installation]

```bash
npx shadcn@latest add @keyframery/time-input
```

This adds `components/keyframery/time-input.tsx` and its primitive, `components/keyframery/primitives/time-input.tsx`, to your project, and installs `@ui-sdk/react`. The motion tokens are added to your global CSS the first time you install a component.

## Usage [#usage]

```tsx
import { TimeInput, TimeInputSegment } from "@/components/keyframery/time-input"
```

The parts and props are the same as UI SDK's Time Input. For every prop, see the [UI SDK time input reference](https://ui-sdk.dev/docs/components/time-input).

## What moves [#what-moves]

| Move      | What you see                                           |
| --------- | ------------------------------------------------------ |
| **Glide** | One highlight glides between hours, minutes and AM/PM. |

Timing and springiness follow the [motion style](/docs/motion-styles) you set, so this component moves like the rest of your app.

## Swap the import [#swap-the-import]

Already using UI SDK's time input with your own styles? Keep your markup and change one line. The primitive adds motion and nothing else.

```tsx
// Before
import { … } from "@ui-sdk/react/time-input"

// After: same parts, same props, now animated
import { … } from "@/components/keyframery/primitives/time-input"
```

## Parts [#parts]

The finished component exports 2 parts:

`TimeInput`, `TimeInputSegment`

## Accessibility [#accessibility]

Keyboard, focus and screen-reader behaviour come from UI SDK and are not changed. With reduced motion turned on, nothing in the time input moves: changes happen at once or with a short fade. More in [Accessibility](/docs/accessibility).

## Source [#source]

The code that lands in your project. It is yours to change.

**components/keyframery/time-input.tsx**

```tsx
"use client"

/* Styled time input: the animated primitive plus the Keyframery look. */

import * as React from "react"

import {
  TimeInput as TimeInputPrimitive,
  TimeInputSegment as TimeInputSegmentPrimitive,
  createTimeInputController,
} from "@/components/keyframery/primitives/time-input"
import { cn } from "@/lib/utils"

/* The field look, as in Input: one hairline variable that hover, focus and invalid change. */
const fieldBox =
  "rounded-[10px] bg-background text-[13px] " +
  "[--field-line:rgb(0_0_0/0.09)] dark:[--field-line:rgb(255_255_255/0.1)] " +
  "shadow-[0_0_0_1px_var(--field-line),0_1px_2px_rgb(0_0_0/0.05)] dark:shadow-[0_0_0_1px_var(--field-line)] " +
  "transition-[box-shadow,background-color] duration-[calc(150ms*var(--kf-speed,1))] ease-house motion-reduce:transition-none! " +
  "hover:[--field-line:rgb(0_0_0/0.16)] dark:hover:[--field-line:rgb(255_255_255/0.18)]"

/* The field. The ring follows whichever segment has focus; invalid wins over focus. */
function TimeInput({
  className,
  children,
  ...props
}: React.ComponentProps<typeof TimeInputPrimitive>) {
  return (
    <TimeInputPrimitive
      className={cn(
        fieldBox,
        "inline-flex h-9 w-fit min-w-0 cursor-text items-center px-2 text-foreground tabular-nums",
        "has-[input:focus-visible]:ring-[3px] has-[input:focus-visible]:ring-ring/25 has-[input:focus-visible]:[--field-line:var(--ring)] dark:has-[input:focus-visible]:[--field-line:var(--ring)]",
        "data-invalid:ring-destructive/25! data-invalid:[--field-line:var(--destructive)]!",
        "data-readonly:bg-muted/50",
        /* The highlight the primitive glides under the focused segment. */
        "before:rounded-[6px] before:bg-accent",
        "data-disabled:cursor-not-allowed data-disabled:bg-muted data-disabled:text-muted-foreground data-disabled:[--field-line:rgb(0_0_0/0.06)] dark:data-disabled:[--field-line:rgb(255_255_255/0.06)]",
        className,
      )}
      {...props}
    >
      {children ??
        ((state) =>
          state.segments.map((segment) => (
            <TimeInputSegment key={segment.key} segment={segment} />
          )))}
    </TimeInputPrimitive>
  )
}

/* A segment sizes to its text. The one with focus sits on the accent tile the primitive glides
   between segments, and an empty one shows its placeholder in a quieter colour. */
function TimeInputSegment({
  className,
  ...props
}: React.ComponentProps<typeof TimeInputSegmentPrimitive>) {
  return (
    <TimeInputSegmentPrimitive
      className={cn(
        "h-6 rounded-[6px] bg-transparent px-[3px] text-center text-[13px] leading-none font-medium text-foreground tabular-nums outline-none",
        "[field-sizing:content] caret-transparent selection:bg-transparent",
        /* Room for the full number even while empty, so the field keeps its width. */
        "data-[type=day]:min-w-[calc(2ch+6px)] data-[type=month]:min-w-[calc(2ch+6px)] data-[type=hour]:min-w-[calc(2ch+6px)] data-[type=minute]:min-w-[calc(2ch+6px)] data-[type=second]:min-w-[calc(2ch+6px)]",
        "data-[type=year]:min-w-[calc(4ch+6px)] data-[type=dayPeriod]:min-w-[calc(2.2em+6px)]",
        "transition-colors duration-[calc(150ms*var(--kf-speed,1))] motion-reduce:transition-none! focus:text-accent-foreground",
        "placeholder:text-muted-foreground/70 data-empty:text-muted-foreground/70",
        "data-[type=literal]:flex data-[type=literal]:items-center data-[type=literal]:px-px data-[type=literal]:font-normal data-[type=literal]:text-muted-foreground/70",
        "data-[type=dayPeriod]:ml-1 data-[type=dayPeriod]:uppercase",
        "data-invalid:not-data-[type=literal]:text-destructive",
        "data-disabled:pointer-events-none data-disabled:text-muted-foreground",
        className,
      )}
      {...props}
    />
  )
}

export { TimeInput, TimeInputSegment, createTimeInputController }
```

**components/keyframery/primitives/time-input.tsx**

```tsx
"use client"

/* Animated drop-in for `@ui-sdk/react/time-input`: same parts, same props. Swap the import and keep your code.
   This file adds motion only. Colours, radii, shadows and spacing stay yours.

   Glide: one highlight sits under the segment with focus and glides to the next segment as focus
   moves, typed or tabbed. It is the root's `before:`; give it your own colour and radius with
   `before:` classes on TimeInput. */

import { TimeInput as TimeInputRoot, TimeInputSegment as TimeInputSegmentPart } from "@ui-sdk/react/time-input"
import * as React from "react"

import { cn } from "@/lib/utils"

export * from "@ui-sdk/react/time-input"

function TimeInput({ className, children, ...props }: React.ComponentProps<typeof TimeInputRoot>) {
  return (
    <TimeInputRoot
      className={cn(
        "relative isolate [anchor-scope:--kf-seg,--kf-seg-rest]",
        "before:pointer-events-none before:absolute before:-z-10 before:opacity-0 before:content-[''] before:[position-anchor:--kf-seg]",
        "before:bg-[color-mix(in_oklch,currentColor_9%,transparent)]",
        "before:top-[anchor(--kf-seg_top,anchor(--kf-seg-rest_top))] before:right-[anchor(--kf-seg_right,anchor(--kf-seg-rest_right))]",
        "before:bottom-[anchor(--kf-seg_bottom,anchor(--kf-seg-rest_bottom))] before:left-[anchor(--kf-seg_left,anchor(--kf-seg-rest_left))]",
        "before:[transition:top_calc(280ms*var(--kf-speed,1))_var(--ease-spring),right_calc(280ms*var(--kf-speed,1))_var(--ease-spring),bottom_calc(280ms*var(--kf-speed,1))_var(--ease-spring),left_calc(280ms*var(--kf-speed,1))_var(--ease-spring),opacity_calc(160ms*var(--kf-speed,1))_var(--ease-house)]",
        "has-[input:focus]:before:opacity-100 motion-reduce:before:[transition:none]!",
        className,
      )}
      {...props}
    >
      {children ??
        ((state) =>
          state.segments.map((segment) => <TimeInputSegment key={segment.key} segment={segment} />))}
    </TimeInputRoot>
  )
}

/* The segment with focus names itself as the highlight's anchor. The first segment is a resting
   anchor: focus passes through "nothing focused" for a moment as it moves between segments, and
   without somewhere to rest the highlight would jump instead of gliding. */
function TimeInputSegment({ className, ...props }: React.ComponentProps<typeof TimeInputSegmentPart>) {
  return <TimeInputSegmentPart className={cn(
        "first:[anchor-name:--kf-seg-rest] focus:[anchor-name:--kf-seg] first:focus:[anchor-name:--kf-seg,--kf-seg-rest]",
        className,
      )} {...props} />
}

export { TimeInput, TimeInputSegment }
```

## Questions

### How does the animated Time Input for UI SDK move?

Glide: One highlight glides between hours, minutes and AM/PM.

### Do I need Motion or Framer Motion for the animated Time Input?

No. Every move is plain CSS, so there is nothing to install besides the component and UI SDK.

### Can I keep my own styling and only take the motion?

Yes. Import from `@/components/keyframery/primitives/time-input` instead. It has the same parts and props as `@ui-sdk/react/time-input` and adds motion only.

### What happens to the Time Input when someone turns on reduced motion?

Nothing moves. State changes happen at once, or with a short fade.
