diff --git a/CHANGELOG.md b/CHANGELOG.md index 6fd43687e..080613611 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## unreleased +- The new [`status_bar` component](https://sql-page.com/component.sql?component=status_bar) displays short status messages and links along the bottom of the screen or inline with page content. - A file input with `multiple` now keeps every selected file, instead of only the last one. The new [`sqlpage.persist_uploaded_files`](https://sql-page.com/functions.sql?function=persist_uploaded_files) function saves them all and returns a JSON array with the `path`, original `name`, `mime_type` and `size` of each file. - Set `wrap` on a `big_number` component to show long titles and descriptions across multiple lines. diff --git a/examples/official-site/sqlpage/migrations/80_status_bar.sql b/examples/official-site/sqlpage/migrations/80_status_bar.sql new file mode 100644 index 000000000..9a4ce68d6 --- /dev/null +++ b/examples/official-site/sqlpage/migrations/80_status_bar.sql @@ -0,0 +1,23 @@ +INSERT INTO component(name, icon, description, introduced_in_version) VALUES + ('status_bar', 'layout-bottombar', + 'Display short status messages and links in a single horizontal bar. Each row adds one segment. When there are several segments, the last one aligns to the far edge. If the segments do not all fit, scroll the bar horizontally to see the rest.', + '0.49.0'); + +INSERT INTO parameter(component, name, description, type, top_level, optional) +SELECT 'status_bar', * FROM (VALUES + ('fixed', 'Keep the bar at the bottom of the screen while the page scrolls. Defaults to true. A fixed bar overlays page content, so leave space below your last item if needed. Set to false to place the bar in the page where the SQL appears.', 'BOOLEAN', TRUE, TRUE), + ('label', 'Accessible name for the bar. Defaults to "Status bar".', 'TEXT', TRUE, TRUE), + ('title', 'Text displayed in the segment.', 'TEXT', FALSE, TRUE), + ('icon', 'Icon displayed before the text.', 'ICON', FALSE, TRUE), + ('color', 'Color of the segment text and icon.', 'COLOR', FALSE, TRUE), + ('tooltip', 'Text shown when hovering over the segment or focusing its link. For an icon without a title, supply a tooltip to give the segment an accessible name.', 'TEXT', FALSE, TRUE), + ('link', 'URL to open when the segment is clicked.', 'URL', FALSE, TRUE) +) p; + +INSERT INTO example(component, description, properties) VALUES + ('status_bar', 'A query workspace shows the database connection, row count, and a link to the function reference. Omit `fixed` to pin this bar to the bottom of the screen.', json('[ + {"component":"status_bar","label":"Query status","fixed":false}, + {"title":"Connected","icon":"database","color":"green","tooltip":"Connected to the reporting database"}, + {"title":"24 rows"}, + {"title":"SQL functions","icon":"book","link":"/functions.sql"} + ]')); diff --git a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql b/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql index 46d1c041a..712cb683f 100644 --- a/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql +++ b/examples/official-site/sqlpage/migrations/99_shared_id_class_attributes.sql @@ -28,6 +28,7 @@ FROM (VALUES ('list', TRUE), ('list', FALSE), ('map', TRUE), + ('status_bar', TRUE), ('tab', TRUE), ('tab', FALSE), ('table', TRUE), @@ -69,6 +70,8 @@ FROM (VALUES ('list', TRUE), ('list', FALSE), ('map', TRUE), + ('status_bar', TRUE), + ('status_bar', FALSE), ('tab', TRUE), ('tab', FALSE), ('table', TRUE), diff --git a/sqlpage/templates/status_bar.handlebars b/sqlpage/templates/status_bar.handlebars new file mode 100644 index 000000000..15c0753b9 --- /dev/null +++ b/sqlpage/templates/status_bar.handlebars @@ -0,0 +1,29 @@ + +
+ {{#each_row}} + {{#if link}} + {{#if icon}}{{/if}} + {{#if title includeZero=true}}{{title}}{{else}}{{tooltip}}{{/if}} + {{#if link}}{{else}}{{/if}} + {{/each_row}} +
diff --git a/tests/end-to-end/fixtures/status-bar/fragment.sql b/tests/end-to-end/fixtures/status-bar/fragment.sql new file mode 100644 index 000000000..22480baef --- /dev/null +++ b/tests/end-to-end/fixtures/status-bar/fragment.sql @@ -0,0 +1,2 @@ +SELECT 'status_bar' AS component, 'Sync status' AS label; +SELECT 'Synced' AS title; diff --git a/tests/end-to-end/fixtures/status-bar/index.sql b/tests/end-to-end/fixtures/status-bar/index.sql new file mode 100644 index 000000000..45eb2e865 --- /dev/null +++ b/tests/end-to-end/fixtures/status-bar/index.sql @@ -0,0 +1,20 @@ +SELECT 'shell' AS component, 'Builds' AS title, 'boxed' AS layout, TRUE AS fixed_top_menu +WHERE $boxed IS NOT NULL; + +SELECT 'status_bar' AS component, 'repository' AS id, 'project-status' AS class, + 'Repository status' AS label, FALSE AS fixed; +SELECT 'Build passed' AS title, 'circle-check' AS icon, 'green' AS color, + 'All checks passed' AS tooltip, '#activity' AS link, 'build-result' AS class; +SELECT 'Branch: feature/improve-search-index' AS title; +SELECT '12 pending changes' AS title; +SELECT 'View history' AS title, '#history' AS link; + +SELECT 'list' AS component, 'Recent activity' AS title, 'activity' AS id, + 'min-vh-100' AS class; +SELECT 'Search index updated' AS title; + +SELECT 'title' AS component, 'Build history' AS contents, 'history' AS id; + +SELECT 'status_bar' AS component; +SELECT 'Connected' AS title; +SELECT 'history' AS icon, 'Open build history' AS tooltip, '#history' AS link; diff --git a/tests/end-to-end/fixtures/status-bar/test.ts b/tests/end-to-end/fixtures/status-bar/test.ts new file mode 100644 index 000000000..ff9bcd283 --- /dev/null +++ b/tests/end-to-end/fixtures/status-bar/test.ts @@ -0,0 +1,94 @@ +import { expect, loadFragment, test } from "../../fixture.ts"; + +test("rows render in order with icons, tooltips and links", async ({ + page, +}) => { + const bar = page.getByRole("status", { name: "Repository status" }); + await expect(bar).toHaveAttribute("id", "repository"); + await expect(bar).toContainClass("project-status"); + await expect(bar).toHaveText( + /Build passed\s*Branch: feature\/improve-search-index\s*12 pending changes\s*View history/, + ); + + const build = bar.getByRole("link", { name: "Build passed", exact: true }); + const history = bar.getByRole("link", { name: "View history" }); + await expect(bar.locator(".build-result")).toContainClass("text-green"); + await expect(build.locator("svg")).toBeVisible(); + const barRight = await bar.evaluate((el) => el.getBoundingClientRect().right); + const textRight = await history.evaluate((el) => { + const text = document.createRange(); + text.selectNodeContents(el); + return text.getBoundingClientRect().right; + }); + expect(barRight - textRight).toBeLessThan(32); + await build.hover(); + await expect(page.getByRole("tooltip")).toHaveText("All checks passed"); + await history.click(); + await expect(page).toHaveURL(/#history$/); + await expect( + page.getByRole("heading", { name: "Build history" }), + ).toBeInViewport(); +}); + +test.describe("without JavaScript", () => { + test.use({ javaScriptEnabled: false }); + + test("the status bar stays at the viewport bottom and the fixed menu fits the boxed shell", async ({ + page, + }) => { + await page.goto("/status-bar/?boxed=1"); + const fixed = page.getByRole("status", { name: "Status bar", exact: true }); + const inline = page.getByRole("status", { name: "Repository status" }); + const bottomGap = () => + fixed.evaluate((bar) => innerHeight - bar.getBoundingClientRect().bottom); + + const header = await page.getByRole("banner").evaluate((el) => { + const { x, width } = el.getBoundingClientRect(); + return { x, width }; + }); + expect(header.x).toBeGreaterThan(0); + expect(await page.getByRole("navigation").boundingBox()).toMatchObject( + header, + ); + await expect(fixed.getByText("Connected", { exact: true })).toBeVisible(); + await expect( + fixed.getByRole("link", { name: "Open build history" }), + ).toBeVisible(); + await expect(inline).toBeInViewport(); + expect(await bottomGap()).toBeCloseTo(0); + await page + .getByRole("heading", { name: "Build history" }) + .scrollIntoViewIfNeeded(); + await expect(inline).not.toBeInViewport(); + expect(await bottomGap()).toBeCloseTo(0); + }); +}); + +test("a narrow bar scrolls to the next focused link without widening the page", async ({ + page, +}) => { + await page.setViewportSize({ width: 320, height: 640 }); + const bar = page.getByRole("status", { name: "Repository status" }); + const history = bar.getByRole("link", { name: "View history" }); + await expect(history).not.toBeInViewport(); + await bar.getByRole("link", { name: "Build passed" }).focus(); + await page.keyboard.press("Tab"); + await expect(history).toBeFocused(); + await expect(history).toBeInViewport({ ratio: 0.99 }); + expect(await page.evaluate(() => document.documentElement.scrollWidth)).toBe( + 320, + ); +}); + +test("a fixed bar disappears with its embedded fragment", async ({ page }) => { + await loadFragment( + page, + "/status-bar/fragment.sql", + "#activity", + "#activity", + ); + const bar = page.getByRole("status", { name: "Sync status" }); + await expect(bar).toHaveText("Synced"); + await page.locator("#activity").evaluate((el) => el.remove()); + await expect(bar).toHaveCount(0); +});