aiellie

Search

Find a component, template or page

GitHub
Design

Share Dialog

Sharing something by link: who can open it, the link to copy, a preview slot, and buttons to post it to X, LinkedIn or Reddit.

fig. 01 · open it full screen from the corner

Installation

npx shadcn@latest add "@aiellie/share-dialog"

shadcn needs the @aiellie registry in components.json first. npx aiellie add share-dialog adds it on its first run, then installs the same way.

Share dialog shares a chat or a document by link. It brings its own Share button; the dialog inside chooses who can open it, only you or anyone with the link, copies the link, and posts it to X, LinkedIn or Reddit. While it's private there's no link to copy.

Getting started

  1. Give it the link

    It renders its own trigger, so it goes where the Share button should be. open is still yours, to open it from elsewhere too.

    components/chat-header.tsx
    "use client"
    
    import * as React from "react"
    
    import { ShareDialog } from "@/components/ui/share-dialog"
    
    export function ChatShare({ id, title }: { id: string; title: string }) {
      const [open, setOpen] = React.useState(false)
    
      return (
        <ShareDialog
          open={open}
          onOpenChange={setOpen}
          title="Share chat"
          url={`https://example.com/c/${id}`}
          text={title}
        />
      )
    }
  2. Save who can open it

    onVisibilityChange fires when the choice changes. Make the link work, or stop it working, on the server.

    <ShareDialog
      defaultVisibility={chat.shared ? "link" : "private"}
      onVisibilityChange={(visibility) => setShared(chat.id, visibility === "link")}
      {...props}
    />

Anatomy

<button data-slot="dialog-trigger" /> {/* Share, a small ghost button */}

<div data-slot="share-dialog"> {/* DialogContent, 448px wide at most */}
  <div data-slot="dialog-header">
    <h2 data-slot="dialog-title">{title}</h2>
    <p data-slot="dialog-description" /> {/* what the current choice means */}
  </div>
  {preview}
  <button data-slot="menu-trigger" /> {/* Only you / Anyone with the link */}
  <div>
    <input readonly /> {/* the link, selected on focus */}
    <button /> {/* Copy link */}
  </div>
  <div data-slot="dialog-footer">
    <a /> <a /> <a /> {/* X, LinkedIn, Reddit */}
  </div>
</div>

The description follows the choice, so it always says what is shared right now. While private, the link field is empty and disabled along with Copy and the posts. text goes with the post to X and Reddit as its words or title.

Examples

Private until shared

Start private and say what sharing means for this kind of thing with descriptions.

Private

A preview of what's shared

preview shows above the controls, like a glimpse of the document.

<ShareDialog
  preview={
    <div className="rounded-sm border px-3 py-2">
      <p className="text-xs font-medium">Launch checklist</p>
      <p className="text-xs text-muted-foreground">12 items, 9 done</p>
    </div>
  }
  {...props}
/>

API reference

ShareDialog

PropTypeDefaultDescription
openbooleanrequiredWhether the dialog is open.
onOpenChange(open: boolean) => voidrequiredCalled when the Share button or Escape opens or closes it.
urlstringrequiredThe link to share.
textstringWhat a post to X or Reddit says alongside the link, like the page's title.
titlestring"Share"The dialog's title.
descriptions{ private?: string; link?: string }What each choice means for what's shared, in place of the general wording.
previewReactNodeShown above the controls, like a glimpse of what's being shared.
defaultVisibility"private" | "link""link"Who can open it when the dialog first renders.
onVisibilityChange(visibility: "private" | "link") => voidCalled when the choice changes.