okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Trial2 merge f-0291-pr-nv (97b3d614f)1306424be

main
2.0 KiB31 linesraw
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 }) {
12    const { copied, copy } = useCopy();
13    return <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 }) {
24    const { copied, copy } = useCopy(COPIED_MENU_MS);
25    const close = useContext(MenuCloseContext);
26    return <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.
28        e.stopPropagation();
29        void 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}