-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathParticipationChip.vue
More file actions
190 lines (176 loc) · 5.37 KB
/
ParticipationChip.vue
File metadata and controls
190 lines (176 loc) · 5.37 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
<template>
<div
ref="toolbar"
class="flex flex-wrap gap-3 my-4 items-center"
role="toolbar"
aria-label="Participation status filters"
@keydown="onToolbarKeydown"
>
<!-- All chip -->
<button
:ref="(el) => setButtonRef(el, 0)"
type="button"
:aria-pressed="selected === null"
title="Show all participations"
:tabindex="focusedIndex === 0 ? 0 : -1"
:class="[
'inline-flex items-center gap-2 px-4 py-1 rounded-full text-sm transition duration-150 ease-out cursor-pointer select-none transform-gpu',
selected === null
? 'bg-black/5 text-slate-900 font-semibold ring-2 ring-offset-1 ring-slate-300'
: 'bg-white text-slate-700 hover:bg-slate-50',
// make focus with keyboard very visible
'focus-visible:scale-105 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-offset-2 focus-visible:ring-slate-400',
]"
@click="setSelected(null)"
@focus="focusedIndex = 0"
@keydown="onButtonKeydown"
>
<Check
v-if="selected === null"
class="w-4 h-4 text-slate-900 stroke-current"
aria-hidden="true"
/>
<span>All</span>
</button>
<!-- Status chips -->
<button
v-for="(status, idx) in statusesOrdered"
:key="status"
:ref="(el) => setButtonRef(el, idx + 1)"
type="button"
:aria-pressed="selected === status"
:title="humanReadableParticipationStatus[status]"
:tabindex="focusedIndex === idx + 1 ? 0 : -1"
:class="[
'inline-flex items-center gap-3 px-4 py-1 rounded-full text-sm transition-colors duration-150 ease-out cursor-pointer select-none transform-gpu',
chipClass(status),
selected === status ? 'font-semibold shadow' : 'font-medium',
// selected gets a persistent ring; otherwise show a visible focus ring for keyboard users
selected === status
? ['ring-2', 'ring-offset-1', participationStatusColor[status].ring]
: [
'focus-visible:outline-none',
'focus-visible:scale-105',
'focus-visible:ring-2',
'focus-visible:ring-offset-2',
participationStatusColor[status].ring,
],
]"
@click="setSelected(status)"
@focus="focusedIndex = idx + 1"
@keydown="onButtonKeydown"
>
<!-- show an icon only when this status is selected to avoid extra left space -->
<Check
v-if="selected === status"
:class="[
'w-4 h-4 stroke-current',
status === 'GIVEN_UP' ? 'text-slate-900' : 'text-white',
]"
aria-hidden="true"
/>
<span class="whitespace-nowrap">{{
humanReadableParticipationStatus[status]
}}</span>
</button>
</div>
</template>
<script setup lang="ts">
import { computed, ref, nextTick, type ComponentPublicInstance } from "vue";
import { Check } from "lucide-vue-next";
import type { ParticipationStatus } from "@/dto";
import {
humanReadableParticipationStatus,
participationStatusColor,
} from "@/dto";
const props = defineProps<{
selected?: ParticipationStatus | null;
}>();
const emit = defineEmits<{
(e: "update:selected", value: ParticipationStatus | null): void;
}>();
const statusesOrdered: ParticipationStatus[] = [
"ANNOUNCED",
"ACCEPTED",
"CONTACTED",
"GIVEN_UP",
"IN_CONVERSATIONS",
"ON_HOLD",
"REJECTED",
"SELECTED",
"SUGGESTED",
];
const selected = computed<ParticipationStatus | null>({
get: () => props.selected ?? null,
set: (val) => emit("update:selected", val),
});
const chipClass = (status: ParticipationStatus) => {
const color = participationStatusColor[status];
return `${color.background} border border-transparent`;
};
function setSelected(val: ParticipationStatus | null) {
selected.value = val;
}
// Keyboard navigation support
const focusedIndex = ref(0);
const chipButtons = ref<(HTMLElement | undefined)[]>([]);
const toolbar = ref<HTMLElement | null>(null);
function focusButton(idx: number) {
nextTick(() => {
const buttons = chipButtons.value || [];
const button = buttons[idx];
if (button) {
focusedIndex.value = idx;
button.focus();
}
});
}
function handleNavigationKeydown(e: KeyboardEvent) {
const key = e.key;
if (
key === "ArrowRight" ||
key === "ArrowDown" ||
key === "Right" ||
key === "Down" ||
key === "39"
) {
e.preventDefault();
focusedIndex.value =
(focusedIndex.value + 1) % (statusesOrdered.length + 1);
focusButton(focusedIndex.value);
return true;
} else if (
key === "ArrowLeft" ||
key === "ArrowUp" ||
key === "Left" ||
key === "Up" ||
key === "37"
) {
e.preventDefault();
focusedIndex.value =
(focusedIndex.value - 1 + statusesOrdered.length + 1) %
(statusesOrdered.length + 1);
focusButton(focusedIndex.value);
return true;
}
return false;
}
function onToolbarKeydown(e: KeyboardEvent) {
if (e.target && (e.target as HTMLElement).tagName === "BUTTON") return;
handleNavigationKeydown(e);
}
function onButtonKeydown(e: KeyboardEvent) {
handleNavigationKeydown(e);
}
function setButtonRef(
el: Element | ComponentPublicInstance | null,
idx: number,
) {
if (!chipButtons.value) chipButtons.value = [];
if (el) {
chipButtons.value[idx] = (el as HTMLElement) ?? undefined;
} else {
chipButtons.value[idx] = undefined;
}
}
</script>