feat(ui): show a tooltip with the full text on truncated titles
Add an `overflowTooltip` Svelte action that sets a native `title` only when an element is actually clipped (ellipsis or line-clamp), re-checking on resize. Native title is keyboard/touch reachable and zero-dep, unlike a hover-only popover. Applied to the clipped titles on MangaCard, BookmarkList, HistoryList, and the Continue-reading shelf. Unit tests cover the overflow decision and the set/clear/update behaviour; an e2e verifies a real-browser truncated card title gets the tooltip while a short one does not. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
60
frontend/src/lib/actions/overflowTooltip.test.ts
Normal file
60
frontend/src/lib/actions/overflowTooltip.test.ts
Normal file
@@ -0,0 +1,60 @@
|
||||
import { describe, it, expect, afterEach } from 'vitest';
|
||||
import { isOverflowing, overflowTooltip } from './overflowTooltip';
|
||||
|
||||
// jsdom doesn't lay out, so scrollWidth/clientWidth are always 0. Stub them
|
||||
// to model a clipped vs fitting element.
|
||||
function makeEl(dims: {
|
||||
scrollWidth?: number;
|
||||
clientWidth?: number;
|
||||
scrollHeight?: number;
|
||||
clientHeight?: number;
|
||||
}): HTMLElement {
|
||||
const el = document.createElement('span');
|
||||
for (const [k, v] of Object.entries(dims)) {
|
||||
Object.defineProperty(el, k, { configurable: true, value: v });
|
||||
}
|
||||
document.body.appendChild(el);
|
||||
return el;
|
||||
}
|
||||
|
||||
afterEach(() => {
|
||||
document.body.innerHTML = '';
|
||||
});
|
||||
|
||||
describe('isOverflowing', () => {
|
||||
it('is true when content is clipped horizontally', () => {
|
||||
expect(isOverflowing(makeEl({ scrollWidth: 200, clientWidth: 100 }))).toBe(true);
|
||||
});
|
||||
|
||||
it('is true when content is clipped vertically (line-clamp)', () => {
|
||||
expect(
|
||||
isOverflowing(makeEl({ scrollWidth: 100, clientWidth: 100, scrollHeight: 80, clientHeight: 40 }))
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('is false when the content fits', () => {
|
||||
expect(isOverflowing(makeEl({ scrollWidth: 100, clientWidth: 100 }))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('overflowTooltip action', () => {
|
||||
it('sets a title with the text when the element is truncated', () => {
|
||||
const el = makeEl({ scrollWidth: 200, clientWidth: 100 });
|
||||
overflowTooltip(el, 'A very long manga title');
|
||||
expect(el.getAttribute('title')).toBe('A very long manga title');
|
||||
});
|
||||
|
||||
it('does not set a title when the text fits', () => {
|
||||
const el = makeEl({ scrollWidth: 100, clientWidth: 100 });
|
||||
overflowTooltip(el, 'Short');
|
||||
expect(el.getAttribute('title')).toBeNull();
|
||||
});
|
||||
|
||||
it('updates the title text when the bound value changes', () => {
|
||||
const el = makeEl({ scrollWidth: 200, clientWidth: 100 });
|
||||
const action = overflowTooltip(el, 'First');
|
||||
expect(el.getAttribute('title')).toBe('First');
|
||||
action.update('Second');
|
||||
expect(el.getAttribute('title')).toBe('Second');
|
||||
});
|
||||
});
|
||||
45
frontend/src/lib/actions/overflowTooltip.ts
Normal file
45
frontend/src/lib/actions/overflowTooltip.ts
Normal file
@@ -0,0 +1,45 @@
|
||||
// Svelte action: show the full text in a native `title` tooltip only when
|
||||
// the element is actually truncated (ellipsis or line-clamp), and keep it in
|
||||
// sync as the element resizes or the text changes.
|
||||
//
|
||||
// Native `title` is deliberate: it's keyboard/touch reachable and zero-dep,
|
||||
// unlike a hover-only custom popover. Applied to clipped titles across list
|
||||
// surfaces (MangaCard, BookmarkList, HistoryList).
|
||||
|
||||
/** True when the element's content is clipped horizontally (ellipsis) or
|
||||
* vertically (line-clamp). A 1px slack absorbs sub-pixel rounding. */
|
||||
export function isOverflowing(node: HTMLElement): boolean {
|
||||
return (
|
||||
node.scrollWidth - node.clientWidth > 1 ||
|
||||
node.scrollHeight - node.clientHeight > 1
|
||||
);
|
||||
}
|
||||
|
||||
export function overflowTooltip(node: HTMLElement, text: string) {
|
||||
let current = text;
|
||||
|
||||
function sync() {
|
||||
if (isOverflowing(node)) node.setAttribute('title', current);
|
||||
else node.removeAttribute('title');
|
||||
}
|
||||
|
||||
// Re-check on size changes (viewport resize, font load, layout shifts).
|
||||
// Guarded for environments without ResizeObserver (SSR / jsdom).
|
||||
let ro: ResizeObserver | undefined;
|
||||
if (typeof ResizeObserver !== 'undefined') {
|
||||
ro = new ResizeObserver(() => sync());
|
||||
ro.observe(node);
|
||||
}
|
||||
|
||||
sync();
|
||||
|
||||
return {
|
||||
update(next: string) {
|
||||
current = next;
|
||||
sync();
|
||||
},
|
||||
destroy() {
|
||||
ro?.disconnect();
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { fileUrl } from '$lib/api/client';
|
||||
import type { Bookmark } from '$lib/api/bookmarks';
|
||||
import { overflowTooltip } from '$lib/actions/overflowTooltip';
|
||||
import BookImage from '@lucide/svelte/icons/book-image';
|
||||
|
||||
let {
|
||||
@@ -34,6 +35,7 @@
|
||||
href="/manga/{b.manga_id}"
|
||||
class="title"
|
||||
data-testid="bookmark-title"
|
||||
use:overflowTooltip={b.manga_title ?? 'Unknown manga'}
|
||||
>
|
||||
{b.manga_title ?? 'Unknown manga'}
|
||||
</a>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { fileUrl } from '$lib/api/client';
|
||||
import { chapterLabel } from '$lib/api/chapters';
|
||||
import { overflowTooltip } from '$lib/actions/overflowTooltip';
|
||||
import type { ReadProgressSummary } from '$lib/api/read_progress';
|
||||
import BookImage from '@lucide/svelte/icons/book-image';
|
||||
|
||||
@@ -66,7 +67,7 @@
|
||||
</span>
|
||||
{/if}
|
||||
</span>
|
||||
<span class="title" data-testid="continue-title-{p.manga_id}">{p.manga_title}</span>
|
||||
<span class="title" data-testid="continue-title-{p.manga_id}" use:overflowTooltip={p.manga_title}>{p.manga_title}</span>
|
||||
<span class="target">{targetLabel(p)}</span>
|
||||
</a>
|
||||
</li>
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
<script lang="ts">
|
||||
import { fileUrl } from '$lib/api/client';
|
||||
import { chapterLabel } from '$lib/api/chapters';
|
||||
import { overflowTooltip } from '$lib/actions/overflowTooltip';
|
||||
import type { ReadProgressSummary } from '$lib/api/read_progress';
|
||||
import IconButton from '$lib/components/IconButton.svelte';
|
||||
import BookImage from '@lucide/svelte/icons/book-image';
|
||||
@@ -96,7 +97,12 @@
|
||||
{/if}
|
||||
</a>
|
||||
<div class="meta">
|
||||
<a href="/manga/{p.manga_id}" class="title" data-testid="{testid}-title">
|
||||
<a
|
||||
href="/manga/{p.manga_id}"
|
||||
class="title"
|
||||
data-testid="{testid}-title"
|
||||
use:overflowTooltip={p.manga_title}
|
||||
>
|
||||
{p.manga_title}
|
||||
</a>
|
||||
<span class="target">
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
import { fileUrl } from '$lib/api/client';
|
||||
import type { Manga } from '$lib/api/client';
|
||||
import type { AuthorRef, GenreRef } from '$lib/api/mangas';
|
||||
import { overflowTooltip } from '$lib/actions/overflowTooltip';
|
||||
import BookImage from '@lucide/svelte/icons/book-image';
|
||||
|
||||
let {
|
||||
@@ -77,7 +78,7 @@
|
||||
{/if}
|
||||
</a>
|
||||
<div class="meta">
|
||||
<a href="/manga/{manga.id}" class="title">{manga.title}</a>
|
||||
<a href="/manga/{manga.id}" class="title" use:overflowTooltip={manga.title}>{manga.title}</a>
|
||||
{#if authors.length > 0}
|
||||
<span class="author">{authors.map((a) => a.name).join(', ')}</span>
|
||||
{/if}
|
||||
|
||||
Reference in New Issue
Block a user