Files
vue-chat-layer/app/components/Chat/ReactionBar.vue
Jakub Zych 269c75b5dd feat: threads, pins, reactions, attachments, link embeds and the command picker
- threads: reply counts, ChatThreadPane with replies, live replies and thread mute
- channel pins in ChatPinsPanel; a refused pin hides the action for the session
- reactions, attachments with upload progress and bearer downloads, inline edits, the delete dialog (moderators also in channels)
- link cards with the registerEmbedRenderer registry; linkify for escaped text
- slash commands: the command picker over chatbots, dispatch with sender-only answers
- slots conversation-actions (DMs only) and sidebar-footer
- chat-tokens.css and chat-base.css, EN and PL copy, README
2026-10-31 14:04:54 +01:00

206 lines
5.8 KiB
Vue

<script setup lang="ts">
// Reaction chips under a message and the add-reaction popover.
// The popover lists the keys the server allows (golem15.chat.reactions), so
// changing that config changes the picker without a rebuild. A chip toggles
// your own reaction; the store updates it at once and reconciles it with
// the server answer and the realtime events.
import type { ChatMessage } from '../../lib/types'
import { reactorsTitle } from '../../lib/chatFormat'
const props = defineProps<{ message: ChatMessage }>()
const emit = defineEmits<{ closed: [] }>()
const PER_ROW = 6
const { t } = useI18n()
const chat = useChat()
const uid = useId()
const popId = `chat-reactions-${uid}`
const root = ref<HTMLElement | null>(null)
const addBtn = ref<HTMLButtonElement | null>(null)
const pop = ref<HTMLElement | null>(null)
const open = ref(false)
const below = ref(false)
const active = ref(0)
/** Where focus goes when the popover closes (the action bar's button, or the add button). */
let returnTo: HTMLElement | null = null
const chips = computed(() =>
(props.message.reactions ?? [])
.filter((r) => r.count > 0)
.map((r) => {
const mine = chat.userId !== null && r.user_ids.includes(chat.userId)
return {
key: r.key,
count: r.count,
mine,
label: `${r.key} ${r.count}. ${mine ? t('chat.reactions.undo') : t('chat.reactions.join')}`,
title: reactorsTitle(r.user_ids, chat.members, (names, rest) => t('chat.reactions.more', { names, n: rest }), t('chat.someone')),
}
}),
)
const keys = computed(() => chat.allowedReactions ?? [])
const ready = computed(() => keys.value.length > 0)
function toggle(key: string) {
void chat.toggleReaction(props.message, key)
}
/** Above the add button, unless the list has no room there. */
function place() {
const el = root.value
const list = el?.closest('.chat-list, .chat-thread-list')
if (!el || !list) {
below.value = false
return
}
const rows = Math.ceil(keys.value.length / PER_ROW)
const height = rows * 36 + (rows - 1) * 4 + 16 + 2 + 8
below.value = el.getBoundingClientRect().top - list.getBoundingClientRect().top < height
}
function buttons(): HTMLButtonElement[] {
return pop.value ? [...pop.value.querySelectorAll<HTMLButtonElement>('button')] : []
}
function focusActive() {
void nextTick(() => buttons()[active.value]?.focus())
}
/** Opens the popover; focus returns to `from` (or the add button) when it closes. */
function openPicker(from?: HTMLElement | null) {
if (!ready.value) {
void chat.loadReactions()
return
}
returnTo = from ?? addBtn.value
active.value = 0
place()
open.value = true
document.addEventListener('pointerdown', onOutside, true)
focusActive()
}
function close(refocus = true) {
if (!open.value) return
open.value = false
document.removeEventListener('pointerdown', onOutside, true)
const target = returnTo
returnTo = null
if (refocus) target?.focus()
emit('closed')
}
function onOutside(e: PointerEvent) {
const t = e.target as Node | null
if (t && (pop.value?.contains(t) || addBtn.value?.contains(t))) return
const target = returnTo
close(false)
// Focus goes back only when the click did not move it somewhere else.
setTimeout(() => {
const a = document.activeElement
if (!a || a === document.body || pop.value?.contains(a)) target?.focus()
})
}
function pick(key: string) {
toggle(key)
close()
}
function onAdd() {
if (open.value) close()
else openPicker(addBtn.value)
}
function onKeydown(e: KeyboardEvent) {
const n = keys.value.length
if (!n) return
let next = active.value
switch (e.key) {
case 'Escape':
e.preventDefault()
e.stopPropagation()
close()
return
case 'ArrowRight':
next = (active.value + 1) % n
break
case 'ArrowLeft':
next = (active.value - 1 + n) % n
break
case 'ArrowDown':
next = active.value + PER_ROW < n ? active.value + PER_ROW : active.value % PER_ROW
break
case 'ArrowUp':
next = active.value - PER_ROW >= 0 ? active.value - PER_ROW : Math.min(n - 1, active.value % PER_ROW + Math.floor((n - 1) / PER_ROW) * PER_ROW)
break
case 'Home':
next = 0
break
case 'End':
next = n - 1
break
case 'Tab':
close(false)
return
default:
return
}
e.preventDefault()
active.value = next
focusActive()
}
onBeforeUnmount(() => document.removeEventListener('pointerdown', onOutside, true))
defineExpose({ openPicker })
</script>
<template>
<div ref="root" class="chat-reactions">
<button
v-for="c in chips"
:key="c.key"
class="chat-reaction-chip"
type="button"
:aria-pressed="c.mine ? 'true' : 'false'"
:aria-label="c.label"
:title="c.title"
@click="toggle(c.key)"
>
<span class="chat-reaction-emoji" aria-hidden="true">{{ c.key }}</span>
<span class="chat-reaction-count" aria-hidden="true">{{ c.count }}</span>
</button>
<span class="chat-reaction-add-wrap">
<button
ref="addBtn"
class="chat-reaction-add"
type="button"
:aria-label="t('chat.reactions.add')"
aria-haspopup="true"
:aria-expanded="open ? 'true' : 'false'"
:aria-controls="open ? popId : undefined"
:disabled="!ready"
@click="onAdd"
>
<ChatIcon name="smile" />
</button>
<div v-if="open" :id="popId" ref="pop" class="chat-reaction-pop" :class="{ 'is-below': below }" role="group" :aria-label="t('chat.reactions.add')" @keydown="onKeydown">
<button
v-for="(k, i) in keys"
:key="k"
class="chat-reaction-pick"
type="button"
:aria-label="k"
:tabindex="i === active ? 0 : -1"
@click="pick(k)"
@focus="active = i"
>
{{ k }}
</button>
</div>
</span>
</div>
</template>