-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'develop' into hm/redesign-key-management-modal
- Loading branch information
Showing
10 changed files
with
173 additions
and
24 deletions.
There are no files selected for viewing
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
64 changes: 64 additions & 0 deletions
64
ui/components/multichain/address-copy-button/address-copy-button.js
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,64 @@ | ||
import React from 'react'; | ||
import PropTypes from 'prop-types'; | ||
import classnames from 'classnames'; | ||
import { ICON_NAMES, ButtonBase } from '../../component-library'; | ||
import { | ||
BackgroundColor, | ||
TextVariant, | ||
TextColor, | ||
Size, | ||
BorderRadius, | ||
AlignItems, | ||
} from '../../../helpers/constants/design-system'; | ||
import { useCopyToClipboard } from '../../../hooks/useCopyToClipboard'; | ||
import { shortenAddress } from '../../../helpers/utils/util'; | ||
import Tooltip from '../../ui/tooltip/tooltip'; | ||
import { useI18nContext } from '../../../hooks/useI18nContext'; | ||
|
||
export const AddressCopyButton = ({ | ||
address, | ||
shorten = false, | ||
wrap = false, | ||
}) => { | ||
const displayAddress = shorten ? shortenAddress(address) : address; | ||
const [copied, handleCopy] = useCopyToClipboard(); | ||
const t = useI18nContext(); | ||
|
||
return ( | ||
<Tooltip position="bottom" title={copied ? t('copiedExclamation') : null}> | ||
<ButtonBase | ||
backgroundColor={BackgroundColor.primaryMuted} | ||
onClick={() => handleCopy(address)} | ||
paddingRight={4} | ||
paddingLeft={4} | ||
size={Size.SM} | ||
variant={TextVariant.bodyXs} | ||
color={TextColor.primaryDefault} | ||
endIconName={copied ? ICON_NAMES.COPY_SUCCESS : ICON_NAMES.COPY} | ||
className={classnames('multichain-address-copy-button', { | ||
'multichain-address-copy-button__address--wrap': wrap, | ||
})} | ||
borderRadius={BorderRadius.pill} | ||
alignItems={AlignItems.center} | ||
data-testid="address-copy-button-text" | ||
> | ||
{displayAddress} | ||
</ButtonBase> | ||
</Tooltip> | ||
); | ||
}; | ||
|
||
AddressCopyButton.propTypes = { | ||
/** | ||
* Address to be copied | ||
*/ | ||
address: PropTypes.string.isRequired, | ||
/** | ||
* Represents if the address should be shortened | ||
*/ | ||
shorten: PropTypes.bool, | ||
/** | ||
* Represents if the element should wrap to multiple lines | ||
*/ | ||
wrap: PropTypes.bool, | ||
}; |
36 changes: 36 additions & 0 deletions
36
ui/components/multichain/address-copy-button/address-copy-button.stories.js
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,36 @@ | ||
import React from 'react'; | ||
import { AddressCopyButton } from '.'; | ||
|
||
export default { | ||
title: 'Components/Multichain/AddressCopyButton', | ||
component: AddressCopyButton, | ||
argTypes: { | ||
address: { | ||
control: 'text', | ||
}, | ||
shorten: { | ||
control: 'boolean', | ||
}, | ||
wrap: { | ||
control: 'boolean', | ||
}, | ||
}, | ||
args: { | ||
address: '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc', | ||
}, | ||
}; | ||
|
||
export const DefaultStory = (args) => <AddressCopyButton {...args} />; | ||
DefaultStory.storyName = 'Default'; | ||
|
||
export const ShortenedStory = (args) => <AddressCopyButton {...args} />; | ||
ShortenedStory.storyName = 'Shortened'; | ||
ShortenedStory.args = { shorten: true }; | ||
|
||
export const WrappedStory = (args) => ( | ||
<div style={{ width: '200px' }}> | ||
<AddressCopyButton {...args} /> | ||
</div> | ||
); | ||
WrappedStory.storyName = 'Wrapped'; | ||
WrappedStory.args = { wrap: true }; |
31 changes: 31 additions & 0 deletions
31
ui/components/multichain/address-copy-button/address-copy-button.test.js
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,31 @@ | ||
import React from 'react'; | ||
import { fireEvent, render } from '@testing-library/react'; | ||
import { AddressCopyButton } from '.'; | ||
|
||
const SAMPLE_ADDRESS = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc'; | ||
|
||
describe('AccountListItem', () => { | ||
it('renders the full address by default', () => { | ||
render(<AddressCopyButton address={SAMPLE_ADDRESS} />); | ||
expect( | ||
document.querySelector('[data-testid="address-copy-button-text"]') | ||
.textContent, | ||
).toStrictEqual(SAMPLE_ADDRESS); | ||
}); | ||
|
||
it('renders a shortened address when it should', () => { | ||
render(<AddressCopyButton address={SAMPLE_ADDRESS} shorten />); | ||
expect( | ||
document.querySelector('[data-testid="address-copy-button-text"]') | ||
.textContent, | ||
).toStrictEqual('0x0dc...e7bc'); | ||
}); | ||
|
||
it('changes icon when clicked', () => { | ||
render(<AddressCopyButton address={SAMPLE_ADDRESS} />); | ||
fireEvent.click(document.querySelector('button')); | ||
expect(document.querySelector('.mm-icon').style.maskImage).toContain( | ||
'copy-success.svg', | ||
); | ||
}); | ||
}); |
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 { AddressCopyButton } from './address-copy-button'; |
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,7 @@ | ||
.multichain-address-copy-button { | ||
&__address--wrap { | ||
word-break: break-word; | ||
min-height: 32px; | ||
height: auto; | ||
} | ||
} |
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
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