Skip to content

Commit

Permalink
Convert ClipboardButton to TypeScript (#51334)
Browse files Browse the repository at this point in the history
* Convert `ClipboardButton` to TypeScript

* Add changelog entry

* Update CHANGELOG.md

---------

Co-authored-by: Lena Morita <lena@jaguchi.com>
  • Loading branch information
sunyatasattva and mirka committed Jun 15, 2023
1 parent 136ff0d commit fb0a8f0
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 18 deletions.
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;
}

1 comment on commit fb0a8f0

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Flaky tests detected in fb0a8f0.
Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

🔍 Workflow run URL: https://github.com/WordPress/gutenberg/actions/runs/5279664060
📝 Reported issues:

Please sign in to comment.