The Coline design system: accessible React components for Coline Apps, themed live by the host workspace, and for Coline Sites in light and dark. Tailwind v4 stylesheet included. MIT.
| 5 | import { IconCheck, IconPlugConnected } from "@tabler/icons-react"; |
| 6 | import { cn } from "../lib/utils"; |
| 7 | import { transitions } from "../lib/motion"; |
| 8 | import { useConnection } from "../hooks/use-connection"; |
| 9 | import { Button } from "./button"; |
| 10 | import { Spinner } from "./spinner"; |
| 11 |
| 12 | /** |
| 13 | * A button that asks once more before a destructive action, in place: |
| 14 | * the first press arms it ("Delete?"), the second confirms. It disarms |
| 15 | * after a few seconds or on blur. |
| 16 | */ |
| 17 | export function ConfirmButton({ |
| 18 | children, |
| 19 | confirmLabel, |
| 20 | onConfirm, |
| 21 | variant = "destructive", |
| 22 | size = "default", |
| 23 | disarmAfterMs = 4000, |
| 24 | className, |
| 25 | }: { |
| 26 | children: React.ReactNode; |
| 27 | confirmLabel?: React.ReactNode; |
| 28 | onConfirm: () => Promise<unknown> | unknown; |
| 29 | variant?: "destructive" | "default" | "outline"; |
| 30 | size?: "default" | "sm"; |
| 31 | disarmAfterMs?: number; |
| 32 | className?: string; |
| 33 | }) { |
| 34 | const [armed, setArmed] = React.useState(false); |
| 35 | const [pending, setPending] = React.useState(false); |
| 36 | React.useEffect(() => { |
| 37 | if (!armed) return; |
| 38 | const timer = setTimeout(() => setArmed(false), disarmAfterMs); |
| 39 | return () => clearTimeout(timer); |
| 40 | }, [armed, disarmAfterMs]); |
| 41 | return ( |
| 42 | <Button |
| 43 | variant={armed ? "destructive" : variant} |
| 44 | size={size} |
| 45 | disabled={pending} |
| 46 | onBlur={() => setArmed(false)} |
| 47 | onClick={() => { |
| 48 | if (!armed) { |
| 49 | setArmed(true); |
| 50 | return; |
| 51 | } |
| 52 | setPending(true); |
| 53 | void Promise.resolve(onConfirm()).finally(() => { |
| 54 | setPending(false); |
| 55 | setArmed(false); |
| 56 | }); |
| 57 | }} |
| 58 | className={cn("overflow-hidden", className)} |
| 59 | > |
| 60 | <AnimatePresence mode="popLayout" initial={false}> |
| 61 | <motion.span |
| 62 | key={pending ? "pending" : armed ? "armed" : "idle"} |
| 63 | initial={{ opacity: 0, y: 8, filter: "blur(2px)" }} |
| 64 | animate={{ opacity: 1, y: 0, filter: "blur(0px)" }} |
| 65 | exit={{ opacity: 0, y: -8, filter: "blur(2px)" }} |
| 66 | transition={transitions.quick} |
| 67 | className="inline-flex items-center gap-1.5" |
| 68 | > |
| 69 | {pending ? <Spinner size="sm" /> : null} |
| 70 | {armed || pending ? (confirmLabel ?? "Are you sure?") : children} |
| 71 | </motion.span> |
| 72 | </AnimatePresence> |
| 73 | </Button> |
| 74 | ); |
| 75 | } |
| 76 |
| 77 | /** Connect (or show) an account on another service, by connection key. */ |
| 78 | export function ConnectButton({ |
| 79 | connectionKey, |
| 80 | label, |
| 81 | className, |
| 82 | }: { |
| 83 | connectionKey: string; |
| 84 | /** Defaults to "Connect <name>". */ |
| 85 | label?: string; |
| 86 | className?: string; |
| 87 | }) { |
| 88 | const connection = useConnection(connectionKey); |
| 89 | const name = connection.status?.name ?? connectionKey; |
| 90 | if (connection.isLoading) { |
| 91 | return ( |
| 92 | <Button variant="outline" disabled className={className}> |
| 93 | <Spinner size="sm" /> |
| 94 | {name} |
| 95 | </Button> |
| 96 | ); |
| 97 | } |
| 98 | if (connection.status?.connected) { |
| 99 | return ( |
| 100 | <span className={cn("inline-flex h-9 items-center gap-2 rounded-4xl border border-positive/30 bg-positive/[0.07] px-3 text-sm text-positive-fg", className)}> |
| 101 | <IconCheck className="size-4" /> |
| 102 | {connection.status.account?.label ? `Connected as ${connection.status.account.label}` : `${name} connected`} |
| 103 | </span> |
| 104 | ); |
| 105 | } |
| 106 | return ( |
| 107 | <Button variant="outline" disabled={connection.isConnecting} onClick={() => void connection.connect()} className={className}> |
| 108 | {connection.isConnecting ? <Spinner size="sm" /> : <IconPlugConnected className="size-4" />} |
| 109 | {label ?? `Connect ${name}`} |
| 110 | </Button> |
| 111 | ); |
| 112 | } |
| 113 |