-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add copy to clipboard component
- Loading branch information
1 parent
563a196
commit 914049e
Showing
7 changed files
with
150 additions
and
0 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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
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,61 @@ | ||
import React, { useCallback } from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import clsx from 'clsx'; | ||
|
||
import { Box } from '../../atoms/box'; | ||
import { Button, Emphasis } from '../../atoms/button'; | ||
import { Input } from '../../atoms/input'; | ||
import { useID } from '../../hooks/id'; | ||
|
||
import './copy.scss'; | ||
|
||
function Copy({ className, value, editable = false, onCopy, ...others }) { | ||
const id = useID(others); | ||
|
||
const handleClick = useCallback(function handleClick() { | ||
// based on https://www.w3schools.com/howto/howto_js_copy_clipboard.asp | ||
|
||
/* Get the text field */ | ||
var copyText = document.getElementById(id); | ||
|
||
/* Select the text field */ | ||
copyText.select(); | ||
copyText.setSelectionRange(0, 99999); /* For mobile devices */ | ||
|
||
/* Copy the text inside the text field */ | ||
document.execCommand('copy'); | ||
|
||
navigator?.clipboard?.readText?.().then(text => { | ||
onCopy?.(text); | ||
}); | ||
}); | ||
|
||
return ( | ||
<Box | ||
borderless | ||
paddingless | ||
className={clsx('cb-copy', className)} | ||
trailing={ | ||
<Button | ||
emphasis={Emphasis.ghost} | ||
icon="content-copy" | ||
onClick={handleClick} | ||
data-testid="cb-copy-button" | ||
/> | ||
} | ||
> | ||
<Input | ||
id={id} | ||
{...(editable ? { value } : { defaultValue: value })} | ||
readOnly={!editable} | ||
/> | ||
</Box> | ||
); | ||
} | ||
|
||
Copy.propTypes = { | ||
editable: PropTypes.bool, | ||
onCopy: PropTypes.func, | ||
}; | ||
|
||
export default Copy; |
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,21 @@ | ||
@import '../../styles/tools/_tools.mixins'; | ||
|
||
.cb-copy { | ||
@include transition(all); | ||
|
||
@include disableable; | ||
@include font; | ||
|
||
@include px(spacing-4); | ||
@include py(spacing-4); | ||
|
||
display: inline-flex; | ||
flex-direction: row; | ||
|
||
// box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), | ||
// 0 2px 4px -1px rgba(0, 0, 0, 0.06); | ||
|
||
// @include hoverable { | ||
// box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2); | ||
// } | ||
} |
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,29 @@ | ||
import React from 'react'; | ||
|
||
import generator from '../../../test/data-generator'; | ||
import Copy from './copy'; | ||
|
||
export default { | ||
title: 'Molecules/Copy', | ||
component: Copy, | ||
docs: { | ||
description: { | ||
story: 'some story *a*markdown**', | ||
}, | ||
}, | ||
}; | ||
|
||
const Template = args => { | ||
return ( | ||
<div className="block"> | ||
<p className="mb-2"> | ||
This is me, a cool Copy to Clipboard ready to be played around. Try me | ||
:) | ||
</p> | ||
|
||
<Copy value={generator.animal()} {...args} className="" /> | ||
</div> | ||
); | ||
}; | ||
|
||
export const Playground = Template.bind({}); |
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,35 @@ | ||
import React from 'react'; | ||
|
||
import { render, userEvent, mount, asTestAttr } from '../../../test/helpers'; | ||
import { Copy } from './index'; | ||
import generator from '../../../test/data-generator'; | ||
|
||
describe('Copy', () => { | ||
it('renders correctly', () => { | ||
const props = { | ||
value: generator.animal(), | ||
}; | ||
|
||
const { getByTestId } = render(<Copy {...props} />); | ||
|
||
expect(getByTestId('cb-copy-button')).toBeTruthy(); | ||
expect(getByTestId('cb-input')).toBeTruthy(); | ||
}); | ||
|
||
it('copies content', () => { | ||
document.execCommand = jest.fn(); | ||
|
||
const props = { | ||
value: generator.animal(), | ||
onCopy: jest.fn(), | ||
}; | ||
|
||
const { getByTestId } = render(<Copy {...props} />); | ||
|
||
userEvent.click(getByTestId('cb-copy-button')); | ||
|
||
expect(document.execCommand).toHaveBeenCalledWith('copy'); | ||
}); | ||
|
||
// TODO: test paste | ||
}); |
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 { default as Copy } from './copy'; |