Skip to content
Closed
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
78 changes: 77 additions & 1 deletion polaris-react/src/components/Tooltip/Tooltip.stories.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import React, {useState} from 'react';
import {QuestionCircleIcon} from '@shopify/polaris-icons';
import {
CheckIcon,
ClipboardIcon,
QuestionCircleIcon,
} from '@shopify/polaris-icons';
import type {ComponentMeta} from '@storybook/react';
import {
Button,
Expand All @@ -13,6 +17,7 @@ import {
BlockStack,
Popover,
Card,
Link,
} from '@shopify/polaris';
import type {TooltipProps} from '@shopify/polaris';

Expand Down Expand Up @@ -566,3 +571,74 @@ export function OneCharacter() {
</Box>
);
}

export function CopyToClipboard() {
const [copy, status] = useCopyToClipboard({
defaultValue: 'hello@example.com',
});

return (
<div style={{maxWidth: 300, paddingTop: 100}}>
<Card>
<InlineStack align="space-between" gap="200">
<Link removeUnderline>hello@example.com</Link>
<Tooltip
dismissOnMouseOut
hoverDelay={500}
preferredPosition="above"
content="Copy"
>
<Button
variant="tertiary"
accessibilityLabel="Copy"
icon={status === 'copied' ? CheckIcon : ClipboardIcon}
onClick={copy}
/>
</Tooltip>
</InlineStack>
</Card>
</div>
);
}

type Status = 'inactive' | 'copied' | 'failed';

interface UseCopyToClipboardOptions {
defaultValue?: string;
timeout?: number;
}

/**
* Copy text to the native clipboard using the `navigator.clipboard` API
* Adapted from https://www.benmvp.com/blog/copy-to-clipboard-react-custom-hook
*/
function useCopyToClipboard(options: UseCopyToClipboardOptions = {}) {
const {defaultValue = '', timeout = 1500} = options;

const [status, setStatus] = React.useState<Status>('inactive');

const copy = React.useCallback(
(value?: string) => {
navigator.clipboard
.writeText(typeof value === 'string' ? value : defaultValue)
.then(
() => setStatus('copied'),
() => setStatus('failed'),
)
.catch((error) => {
throw error;
});
},
[defaultValue],
);

React.useEffect(() => {
if (status === 'inactive') return;

const timeoutId = setTimeout(() => setStatus('inactive'), timeout);

return () => clearTimeout(timeoutId);
}, [status, timeout]);

return [copy, status] as const;
}
13 changes: 12 additions & 1 deletion polaris-react/src/components/Tooltip/Tooltip.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,13 @@ export function Tooltip({

const handleKeyUp = useCallback(
(event: React.KeyboardEvent) => {
if (event.key !== 'Escape') return;
if (
event.key !== 'Escape' &&
event.key !== 'Enter' &&
event.key !== ' '
) {
return;
}
handleClose?.();
handleBlur();
persistOnClick && togglePersisting();
Expand Down Expand Up @@ -206,6 +212,11 @@ export function Tooltip({
handleFocus();
}}
onBlur={() => {
if (hoverDelayTimeout.current) {
clearTimeout(hoverDelayTimeout.current);
hoverDelayTimeout.current = null;
}

handleClose();
handleBlur();

Expand Down