[Glitch] Refactor "copy to clipboard" functionality into hook
Port bd2e86d7f4f30b290ad1fb4dce6c1b9846776a3d to glitch-soc Signed-off-by: Claire <claire.github-309c@sitedethib.com>
This commit is contained in:
parent
daf498a743
commit
2d58ea3d50
@ -11,7 +11,7 @@ import NotificationsIcon from '@/material-icons/400-24px/notifications.svg?react
|
|||||||
import NotificationsActiveIcon from '@/material-icons/400-24px/notifications_active-fill.svg?react';
|
import NotificationsActiveIcon from '@/material-icons/400-24px/notifications_active-fill.svg?react';
|
||||||
import ShareIcon from '@/material-icons/400-24px/share.svg?react';
|
import ShareIcon from '@/material-icons/400-24px/share.svg?react';
|
||||||
|
|
||||||
import { CopyIconButton } from '../copy_icon_button';
|
import { CopyIconButton } from '../copy_button';
|
||||||
import { FollowButton } from '../follow_button';
|
import { FollowButton } from '../follow_button';
|
||||||
import { IconButton } from '../icon_button';
|
import { IconButton } from '../icon_button';
|
||||||
|
|
||||||
|
|||||||
@ -7,17 +7,16 @@ import classNames from 'classnames';
|
|||||||
|
|
||||||
import Overlay from 'react-overlays/esm/Overlay';
|
import Overlay from 'react-overlays/esm/Overlay';
|
||||||
|
|
||||||
import { showAlert } from '@/flavours/glitch/actions/alerts';
|
|
||||||
import { useAccount } from '@/flavours/glitch/hooks/useAccount';
|
import { useAccount } from '@/flavours/glitch/hooks/useAccount';
|
||||||
import { useRelationship } from '@/flavours/glitch/hooks/useRelationship';
|
import { useRelationship } from '@/flavours/glitch/hooks/useRelationship';
|
||||||
import { useAppDispatch, useAppSelector } from '@/flavours/glitch/store';
|
import { useAppSelector } from '@/flavours/glitch/store';
|
||||||
import AtIcon from '@/material-icons/400-24px/alternate_email.svg?react';
|
import AtIcon from '@/material-icons/400-24px/alternate_email.svg?react';
|
||||||
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
||||||
import HelpIcon from '@/material-icons/400-24px/help.svg?react';
|
import HelpIcon from '@/material-icons/400-24px/help.svg?react';
|
||||||
import DomainIcon from '@/material-icons/400-24px/language.svg?react';
|
import DomainIcon from '@/material-icons/400-24px/language.svg?react';
|
||||||
|
|
||||||
import { FollowsYouBadge } from '../badge';
|
import { FollowsYouBadge } from '../badge';
|
||||||
import { Button } from '../button';
|
import { CopyButton } from '../copy_button';
|
||||||
import { DisplayName } from '../display_name';
|
import { DisplayName } from '../display_name';
|
||||||
import { Icon } from '../icon';
|
import { Icon } from '../icon';
|
||||||
|
|
||||||
@ -90,17 +89,6 @@ const AccountNameHelp: FC<{
|
|||||||
|
|
||||||
const handle = `@${username}@${domain}`;
|
const handle = `@${username}@${domain}`;
|
||||||
|
|
||||||
const dispatch = useAppDispatch();
|
|
||||||
const [copied, setCopied] = useState(false);
|
|
||||||
const handleCopy = useCallback(() => {
|
|
||||||
void navigator.clipboard.writeText(handle);
|
|
||||||
setCopied(true);
|
|
||||||
dispatch(showAlert({ message: messages.copied }));
|
|
||||||
setTimeout(() => {
|
|
||||||
setCopied(false);
|
|
||||||
}, 700);
|
|
||||||
}, [handle, dispatch]);
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<button
|
<button
|
||||||
@ -182,21 +170,25 @@ const AccountNameHelp: FC<{
|
|||||||
tagName='p'
|
tagName='p'
|
||||||
/>
|
/>
|
||||||
|
|
||||||
<Button onClick={handleCopy} className={classes.handleCopy}>
|
<CopyButton value={handle} className={classes.handleCopy}>
|
||||||
<Icon id='copy' icon={ContentCopyIcon} />
|
{(wasCopied) => (
|
||||||
{!copied && (
|
<>
|
||||||
<FormattedMessage
|
<Icon id='copy' icon={ContentCopyIcon} />
|
||||||
id='account.name.copy'
|
{!wasCopied && (
|
||||||
defaultMessage='Copy handle'
|
<FormattedMessage
|
||||||
/>
|
id='account.name.copy'
|
||||||
|
defaultMessage='Copy handle'
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
{wasCopied && (
|
||||||
|
<FormattedMessage
|
||||||
|
id='copypaste.copied'
|
||||||
|
defaultMessage='Copied'
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
)}
|
)}
|
||||||
{copied && (
|
</CopyButton>
|
||||||
<FormattedMessage
|
|
||||||
id='copypaste.copied'
|
|
||||||
defaultMessage='Copied'
|
|
||||||
/>
|
|
||||||
)}
|
|
||||||
</Button>
|
|
||||||
</div>
|
</div>
|
||||||
)}
|
)}
|
||||||
</Overlay>
|
</Overlay>
|
||||||
|
|||||||
75
app/javascript/flavours/glitch/components/copy_button.tsx
Normal file
75
app/javascript/flavours/glitch/components/copy_button.tsx
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
import { useState, useCallback } from 'react';
|
||||||
|
|
||||||
|
import { defineMessages } from 'react-intl';
|
||||||
|
|
||||||
|
import classNames from 'classnames';
|
||||||
|
|
||||||
|
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
||||||
|
import { showAlert } from 'flavours/glitch/actions/alerts';
|
||||||
|
import { IconButton } from 'flavours/glitch/components/icon_button';
|
||||||
|
import { useAppDispatch } from 'flavours/glitch/store';
|
||||||
|
|
||||||
|
import { Button } from './button';
|
||||||
|
|
||||||
|
const messages = defineMessages({
|
||||||
|
copied: {
|
||||||
|
id: 'copy_icon_button.copied',
|
||||||
|
defaultMessage: 'Copied to clipboard',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
export function useCopyToClipboard({ text }: { text: string }) {
|
||||||
|
const [wasCopied, setWasCopied] = useState(false);
|
||||||
|
const dispatch = useAppDispatch();
|
||||||
|
|
||||||
|
const copyText = useCallback(() => {
|
||||||
|
void navigator.clipboard.writeText(text);
|
||||||
|
setWasCopied(true);
|
||||||
|
dispatch(showAlert({ message: messages.copied }));
|
||||||
|
setTimeout(() => {
|
||||||
|
setWasCopied(false);
|
||||||
|
}, 700);
|
||||||
|
}, [setWasCopied, text, dispatch]);
|
||||||
|
|
||||||
|
return { copyText, wasCopied };
|
||||||
|
}
|
||||||
|
|
||||||
|
export const CopyButton: React.FC<
|
||||||
|
Omit<
|
||||||
|
React.ComponentPropsWithoutRef<typeof Button>,
|
||||||
|
'onClick' | 'text' | 'children'
|
||||||
|
> & {
|
||||||
|
value: string;
|
||||||
|
children: React.ReactNode | ((wasCopied: boolean) => React.ReactNode);
|
||||||
|
}
|
||||||
|
> = ({ value, children, ...otherProps }) => {
|
||||||
|
const { copyText, wasCopied } = useCopyToClipboard({ text: value });
|
||||||
|
|
||||||
|
const label = typeof children === 'function' ? children(wasCopied) : children;
|
||||||
|
|
||||||
|
return (
|
||||||
|
<Button {...otherProps} onClick={copyText}>
|
||||||
|
{label}
|
||||||
|
</Button>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const CopyIconButton: React.FC<{
|
||||||
|
title: string;
|
||||||
|
value: string;
|
||||||
|
className?: string;
|
||||||
|
'aria-describedby'?: string;
|
||||||
|
}> = ({ title, value, className, 'aria-describedby': ariaDescribedBy }) => {
|
||||||
|
const { copyText, wasCopied } = useCopyToClipboard({ text: value });
|
||||||
|
|
||||||
|
return (
|
||||||
|
<IconButton
|
||||||
|
className={classNames(className, wasCopied ? 'copied' : 'copyable')}
|
||||||
|
title={title}
|
||||||
|
onClick={copyText}
|
||||||
|
icon='copy-icon'
|
||||||
|
iconComponent={ContentCopyIcon}
|
||||||
|
aria-describedby={ariaDescribedBy}
|
||||||
|
/>
|
||||||
|
);
|
||||||
|
};
|
||||||
@ -1,47 +0,0 @@
|
|||||||
import { useState, useCallback } from 'react';
|
|
||||||
|
|
||||||
import { defineMessages } from 'react-intl';
|
|
||||||
|
|
||||||
import classNames from 'classnames';
|
|
||||||
|
|
||||||
import ContentCopyIcon from '@/material-icons/400-24px/content_copy.svg?react';
|
|
||||||
import { showAlert } from 'flavours/glitch/actions/alerts';
|
|
||||||
import { IconButton } from 'flavours/glitch/components/icon_button';
|
|
||||||
import { useAppDispatch } from 'flavours/glitch/store';
|
|
||||||
|
|
||||||
const messages = defineMessages({
|
|
||||||
copied: {
|
|
||||||
id: 'copy_icon_button.copied',
|
|
||||||
defaultMessage: 'Copied to clipboard',
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
export const CopyIconButton: React.FC<{
|
|
||||||
title: string;
|
|
||||||
value: string;
|
|
||||||
className?: string;
|
|
||||||
'aria-describedby'?: string;
|
|
||||||
}> = ({ title, value, className, 'aria-describedby': ariaDescribedBy }) => {
|
|
||||||
const [copied, setCopied] = useState(false);
|
|
||||||
const dispatch = useAppDispatch();
|
|
||||||
|
|
||||||
const handleClick = useCallback(() => {
|
|
||||||
void navigator.clipboard.writeText(value);
|
|
||||||
setCopied(true);
|
|
||||||
dispatch(showAlert({ message: messages.copied }));
|
|
||||||
setTimeout(() => {
|
|
||||||
setCopied(false);
|
|
||||||
}, 700);
|
|
||||||
}, [setCopied, value, dispatch]);
|
|
||||||
|
|
||||||
return (
|
|
||||||
<IconButton
|
|
||||||
className={classNames(className, copied ? 'copied' : 'copyable')}
|
|
||||||
title={title}
|
|
||||||
onClick={handleClick}
|
|
||||||
icon='copy-icon'
|
|
||||||
iconComponent={ContentCopyIcon}
|
|
||||||
aria-describedby={ariaDescribedBy}
|
|
||||||
/>
|
|
||||||
);
|
|
||||||
};
|
|
||||||
@ -4,7 +4,7 @@ import { useIntl } from 'react-intl';
|
|||||||
|
|
||||||
import classNames from 'classnames';
|
import classNames from 'classnames';
|
||||||
|
|
||||||
import { CopyIconButton } from 'flavours/glitch/components/copy_icon_button';
|
import { CopyIconButton } from '@/flavours/glitch/components/copy_button';
|
||||||
|
|
||||||
import classes from './copy_link_field.module.scss';
|
import classes from './copy_link_field.module.scss';
|
||||||
import { FormFieldWrapper } from './form_field_wrapper';
|
import { FormFieldWrapper } from './form_field_wrapper';
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user