Skip to content

armgit / TREK

Public Imported from GitHub
0

The most complete open-source travel planner: real-time collaboration, bookings, budgets, packing lists, journals, offline PWA, SSO and an AI-ready MCP server, with planet-wide place search and routing built in.

fix(collections): fit a row's labels without setting state in an effect, and bring them back when the row grows

Mauricefbafa270c401
diff --git a/client/src/components/Collections/CollectionPlaceTags.tsx b/client/src/components/Collections/CollectionPlaceTags.tsx
index 983bcc16f..afaecc134 100644
--- a/client/src/components/Collections/CollectionPlaceTags.tsx
+++ b/client/src/components/Collections/CollectionPlaceTags.tsx
@@ -1,11 +1,11 @@
import type { CollectionLabel, CollectionPlace } from '@trek/shared';
-import React, { useEffect, useLayoutEffect, useRef, useState } from 'react';
+import React, { createElement, useEffect, useRef, useState } from 'react';
import { getCategoryIcon } from '../shared/categoryIcons';
import { Tooltip } from '../shared/Tooltip';
/**
* One observer for every row and chip of a list, so a list of hundreds of places does
- * not hold one per element.
+ * not hold one per element. An element starts with one notification once observed.
*/
const sizeWatch = new Map<Element, () => void>();
let sizeObserver: ResizeObserver | null = null;
@@ -51,23 +51,34 @@ function ChipTooltip({
const MORE_CHIP = 34;
/**
- * How many labels fit beside the category in `row`, measured with every label drawn at
- * its own width: as many as the row holds with room left for the "+N" chip, and at
- * least one, which then shortens its name.
+ * A chip's own width, also while the row squeezes it: the part its name is cut short by
+ * is the name's scroll width over its box, up to the widest the chip may grow.
+ */
+const naturalWidth = (el: HTMLElement): number => {
+ const text = el.querySelector<HTMLElement>('.col-lrow-txt');
+ const full = el.offsetWidth + (text ? text.scrollWidth - text.clientWidth : 0);
+ const cap = parseFloat(getComputedStyle(el).maxWidth);
+ return Number.isFinite(cap) ? Math.min(full, cap) : full;
+};
+
+/**
+ * How many labels fit beside the category in `row`: as many as it holds with room left
+ * for the "+N" chip, and at least one, which then shortens its name. Every label is in
+ * the row, the ones that do not fit kept out of sight, so each can be measured.
*/
function labelsThatFit(row: HTMLElement): number {
const gap = parseFloat(getComputedStyle(row).columnGap) || 0;
const labels = Array.from(row.querySelectorAll<HTMLElement>('.col-lrow-label'));
const fixed = Array.from(row.querySelectorAll<HTMLElement>('.col-lrow-cat, .col-lrow-div'))
.filter((el) => el.offsetWidth > 0)
- .reduce((sum, el) => sum + el.offsetWidth + gap, 0);
+ .reduce((sum, el) => sum + naturalWidth(el) + gap, 0);
let used = fixed;
let fit = 0;
for (let i = 0; i < labels.length; i++) {
const left = labels.length - i - 1;
- const need = used + labels[i].offsetWidth + (left > 0 ? gap + MORE_CHIP : 0);
+ const need = used + naturalWidth(labels[i]) + (left > 0 ? gap + MORE_CHIP : 0);
if (need > row.clientWidth && i > 0) break;
- used += labels[i].offsetWidth + gap;
+ used += naturalWidth(labels[i]) + gap;
fit = i + 1;
}
return fit;
@@ -87,49 +98,38 @@ export default function CollectionPlaceTags({
}): React.ReactElement | null {
const ref = useRef<HTMLDivElement>(null);
const [fit, setFit] = useState(labels.length);
- const [measuring, setMeasuring] = useState(true);
const labelKey = labels.map((l) => `${l.id}:${l.name}`).join('|');
- const category = place.category?.name ? place.category : null;
-
- useLayoutEffect(() => {
- setMeasuring(true);
- }, [labelKey, category?.name]);
-
- useLayoutEffect(() => {
- const row = ref.current;
- if (!measuring || !row) return;
- setFit(labelsThatFit(row));
- setMeasuring(false);
- }, [measuring]);
+ const categoryName = place.category?.name ?? null;
+ // Fitted on every change of the row's width and once more whenever its labels change:
+ // observing anew hands the observer's first notification to the new labels.
useEffect(() => {
const row = ref.current;
if (!row) return undefined;
- let width = row.clientWidth;
- return watchSize(row, () => {
- if (row.clientWidth === width) return;
- width = row.clientWidth;
- setMeasuring(true);
- });
- }, []);
+ return watchSize(row, () => setFit(labelsThatFit(row)));
+ }, [labelKey, categoryName]);
- if (!labels.length && !category) return null;
- const shown = measuring ? labels : labels.slice(0, Math.max(1, fit));
- const hidden = labels.slice(shown.length);
- const CatIcon = category ? getCategoryIcon(category.icon ?? undefined) : null;
+ if (!labels.length && !categoryName) return null;
+ const shown = Math.min(labels.length, Math.max(1, fit));
+ const hidden = labels.slice(shown);
return (
- <div ref={ref} className={`col-lrow-tags${measuring ? 'measuring' : ''}`}>
- {category && CatIcon && (
- <ChipTooltip label={category.name}>
- <span className="col-lrow-cat" style={{ ['--cat' as string]: category.color || '#6366f1' }}>
- <CatIcon size={11} /> <span className="col-lrow-txt">{category.name}</span>
+ <div ref={ref} className="col-lrow-tags">
+ {categoryName && (
+ <ChipTooltip label={categoryName}>
+ <span className="col-lrow-cat" style={{ ['--cat' as string]: place.category?.color || '#6366f1' }}>
+ {createElement(getCategoryIcon(place.category?.icon ?? undefined), { size: 11 })}{' '}
+ <span className="col-lrow-txt">{categoryName}</span>
</span>
</ChipTooltip>
)}
- {shown.map((l) => (
+ {labels.map((l, i) => (
<ChipTooltip key={l.id} label={l.name}>
- <span className="col-lrow-label" style={{ ['--label' as string]: l.color || 'var(--accent)' }}>
+ <span
+ className={`col-lrow-label${i < shown ? '' : ' is-over'}`}
+ style={{ ['--label' as string]: l.color || 'var(--accent)' }}
+ aria-hidden={i < shown ? undefined : true}
+ >
<span className="col-labelchip-dot" /> <span className="col-lrow-txt">{l.name}</span>
</span>
</ChipTooltip>
diff --git a/client/src/styles/collections.css b/client/src/styles/collections.css
index bfbea59a3..b5a6a0d67 100644
--- a/client/src/styles/collections.css
+++ b/client/src/styles/collections.css
@@ -289,16 +289,16 @@
name and status on top, labels and category below, still on one line. */
.trek-dash .col-listview { container-type: inline-size; }
.trek-dash .col-lrow-place {
- grid-template-columns: 40px minmax(12rem, 1fr) minmax(0, max-content) auto;
+ grid-template-columns: 40px minmax(12rem, max-content) minmax(0, 1fr) auto;
grid-template-areas: "tile main tags status";
}
.trek-dash .col-lrow-place > :first-child { grid-area: tile; }
.trek-dash .col-lrow-place > .li { grid-area: main; }
-.trek-dash .col-lrow-tags { grid-area: tags; display: flex; align-items: center; gap: 8px; min-width: 0; overflow: hidden; }
+.trek-dash .col-lrow-tags { grid-area: tags; position: relative; display: flex; justify-content: flex-end; align-items: center; gap: 8px; min-width: 0; overflow: hidden; }
+/* A label the row has no room for stays in it, out of sight, so it can be measured. */
+.trek-dash .col-lrow-tags > .is-over { position: absolute; inset-inline-start: 0; visibility: hidden; pointer-events: none; }
.trek-dash .col-lrow-tags > * { flex-shrink: 0; }
.trek-dash .col-lrow-tags > .col-lrow-label, .trek-dash .col-lrow-tags > .col-lrow-cat { flex-shrink: 1; min-width: 0; }
-/* While the row fits its labels every chip keeps its own width (CollectionPlaceTags). */
-.trek-dash .col-lrow-tags.measuring > * { flex-shrink: 0; }
.trek-dash .col-lrow-more { display: inline-flex; align-items: center; flex-shrink: 0; padding: 3px 8px; border-radius: 999px; font-size: 11px; font-weight: 600; color: var(--ink-3); background: var(--surface-2); cursor: default; }
.trek-dash .col-lrow-status { grid-area: status; display: flex; }
@container (max-width: 760px) {
@@ -308,7 +308,7 @@
row-gap: 0; align-items: start;
}
.trek-dash .col-lrow-place > :first-child { align-self: center; }
- .trek-dash .col-lrow-tags { margin-top: 8px; }
+ .trek-dash .col-lrow-tags { margin-top: 8px; justify-content: flex-start; }
.trek-dash .col-lrow-tags .col-lrow-div { display: none; }
.trek-dash .col-lrow-tags > .col-lrow-label, .trek-dash .col-lrow-tags > .col-lrow-cat { max-width: 170px; }
}