nifty-wallet/ui/app/components/copyButton.js

62 lines
1.4 KiB
JavaScript
Raw Normal View History

2016-06-29 14:11:12 -07:00
const Component = require('react').Component
const h = require('react-hyperscript')
const inherits = require('util').inherits
const copyToClipboard = require('copy-to-clipboard')
const Tooltip = require('./tooltip')
2016-06-29 15:57:59 -07:00
2016-06-29 14:11:12 -07:00
module.exports = CopyButton
inherits(CopyButton, Component)
function CopyButton () {
Component.call(this)
}
2016-06-29 15:57:59 -07: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 14:11:12 -07:00
CopyButton.prototype.render = function () {
const props = this.props
2016-06-29 15:57:59 -07:00
const state = this.state || {}
2016-06-29 14:11:12 -07:00
const value = props.value
2016-06-29 15:57:59 -07:00
const copied = state.copied
const message = copied ? 'Copied' : props.title || ' Copy '
2016-06-29 14:11:12 -07:00
2016-06-29 15:57:59 -07:00
return h('.copy-button', {
2016-06-29 14:39:25 -07:00
style: {
display: 'flex',
alignItems: 'center',
},
2016-06-29 15:57:59 -07: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()
},
}),
]),
])
}
CopyButton.prototype.debounceRestore = function() {
this.setState({ copied: true })
clearTimeout(this.timeout)
this.timeout = setTimeout(() => {
this.setState({ copied: false })
}, 850)
2016-06-29 14:11:12 -07:00
}