copy-action.tsx
This file
Path
apps/web/src/features/shared/copy-action.tsx
131 lines · 3.6 KiB · TypeScript
Last changed by
Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
History of this fileTake it
Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
line 1why, one step back1/**Merge build/comments-web-features into mainoscabrielbefore adoption74bfc72
lines 2–3why, one step back2 * The command is always text in the page, so an agent reading the HTML and a3 * screen reader both get it, never only a clipboard payload.Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
line 4why, one step back4 */Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
line 5why, one step back5import { Brand } from "@repo/brand";Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 6–91why, one step back6import { Effect, Option } from "effect";7import { Check, Copy } from "lucide-react";8import { useState } from "react";910import { useCopiedCommand, useNumberedCommand } from "./keys.tsx";1112const copyFor = (command: string, shown: (copied: boolean) => void) =>13 Effect.tryPromise(() => navigator.clipboard.writeText(command)).pipe(14 Effect.andThen(15 Effect.sync(() => {16 shown(true);17 })18 ),19 Effect.andThen(Effect.sleep("2 seconds")),20 Effect.andThen(21 Effect.sync(() => {22 shown(false);23 })24 ),25 Effect.ignore26 );2728export const useCopy = (command: string) => {29 const [pressed, setPressed] = useState(false);30 const byKey = useCopiedCommand();3132 return {33 copied: pressed || Option.contains(byKey, command),34 copy: () => {35 Effect.runFork(copyFor(command, setPressed));36 },37 };38};3940interface CopyProps {41 readonly command: string;42 readonly label: string;43 readonly numbered?: boolean;44}4546const ActionButton = ({47 command,48 label,49 number,50}: Omit<CopyProps, "numbered"> & { readonly number: number | undefined }) => {51 const { copied, copy } = useCopy(command);52 const Icon = copied ? Check : Copy;5354 return (55 <button56 aria-label={label}57 className="text-muted-foreground hover:bg-surface hover:text-foreground relative z-10 inline-flex size-8 shrink-0 items-center justify-center transition-colors duration-[120ms] focus-visible:outline-offset-[-2px]"58 onClick={copy}59 title={number === undefined ? label : `${label} (${number})`}60 type="button"61 >62 <Icon63 aria-hidden="true"64 className={`size-4 ${copied ? "text-foreground" : ""}`}65 strokeWidth={1.75}66 />67 <code className="sr-only">{command}</code>68 <span69 aria-live="polite"70 className={71 copied72 ? "text-label text-foreground bg-background pointer-events-none absolute right-full mr-1 whitespace-nowrap"73 : "sr-only"74 }75 >76 {copied ? "Copied" : ""}77 </span>78 </button>79 );80};8182const NumberedAction = (props: Omit<CopyProps, "numbered">) => (83 <ActionButton {...props} number={useNumberedCommand(props.command)} />84);8586export const CopyAction = ({ numbered = false, ...props }: CopyProps) =>87 numbered ? (88 <NumberedAction {...props} />89 ) : (90 <ActionButton {...props} number={undefined} />91 );Finish the Control room: every page passes axe and a keyboard walk in both schemesoscabrielbefore adoption3fd50a5
lines 92–104why, one step back9293export const ClaimAction = ({94 ask,95 project,96}: {97 readonly ask: string;98 readonly project: string;99}) => (100 <CopyAction101 command={`${Brand.cli} claim ${ask} --project ${project}`}102 label="Copy claim command"103 />104);Draw every list and subject from one row, one status line, one family fan and one set of fact blocksoscabrielbefore adoption886b726
lines 105–131why, one step back105106export const CommandChip = ({ command }: { readonly command: string }) => {107 const { copied, copy } = useCopy(command);108 const Icon = copied ? Check : Copy;109110 return (111 <button112 className="border-rule bg-surface hover:border-input text-mono inline-flex max-w-full min-w-0 items-center gap-1.5 border px-1.5 align-baseline font-mono transition-colors duration-[120ms]"113 onClick={copy}114 title={`Copy: ${command}`}115 type="button"116 >117 <span className="sr-only">Copy </span>118 <code className="min-w-0 text-left [overflow-wrap:anywhere]">119 {command}120 </code>121 <Icon122 aria-hidden="true"123 className="text-muted-foreground size-3.5 shrink-0"124 strokeWidth={1.75}125 />126 <span aria-live="polite" className="sr-only">127 {copied ? "Copied" : ""}128 </span>129 </button>130 );131};