fix(ui): robust grid table states — natural row heights, themed pagination, DOM skeleton

Rows expanded unboundedly because `fixedHeader: true` + a fixed `height`
forced a scroll container that filled itself with stretched rows on short
result sets. Pagination text rendered white-on-white because the shared
button base (app-shell.css) redefines `--app-color` as `--app-primary-inverse`
in the button scope, so `color: var(--app-color)` in an override resolves
to white. Sort-column headers looked misaligned because Grid.js uses floats
to place label and sort icon, which ignore the cell's vertical-align.

- Sticky header is now opt-in; default scroll behavior is page-level.
- Row heights come from padding + line-height only (no fixed `height`).
- Pagination buttons theme via `--app-background-color`/`--app-color`
  redefinition (the Pico convention) using `--app-form-element-color`,
  which is not shadowed in the button scope. `margin-inline-start: auto`
  on `.gridjs-pages` guarantees right-alignment even without a summary.
- Sort headers use `inline-block` + `vertical-align: middle` instead of
  vendor floats so label and icon share one cross-axis center.
- Loading UX replaced with a real `<table>` skeleton that mirrors the
  final column count + outer layout (card + footer with summary and
  5 pager placeholders). Grid.js mount point is hidden via
  `.app-grid-shell-loading > :not(.app-grid-skeleton)` — vendor wraps
  its own `.gridjs-container` one level deeper, so a direct-child
  selector on the mount element is required.
- Re-fetch state shows a thin top progress bar + faded tbody (GitHub
  pattern) so existing data stays as context.
- `prefers-reduced-motion` disables all skeleton/progress animations.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This commit is contained in:
2026-04-22 20:22:51 +02:00
parent 0c8ccae0fb
commit 4cf4ccc595
2 changed files with 407 additions and 91 deletions

View File

@@ -135,7 +135,11 @@ export function createServerGrid(options) {
}
const uiConfig = ui && typeof ui === 'object' ? ui : {};
const stickyHeaderEnabled = uiConfig.stickyHeader !== false;
// Sticky header is opt-in: enabling it forces a fixed-height scroll container
// inside the wrapper (Grid.js requirement for `fixedHeader`). On short result
// sets that container fills empty space and makes rows visually expand. The
// Stripe-like default scrolls on the page, not in the table.
const stickyHeaderEnabled = uiConfig.stickyHeader === true;
const gridHeight = typeof uiConfig.height === 'string' && uiConfig.height.trim() !== ''
? uiConfig.height.trim()
: DEFAULT_GRID_HEIGHT;
@@ -297,6 +301,7 @@ export function createServerGrid(options) {
|| 'Press Enter to open row'
).trim() || 'Press Enter to open row';
const gridRetryLabel = String(language?.errorRetry || 'Retry').trim() || 'Retry';
const gridLoadingLabel = String(language?.loading || 'Loading').trim() || 'Loading';
const resolveRowOpenUrl = (rowData, rowIndex = -1) => {
if (!rowInteractionEnabled) {
return '';
@@ -721,6 +726,106 @@ export function createServerGrid(options) {
plugins
});
// DOM-based skeleton (Stripe/GitHub-style): render a real table with the
// same column structure as the final grid, so the transition to real data
// is layout-stable and never flickers. We wrap the container in a shell so
// the skeleton can live as a sibling — Grid.js' render() controls the
// container's subtree and would wipe any skeleton placed inside it.
const shellEl = document.createElement('div');
shellEl.className = 'app-grid-shell';
containerEl.parentNode?.insertBefore(shellEl, containerEl);
shellEl.appendChild(containerEl);
const skeletonEnabled = loadingMode === 'skeleton';
const SKELETON_ROW_COUNT = 15;
const SKELETON_BAR_WIDTHS = ['72%', '58%', '64%', '44%', '70%', '52%', '60%', '48%'];
const SKELETON_PAGINATION_BUTTONS = 5;
const visibleSkeletonColumns = normalizedColumns.filter((col) => !col?.hidden && !col?.plugin);
const buildSkeletonBar = (width) => {
const bar = document.createElement('span');
bar.className = 'app-grid-skeleton-bar';
bar.style.width = width;
return bar;
};
const buildSkeleton = () => {
const root = document.createElement('div');
root.className = 'app-grid-skeleton';
root.setAttribute('role', 'status');
root.setAttribute('aria-label', gridLoadingLabel);
root.setAttribute('aria-live', 'polite');
const srLabel = document.createElement('span');
srLabel.className = 'app-grid-skeleton-sr';
srLabel.textContent = gridLoadingLabel;
root.appendChild(srLabel);
const table = document.createElement('table');
table.className = 'app-grid-skeleton-table';
table.setAttribute('aria-hidden', 'true');
const thead = document.createElement('thead');
const headRow = document.createElement('tr');
visibleSkeletonColumns.forEach(() => {
const th = document.createElement('th');
th.className = 'app-grid-skeleton-cell app-grid-skeleton-cell-head';
th.appendChild(buildSkeletonBar('55%'));
headRow.appendChild(th);
});
thead.appendChild(headRow);
table.appendChild(thead);
const tbody = document.createElement('tbody');
for (let rowIdx = 0; rowIdx < SKELETON_ROW_COUNT; rowIdx += 1) {
const tr = document.createElement('tr');
tr.className = 'app-grid-skeleton-row';
visibleSkeletonColumns.forEach((_col, colIdx) => {
const td = document.createElement('td');
td.className = 'app-grid-skeleton-cell';
const width = SKELETON_BAR_WIDTHS[(rowIdx + colIdx) % SKELETON_BAR_WIDTHS.length];
td.appendChild(buildSkeletonBar(width));
tr.appendChild(td);
});
tbody.appendChild(tr);
}
table.appendChild(tbody);
root.appendChild(table);
// Footer skeleton: mirrors the real pagination layout — summary (results
// count) on the left, pagination buttons on the right — so the transition
// doesn't shift the surrounding page layout.
const footer = document.createElement('div');
footer.className = 'app-grid-skeleton-footer';
footer.setAttribute('aria-hidden', 'true');
const summary = document.createElement('div');
summary.className = 'app-grid-skeleton-summary';
summary.appendChild(buildSkeletonBar('100%'));
footer.appendChild(summary);
const pager = document.createElement('div');
pager.className = 'app-grid-skeleton-pager';
for (let i = 0; i < SKELETON_PAGINATION_BUTTONS; i += 1) {
const btn = document.createElement('span');
btn.className = 'app-grid-skeleton-pager-button';
pager.appendChild(btn);
}
footer.appendChild(pager);
root.appendChild(footer);
return root;
};
let skeletonEl = null;
if (skeletonEnabled && visibleSkeletonColumns.length > 0) {
skeletonEl = buildSkeleton();
shellEl.insertBefore(skeletonEl, containerEl);
shellEl.classList.add('app-grid-shell-loading');
containerEl.setAttribute('aria-busy', 'true');
}
grid.render(containerEl);
setTimeout(() => {
initSelectAll();
@@ -732,6 +837,12 @@ export function createServerGrid(options) {
if (hasLoadedOnce) {return;}
hasLoadedOnce = true;
containerEl.classList.add('gridjs-has-loaded');
containerEl.removeAttribute('aria-busy');
shellEl.classList.remove('app-grid-shell-loading');
if (skeletonEl) {
skeletonEl.remove();
skeletonEl = null;
}
};
const setUpdating = () => containerEl.classList.add('gridjs-is-updating');
const clearUpdating = () => containerEl.classList.remove('gridjs-is-updating');