summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/vue_shared/components/modal_copy_button.vue
blob: 41c92fdba4f3049893a1736b8ad41ce0412c9102 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
<script>
import { GlButton, GlTooltipDirective } from '@gitlab/ui';
import ClipboardJS from 'clipboard';
import { uniqueId } from 'lodash';
import { BV_HIDE_TOOLTIP } from '~/lib/utils/constants';

export default {
  components: {
    GlButton,
  },
  directives: {
    GlTooltip: GlTooltipDirective,
  },
  props: {
    text: {
      type: String,
      required: false,
      default: '',
    },
    id: {
      type: String,
      required: false,
      default: () => uniqueId('modal-copy-button-'),
    },
    container: {
      type: String,
      required: false,
      default: '',
    },
    cssClasses: {
      type: String,
      required: false,
      default: '',
    },
    modalId: {
      type: String,
      required: false,
      default: '',
    },
    target: {
      type: String,
      required: false,
      default: '',
    },
    title: {
      type: String,
      required: true,
    },
    tooltipPlacement: {
      type: String,
      required: false,
      default: 'top',
    },
    tooltipContainer: {
      type: String,
      required: false,
      default: null,
    },
    category: {
      type: String,
      required: false,
      default: 'primary',
    },
    size: {
      type: String,
      required: false,
      default: 'medium',
    },
  },
  computed: {
    modalDomId() {
      return this.modalId ? `#${this.modalId}` : '';
    },
  },
  mounted() {
    this.$nextTick(() => {
      this.clipboard = new ClipboardJS(this.$el, {
        container:
          document.querySelector(`${this.modalDomId} div.modal-content`) ||
          document.getElementById(this.container) ||
          document.body,
      });
      this.clipboard
        .on('success', (e) => {
          this.$root.$emit(BV_HIDE_TOOLTIP, this.id);
          this.$emit('success', e);
          // Clear the selection and blur the trigger so it loses its border
          e.clearSelection();
          e.trigger.blur();
        })
        .on('error', (e) => this.$emit('error', e));
    });
  },
  destroyed() {
    if (this.clipboard) {
      this.clipboard.destroy();
    }
  },
};
</script>
<template>
  <gl-button
    :id="id"
    v-gl-tooltip="{ placement: tooltipPlacement, container: tooltipContainer }"
    :class="cssClasses"
    :data-clipboard-target="target"
    :data-clipboard-text="text"
    :title="title"
    :aria-label="title"
    :category="category"
    :size="size"
    icon="copy-to-clipboard"
  >
    <slot></slot>
  </gl-button>
</template>