From 8bea9eeddfa6e2e602f32c7130e39a8eb5f191ae Mon Sep 17 00:00:00 2001 From: Andrew Fontaine Date: Thu, 6 Jun 2019 09:36:17 +0000 Subject: [PATCH] Add a New Copy Button That Works in Modals This copy button manages a local instance of the Clipboard plugin specific to it, which means it is created/destroyed on the creation/destruction of the component. This allows it to work well in gitlab-ui modals, as the event listeners are bound on creation of the button. It also allows for bindings to the `container` option of the Clipboard plugin, which allows it to work within the focus trap set by bootstrap's modals. --- .../components/modal_copy_button.vue | 121 ++++++++++++++++++ .../unreleased/copy-button-in-modals.yml | 5 + doc/development/fe_guide/frontend_faq.md | 14 ++ .../components/modal_copy_button_spec.js | 40 ++++++ 4 files changed, 180 insertions(+) create mode 100644 app/assets/javascripts/vue_shared/components/modal_copy_button.vue create mode 100644 changelogs/unreleased/copy-button-in-modals.yml create mode 100644 spec/frontend/vue_shared/components/modal_copy_button_spec.js diff --git a/app/assets/javascripts/vue_shared/components/modal_copy_button.vue b/app/assets/javascripts/vue_shared/components/modal_copy_button.vue new file mode 100644 index 00000000000..bf59a6abf3f --- /dev/null +++ b/app/assets/javascripts/vue_shared/components/modal_copy_button.vue @@ -0,0 +1,121 @@ + + diff --git a/changelogs/unreleased/copy-button-in-modals.yml b/changelogs/unreleased/copy-button-in-modals.yml new file mode 100644 index 00000000000..bc18eb9ab26 --- /dev/null +++ b/changelogs/unreleased/copy-button-in-modals.yml @@ -0,0 +1,5 @@ +--- +title: Add a New Copy Button That Works in Modals +merge_request: 28676 +author: +type: added diff --git a/doc/development/fe_guide/frontend_faq.md b/doc/development/fe_guide/frontend_faq.md index 77f064a21a9..e4225f2bc39 100644 --- a/doc/development/fe_guide/frontend_faq.md +++ b/doc/development/fe_guide/frontend_faq.md @@ -25,3 +25,17 @@ document.body.dataset.page ``` Find here the [source code setting the attribute](https://gitlab.com/gitlab-org/gitlab-ce/blob/cc5095edfce2b4d4083a4fb1cdc7c0a1898b9921/app/views/layouts/application.html.haml#L4). + +### `modal_copy_button` vs `clipboard_button` + +The `clipboard_button` uses the `copy_to_clipboard.js` behaviour, which is +initialized on page load, so if there are vue-based clipboard buttons that +don't exist at page load (such as ones in a `GlModal`), they do not have the +click handlers associated with the clipboard package. + +`modal_copy_button` was added that manages an instance of the +[`clipboard` plugin](https://www.npmjs.com/package/clipboard) specific to +the instance of that component, which means that clipboard events are +bound on mounting and destroyed when the button is, mitigating the above +issue. It also has bindings to a particular container or modal ID +available, to work with the focus trap created by our GlModal. diff --git a/spec/frontend/vue_shared/components/modal_copy_button_spec.js b/spec/frontend/vue_shared/components/modal_copy_button_spec.js new file mode 100644 index 00000000000..f1943861523 --- /dev/null +++ b/spec/frontend/vue_shared/components/modal_copy_button_spec.js @@ -0,0 +1,40 @@ +import Vue from 'vue'; +import { shallowMount } from '@vue/test-utils'; +import modalCopyButton from '~/vue_shared/components/modal_copy_button.vue'; + +describe('modal copy button', () => { + const Component = Vue.extend(modalCopyButton); + let wrapper; + + afterEach(() => { + wrapper.destroy(); + }); + + beforeEach(() => { + wrapper = shallowMount(Component, { + propsData: { + text: 'copy me', + title: 'Copy this value into Clipboard!', + }, + }); + }); + + describe('clipboard', () => { + it('should fire a `success` event on click', () => { + document.execCommand = jest.fn(() => true); + window.getSelection = jest.fn(() => ({ + toString: jest.fn(() => 'test'), + removeAllRanges: jest.fn(), + })); + wrapper.trigger('click'); + expect(wrapper.emitted().success).not.toBeEmpty(); + expect(document.execCommand).toHaveBeenCalledWith('copy'); + }); + it("should propagate the clipboard error event if execCommand doesn't work", () => { + document.execCommand = jest.fn(() => false); + wrapper.trigger('click'); + expect(wrapper.emitted().error).not.toBeEmpty(); + expect(document.execCommand).toHaveBeenCalledWith('copy'); + }); + }); +}); -- GitLab