okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1/** The Copy controls of the app, with one shared behaviour: the control itself shows that the copy worked (#4748). */ 2import { createContext , useContext } from 'react' ; 3import { tr } from '../lib/i18n' ; 4import { cx } from '../lib/format' ; 5import { COPIED_MENU_MS , useCopy } from '../lib/use-copy' ; 6import { Icon } from './ui' ; 7 8type Source = string | (() => string | Promise < string >); 9 10/** A round icon button that copies `text`: the icon turns into a check mark for a moment, and a screen reader hears "Copied". */ 11export function CopyBtn ({ text, label, title, disabled, size= 19 } :{ text :Source ; label :string ; title :string ; disabled ?:boolean ; size ?:number }) { 12const { copied, copy} = useCopy (); 13return < button type = "button" className = { cx ( 'st-btn st-btn--icon' , copied && 'is-copied' )} data-copied = { copied || undefined } title = { tr ( copied ?'Copied' :title )} aria-label = { tr ( title )} disabled = { disabled } onClick = {() => void copy ( text , label )}> 14< Icon name = { copied ?'check' :'copy' } size = { size }/> 15< span className = "sr-only" role = "status" >{ copied ?tr ( 'Copied' ) :'' }</ span > 16</ button >; 17} 18 19/** What closes the menu a menu item is in; Chat's message menu provides it. */ 20export const MenuCloseContext = createContext <() => void >(() => {}); 21 22/** A menu item that copies `text`: it says "Copied" for a moment, and then the menu closes. */ 23export function CopyMenuItem ({ text, label, children, disabled, onClose} :{ text :Source ; label :string ; children :string ; disabled ?:boolean ; onClose ?:() => void }) { 24const { copied, copy} = useCopy ( COPIED_MENU_MS ); 25const close = useContext ( MenuCloseContext ); 26return < button role = "menuitem" className = "menu__item" data-copied = { copied || undefined } disabled = { disabled } onClick = { e => { 27// The menu would close on this click before the person sees that it worked. 28e . stopPropagation (); 29void copy ( text , label ). then ( ok => { setTimeout ( onClose ?? close , ok ?COPIED_MENU_MS :0 ); }); 30}}>< Icon name = { copied ?'check' :'copy' }/>{ tr ( copied ?'Copied' :children )}</ button >; 31}