{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "dialog-15",
  "title": "Invite dialog",
  "description": "Email rows added one at a time and a link to copy.",
  "dependencies": [
    "motion"
  ],
  "registryDependencies": [
    "https://ui.ndekocode.dev/r/button.json",
    "https://ui.ndekocode.dev/r/dialog.json",
    "https://ui.ndekocode.dev/r/icons.json",
    "https://ui.ndekocode.dev/r/input.json",
    "https://ui.ndekocode.dev/r/label.json",
    "https://ui.ndekocode.dev/r/theme.json",
    "https://ui.ndekocode.dev/r/tooltip.json",
    "https://ui.ndekocode.dev/r/use-prefers-reduced-motion.json"
  ],
  "files": [
    {
      "path": "registry/ndeko/components/dialog/dialog-15.tsx",
      "content": "'use client';\n\nimport {\n  AnimatePresence,\n  motion,\n  type Transition,\n  type Variants,\n} from 'motion/react';\nimport { useEffect, useId, useRef, useState } from 'react';\n\nimport { usePrefersReducedMotion } from '@/registry/ndeko/hooks/use-prefers-reduced-motion';\nimport {\n  CheckIcon,\n  CopyIcon,\n  LoaderCircleIcon,\n  PlusIcon,\n  UserRoundPlusIcon,\n} from '@/registry/ndeko/lib/icons';\nimport { Button } from '@/registry/ndeko/ui/button';\nimport {\n  Dialog,\n  DialogContent,\n  DialogDescription,\n  DialogHeader,\n  DialogTitle,\n  DialogTrigger,\n} from '@/registry/ndeko/ui/dialog';\nimport { Input } from '@/registry/ndeko/ui/input';\nimport { Label } from '@/registry/ndeko/ui/label';\nimport {\n  Tooltip,\n  TooltipContent,\n  TooltipTrigger,\n} from '@/registry/ndeko/ui/tooltip';\n\ntype SubmitState = 'idle' | 'sending' | 'sent';\n\ntype InviteRow = {\n  id: number;\n  email: string;\n};\n\nconst INITIAL_ROWS: InviteRow[] = [\n  { id: 1, email: 'kofi@fernhill.studio' },\n  { id: 2, email: 'lena@fernhill.studio' },\n  { id: 3, email: '' },\n];\n\nconst INVITE_LINK = 'https://acme.example/join/k7p2m9';\nconst COPIED_RESET_MS = 1500;\nconst SENDING_DURATION_MS = 1000;\n\nconst ICON_TILE =\n  'keycap-3d text-muted-foreground flex size-10 shrink-0 items-center justify-center rounded-lg';\n\nconst SWAP_TRANSITION: Transition = { duration: 0.15, ease: [0.2, 0, 0, 1] };\nconst ROW_TRANSITION: Transition = { duration: 0.2, ease: [0.2, 0, 0, 1] };\nconst SHIFT_TRANSITION: Transition = {\n  type: 'spring',\n  bounce: 0,\n  duration: 0.3,\n};\nconst INSTANT_TRANSITION: Transition = { duration: 0 };\n\nconst ROW_VARIANTS: Variants = {\n  hidden: { height: 0, opacity: 0 },\n  shown: { height: 'auto', opacity: 1 },\n};\n\nconst ICON_VARIANTS: Variants = {\n  enter: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n  visible: { opacity: 1, scale: 1, filter: 'blur(0px)' },\n  exit: { opacity: 0, scale: 0.6, filter: 'blur(2px)' },\n};\n\nconst ROW_FADE_VARIANTS: Variants = {\n  hidden: { opacity: 0 },\n  shown: { opacity: 1 },\n};\n\nconst LABEL_VARIANTS: Variants = {\n  enter: { opacity: 0, filter: 'blur(2px)' },\n  visible: { opacity: 1, filter: 'blur(0px)' },\n  exit: { opacity: 0, filter: 'blur(2px)' },\n};\n\nconst ICON_FADE_VARIANTS: Variants = {\n  enter: { opacity: 0 },\n  visible: { opacity: 1 },\n  exit: { opacity: 0 },\n};\n\nfunction rowVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return ROW_FADE_VARIANTS;\n  }\n  return ROW_VARIANTS;\n}\n\nfunction iconVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return ICON_FADE_VARIANTS;\n  }\n  return ICON_VARIANTS;\n}\n\nfunction labelVariantsOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return ICON_FADE_VARIANTS;\n  }\n  return LABEL_VARIANTS;\n}\n\nfunction shiftTransitionOf(shouldReduceMotion: boolean | null) {\n  if (shouldReduceMotion) {\n    return INSTANT_TRANSITION;\n  }\n  return SHIFT_TRANSITION;\n}\n\nfunction sentLabelOf(count: number) {\n  if (count === 1) {\n    return '1 invite sent';\n  }\n  return `${count} invites sent`;\n}\n\nfunction submitViewOf(state: SubmitState, filledCount: number) {\n  if (state === 'sending') {\n    return {\n      key: 'sending',\n      icon: <LoaderCircleIcon className='animate-spin' />,\n      label: 'Sending invites…',\n    };\n  }\n  if (state === 'sent') {\n    return {\n      key: 'sent',\n      icon: <CheckIcon />,\n      label: sentLabelOf(filledCount),\n    };\n  }\n  return { key: 'idle', icon: null, label: 'Send invites' };\n}\n\nfunction isFilled(row: InviteRow) {\n  const email = row.email.trim();\n  return email !== '';\n}\n\nfunction copyGlyphOf(isCopied: boolean) {\n  if (isCopied) {\n    return {\n      key: 'copied',\n      icon: <CheckIcon className='text-success-foreground' />,\n    };\n  }\n  return { key: 'copy', icon: <CopyIcon /> };\n}\n\nfunction copyLabelOf(isCopied: boolean) {\n  if (isCopied) {\n    return 'Link copied';\n  }\n  return 'Copy invite link';\n}\n\nfunction nextRowIdOf(rows: InviteRow[]) {\n  const ids = rows.map((row) => row.id);\n  const highestId = Math.max(0, ...ids);\n  return highestId + 1;\n}\n\nfunction lastRowRefOf(\n  index: number,\n  rowCount: number,\n  ref: React.RefObject<HTMLInputElement | null>\n) {\n  const isLastRow = index === rowCount - 1;\n  if (isLastRow) {\n    return ref;\n  }\n  return undefined;\n}\n\nexport default function Dialog15() {\n  const id = useId();\n  const emailsLabelId = `${id}-emails`;\n  const linkId = `${id}-link`;\n  const [rows, setRows] = useState(INITIAL_ROWS);\n  const [isCopied, setIsCopied] = useState(false);\n  const [submitState, setSubmitState] = useState<SubmitState>('idle');\n  const [addedRowCount, setAddedRowCount] = useState(0);\n  const lastInputRef = useRef<HTMLInputElement>(null);\n  const shouldReduceMotion = usePrefersReducedMotion();\n  const rowVariants = rowVariantsOf(shouldReduceMotion);\n  const iconVariants = iconVariantsOf(shouldReduceMotion);\n  const labelVariants = labelVariantsOf(shouldReduceMotion);\n  const shiftTransition = shiftTransitionOf(shouldReduceMotion);\n  const glyph = copyGlyphOf(isCopied);\n  const filledCount = rows.filter(isFilled).length;\n  const submitView = submitViewOf(submitState, filledCount);\n  const hasSubmitIcon = submitView.icon !== null;\n  const isIdle = submitState === 'idle';\n  const hasNoEmail = filledCount === 0;\n  const isSubmitBlocked = !isIdle || hasNoEmail;\n\n  useEffect(() => {\n    if (addedRowCount > 0) {\n      lastInputRef.current?.focus();\n    }\n  }, [addedRowCount]);\n\n  useEffect(() => {\n    if (submitState !== 'sending') {\n      return;\n    }\n    const timeout = window.setTimeout(() => {\n      setSubmitState('sent');\n    }, SENDING_DURATION_MS);\n    return () => window.clearTimeout(timeout);\n  }, [submitState]);\n\n  useEffect(() => {\n    if (!isCopied) {\n      return;\n    }\n    const timeout = window.setTimeout(() => {\n      setIsCopied(false);\n    }, COPIED_RESET_MS);\n    return () => window.clearTimeout(timeout);\n  }, [isCopied]);\n\n  function resetOnOpen(open: boolean) {\n    if (open) {\n      setSubmitState('idle');\n    }\n  }\n\n  function sendInvites(event: React.FormEvent<HTMLFormElement>) {\n    event.preventDefault();\n    if (isSubmitBlocked) {\n      return;\n    }\n    setSubmitState('sending');\n  }\n\n  function addRow() {\n    setRows((current) => {\n      const newRow = { id: nextRowIdOf(current), email: '' };\n      return [...current, newRow];\n    });\n    setAddedRowCount((count) => count + 1);\n  }\n\n  function updateEmail(rowId: number, email: string) {\n    setRows((current) =>\n      current.map((row) => {\n        if (row.id !== rowId) {\n          return row;\n        }\n        return { ...row, email };\n      })\n    );\n  }\n\n  async function copyLink() {\n    try {\n      await navigator.clipboard.writeText(INVITE_LINK);\n      setIsCopied(true);\n    } catch {\n      setIsCopied(false);\n    }\n  }\n\n  return (\n    <Dialog onOpenChange={resetOnOpen}>\n      <DialogTrigger render={<Button variant='outline' />}>\n        Invite teammates\n      </DialogTrigger>\n      <DialogContent initialFocus={lastInputRef}>\n        <div className='flex flex-col gap-4'>\n          <span aria-hidden='true' className={ICON_TILE}>\n            <UserRoundPlusIcon className='size-5' />\n          </span>\n          <DialogHeader>\n            <DialogTitle>Invite teammates</DialogTitle>\n            <DialogDescription>\n              People you invite join Fernhill Studio and see every project\n              shared with the team.\n            </DialogDescription>\n          </DialogHeader>\n        </div>\n        <form className='flex flex-col gap-5' onSubmit={sendInvites}>\n          <div className='flex flex-col gap-2'>\n            <span\n              id={emailsLabelId}\n              className='text-foreground text-[13px] leading-none font-medium'\n            >\n              Invite by email\n            </span>\n            <div\n              role='group'\n              aria-labelledby={emailsLabelId}\n              className='-my-1 flex flex-col'\n            >\n              <AnimatePresence initial={false}>\n                {rows.map((row, index) => (\n                  <motion.div\n                    key={row.id}\n                    className='-mx-1 overflow-hidden px-1 py-1'\n                    variants={rowVariants}\n                    initial='hidden'\n                    animate='shown'\n                    exit='hidden'\n                    transition={ROW_TRANSITION}\n                  >\n                    <Input\n                      ref={lastRowRefOf(index, rows.length, lastInputRef)}\n                      type='email'\n                      aria-label={`Email ${index + 1}`}\n                      placeholder='teammate@fernhill.studio'\n                      value={row.email}\n                      readOnly={!isIdle}\n                      onChange={(event) => {\n                        updateEmail(row.id, event.currentTarget.value);\n                      }}\n                    />\n                  </motion.div>\n                ))}\n              </AnimatePresence>\n            </div>\n            <Button\n              type='button'\n              variant='ghost'\n              size='sm'\n              className='-ms-2.5 w-fit'\n              disabled={!isIdle}\n              onClick={addRow}\n            >\n              <PlusIcon className='opacity-60' />\n              Add another\n            </Button>\n          </div>\n          <Button\n            type='submit'\n            size='lg'\n            className='w-full data-disabled:opacity-100'\n            disabled={isSubmitBlocked}\n            focusableWhenDisabled\n          >\n            <AnimatePresence mode='popLayout' initial={false}>\n              {hasSubmitIcon && (\n                <motion.span\n                  key={submitView.key}\n                  className='flex'\n                  variants={iconVariants}\n                  initial='enter'\n                  animate='visible'\n                  exit='exit'\n                  transition={SWAP_TRANSITION}\n                >\n                  {submitView.icon}\n                </motion.span>\n              )}\n            </AnimatePresence>\n            <motion.span\n              layout\n              transition={shiftTransition}\n              className='relative flex'\n              aria-live='polite'\n            >\n              <AnimatePresence mode='popLayout' initial={false}>\n                <motion.span\n                  key={submitView.key}\n                  className='flex'\n                  variants={labelVariants}\n                  initial='enter'\n                  animate='visible'\n                  exit='exit'\n                  transition={SWAP_TRANSITION}\n                >\n                  {submitView.label}\n                </motion.span>\n              </AnimatePresence>\n            </motion.span>\n          </Button>\n        </form>\n        <div className='mt-2 grid gap-2'>\n          <Label htmlFor={linkId}>Invite with a link</Label>\n          <div className='relative'>\n            <Input\n              id={linkId}\n              readOnly\n              defaultValue={INVITE_LINK}\n              className='pe-9'\n            />\n            <Tooltip>\n              <TooltipTrigger\n                delay={0}\n                render={\n                  <Button\n                    type='button'\n                    variant='ghost'\n                    size='icon-sm'\n                    onClick={copyLink}\n                    disabled={isCopied}\n                    focusableWhenDisabled\n                    aria-label={copyLabelOf(isCopied)}\n                    className='text-muted-foreground absolute inset-y-0 end-0.5 my-auto data-disabled:opacity-100'\n                  />\n                }\n              >\n                <AnimatePresence mode='popLayout' initial={false}>\n                  <motion.span\n                    key={glyph.key}\n                    className='flex'\n                    variants={iconVariants}\n                    initial='enter'\n                    animate='visible'\n                    exit='exit'\n                    transition={SWAP_TRANSITION}\n                  >\n                    {glyph.icon}\n                  </motion.span>\n                </AnimatePresence>\n              </TooltipTrigger>\n              <TooltipContent>Copy invite link</TooltipContent>\n            </Tooltip>\n          </div>\n        </div>\n      </DialogContent>\n    </Dialog>\n  );\n}\n",
      "type": "registry:component"
    }
  ],
  "categories": [
    "components",
    "dialog"
  ],
  "type": "registry:component"
}