Add new CopyableInput component, use in EmbedModal
This commit is contained in:
parent
5a6dcf0c4d
commit
3909c74c00
|
@ -0,0 +1,42 @@
|
||||||
|
import React, { useRef } from 'react';
|
||||||
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
|
import { Button, HStack, Input } from './ui';
|
||||||
|
|
||||||
|
interface ICopyableInput {
|
||||||
|
/** Text to be copied. */
|
||||||
|
value?: string,
|
||||||
|
}
|
||||||
|
|
||||||
|
/** An input with copy abilities. */
|
||||||
|
const CopyableInput: React.FC<ICopyableInput> = ({ value }) => {
|
||||||
|
const input = useRef<HTMLInputElement>(null);
|
||||||
|
|
||||||
|
const selectInput = () => {
|
||||||
|
input.current?.select();
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<HStack alignItems='center'>
|
||||||
|
<Input
|
||||||
|
ref={input}
|
||||||
|
type='text'
|
||||||
|
value={value}
|
||||||
|
className='rounded-r-none'
|
||||||
|
outerClassName='flex-grow'
|
||||||
|
onClick={selectInput}
|
||||||
|
readOnly
|
||||||
|
/>
|
||||||
|
|
||||||
|
<Button
|
||||||
|
theme='primary'
|
||||||
|
className='mt-1 h-full rounded-l-none rounded-r-lg'
|
||||||
|
onClick={selectInput}
|
||||||
|
>
|
||||||
|
<FormattedMessage id='input.copy' defaultMessage='Copy' />
|
||||||
|
</Button>
|
||||||
|
</HStack>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default CopyableInput;
|
|
@ -1,3 +1,4 @@
|
||||||
|
import classNames from 'classnames';
|
||||||
import * as React from 'react';
|
import * as React from 'react';
|
||||||
import { Link } from 'react-router-dom';
|
import { Link } from 'react-router-dom';
|
||||||
|
|
||||||
|
@ -12,8 +13,8 @@ interface IButton {
|
||||||
block?: boolean,
|
block?: boolean,
|
||||||
/** Elements inside the <button> */
|
/** Elements inside the <button> */
|
||||||
children?: React.ReactNode,
|
children?: React.ReactNode,
|
||||||
/** @deprecated unused */
|
/** Extra class names for the button. */
|
||||||
classNames?: string,
|
className?: string,
|
||||||
/** Prevent the button from being clicked. */
|
/** Prevent the button from being clicked. */
|
||||||
disabled?: boolean,
|
disabled?: boolean,
|
||||||
/** URL to an SVG icon to render inside the button. */
|
/** URL to an SVG icon to render inside the button. */
|
||||||
|
@ -22,8 +23,6 @@ interface IButton {
|
||||||
onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void,
|
onClick?: (event: React.MouseEvent<HTMLButtonElement>) => void,
|
||||||
/** A predefined button size. */
|
/** A predefined button size. */
|
||||||
size?: ButtonSizes,
|
size?: ButtonSizes,
|
||||||
/** @deprecated unused */
|
|
||||||
style?: React.CSSProperties,
|
|
||||||
/** Text inside the button. Takes precedence over `children`. */
|
/** Text inside the button. Takes precedence over `children`. */
|
||||||
text?: React.ReactNode,
|
text?: React.ReactNode,
|
||||||
/** Makes the button into a navlink, if provided. */
|
/** Makes the button into a navlink, if provided. */
|
||||||
|
@ -47,6 +46,7 @@ const Button = React.forwardRef<HTMLButtonElement, IButton>((props, ref): JSX.El
|
||||||
theme = 'secondary',
|
theme = 'secondary',
|
||||||
to,
|
to,
|
||||||
type = 'button',
|
type = 'button',
|
||||||
|
className,
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const themeClass = useButtonStyles({
|
const themeClass = useButtonStyles({
|
||||||
|
@ -72,7 +72,7 @@ const Button = React.forwardRef<HTMLButtonElement, IButton>((props, ref): JSX.El
|
||||||
|
|
||||||
const renderButton = () => (
|
const renderButton = () => (
|
||||||
<button
|
<button
|
||||||
className={themeClass}
|
className={classNames(themeClass, className)}
|
||||||
disabled={disabled}
|
disabled={disabled}
|
||||||
onClick={handleClick}
|
onClick={handleClick}
|
||||||
ref={ref}
|
ref={ref}
|
||||||
|
|
|
@ -2,8 +2,9 @@ import React, { useEffect } from 'react';
|
||||||
import { FormattedMessage } from 'react-intl';
|
import { FormattedMessage } from 'react-intl';
|
||||||
|
|
||||||
import { closeModal } from 'soapbox/actions/modals';
|
import { closeModal } from 'soapbox/actions/modals';
|
||||||
|
import CopyableInput from 'soapbox/components/copyable-input';
|
||||||
import SafeEmbed from 'soapbox/components/safe-embed';
|
import SafeEmbed from 'soapbox/components/safe-embed';
|
||||||
import { Modal, Stack, Text, Input, Divider } from 'soapbox/components/ui';
|
import { Modal, Stack, Text, Divider } from 'soapbox/components/ui';
|
||||||
import { useAppDispatch } from 'soapbox/hooks';
|
import { useAppDispatch } from 'soapbox/hooks';
|
||||||
import useEmbed from 'soapbox/queries/embed';
|
import useEmbed from 'soapbox/queries/embed';
|
||||||
|
|
||||||
|
@ -22,10 +23,6 @@ const EmbedModal: React.FC<IEmbedModal> = ({ url, onError }) => {
|
||||||
}
|
}
|
||||||
}, [isError]);
|
}, [isError]);
|
||||||
|
|
||||||
const handleInputClick: React.MouseEventHandler<HTMLInputElement> = (e) => {
|
|
||||||
e.currentTarget.select();
|
|
||||||
};
|
|
||||||
|
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
dispatch(closeModal('EMBED'));
|
dispatch(closeModal('EMBED'));
|
||||||
};
|
};
|
||||||
|
@ -40,12 +37,7 @@ const EmbedModal: React.FC<IEmbedModal> = ({ url, onError }) => {
|
||||||
<FormattedMessage id='embed.instructions' defaultMessage='Embed this post on your website by copying the code below.' />
|
<FormattedMessage id='embed.instructions' defaultMessage='Embed this post on your website by copying the code below.' />
|
||||||
</Text>
|
</Text>
|
||||||
|
|
||||||
<Input
|
<CopyableInput value={embed?.html || ''} />
|
||||||
type='text'
|
|
||||||
readOnly
|
|
||||||
value={embed?.html || ''}
|
|
||||||
onClick={handleInputClick}
|
|
||||||
/>
|
|
||||||
</Stack>
|
</Stack>
|
||||||
|
|
||||||
<div className='py-9'>
|
<div className='py-9'>
|
||||||
|
|
Loading…
Reference in New Issue