Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert ClipboardButton to TypeScript #51334

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@

- `Popover`: Allow legitimate 0 positions to update popover position ([#51320](https://github.com/WordPress/gutenberg/pull/51320)).

### Internal

- `ClipboardButton`: Convert to TypeScript ([#51334](https://github.com/WordPress/gutenberg/pull/51334)).

## 25.1.0 (2023-06-07)

### Enhancements
Expand All @@ -27,7 +31,7 @@
### Experimental

- `DropdownMenu` v2: Tweak styles ([#50967](https://github.com/WordPress/gutenberg/pull/50967), [#51097](https://github.com/WordPress/gutenberg/pull/51097)).
- `DropdownMenu` v2: change default placement to match the legacy `DropdownMenu` component ([#51133](https://github.com/WordPress/gutenberg/pull/51133)).
- `DropdownMenu` v2: change default placement to match the legacy `DropdownMenu` component ([#51133](https://github.com/WordPress/gutenberg/pull/51133)).
- `DropdownMenu` v2: Render in the default `Popover.Slot` ([#51046](https://github.com/WordPress/gutenberg/pull/51046)).

## 25.0.0 (2023-05-24)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,45 +14,40 @@ import deprecated from '@wordpress/deprecated';
* Internal dependencies
*/
import Button from '../button';
import type { ClipboardButtonProps } from './types';
import type { WordPressComponentProps } from '../ui/context';

const TIMEOUT = 4000;

/**
* @param {Object} props
* @param {string} [props.className]
* @param {import('react').ReactNode} props.children
* @param {() => void} props.onCopy
* @param {() => void} [props.onFinishCopy]
* @param {string} props.text
*/
export default function ClipboardButton( {
className,
children,
onCopy,
onFinishCopy,
text,
...buttonProps
} ) {
}: WordPressComponentProps< ClipboardButtonProps, 'button', false > ) {
deprecated( 'wp.components.ClipboardButton', {
since: '5.8',
alternative: 'wp.compose.useCopyToClipboard',
} );

/** @type {import('react').MutableRefObject<ReturnType<setTimeout> | undefined>} */
const timeoutId = useRef();
const timeoutId = useRef< NodeJS.Timeout >();
const ref = useCopyToClipboard( text, () => {
onCopy();
// @ts-expect-error: Should check if .current is defined, but not changing because this component is deprecated.
clearTimeout( timeoutId.current );
if ( timeoutId.current ) {
clearTimeout( timeoutId.current );
}

if ( onFinishCopy ) {
timeoutId.current = setTimeout( () => onFinishCopy(), TIMEOUT );
}
} );

useEffect( () => {
// @ts-expect-error: Should check if .current is defined, but not changing because this component is deprecated.
clearTimeout( timeoutId.current );
if ( timeoutId.current ) {
clearTimeout( timeoutId.current );
}
}, [] );

const classes = classnames( 'components-clipboard-button', className );
Expand All @@ -62,8 +57,7 @@ export default function ClipboardButton( {
// This causes documentHasSelection() in the copy-handler component to
// mistakenly override the ClipboardButton, and copy a serialized string
// of the current block instead.
/** @type {import('react').ClipboardEventHandler<HTMLButtonElement>} */
const focusOnCopyEventTarget = ( event ) => {
const focusOnCopyEventTarget: React.ClipboardEventHandler = ( event ) => {
// @ts-expect-error: Should be currentTarget, but not changing because this component is deprecated.
event.target.focus();
};
Expand Down
11 changes: 11 additions & 0 deletions packages/components/src/clipboard-button/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/**
* External dependencies
*/
import type { ReactNode } from 'react';

export interface ClipboardButtonProps {
children: ReactNode;
onCopy: () => void;
onFinishCopy?: () => void;
text: string;
}