pinafore/src/routes/_components/dialog/components/CopyDialog.html

75 lines
1.8 KiB
HTML

<ModalDialog
{id}
{label}
{title}
background="var(--main-bg)"
>
<form class="copy-dialog-form">
<input value={text}
ref:input
>
<button type="button" on:click="onClick()">
Copy
</button>
</form>
</ModalDialog>
<style>
.copy-dialog-form {
display: grid;
grid-template-rows: min-content min-content;
grid-template-columns: 1fr;
grid-gap: 10px;
padding: 10px 20px;
width: 400px;
max-width: calc(100vw - 40px);
}
</style>
<script>
import ModalDialog from './ModalDialog.html'
import { show } from '../helpers/showDialog'
import { close } from '../helpers/closeDialog'
import { oncreate as onCreateDialog } from '../helpers/onCreateDialog'
import { toast } from '../../toast/toast'
import { doubleRAF } from '../../../_utils/doubleRAF'
function copyFromInput (input) {
// workarounds for iOS, via https://stackoverflow.com/a/34046084
let range = document.createRange()
range.selectNodeContents(input)
let selection = window.getSelection()
selection.removeAllRanges()
selection.addRange(range)
input.setSelectionRange(0, 9999999)
document.execCommand('copy')
}
export default {
oncreate () {
onCreateDialog.call(this)
let { text } = this.get()
let { input } = this.refs
// double raf is to work around a11y-dialog trying to set the input
doubleRAF(() => {
input.focus()
input.setSelectionRange(0, text.length)
})
},
methods: {
show,
close,
onClick () {
let { input } = this.refs
copyFromInput(input)
toast.say('Copied to clipboard')
this.close()
}
},
data: () => ({
text: ''
}),
components: {
ModalDialog
}
}
</script>