-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
58 changes: 58 additions & 0 deletions
58
packages/ui/src/ui/copy-to-clipboard/copy-to-clipboard.module.css
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
.root { | ||
padding: var(--space-xxxsmall); | ||
} | ||
|
||
.wrapper { | ||
display: flex; | ||
gap: var(--space-xxsmall); | ||
align-items: center; | ||
} | ||
|
||
.content { | ||
flex: 1 1 auto; | ||
white-space: nowrap; | ||
line-height: 1.1; | ||
} | ||
|
||
.icon { | ||
flex: 0 0 auto; | ||
position: relative; | ||
width: var(--space-small); | ||
height: var(--space-small); | ||
} | ||
|
||
.iconCopy, | ||
.iconDone { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
} | ||
|
||
.iconCopy { | ||
color: var(--color-text-ultra-light); | ||
} | ||
|
||
.iconDone { | ||
color: var(--color-success); | ||
} | ||
|
||
/** done state */ | ||
.iconCopy { | ||
opacity: 1; | ||
transition: var(--ui-transition-out); | ||
} | ||
|
||
.iconDone { | ||
opacity: 0; | ||
transition: var(--ui-transition-out); | ||
} | ||
|
||
.done .iconCopy { | ||
opacity: 0; | ||
transition: var(--ui-transition-in); | ||
} | ||
|
||
.done .iconDone { | ||
opacity: 1; | ||
transition: var(--ui-transition-in); | ||
} |
33 changes: 33 additions & 0 deletions
33
packages/ui/src/ui/copy-to-clipboard/copy-to-clipboard.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import { Meta, StoryObj } from '@storybook/react'; | ||
import { CopyToClipboard } from '.'; | ||
|
||
const meta: Meta<typeof CopyToClipboard> = { | ||
title: 'UI/CopyToClipboard', | ||
component: CopyToClipboard, | ||
}; | ||
|
||
export default meta; | ||
|
||
type Story = StoryObj<Meta>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
text: 'Lorem ipsum dolor sit amd', | ||
}, | ||
}; | ||
|
||
export const Inline: Story = { | ||
args: { | ||
...Default.args, | ||
children: Default.args?.text, | ||
outline: true, | ||
size: 'small', | ||
}, | ||
}; | ||
|
||
export const Outline: Story = { | ||
args: { | ||
...Default.args, | ||
outline: true, | ||
}, | ||
}; |
57 changes: 57 additions & 0 deletions
57
packages/ui/src/ui/copy-to-clipboard/copy-to-clipboard.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import type { ComponentProps } from 'react'; | ||
import React, { useCallback, useState } from 'react'; | ||
import cx from 'classnames'; | ||
import { useCopyToClipboard } from 'react-use'; | ||
|
||
import { Button } from '../button'; | ||
import { Icon } from '../icon'; | ||
import { Tooltip } from '../tooltip'; | ||
import css from './copy-to-clipboard.module.css'; | ||
|
||
export type CopyToClipboardProps = { | ||
/** | ||
* Text to copy to clipboard | ||
*/ | ||
text: string; | ||
} & ComponentProps<typeof Button>; | ||
|
||
export const CopyToClipboard = (props: CopyToClipboardProps) => { | ||
const { className = '', children, text, ...restProps } = props; | ||
|
||
const [, copyToClipboard] = useCopyToClipboard(); | ||
const [doneTimeout, setDoneTimeout] = useState<ReturnType<typeof setTimeout> | null>(); | ||
|
||
const handleOnClick = useCallback(() => { | ||
if (doneTimeout) { | ||
clearTimeout(doneTimeout); | ||
setDoneTimeout(null); | ||
} | ||
|
||
setDoneTimeout(null); | ||
copyToClipboard(text); | ||
|
||
const done = setTimeout(() => setDoneTimeout(null), 2000); | ||
setDoneTimeout(done); | ||
}, [text, doneTimeout, setDoneTimeout]); | ||
|
||
const rootClassName = cx(css.root, className, doneTimeout && css.done); | ||
|
||
return ( | ||
<Tooltip | ||
title={doneTimeout ? 'Copied' : 'Copy to clipboard'} | ||
as={Button} | ||
type="button" | ||
onClick={handleOnClick} | ||
className={rootClassName} | ||
{...restProps} | ||
> | ||
<span className={css.wrapper}> | ||
{children && <span className={css.content}>{children}</span>} | ||
<span className={css.icon}> | ||
<Icon className={css.iconCopy} glyph={Icon.ICONS.CLIPBOARD} /> | ||
<Icon className={css.iconDone} glyph={Icon.ICONS.CLIPBOARD_CHECK} /> | ||
</span> | ||
</span> | ||
</Tooltip> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './copy-to-clipboard'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters