Files
threshold-ui/ui/admin/app/components/copyable/index.js
T

68 lines
1.6 KiB
JavaScript
Raw Normal View History

/**
* Copyright (c) HashiCorp, Inc.
* SPDX-License-Identifier: MPL-2.0
*/
import Component from '@glimmer/component';
import ClipboardJS from 'clipboard';
import { action } from '@ember/object';
import { generateComponentID } from 'rose/utilities/component-auto-id';
import { tracked } from '@glimmer/tracking';
import { later } from '@ember/runloop';
2020-09-29 15:08:21 -04:00
export default class CopyableComponent extends Component {
// =attributes
id = generateComponentID();
copyableButtonId = `copyable-button-${this.id}`;
2021-09-16 14:03:26 -07:00
actionIcon = 'flight-icons/svg/clipboard-copy-16';
successIcon = 'flight-icons/svg/clipboard-checked-16';
actionText = this.args.buttonText;
successText = this.args.acknowledgeText;
@tracked copied = false;
2020-10-05 12:35:07 -04:00
clipboard = null;
confirmCopy() {
this.copied = true;
later(() => (this.copied = false), 1000);
}
/**
* Checks for ClipboardJS support.
* @return {boolean}
*/
get isClipboardLibrarySupported() {
2020-09-29 15:08:21 -04:00
return ClipboardJS.isSupported();
}
// =actions
/**
* Initialize ClipboardJS library and register for it's events.
*/
@action
register() {
2021-03-05 17:27:00 -05:00
this.tearDown();
2020-10-05 12:35:07 -04:00
/* istanbul ignore next */
this.clipboard = new ClipboardJS(`#${this.copyableButtonId}`, {
text: () => this.args.text,
});
/* istanbul ignore next */
this.clipboard.on('success', (clipboardEvent) => {
clipboardEvent.clearSelection();
this.confirmCopy();
2020-10-05 12:35:07 -04:00
});
}
/**
* Destroy ClipboardJS library instance and cancel icon timer.
*/
@action
2021-03-05 17:27:00 -05:00
tearDown() {
this.clipboard?.destroy();
2020-10-05 12:35:07 -04:00
this.clipboard = null;
}
}