Skip to content

Commit 8381d87

Browse files
simonwclaude
andauthored
Collapse column statistics pills behind a "+N more" toggle (#3064)
The column pills in the Column statistics dialog had their own scroll box that cut rows of pills in half, and capped each pill at 16rem so long column names were truncated even when there was room. The pill list now shows at most three rows, ending in a "+N more" button that expands every pill in place ("Show fewer" collapses them again). The selected column's pill is always kept visible. The pills and the statistics share the dialog's one scroll area: collapsed, the pills stay pinned at the top; expanded, they scroll away with the statistics. The pills are re-fitted when the dialog width changes. Pill names now only truncate when too long for a whole row, with the full name in a title attribute. Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 4526010 commit 8381d87

3 files changed

Lines changed: 243 additions & 32 deletions

File tree

‎datasette/static/app.css‎

Lines changed: 56 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3162,28 +3162,56 @@ dialog.column-stats-dialog {
31623162
min-width: 0;
31633163
}
31643164

3165-
/* A pill for each column, to switch the dialog between columns */
3165+
/* The pills and the statistics scroll together, so the pill list never
3166+
needs a scrollbar of its own */
3167+
.column-stats-scroll {
3168+
min-height: 0;
3169+
overflow-y: auto;
3170+
/* Keeps the pill rows the same width whether or not it scrolls */
3171+
scrollbar-gutter: stable;
3172+
}
3173+
3174+
.column-stats-dialog .column-stats-body {
3175+
overflow: visible;
3176+
}
3177+
3178+
/* A pill for each column, to switch the dialog between columns. Collapsed,
3179+
it shows at most three rows (column-stats.js hides the rest behind a
3180+
"+N more" pill) and stays pinned while the statistics scroll beneath it.
3181+
Expanded, every pill shows and they scroll away with the statistics. */
31663182
.column-stats-columns {
3167-
position: relative;
3183+
position: sticky;
3184+
top: 0;
3185+
z-index: 1;
31683186
display: flex;
31693187
flex-wrap: wrap;
31703188
gap: 6px;
3171-
flex-shrink: 0;
3172-
max-height: 5.6rem;
3173-
overflow-y: auto;
31743189
padding: 10px 24px;
31753190
border-bottom: 1px solid var(--rule);
3191+
background: var(--card);
3192+
}
3193+
3194+
.column-stats-columns.is-expanded {
3195+
position: static;
31763196
}
31773197

31783198
.column-stats-columns[hidden] {
31793199
display: none;
31803200
}
31813201

3182-
.column-stats-column {
3202+
/* The tablist holds only the tabs, but the pills and the "+N more" button
3203+
share one row layout */
3204+
.column-stats-tablist {
3205+
display: contents;
3206+
}
3207+
3208+
.column-stats-column,
3209+
.column-stats-more {
31833210
display: inline-flex;
31843211
align-items: baseline;
31853212
gap: 6px;
3186-
max-width: 16rem;
3213+
/* Only truncate names too long to fit on a row by themselves */
3214+
max-width: 100%;
31873215
padding: 3px 10px;
31883216
border: 1px solid var(--rule);
31893217
border-radius: 999px;
@@ -3196,6 +3224,26 @@ dialog.column-stats-dialog {
31963224
white-space: nowrap;
31973225
}
31983226

3227+
.column-stats-column[hidden],
3228+
.column-stats-more[hidden] {
3229+
display: none;
3230+
}
3231+
3232+
.column-stats-more {
3233+
border-style: dashed;
3234+
color: var(--accent);
3235+
}
3236+
3237+
.column-stats-more:hover {
3238+
border-color: var(--accent);
3239+
border-style: solid;
3240+
}
3241+
3242+
.column-stats-more:focus-visible {
3243+
outline: 2px solid var(--accent);
3244+
outline-offset: 2px;
3245+
}
3246+
31993247
.column-stats-column:hover {
32003248
border-color: var(--accent);
32013249
}
@@ -3206,6 +3254,7 @@ dialog.column-stats-dialog {
32063254
}
32073255

32083256
.column-stats-column-name {
3257+
min-width: 0;
32093258
overflow: hidden;
32103259
text-overflow: ellipsis;
32113260
}

‎datasette/static/column-stats.js‎

Lines changed: 108 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -927,8 +927,13 @@ limit ${sqlNumber(limit)}`;
927927
<span class="modal-title" id="${TITLE_ID}">Column statistics</span>
928928
<span class="modal-meta"></span>
929929
</div>
930-
<div class="column-stats-columns" role="tablist" aria-label="Columns" hidden></div>
931-
<div class="modal-body column-stats-body" id="column-stats-panel" role="tabpanel"></div>
930+
<div class="column-stats-scroll">
931+
<div class="column-stats-columns" hidden>
932+
<div class="column-stats-tablist" role="tablist" aria-label="Columns"></div>
933+
<button type="button" class="column-stats-more" aria-expanded="false" hidden></button>
934+
</div>
935+
<div class="modal-body column-stats-body" id="column-stats-panel" role="tabpanel"></div>
936+
</div>
932937
<div class="modal-footer">
933938
<span class="footer-info" aria-live="polite"></span>
934939
<button type="button" class="modal-btn modal-btn-ghost column-stats-close">Close</button>
@@ -940,8 +945,11 @@ limit ${sqlNumber(limit)}`;
940945
dialog: dialog,
941946
meta: dialog.querySelector(".modal-meta"),
942947
body: dialog.querySelector(".column-stats-body"),
943-
tabList: dialog.querySelector(".column-stats-columns"),
948+
columns: dialog.querySelector(".column-stats-columns"),
949+
tabList: dialog.querySelector(".column-stats-tablist"),
950+
moreButton: dialog.querySelector(".column-stats-more"),
944951
tabs: [],
952+
expanded: false,
945953
footerInfo: dialog.querySelector(".footer-info"),
946954
closeButton: dialog.querySelector(".column-stats-close"),
947955
controller: null,
@@ -952,6 +960,7 @@ limit ${sqlNumber(limit)}`;
952960
modal.requestClose("cancel");
953961
});
954962
dialog.addEventListener("close", () => {
963+
setExpanded(state, false);
955964
if (state.controller) {
956965
state.controller.abort();
957966
state.controller = null;
@@ -971,7 +980,7 @@ limit ${sqlNumber(limit)}`;
971980
if (!columns.length) {
972981
return;
973982
}
974-
s.tabList.hidden = false;
983+
s.columns.hidden = false;
975984
s.tabs = columns.map((column, index) => {
976985
var tab = h(
977986
"button",
@@ -984,6 +993,8 @@ limit ${sqlNumber(limit)}`;
984993
"aria-controls": s.body.id || null,
985994
tabindex: "-1",
986995
"data-column": column.name,
996+
// Shows the full name if a very long one is truncated
997+
title: column.name,
987998
},
988999
[
9891000
h("span", {
@@ -1016,8 +1027,11 @@ limit ${sqlNumber(limit)}`;
10161027
});
10171028
s.tabs.forEach((tab) => s.tabList.appendChild(tab));
10181029
resetTabStopOnLeave(s);
1030+
// Arrow keys move between the pills on show; the "+N more" button
1031+
// reveals the rest
10191032
s.tabList.addEventListener("keydown", (event) => {
1020-
var index = s.tabs.indexOf(document.activeElement);
1033+
var visible = s.tabs.filter((tab) => !tab.hidden);
1034+
var index = visible.indexOf(document.activeElement);
10211035
if (index === -1) {
10221036
return;
10231037
}
@@ -1027,17 +1041,95 @@ limit ${sqlNumber(limit)}`;
10271041
ArrowLeft: index - 1,
10281042
ArrowUp: index - 1,
10291043
Home: 0,
1030-
End: s.tabs.length - 1,
1044+
End: visible.length - 1,
10311045
}[event.key];
10321046
if (next === undefined) {
10331047
return;
10341048
}
10351049
event.preventDefault();
1036-
next = (next + s.tabs.length) % s.tabs.length;
1037-
setTabStop(s, s.tabs[next]);
1038-
s.tabs[next].focus();
1039-
scrollTabIntoView(s, s.tabs[next]);
1050+
next = (next + visible.length) % visible.length;
1051+
setTabStop(s, visible[next]);
1052+
visible[next].focus();
1053+
});
1054+
s.moreButton.addEventListener("click", () => {
1055+
setExpanded(s, !s.expanded);
1056+
fitColumnTabs(s);
10401057
});
1058+
// Pills wrap differently at a new width, so fit them again. Only the
1059+
// width matters: fitting them changes the height.
1060+
var lastWidth = 0;
1061+
new ResizeObserver((entries) => {
1062+
var width = entries[entries.length - 1].contentRect.width;
1063+
if (width && width !== lastWidth) {
1064+
lastWidth = width;
1065+
fitColumnTabs(s);
1066+
}
1067+
}).observe(s.columns);
1068+
}
1069+
1070+
function setExpanded(s, expanded) {
1071+
s.expanded = expanded;
1072+
s.columns.classList.toggle("is-expanded", expanded);
1073+
s.moreButton.setAttribute("aria-expanded", String(expanded));
1074+
}
1075+
1076+
// Collapsed, show as many pills as fit in MAX_TAB_ROWS rows with a
1077+
// "+N more" button at the end of the last one, always including the
1078+
// selected column's pill. Expanded, show every pill and a "Show fewer"
1079+
// button.
1080+
var MAX_TAB_ROWS = 3;
1081+
1082+
function fitColumnTabs(s) {
1083+
var tabs = s.tabs;
1084+
var more = s.moreButton;
1085+
if (!tabs.length || !s.dialog.open) {
1086+
return;
1087+
}
1088+
tabs.forEach((tab) => {
1089+
tab.hidden = false;
1090+
});
1091+
more.hidden = true;
1092+
var rowTops = Array.from(new Set(tabs.map((tab) => tab.offsetTop)));
1093+
if (rowTops.length <= MAX_TAB_ROWS) {
1094+
setExpanded(s, false);
1095+
return;
1096+
}
1097+
more.hidden = false;
1098+
if (s.expanded) {
1099+
more.textContent = "Show fewer";
1100+
more.removeAttribute("aria-label");
1101+
return;
1102+
}
1103+
var lastRowTop = rowTops.sort((a, b) => a - b)[MAX_TAB_ROWS - 1];
1104+
var selectedIndex = tabs.findIndex(
1105+
(tab) => tab.getAttribute("aria-selected") === "true",
1106+
);
1107+
// Show the first `count` pills, plus the selected one if it's later
1108+
function showFirst(count) {
1109+
var hiddenCount = 0;
1110+
tabs.forEach((tab, index) => {
1111+
tab.hidden = index >= count && index !== selectedIndex;
1112+
hiddenCount += tab.hidden ? 1 : 0;
1113+
});
1114+
more.textContent = `+${hiddenCount} more`;
1115+
more.setAttribute(
1116+
"aria-label",
1117+
`Show ${hiddenCount} more column${hiddenCount === 1 ? "" : "s"}`,
1118+
);
1119+
return more.offsetTop <= lastRowTop;
1120+
}
1121+
// The most pills that still leave the button on the last row
1122+
var low = 0;
1123+
var high = tabs.length - 1;
1124+
while (low < high) {
1125+
var middle = Math.ceil((low + high) / 2);
1126+
if (showFirst(middle)) {
1127+
low = middle;
1128+
} else {
1129+
high = middle - 1;
1130+
}
1131+
}
1132+
showFirst(low);
10411133
}
10421134

10431135
// Tabbing back into the pills lands on the selected column, even after
@@ -1081,18 +1173,6 @@ limit ${sqlNumber(limit)}`;
10811173
return selected;
10821174
}
10831175

1084-
// Scroll within the pill list only, never the page behind the dialog.
1085-
function scrollTabIntoView(s, tab) {
1086-
var list = s.tabList;
1087-
var top = tab.offsetTop - list.offsetTop;
1088-
var bottom = top + tab.offsetHeight;
1089-
if (top < list.scrollTop) {
1090-
list.scrollTop = top - 6;
1091-
} else if (bottom > list.scrollTop + list.clientHeight) {
1092-
list.scrollTop = bottom - list.clientHeight + 6;
1093-
}
1094-
}
1095-
10961176
// Link to the table filtered to a value, on top of the current filters if
10971177
// the statistics were calculated using them.
10981178
function filterUrl(column, value, keepFilters) {
@@ -1233,15 +1313,18 @@ limit ${sqlNumber(limit)}`;
12331313
if (tableFilter) {
12341314
overviewSection.body.before(renderScope());
12351315
}
1316+
if (activeTab) {
1317+
// An earlier fit may have hidden it; it can only be focused if shown.
1318+
// fitColumnTabs() below always keeps it.
1319+
activeTab.hidden = false;
1320+
}
12361321
s.modal.show({
12371322
returnFocusTo: options.returnFocusTo,
12381323
initialFocus: options.focusScopeToggle
12391324
? scopeToggle
12401325
: activeTab || s.closeButton,
12411326
});
1242-
if (activeTab) {
1243-
scrollTabIntoView(s, activeTab);
1244-
}
1327+
fitColumnTabs(s);
12451328

12461329
// Say which rows the statistics cover, with a button to switch between
12471330
// the filtered rows and the whole table.

‎tests/test_playwright.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3244,6 +3244,85 @@ def test_column_statistics_switch_columns(page, datasette_server):
32443244
assert page_errors == []
32453245

32463246

3247+
@pytest.mark.playwright
3248+
def test_column_statistics_more_columns_toggle(page, datasette_server):
3249+
from playwright.sync_api import expect
3250+
3251+
page_errors = []
3252+
page.on("pageerror", lambda error: page_errors.append(str(error)))
3253+
page.set_viewport_size({"width": 800, "height": 900})
3254+
names = [f"column_{i:02d}" for i in range(1, 31)]
3255+
sql = "select " + ", ".join(f"score as {name}" for name in names)
3256+
sql += " from stats_demo"
3257+
page.goto(datasette_server + "data/-/query?" + urllib.parse.urlencode({"sql": sql}))
3258+
dialog = page.locator("#column-stats-dialog")
3259+
tabs = dialog.get_by_role("tab")
3260+
more = dialog.locator(".column-stats-more")
3261+
3262+
def open_first_column():
3263+
page.locator("table.rows-and-columns th").nth(0).locator(
3264+
".column-menu-trigger"
3265+
).click()
3266+
page.locator("#column-actions-menu").get_by_role(
3267+
"menuitem", name="Column statistics", exact=True
3268+
).click()
3269+
page.locator('#column-stats-dialog[data-loading="false"]').wait_for()
3270+
3271+
def visible_tabs():
3272+
return tabs.evaluate_all(
3273+
"tabs => tabs.filter(tab => !tab.hidden).map(tab => tab.dataset.column)"
3274+
)
3275+
3276+
def row_count():
3277+
return dialog.locator(".column-stats-columns").evaluate("""columns => new Set(
3278+
Array.from(columns.querySelectorAll(
3279+
'.column-stats-column:not([hidden]), .column-stats-more'
3280+
)).map(node => node.offsetTop)
3281+
).size""")
3282+
3283+
open_first_column()
3284+
# Collapsed: the first pills in three rows, ending in "+N more"
3285+
shown = visible_tabs()
3286+
hidden_count = len(names) - len(shown)
3287+
assert hidden_count > 0
3288+
assert shown == names[: len(shown)]
3289+
expect(more).to_have_text(f"+{hidden_count} more")
3290+
expect(more).to_have_attribute("aria-expanded", "false")
3291+
assert row_count() == 3
3292+
# The pill list never scrolls on its own
3293+
assert dialog.locator(".column-stats-columns").evaluate(
3294+
"columns => columns.scrollHeight <= columns.clientHeight"
3295+
)
3296+
# Arrow keys only move between the pills on show
3297+
expect(dialog.get_by_role("tab", name="column_01 integer")).to_be_focused()
3298+
page.keyboard.press("ArrowLeft")
3299+
expect(dialog.get_by_role("tab", name=f"{shown[-1]} integer")).to_be_focused()
3300+
# Expanded: every pill, and switching column stays expanded
3301+
more.click()
3302+
expect(more).to_have_text("Show fewer")
3303+
expect(more).to_have_attribute("aria-expanded", "true")
3304+
assert visible_tabs() == names
3305+
dialog.get_by_role("tab", name="column_30 integer").click()
3306+
page.locator('#column-stats-dialog[data-loading="false"]').wait_for()
3307+
expect(dialog).to_have_attribute("data-column", "column_30")
3308+
assert visible_tabs() == names
3309+
# Collapsing keeps the selected pill, after the others
3310+
more.click()
3311+
expect(more).to_have_attribute("aria-expanded", "false")
3312+
shown = visible_tabs()
3313+
assert shown[-1] == "column_30"
3314+
assert shown[:-1] == names[: len(shown) - 1]
3315+
expect(more).to_have_text(f"+{len(names) - len(shown)} more")
3316+
assert row_count() == 3
3317+
# Reopening starts collapsed
3318+
page.keyboard.press("Escape")
3319+
expect(dialog).not_to_be_visible()
3320+
open_first_column()
3321+
expect(more).to_have_attribute("aria-expanded", "false")
3322+
assert "column_30" not in visible_tabs()
3323+
assert page_errors == []
3324+
3325+
32473326
CLIPBOARD_STUB = """
32483327
(() => {
32493328
window.clipboardText = null;

0 commit comments

Comments
 (0)