summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/behaviors/shortcuts/shortcuts_toggle.vue
blob: a53b1b06be94175f718c7a02442898be775eb206 (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
<script>
import { GlToggle, GlSprintf } from '@gitlab/ui';
import AccessorUtilities from '~/lib/utils/accessor';
import { disableShortcuts, enableShortcuts, shouldDisableShortcuts } from './shortcuts_toggle';

export default {
  components: {
    GlSprintf,
    GlToggle,
  },
  data() {
    return {
      localStorageUsable: AccessorUtilities.isLocalStorageAccessSafe(),
      shortcutsEnabled: !shouldDisableShortcuts(),
    };
  },
  methods: {
    onChange(value) {
      this.shortcutsEnabled = value;
      if (value) {
        enableShortcuts();
      } else {
        disableShortcuts();
      }
    },
  },
};
</script>

<template>
  <div v-if="localStorageUsable" class="d-inline-flex align-items-center js-toggle-shortcuts">
    <gl-toggle
      v-model="shortcutsEnabled"
      aria-describedby="shortcutsToggle"
      class="prepend-left-10 mb-0"
      label-position="right"
      @change="onChange"
    >
      <template #labelOn>
        <gl-sprintf
          :message="__('%{screenreaderOnlyStart}Keyboard shorcuts%{screenreaderOnlyEnd} Enabled')"
        >
          <template #screenreaderOnly="{ content }">
            <span class="sr-only">{{ content }}</span>
          </template>
        </gl-sprintf>
      </template>
      <template #labelOff>
        <gl-sprintf
          :message="__('%{screenreaderOnlyStart}Keyboard shorcuts%{screenreaderOnlyEnd} Disabled')"
        >
          <template #screenreaderOnly="{ content }">
            <span class="sr-only">{{ content }}</span>
          </template>
        </gl-sprintf>
      </template>
    </gl-toggle>
    <div id="shortcutsToggle" class="sr-only">{{ __('Enable or disable keyboard shortcuts') }}</div>
  </div>
</template>