1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-18 15:33:21 +01:00
metamask-extension/ui/app/components/copyButton.js

67 lines
1.5 KiB
JavaScript
Raw Normal View History

2016-06-29 23:11:12 +02:00
const Component = require('react').Component
const PropTypes = require('prop-types')
2016-06-29 23:11:12 +02:00
const h = require('react-hyperscript')
const inherits = require('util').inherits
const copyToClipboard = require('copy-to-clipboard')
const connect = require('react-redux').connect
2016-06-29 23:11:12 +02:00
const Tooltip = require('./tooltip')
2016-06-30 00:57:59 +02:00
CopyButton.contextTypes = {
t: PropTypes.func,
}
module.exports = connect()(CopyButton)
2016-06-29 23:11:12 +02:00
2016-06-29 23:11:12 +02:00
inherits(CopyButton, Component)
function CopyButton () {
Component.call(this)
}
2016-06-30 00:57:59 +02:00
// As parameters, accepts:
// "value", which is the value to copy (mandatory)
// "title", which is the text to show on hover (optional, defaults to 'Copy')
2016-06-29 23:11:12 +02:00
CopyButton.prototype.render = function () {
const props = this.props
2016-06-30 00:57:59 +02:00
const state = this.state || {}
2016-06-29 23:11:12 +02:00
const value = props.value
2016-06-30 00:57:59 +02:00
const copied = state.copied
const message = copied ? this.context.t('copiedButton') : props.title || this.context.t('copyButton')
2016-06-29 23:11:12 +02:00
2016-06-30 00:57:59 +02:00
return h('.copy-button', {
2016-06-29 23:39:25 +02:00
style: {
display: 'flex',
alignItems: 'center',
},
2016-06-30 00:57:59 +02:00
}, [
h(Tooltip, {
title: message,
}, [
h('i.fa.fa-clipboard.cursor-pointer.color-orange', {
style: {
margin: '5px',
},
onClick: (event) => {
event.preventDefault()
event.stopPropagation()
copyToClipboard(value)
this.debounceRestore()
},
}),
]),
])
}
2016-11-11 19:26:12 +01:00
CopyButton.prototype.debounceRestore = function () {
2016-06-30 00:57:59 +02:00
this.setState({ copied: true })
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.setState({ copied: false })
}, 850)
2016-06-29 23:11:12 +02:00
}