{
  "$schema": "https://ui.shadcn.com/schema/registry-item.json",
  "name": "share-button",
  "title": "ShareButton",
  "description": "A button that shares a link: through the system's share sheet where there is one (phones, Safari), and by copying it where there is not, when it says \"Link copied\" for two seconds and screen readers hear it once.",
  "registryDependencies": [
    "@ouro/button",
    "@ouro/lib-announce",
    "@ouro/lib-class-name-check",
    "@ouro/lib-cn",
    "@ouro/lib-env",
    "@ouro/lib-share",
    "@ouro/lib-use-dev-messages"
  ],
  "files": [
    {
      "path": "registry/ouro/components/ouro/share-button/share-button.tsx",
      "content": "\"use client\";\n\nimport { useEffect, useRef, useState, type ButtonHTMLAttributes, type JSX, type MouseEvent, type Ref, type SVGProps } from \"react\";\nimport { announce } from \"@/registry/ouro/lib/ouro/announce\";\nimport { useAppearanceClassNameCheck } from \"@/registry/ouro/lib/ouro/class-name-check\";\nimport { cn } from \"@/registry/ouro/lib/ouro/cn\";\nimport { canShare, share, type ShareResult } from \"@/registry/ouro/lib/ouro/share\";\nimport { useDevWarnings } from \"@/registry/ouro/lib/ouro/use-dev-messages\";\nimport { Button } from \"@/registry/ouro/components/ouro/button/button\";\n\n/** The props ShareButton adds to the `<button>` attributes it passes through. */\nexport interface ShareButtonOwnProps {\n  /** The address to share. @default the page's own address */\n  url?: string | undefined;\n  /** A title for the link, where the receiving app takes one. */\n  title?: string | undefined;\n  /** A line about the link, where the receiving app takes one. */\n  text?: string | undefined;\n  /** The button's label, and its name for screen readers with `hideLabel`. @default \"Share\" */\n  label?: string | undefined;\n  /**\n   * What it says, and screen readers hear, when there was no share sheet and the link was copied instead. @default \"Link copied\"\n   *\n   * @claim \"What it says, and screen readers\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > copies the link where there is no share sheet, says Link copied once, and reads Share again after two seconds)\n   */\n  copiedLabel?: string | undefined;\n  /**\n   * What it says for two seconds when neither sharing nor copying worked: short, so the button keeps its width.\n   * @default \"Browser blocked sharing\" when the share sheet refused, \"Browser blocked copying\" where there is no share sheet\n   * @claim \"What it says for two seconds\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, when the share sheet refuses and copying is blocked, shows Browser blocked sharing and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, where there is no share sheet, shows Browser blocked copying, with how to copy the link as its tooltip, and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > keeps a 390px-wide page from scrolling sideways when it says the browser blocked sharing, at every size)\n   * @claim \"when the share sheet refused\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, when the share sheet refuses and copying is blocked, shows Browser blocked sharing and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, where there is no share sheet, shows Browser blocked copying, with how to copy the link as its tooltip, and announces the whole sentence once)\n   */\n  failedLabel?: string | undefined;\n  /**\n   * What screen readers hear, and the button's tooltip says while it shows `failedLabel`, when\n   * neither sharing nor copying worked: what happened and what to do instead.\n   * @default \"The browser refused the share and blocked copying the link. Copy the link by hand.\" when the share sheet refused, \"This browser has no share sheet, and it blocked copying the link. Copy the link by hand.\" where there is none, or `failedLabel` when only that is given\n   * @claim \"when the share sheet refused\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, when the share sheet refuses and copying is blocked, shows Browser blocked sharing and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, where there is no share sheet, shows Browser blocked copying, with how to copy the link as its tooltip, and announces the whole sentence once)\n   */\n  failedMessage?: string | undefined;\n  /** Shows only the icon; the label stays the button's name for screen readers. */\n  hideLabel?: boolean | undefined;\n  /** Visual weight, as Button's. @default \"outline\" */\n  variant?: \"solid\" | \"soft\" | \"outline\" | \"plain\" | (string & { 'Use \"solid\", \"soft\", \"outline\" or \"plain\"; ghost is \"plain\"': never }) | undefined;\n  /** Control size, as Button's. @default \"md\" */\n  size?: \"sm\" | \"md\" | \"lg\" | (string & { 'Use \"sm\", \"md\" or \"lg\"': never }) | undefined;\n  /** Called with how the share ended: `shared`, `copied`, `cancelled` or `failed`. */\n  onShare?: ((result: ShareResult) => void) | undefined;\n  /** Layout only: margins, grid placement (`ms-auto`). Appearance classes warn in development. */\n  className?: string | undefined;\n  ref?: Ref<HTMLButtonElement> | undefined;\n\n  /** Not a prop. The address is `url`: `<ShareButton url={link} />`. */\n  href?: { 'Use url={link}': never };\n  /** Not a prop. The label is `label`: `<ShareButton label=\"Share recipe\" />`. */\n  children?: { 'Use label=\"Share\"': never };\n  /** Not a prop. ShareButton shares on click itself; to react to it, pass `onShare`. */\n  onClick?: { 'Use onShare={(result) => ...}': never };\n}\n\n/** Props for {@link ShareButton}: its own props and the `<button>` attributes it passes through. */\nexport type ShareButtonProps = Omit<ButtonHTMLAttributes<HTMLButtonElement>, keyof ShareButtonOwnProps | \"type\" | \"color\"> & ShareButtonOwnProps;\n\nconst variants = [\"solid\", \"soft\", \"outline\", \"plain\"] as const;\nconst sizes = [\"sm\", \"md\", \"lg\"] as const;\nconst CONFIRM_MS = 2000;\n\nfunction ShareIcon(props: SVGProps<SVGSVGElement>): JSX.Element {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" {...props}>\n      <path d=\"M4 12v7a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2v-7\" />\n      <path d=\"m16 6-4-4-4 4\" />\n      <path d=\"M12 2v13\" />\n    </svg>\n  );\n}\n\nfunction CheckIcon(props: SVGProps<SVGSVGElement>): JSX.Element {\n  return (\n    <svg aria-hidden=\"true\" viewBox=\"0 0 24 24\" fill=\"none\" stroke=\"currentColor\" strokeWidth=\"2\" strokeLinecap=\"round\" strokeLinejoin=\"round\" {...props}>\n      <path d=\"M20 6 9 17l-5-5\" />\n    </svg>\n  );\n}\n\n/**\n * A button that shares a link: through the system's share sheet where there is one (phones,\n * Safari), and by copying it where there is not, when it says \"Link copied\" for two seconds\n * and screen readers hear it once. It shares the page's own address unless given `url`. When\n * neither works, it says for two seconds which the browser blocked, \"Browser blocked sharing\"\n * or, where there is no share sheet, \"Browser blocked copying\", with the whole of\n * `failedMessage` as its tooltip, and screen readers hear that whole sentence once.\n *\n * Use `CopyButton` instead to copy any text without a share sheet, and the `share()` helper\n * for a share action inside a menu.\n *\n * @group Actions\n * @see https://ui.ouroboros.bot/docs/share-button\n *\n * @example\n * <ShareButton url={`https://example.com/notes/${note.id}`} title={note.title} />\n * @claim \"A button that shares a link\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > copies the link where there is no share sheet, says Link copied once, and reads Share again after two seconds)\n * @claim \"A button that shares a link\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > says its failedLabel, once, when neither sharing nor copying works)\n * @claim \"When neither works, it says for\" registry/ouro/components/ouro/share-button/share-button.test.tsx verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, where there is no share sheet, shows Browser blocked copying, with how to copy the link as its tooltip, and announces the whole sentence once) verify:testExists(registry/ouro/components/ouro/share-button/share-button.test.tsx::ShareButton > by default, when the share sheet refuses and copying is blocked, shows Browser blocked sharing and announces the whole sentence once)\n */\nexport function ShareButton(props: ShareButtonProps): JSX.Element {\n  const {\n    url,\n    title,\n    text,\n    label = \"Share\",\n    copiedLabel = \"Link copied\",\n    failedLabel: failedLabelProp,\n    failedMessage: failedMessageProp,\n    hideLabel,\n    variant: variantProp = \"outline\",\n    size: sizeProp = \"md\",\n    onShare,\n    className,\n    ref,\n    // Guard props (hint types), dropped here so they never reach the DOM.\n    href,\n    children,\n    onClick,\n    ...rest\n  } = props;\n  const variant = (variants as readonly string[]).includes(variantProp) ? (variantProp as (typeof variants)[number]) : \"outline\";\n  const size = (sizes as readonly string[]).includes(sizeProp) ? (sizeProp as (typeof sizes)[number]) : \"md\";\n  useAppearanceClassNameCheck(\n    \"ShareButton\",\n    className,\n    process.env.NODE_ENV !== \"production\"\n      ? (token: string) => `remove \"${token}\"; use variant=\"plain\" or size=\"sm\"`\n      : /* istanbul ignore next @preserve -- production only */ \"\",\n  );\n  useDevWarnings(\n    \"ShareButton\",\n    process.env.NODE_ENV !== \"production\"\n      ? () => [\n          variant !== variantProp && [`variant=\"${String(variantProp)}\" is not a variant; use \"solid\", \"soft\", \"outline\" or \"plain\"`, '<ShareButton variant=\"plain\" />'],\n          size !== sizeProp && [`size=\"${String(sizeProp)}\" is not a size; use \"sm\", \"md\" or \"lg\"`, '<ShareButton size=\"sm\" />'],\n          href !== undefined && [\"href is not a prop\", `<ShareButton url=\"${typeof href === \"string\" ? href : \"https://example.com\"}\" />`],\n          children !== undefined && [\"ShareButton takes no children; its label is label\", `<ShareButton label=\"${typeof children === \"string\" ? children : \"Share\"}\" />`],\n          onClick !== undefined && [\"onClick is not a prop; ShareButton shares on click itself\", \"<ShareButton onShare={(result) => {}} />\"],\n        ]\n      : /* istanbul ignore next @preserve -- production only */ undefined,\n  );\n  const [status, setStatus] = useState<\"idle\" | \"copied\" | \"failed\">(\"idle\");\n  // Whether the browser had a share sheet at the last press, so a failure names what it blocked.\n  const [sheet, setSheet] = useState<boolean>();\n  const timer = useRef<ReturnType<typeof setTimeout> | undefined>(undefined);\n  useEffect(() => () => clearTimeout(timer.current), []);\n\n  /** What a failure says: which the browser blocked, the share or the copy. */\n  const failure = (hadSheet: boolean | undefined) => {\n    const fallback = hadSheet\n      ? { label: \"Browser blocked sharing\", message: \"The browser refused the share and blocked copying the link. Copy the link by hand.\" }\n      : { label: \"Browser blocked copying\", message: \"This browser has no share sheet, and it blocked copying the link. Copy the link by hand.\" };\n    return { label: failedLabelProp ?? fallback.label, message: failedMessageProp ?? failedLabelProp ?? fallback.message };\n  };\n\n  const go = async (event: MouseEvent<HTMLButtonElement>) => {\n    event.preventDefault();\n    const data = { url: url ?? location.href, title, text };\n    const hadSheet = canShare(data);\n    const result = await share(data);\n    onShare?.(result);\n    if (result !== \"copied\" && result !== \"failed\") return;\n    clearTimeout(timer.current);\n    setStatus(result);\n    setSheet(hadSheet);\n    announce(result === \"copied\" ? copiedLabel : failure(hadSheet).message);\n    timer.current = setTimeout(() => setStatus(\"idle\"), CONFIRM_MS);\n  };\n\n  const failed = failure(sheet);\n  const shown = status === \"copied\" ? copiedLabel : status === \"failed\" ? failed.label : label;\n  const Icon = status === \"copied\" ? CheckIcon : ShareIcon;\n  return (\n    <span data-ouro=\"share-button\" data-status={status} className={cn(\"inline-flex\", className)}>\n      <Button\n        data-ouro-control=\"share-button\"\n        data-ouro-part=\"trigger\"\n        {...rest}\n        ref={ref}\n        variant={variant}\n        size={size}\n        tone=\"secondary\"\n        onClick={go}\n        {...(status === \"failed\" ? { title: failed.message } : {})}\n        {...(hideLabel ? { \"aria-label\": shown, className: \"w-(--ouro-button-h) px-0\" } : { startIcon: Icon })}\n      >\n        {hideLabel ? <Icon className=\"size-[1.25em] shrink-0\" /> : shown}\n      </Button>\n    </span>\n  );\n}\n",
      "type": "registry:ui",
      "target": "@components/ouro/share-button/share-button.tsx"
    }
  ],
  "docs": "A button that shares a link: through the system's share sheet where there is one (phones, Safari), and by copying it where there is not, when it says \"Link copied\" for two seconds and screen readers hear it once. Import: import { ShareButton } from \"@/components/ouro/share-button/share-button\", with @/components replaced by your components alias (aliases.components in components.json, such as ~/components). Also installed: button, the helpers they import in lib/ouro. Needs ouro-base in your stylesheet: add that item once per app, from the same place as this one, if you have not. Docs: https://ui.ouroboros.bot/docs/share-button.md Ouro UI v1.3.257.",
  "type": "registry:ui"
}