{"$schema":"https://ui.shadcn.com/schema/registry-item.json","name":"dynamic-island","type":"registry:ui","title":"Dynamic Island","description":"A squircle pill that eases into a slab when tapped, swaps its content on the way, and glows red on alert.","dependencies":["motion","figma-squircle","react-use-measure"],"registryDependencies":["utils"],"files":[{"path":"registry/ui/dynamic-island.tsx","type":"registry:ui","content":"\"use client\";\n\n// Adapted from Calamansi UI by fujiDevv (MIT License).\n// https://github.com/fujiDevv/calamansi-ui\n\nimport { forwardRef, useMemo, useState, type KeyboardEvent, type ReactNode } from \"react\";\nimport { motion, AnimatePresence, useReducedMotion } from \"motion/react\";\nimport { getSvgPath } from \"figma-squircle\";\nimport useMeasure from \"react-use-measure\";\nimport { cn } from \"@/lib/utils\";\n\nexport type DynamicIslandState = \"idle\" | \"compact\" | \"expanded\" | \"alert\";\nexport type DynamicIslandVariant = \"white\" | \"calamansi\" | \"slate\" | \"citrus\";\n\nconst LIFT = \"drop-shadow(0 14px 28px rgba(0, 0, 0, 0.24))\";\n/** The alert state glows instead of just lifting. */\nconst ALERT_GLOW = \"drop-shadow(0 0 22px rgba(255, 65, 54, 0.75))\";\n/** Corner as a share of the height, so the short pill keeps a squircle corner instead of a stadium. */\nconst SHARE = 0.44;\nconst RADIUS = 28;\n\n/**\n * The morph animates the real box with a CSS transition (not a layout transform),\n * so the squircle clip path is re-measured every frame and never squashes.\n */\nconst MORPH_EASE = \"ease-[cubic-bezier(0.22,1,0.36,1)] duration-500\";\nconst BOX_MORPH = `transition-[width,height,max-width] ${MORPH_EASE} motion-reduce:transition-none`;\nconst PADDING_MORPH = `transition-[padding] ${MORPH_EASE} motion-reduce:transition-none`;\n\nconst SWAP_EASE: [number, number, number, number] = [0.22, 1, 0.36, 1];\nconst SWAP_IN = { duration: 0.18, ease: SWAP_EASE };\nconst SWAP_OUT = { duration: 0.1, ease: SWAP_EASE };\n\nconst VARIANTS: Record<DynamicIslandVariant, { paint: string; ink: string }> = {\n  white: { paint: \"bg-white dark:bg-[#1c1c1f]\", ink: \"text-foreground\" },\n  calamansi: {\n    paint:\n      \"bg-gradient-to-br from-[#8fa37d] via-[#5c7a67] to-[#39564a] dark:from-[#1b281f] dark:via-[#16221a] dark:to-[#0e1611]\",\n    ink: \"text-white\",\n  },\n  slate: {\n    paint:\n      \"bg-gradient-to-br from-[#a79cb7] via-[#687396] to-[#4a5a7f] dark:from-[#1e1b4b] dark:via-[#1e293b] dark:to-[#0f172a]\",\n    ink: \"text-white\",\n  },\n  citrus: {\n    paint:\n      \"bg-gradient-to-br from-[#d69f7e] via-[#b87152] to-[#7d4128] dark:from-[#2e170c] dark:via-[#22120b] dark:to-[#140a06]\",\n    ink: \"text-white\",\n  },\n};\n\nconst SIZES: Record<DynamicIslandState, string> = {\n  idle: \"h-10 w-32\",\n  compact: \"h-12 w-full max-w-68\",\n  alert: \"h-12 w-full max-w-80\",\n  expanded: \"h-52 w-full max-w-88 sm:max-w-96\",\n};\n\n/** A superellipse surface, re-measured as the island resizes. Content is clipped by the same path. */\nfunction Squircle({ className, filter, children }: { className?: string; filter?: string; children?: ReactNode }) {\n  const [ref, bounds] = useMeasure({ offsetSize: true });\n  const corner = bounds.height > 0 ? Math.min(RADIUS, bounds.height * SHARE) : RADIUS;\n  const path = useMemo(\n    () =>\n      bounds.width > 0 && bounds.height > 0\n        ? getSvgPath({ width: bounds.width, height: bounds.height, cornerRadius: corner, cornerSmoothing: 1 })\n        : null,\n    [bounds.width, bounds.height, corner],\n  );\n  return (\n    <span aria-hidden=\"true\" className=\"pointer-events-none absolute inset-0 block\" style={{ filter: filter ?? LIFT }}>\n      <span\n        ref={ref}\n        style={path ? { clipPath: `path('${path}')` } : { borderRadius: `min(${RADIUS}px, ${SHARE * 100}%)` }}\n        className={cn(\"relative block size-full overflow-hidden\", className)}\n      >\n        {children}\n      </span>\n    </span>\n  );\n}\n\nexport interface DynamicIslandProps {\n  /** Controlled state. */\n  state?: DynamicIslandState;\n  /** Initial state when uncontrolled. */\n  defaultState?: DynamicIslandState;\n  /** Fires when the island asks to change state (tap toggles compact/expanded). */\n  onStateChange?: (state: DynamicIslandState) => void;\n  /** Primary icon, drawn in a chip tinted from the surface ink. */\n  icon?: ReactNode;\n  /** Leading slot in compact/alert state, used when no `icon` is given. */\n  leading?: ReactNode;\n  /** Trailing slot in compact/alert state (timer, badge, waveform). */\n  trailing?: ReactNode;\n  /** Label shown in compact/alert state. */\n  title?: ReactNode;\n  /** Content shown when expanded. */\n  expandedContent?: ReactNode;\n  /** Tap (or Enter/Space) toggles between compact and expanded. */\n  interactive?: boolean;\n  /** Ambient pulse dot in compact/idle states. */\n  pulse?: boolean;\n  /** Surface palette. */\n  variant?: DynamicIslandVariant;\n  /** Accessible name for the island toggle. */\n  label?: string;\n  className?: string;\n}\n\n/**\n * A squircle pill that eases into a slab when tapped and glows red when it alerts.\n * It renders in normal flow, so place it wherever you like (e.g. `absolute top-3`).\n */\nexport const DynamicIsland = forwardRef<HTMLDivElement, DynamicIslandProps>(function DynamicIsland(\n  {\n    state,\n    defaultState = \"compact\",\n    onStateChange,\n    icon,\n    leading,\n    trailing,\n    title,\n    expandedContent,\n    interactive = true,\n    pulse = false,\n    variant = \"calamansi\",\n    label = \"Dynamic island\",\n    className,\n  },\n  ref,\n) {\n  const [internalState, setInternalState] = useState<DynamicIslandState>(defaultState);\n  const currentState = state ?? internalState;\n  const reduceMotion = useReducedMotion();\n  const palette = VARIANTS[variant] ?? VARIANTS.calamansi;\n\n  const isExpanded = currentState === \"expanded\";\n  const isAlert = currentState === \"alert\";\n  const showPulse = pulse && (currentState === \"compact\" || currentState === \"idle\");\n\n  const iconElement = icon ? (\n    <span className=\"flex size-7 shrink-0 items-center justify-center rounded-xl bg-current/15 shadow-xs ring-1 ring-current/20\">\n      {icon}\n    </span>\n  ) : leading ? (\n    <span className=\"shrink-0\">{leading}</span>\n  ) : null;\n\n  const toggleExpand = () => {\n    if (!interactive) return;\n    const next: DynamicIslandState = isExpanded ? \"compact\" : \"expanded\";\n    if (state === undefined) setInternalState(next);\n    onStateChange?.(next);\n  };\n\n  const onKeyDown = (e: KeyboardEvent<HTMLDivElement>) => {\n    if (e.target !== e.currentTarget) return;\n    if (e.key === \"Enter\" || e.key === \" \") {\n      e.preventDefault();\n      toggleExpand();\n    }\n  };\n\n  return (\n    <div className={cn(\"flex w-full items-center justify-center p-2 select-none\", isAlert && \"animate-pulse\", className)}>\n      <motion.div\n        ref={ref}\n        role={interactive ? \"button\" : undefined}\n        tabIndex={interactive ? 0 : undefined}\n        aria-label={interactive ? label : undefined}\n        aria-expanded={interactive ? isExpanded : undefined}\n        onClick={toggleExpand}\n        onKeyDown={interactive ? onKeyDown : undefined}\n        className={cn(\n          \"relative flex cursor-pointer items-center justify-between rounded-[18px] p-1 outline-none select-none focus-visible:ring-2 focus-visible:ring-ring sm:p-1.5\",\n          BOX_MORPH,\n          palette.ink,\n          SIZES[currentState] ?? SIZES.compact,\n          !interactive && \"cursor-default\",\n        )}\n      >\n        <Squircle className={palette.paint} filter={isAlert ? `${LIFT} ${ALERT_GLOW}` : undefined}>\n          <div\n            className={cn(\n              \"pointer-events-auto relative flex size-full\",\n              PADDING_MORPH,\n              isExpanded ? \"flex-col justify-between p-4 sm:p-5\" : \"items-center justify-between px-3.5\",\n            )}\n          >\n            <AnimatePresence mode=\"wait\" initial={false}>\n              {isExpanded ? (\n                <motion.div\n                  key=\"expanded\"\n                  initial={reduceMotion ? false : { opacity: 0, y: 6 }}\n                  animate={{ opacity: 1, y: 0 }}\n                  exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: -6, transition: SWAP_OUT }}\n                  transition={reduceMotion ? { duration: 0 } : SWAP_IN}\n                  className=\"relative flex size-full flex-col justify-between gap-3\"\n                >\n                  {expandedContent}\n                </motion.div>\n              ) : (\n                <motion.div\n                  key=\"collapsed\"\n                  initial={reduceMotion ? false : { opacity: 0, y: -6 }}\n                  animate={{ opacity: 1, y: 0 }}\n                  exit={reduceMotion ? { opacity: 0 } : { opacity: 0, y: 6, transition: SWAP_OUT }}\n                  transition={reduceMotion ? { duration: 0 } : SWAP_IN}\n                  className=\"relative flex size-full items-center justify-between gap-3\"\n                >\n                  <div className=\"flex min-w-0 items-center gap-2.5\">\n                    {iconElement}\n                    {title && (\n                      <div className=\"truncate text-xs font-semibold tracking-tight\">{title}</div>\n                    )}\n                    {showPulse && (\n                      <span aria-hidden=\"true\" className=\"relative flex size-1.5 shrink-0\">\n                        {!reduceMotion && (\n                          <span className=\"absolute inline-flex size-full animate-ping rounded-full bg-current opacity-60\" />\n                        )}\n                        <span className=\"relative inline-flex size-1.5 rounded-full bg-current\" />\n                      </span>\n                    )}\n                  </div>\n                  {trailing && (\n                    <div className=\"flex shrink-0 items-center gap-2 text-xs font-medium text-current/75\">{trailing}</div>\n                  )}\n                </motion.div>\n              )}\n            </AnimatePresence>\n          </div>\n        </Squircle>\n\n        {isAlert && (\n          <div\n            aria-hidden=\"true\"\n            className=\"pointer-events-none absolute top-1/2 -right-1.5 flex size-4.5 -translate-y-1/2 items-center justify-center rounded-full border border-white/40 bg-rose-500 text-[10px] font-bold text-white shadow-xs\"\n          >\n            !\n          </div>\n        )}\n      </motion.div>\n    </div>\n  );\n});\n\nDynamicIsland.displayName = \"DynamicIsland\";\n"}]}