Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/frontend/src/components/WhyTabs.vue
blob: 3e0a04d8592d0dc66dbc924fb3a349b93a6878d0 (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
<!-- SPDX-FileCopyrightText: 2026 Progmasoft <support@progmasoft.com> -->
<!-- SPDX-License-Identifier: AGPL-3.0-or-later WITH AdditionRef-Progmasoft-Patent-Grant-1.1 -->

<script setup lang="ts">
import { computed, ref, watch } from "vue";
import type { WhyTab } from "../content/landing";

/**
 * A row of tabs over one code panel. Each tab shows one strength with a short caption and a sample that the
 * compiler repository checks. The arrow keys, Home and End move between tabs, as the ARIA tabs pattern asks.
 */
const props = defineProps<{
  tabs: readonly WhyTab[];
  copyLabel: string;
  copiedLabel: string;
  id: string;
}>();

const selected = ref(0);
const copied = ref(false);
const current = computed(() => props.tabs[selected.value] ?? props.tabs[0]);
const buttons = ref<HTMLButtonElement[]>([]);

watch(
  () => props.tabs,
  () => {
    selected.value = 0;
  },
);

function select(index: number, focus = false) {
  const count = props.tabs.length;
  selected.value = (index + count) % count;
  copied.value = false;
  if (focus) buttons.value[selected.value]?.focus();
}

function onKey(event: KeyboardEvent) {
  const moves: Record<string, () => number> = {
    ArrowRight: () => selected.value + 1,
    ArrowLeft: () => selected.value - 1,
    Home: () => 0,
    End: () => props.tabs.length - 1,
  };
  const move = moves[event.key];
  if (!move) return;
  event.preventDefault();
  select(move(), true);
}

async function copy() {
  try {
    await navigator.clipboard.writeText(current.value.code);
    copied.value = true;
  } catch {
    copied.value = false;
  }
}
</script>

<template>
  <div class="why-tabs">
    <div class="why-tablist" role="tablist" @keydown="onKey">
      <button
        v-for="(tab, index) in tabs"
        :id="`${id}-tab-${index}`"
        :key="tab.label"
        ref="buttons"
        role="tab"
        type="button"
        :aria-selected="index === selected"
        :aria-controls="`${id}-panel`"
        :tabindex="index === selected ? 0 : -1"
        @click="select(index)"
      >
        {{ tab.label }}
      </button>
    </div>
    <div
      :id="`${id}-panel`"
      class="why-panel"
      role="tabpanel"
      :aria-labelledby="`${id}-tab-${selected}`"
    >
      <p class="why-caption">{{ current.caption }}</p>
      <div class="why-code" dir="ltr">
        <div class="why-code-bar">
          <span>{{ current.file }}</span>
          <button type="button" class="why-copy" @click="copy">
            {{ copied ? copiedLabel : copyLabel }}
          </button>
        </div>
        <pre><code>{{ current.code }}</code></pre>
      </div>
    </div>
  </div>
</template>