{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "save-bar",
  "title": "Save bar",
  "description": "A bar that rises from the bottom of the screen while a form has unsaved changes, with Discard and Save. It shows the save running, leaves once it has worked, and swells to say no when the person tries to leave first.",
  "dependencies": [
    "@ui-sdk/react",
    "@ui-sdk/core"
  ],
  "registryDependencies": [
    "https://kinoui.com/r/button.json",
    "https://kinoui.com/r/spinner.json",
    "https://kinoui.com/r/form-changes.json",
    "https://kinoui.com/r/keys.json",
    "https://kinoui.com/r/refs.json",
    "https://kinoui.com/r/swap.json",
    "https://kinoui.com/r/theme.json"
  ],
  "files": [
    {
      "path": "registry/components/kino/save-bar.tsx",
      "content": "'use client'\n\nimport { Button } from '@/registry/components/kino/button'\nimport { Spinner, useSpinner } from '@/registry/components/kino/spinner'\nimport { useSaveBar } from '@/registry/lib/form-changes'\nimport { useAriaKeyshortcuts } from '@/registry/lib/keys'\nimport { mergeRefs } from '@/registry/lib/refs'\nimport { Swap, SwapText, swapIconClassName } from '@/registry/lib/swap'\nimport { isRTL } from '@ui-sdk/core'\nimport {\n  type ComponentProps,\n  createContext,\n  type ReactNode,\n  use,\n  useId,\n  useLayoutEffect,\n  useMemo,\n  useRef,\n} from 'react'\nimport { createPortal } from 'react-dom'\n\nimport { cn } from '@/lib/utils'\n\n/** Where the Save bars inside it render, in place of the page's end. */\nconst SaveBarContainerContext = createContext<HTMLElement | null>(null)\n\n/**\n * Renders the Save bars inside it into `container`, an element of your own,\n * rather than at the end of the page: for a bar rendered by a part, such as\n * the Settings page's, whose own `container` you don't pass.\n */\nfunction SaveBarContainer({\n  container,\n  children,\n}: {\n  container: HTMLElement | null\n  children: ReactNode\n}) {\n  return <SaveBarContainerContext value={container}>{children}</SaveBarContainerContext>\n}\n\n/*\n * A bar that floats up from the bottom of the screen, centred on its form,\n * while the form has changes it hasn't saved. It goes in a Kino Form, after\n * the fields, and renders at the end of the page, so no layer around the\n * form can cover it; inside a dialog it stays in the dialog. Tab still\n * reaches it after the form's last field, a screen reader reads it there,\n * and Save submits the form. It keeps one width whatever it says, so its\n * edges and buttons never move: the message keeps room for its longest\n * text, and the spinner shows beside the message rather than in Save.\n */\nfunction SaveBar({\n  className,\n  onDiscard,\n  container: containerProp,\n  ref,\n  ...props\n}: Omit<ComponentProps<'div'>, 'children'> & {\n  /** Runs after Discard has put every Field back, for state the form doesn't hold. */\n  onDiscard?: () => void\n  /** An element of your own to render into, in place of the end of the page. */\n  container?: HTMLElement | null\n}) {\n  const frame = useRef<HTMLDivElement>(null)\n  const contextContainer = use(SaveBarContainerContext)\n  const { barRef, open, view, text, texts, saving, locked, discard, placement } =\n    useSaveBar({\n      frame,\n      onDiscard,\n      container: containerProp ?? contextContainer,\n    })\n  const { here, into, away, anchorRef, guardRef, onGuardFocus, onButtonKeyDown, formId } =\n    placement\n  const refs = useMemo(() => mergeRefs(ref, barRef), [ref, barRef])\n  const saveKeys = useAriaKeyshortcuts('mod+s')\n  const spinning = useSpinner(saving)\n  const barId = useId()\n  // Rendered out of the form's tree, so it takes the form's reading direction as it opens.\n  useLayoutEffect(() => {\n    if (frame.current) frame.current.dir = isRTL(anchorRef.current) ? 'rtl' : 'ltr'\n  }, [open, into, anchorRef])\n\n  const bar = (\n    <div\n      ref={frame}\n      data-slot='save-bar-frame'\n      // Across the form's width, so the bar sits centred under it however the page lays it out; above another bar open before it.\n      className='@container pointer-events-none fixed bottom-0 left-(--save-bar-left,0px) z-40 flex w-(--save-bar-width,100%) -translate-y-(--save-bar-stack,0px) justify-center px-4 transition-[translate] duration-(--motion-layout-duration) ease-(--motion-layout-easing) motion-reduce:transition-none'\n    >\n      <div\n        ref={refs}\n        id={barId}\n        role='region'\n        data-slot='save-bar'\n        data-open={open ? '' : undefined}\n        data-view={view}\n        aria-busy={saving || undefined}\n        className={cn(\n          // The message starts as far in as Save's label ends: the gap, the buttons' padding and their 1px border.\n          'group/save-bar pointer-events-auto mb-(--save-bar-inset,1rem) flex @max-sm:flex-wrap max-w-full min-w-0 items-center gap-(--save-bar-gap) [--save-bar-gap:calc(var(--surface-padding)/2)] p-(--save-bar-gap) ps-[calc(var(--save-bar-gap)+var(--control-padding)+1px)]',\n          // The bar's corner is its buttons' corner and the padding round them, so the curves run alike.\n          'bg-popover text-popover-foreground ring-foreground/10 rounded-[calc(var(--control-radius-sm)+var(--save-bar-gap))] shadow-lg ring-1 forced-colors:outline',\n          // Rises from under the screen's edge as it arrives, and drops back as it goes; hidden once gone.\n          // Under reduced motion it only fades, from where it rests.\n          '[transition:translate_var(--motion-enter-duration)_var(--motion-enter-easing),opacity_var(--motion-enter-duration)_var(--motion-enter-easing),scale_var(--motion-press-duration)_var(--motion-press-easing),box-shadow_var(--motion-press-duration)_var(--motion-press-fade-easing)] not-data-open:invisible not-data-open:translate-y-[calc(100%+var(--save-bar-inset,1rem))] not-data-open:opacity-0 not-data-open:[transition:translate_var(--motion-exit-duration)_var(--motion-exit-easing),opacity_var(--motion-exit-duration)_var(--motion-exit-easing),visibility_0s_var(--motion-exit-easing)_var(--motion-exit-duration)] motion-reduce:not-data-open:translate-y-0 motion-reduce:[transition-property:opacity] motion-reduce:not-data-open:[transition-property:opacity,visibility]',\n          // Leaving with changes unsaved, it swells as the Alert dialog does, and its ring turns to the warning colour.\n          'data-refused:scale-[calc(1+(1-var(--motion-press-scale))*0.6)] data-refused:duration-[calc(var(--motion-press-duration)*0.4)] motion-reduce:data-refused:scale-100',\n          'data-[view=blocked]:ring-destructive/60 data-[view=failed]:ring-destructive/60 forced-colors:data-[view=blocked]:outline-2 forced-colors:data-[view=blocked]:outline-dashed',\n          className,\n        )}\n        {...props}\n      >\n        {/* Every text sits in one cell, hidden but the one it says, so the cell is as wide as the longest. */}\n        <span\n          className={cn(\n            // In a narrow form, the message takes a line of its own over the buttons.\n            'grid min-w-0 text-sm font-medium @max-sm:basis-full',\n            (view === 'blocked' || view === 'failed') && 'text-destructive',\n          )}\n        >\n          {Object.entries(texts).map(([key, room]) => (\n            <span\n              key={key}\n              aria-hidden\n              className='invisible flex min-w-0 items-center gap-1.5 [grid-area:1/1]'\n            >\n              {key === 'saving' ? <span className='size-4 shrink-0' /> : null}\n              <span className='truncate'>{room}</span>\n            </span>\n          ))}\n          <span className='flex min-w-0 items-center [grid-area:1/1]'>\n            <Swap align='start' roll className={swapIconClassName}>\n              {spinning ? (\n                <span className='flex pe-1.5'>\n                  <Spinner aria-hidden />\n                </span>\n              ) : null}\n            </Swap>\n            <SwapText text={text} className='min-w-0' />\n          </span>\n        </span>\n        <div className='ms-auto flex shrink-0 items-center gap-(--save-bar-gap)'>\n          <Button\n            type='button'\n            variant='ghost'\n            size='sm'\n            disabled={locked}\n            focusableWhenDisabled\n            onClick={discard}\n            tabIndex={away ? -1 : undefined}\n            onKeyDown={onButtonKeyDown('first')}\n          >\n            Discard\n          </Button>\n          <Button\n            type='submit'\n            // Away from its form, it still submits it.\n            form={formId}\n            size='sm'\n            disabled={locked}\n            focusableWhenDisabled\n            ref={saveKeys}\n            tabIndex={away ? -1 : undefined}\n            onKeyDown={onButtonKeyDown('last')}\n          >\n            Save\n          </Button>\n        </div>\n      </div>\n    </div>\n  )\n\n  return (\n    <>\n      <span ref={anchorRef} hidden />\n      {away ? (\n        <>\n          {/* While it shows, Tab from the form's last field goes on into it. It has no name: it hands focus on the moment it has it. */}\n          {open ? (\n            <span\n              ref={guardRef}\n              // oxlint-disable-next-line jsx-a11y/no-noninteractive-tabindex -- a focus guard: it hands focus on into the bar the moment it has it, as a focus trap's guards do.\n              tabIndex={0}\n              data-slot='save-bar-guard'\n              onFocus={onGuardFocus}\n              className='sr-only'\n            />\n          ) : null}\n          {/* A screen reader reads it here, after the form's fields. */}\n          <span aria-owns={barId} />\n          {into ? createPortal(bar, into) : null}\n        </>\n      ) : here ? (\n        bar\n      ) : null}\n    </>\n  )\n}\n\nexport { SaveBar, SaveBarContainer }\n",
      "type": "registry:component",
      "target": "@components/kino/save-bar.tsx"
    }
  ],
  "type": "registry:component"
}