fix: 🐛 allow copyable to work within a modal

This commit is contained in:
Randall Morey committed 2021-02-08 12:50:42 -05:00
1 parent bbe511549b
commit d68cc83adf
2 files changed
+34 -6

No files matched your search

+21 -1
View File
@@ -1,9 +1,10 @@
import Component from '@glimmer/component';
import ClipboardJS from 'clipboard';
import { action } from '@ember/object';
import { getOwner } from '@ember/application';
import { generateComponentID } from 'rose/utilities/component-auto-id';
import { task, timeout } from 'ember-concurrency';
import { tracked } from '@glimmer/tracking';
import ClipboardJS from 'clipboard';
export default class CopyableComponent extends Component {
@@ -46,6 +47,18 @@ export default class CopyableComponent extends Component {
return ClipboardJS.isSupported();
}
// =methods
/**
* Returns the first element with the specified class name if exists,
* otherwise undefined.
* @return {?HTMLElement}
*/
getFirstElementByClassName(className) {
const document = getOwner(this).lookup('service:-document').documentElement;
return document.getElementsByClassName(className)[0];
}
// =actions
/**
@@ -57,6 +70,13 @@ export default class CopyableComponent extends Component {
/* istanbul ignore next */
this.clipboard = new ClipboardJS(`#${this.copyableButtonId}`, {
text: () => this.args.text,
// ClipboardJS depends on inserting a hidden textarea that receives
// focus in order to copy text to the clipboard. However, our modal
// has a focus trap that prevents this focus. Thus we try to set the
// container to a modal (if exists), since this is the only container
// a user would be able to interact with. If a modal doesn't exist,
// container is set to `undefined` and thus uses the default body element.
container: this.getFirstElementByClassName('rose-dialog')
});
/* istanbul ignore next */
this.clipboard.on('success', (clipboardEvent) => {
+13 -5
View File
@@ -57,11 +57,19 @@
@dismiss={{accept}}
as |dialog|>
<dialog.body>
{{t 'states.connection.success.description'
port=confirmation.text.port
address=confirmation.text.address
protocol=confirmation.text.protocol
}}
<Copyable
@text={{concat confirmation.text.address ":" confirmation.text.port}}
@buttonText={{t "actions.copy-to-clipboard"}}
@acknowledgeText={{t "states.copied"}}
>
{{t 'states.connection.success.description'
port=confirmation.text.port
address=confirmation.text.address
protocol=confirmation.text.protocol
}}
</Copyable>
</dialog.body>
<dialog.footer>
<Rose::Button @style="primary" {{on "click" accept}}>