-
-
Notifications
You must be signed in to change notification settings - Fork 175
Add stacked toast notification component #1359
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,49 @@ | ||
| INSERT INTO component(name, icon, description, introduced_in_version) VALUES | ||
| ('toast', 'notification', ' | ||
| Displays a brief notification above the page. Each top-level `toast` row creates one notification, and consecutive toasts at the same position are queued in a shared stack. | ||
| Ordinary notifications use `role="status"` and `aria-live="polite"`. Colored variants retain a readable contrasting foreground. Automatic dismissal and the optional manual close control are configured independently.', '0.46.0'); | ||
|
|
||
| INSERT INTO parameter(component, name, description, type, top_level, optional) SELECT 'toast', * FROM (VALUES | ||
| ('title', 'Optional notification heading.', 'TEXT', TRUE, TRUE), | ||
| ('description', 'Escaped plain-text body. This is used only when `description_md` is not supplied.', 'TEXT', TRUE, TRUE), | ||
| ('description_md', 'Rich-text alternative to `description`, rendered as Markdown. When both properties are supplied, `description_md` takes precedence.', 'TEXT', TRUE, TRUE), | ||
| ('icon', 'Optional [Tabler icon](https://tabler.io/icons) name.', 'ICON', TRUE, TRUE), | ||
| ('color', 'Optional Tabler color. The default is the neutral toast appearance; colored variants use the matching contrasting foreground utility.', 'COLOR', TRUE, TRUE), | ||
| ('dismissible', 'Whether to render an accessible manual close button. Defaults to false and is independent of automatic dismissal.', 'BOOLEAN', TRUE, TRUE), | ||
| ('duration', 'Automatic dismissal delay in milliseconds. Defaults to 5000. Set to 0 to keep the toast visible until manually dismissed (when `dismissible` is true) or the page is left.', 'INTEGER', TRUE, TRUE), | ||
| ('position', 'Screen placement: `top-start`, `top-center`, `top-end`, `bottom-start`, `bottom-center`, or `bottom-end`. Defaults to `top-end`; invalid values safely fall back to that default.', 'TEXT', TRUE, TRUE), | ||
| ('trigger', 'Optional URL fragment that opens the toast without reloading the page, with or without the leading `#`. When set, the toast does not open on page load and can be opened repeatedly by a link or button whose target is that fragment. Multiple toasts can share a trigger to open as a stack.', 'TEXT', TRUE, TRUE), | ||
| ('id', 'Optional stable HTML ID for the toast.', 'TEXT', TRUE, TRUE), | ||
| ('class', 'Optional custom CSS class appended to the toast.', 'TEXT', TRUE, TRUE) | ||
| ) x; | ||
|
|
||
| INSERT INTO example(component, description, properties) VALUES | ||
| ('toast', 'A “Changes saved” confirmation that disappears automatically after the default 5000 milliseconds.', json('[ | ||
| {"component":"toast","id":"toast-auto","title":"Changes saved","description":"Your changes have been saved.","icon":"check","color":"green"} | ||
| ]')), | ||
| ('toast', 'A persistent error notification that disables automatic dismissal and requires the user to activate its close button.', json('[ | ||
| {"component":"toast","id":"toast-dismissible","trigger":"persistent-error","title":"Could not save","description":"Review the highlighted fields and try again.","icon":"alert-triangle","color":"red","duration":0,"dismissible":true}, | ||
| {"component":"toast","id":"toast-nondismissible","trigger":"persistent-status","title":"Connection unavailable","description":"This persistent notification has no manual close control.","duration":0,"dismissible":false}, | ||
| {"component":"button"}, | ||
| {"title":"Show dismissible error","link":"#persistent-error","color":"red"}, | ||
| {"title":"Show non-dismissible status","link":"#persistent-status"} | ||
| ]')), | ||
| ('toast', 'A persistent rich Markdown notification. Markdown takes precedence over plain text and renders emphasis and a link as HTML, while plain-text content remains escaped.', json('[ | ||
| {"component":"toast","id":"toast-markdown","trigger":"rich-notifications","title":"Release available","description":"<strong>This fallback stays escaped</strong>","description_md":"Version **2.0** is ready. [Read the notes](https://example.com/releases).","duration":0}, | ||
| {"component":"toast","id":"toast-plain","trigger":"rich-notifications","description":"<strong>Plain text stays escaped</strong>","duration":0}, | ||
| {"component":"button"}, | ||
| {"title":"Show rich notifications","link":"#rich-notifications"} | ||
| ]')), | ||
| ('toast', 'Several persistent queued notifications. Toasts with the same position share a container and stack instead of overlapping.', json('[ | ||
| {"component":"toast","id":"toast-stack-one","trigger":"queued-notifications","title":"Import started","description":"Preparing records.","duration":0}, | ||
| {"component":"toast","id":"toast-stack-two","trigger":"queued-notifications","title":"Import running","description":"Processing records.","duration":0}, | ||
| {"component":"toast","id":"toast-short","trigger":"queued-notifications","title":"Temporary update","description":"This message closes shortly.","duration":2000}, | ||
| {"component":"button"}, | ||
| {"title":"Show queued notifications","link":"#queued-notifications"} | ||
| ]')), | ||
| ('toast', 'A notification placed at the bottom center of the screen instead of the default top end.', json('[ | ||
| {"component":"toast","id":"toast-bottom-center","trigger":"bottom-notification","title":"Download ready","description":"Your export is ready.","position":"bottom-center","duration":0}, | ||
| {"component":"button"}, | ||
| {"title":"Show bottom notification","link":"#bottom-notification"} | ||
| ]')); |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -325,11 +325,104 @@ function add_init_fn(f) { | |
| if (document.readyState !== "loading") setTimeout(f, 0); | ||
| } | ||
|
|
||
| const toast_positions = { | ||
| "top-start": ["top-0", "start-0"], | ||
| "top-center": ["top-0", "start-50", "translate-middle-x"], | ||
| "top-end": ["top-0", "end-0"], | ||
| "bottom-start": ["bottom-0", "start-0"], | ||
| "bottom-center": ["bottom-0", "start-50", "translate-middle-x"], | ||
| "bottom-end": ["bottom-0", "end-0"], | ||
| }; | ||
| const toast_instances = new WeakMap(); | ||
|
|
||
| function normalize_hash(hash) { | ||
| return hash?.replace(/^#/, ""); | ||
| } | ||
|
|
||
| function open_toasts_for_hash() { | ||
| const hash = normalize_hash(window.location.hash); | ||
| if (!hash) return; | ||
| for (const toast of document.querySelectorAll("[data-toast-trigger]")) { | ||
| if (normalize_hash(toast.dataset.toastTrigger) === hash) { | ||
| toast_instances.get(toast)?.show(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| function sqlpage_toast() { | ||
| const bootstrap = window.bootstrap || window.tabler?.bootstrap; | ||
| if (!bootstrap?.Toast) return; | ||
|
|
||
| for (const toast of document.querySelectorAll('[data-pre-init="toast"]')) { | ||
| const requested_position = toast.dataset.position; | ||
| const position = toast_positions[requested_position] | ||
| ? requested_position | ||
| : "top-end"; | ||
| toast.dataset.position = position; | ||
|
|
||
| let container = document.querySelector( | ||
| `.toast-container[data-sqlpage-toast-position="${position}"]`, | ||
| ); | ||
| if (!container) { | ||
| container = document.createElement("div"); | ||
| container.classList.add( | ||
| "toast-container", | ||
| "position-fixed", | ||
| "p-3", | ||
| ...toast_positions[position], | ||
|
Comment on lines
+368
to
+372
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When a query returns enough persistent ( Useful? React with 👍 / 👎. |
||
| ); | ||
| container.dataset.sqlpageToastPosition = position; | ||
| container.dataset.sqlpageGenerated = "toast-container"; | ||
| document.body.appendChild(container); | ||
| } | ||
|
|
||
| toast.removeAttribute("data-pre-init"); | ||
| container.appendChild(toast); | ||
| const requested_duration = Number.parseInt(toast.dataset.duration, 10); | ||
| const duration = | ||
| Number.isFinite(requested_duration) && requested_duration >= 0 | ||
| ? requested_duration | ||
| : 5000; | ||
| toast.dataset.duration = String(duration); | ||
| const instance = new bootstrap.Toast(toast, { | ||
| autohide: duration !== 0, | ||
| delay: duration > 0 ? duration : 5000, | ||
| }); | ||
| toast_instances.set(toast, instance); | ||
| toast.addEventListener("hidden.bs.toast", () => { | ||
| if (toast.dataset.toastTrigger) { | ||
| if ( | ||
| normalize_hash(window.location.hash) === | ||
| normalize_hash(toast.dataset.toastTrigger) | ||
| ) { | ||
| window.history.replaceState(null, "", "#"); | ||
| } | ||
| return; | ||
| } | ||
| toast.remove(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
When automatic, non-triggered toasts are hidden, removing the DOM node without calling the Bootstrap instance's Useful? React with 👍 / 👎. |
||
| if ( | ||
| container.dataset.sqlpageGenerated === "toast-container" && | ||
| !container.querySelector(".toast") | ||
| ) { | ||
| container.remove(); | ||
| } | ||
| }); | ||
| if (toast.dataset.toastTrigger) { | ||
| open_toasts_for_hash(); | ||
| } else { | ||
| instance.show(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| add_init_fn(sqlpage_table); | ||
| add_init_fn(sqlpage_map); | ||
| add_init_fn(sqlpage_card); | ||
| add_init_fn(sqlpage_form); | ||
| add_init_fn(load_scripts); | ||
| add_init_fn(sqlpage_toast); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
On an initial page load, this initializer cannot run until the deferred AGENTS.md reference: AGENTS.md:L36-L42 Useful? React with 👍 / 👎. |
||
| add_init_fn(open_toasts_for_hash); | ||
| window.addEventListener("hashchange", open_toasts_for_hash); | ||
|
|
||
| function init_bootstrap_components(event) { | ||
| const bootstrap = window.bootstrap || window.tabler.bootstrap; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| <div{{#if id}} id="{{id}}"{{/if}} class="toast{{#if color}} bg-{{color}} text-{{color}}-fg{{/if}}{{#if class}} {{class}}{{/if}}" role="status" aria-live="polite" aria-atomic="true" data-pre-init="toast" data-duration="{{default duration 5000}}" data-position="{{default position 'top-end'}}"{{#if trigger}} data-toast-trigger="{{trigger}}"{{/if}}> | ||
| <div class="toast-body d-flex align-items-start gap-2"> | ||
| {{~#if icon~}} | ||
| <span class="flex-shrink-0" aria-hidden="true">{{~icon_img icon~}}</span> | ||
| {{~/if~}} | ||
| <div class="flex-fill overflow-auto"> | ||
| {{~#if title~}}<div class="fw-bold mb-1">{{title}}</div>{{~/if~}} | ||
| {{~#if description_md~}} | ||
| <div class="toast-description toast-description-markdown">{{{markdown description_md}}}</div> | ||
| {{~else~}} | ||
| {{~#if description~}}<div class="toast-description">{{description}}</div>{{~/if~}} | ||
| {{~/if~}} | ||
| </div> | ||
| {{~#if dismissible~}} | ||
| <button type="button" class="btn-close{{#if color}} btn-close-white{{/if}} flex-shrink-0" data-bs-dismiss="toast" aria-label="Close notification"></button> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For every colored dismissible toast this forces a white close icon, while light Tabler colors such as Useful? React with 👍 / 👎. |
||
| {{~/if~}} | ||
| </div> | ||
| </div> | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -41,6 +41,80 @@ test("map", async ({ page }) => { | |
| await expect(page.locator(".leaflet-marker-icon").first()).toBeVisible(); | ||
| }); | ||
|
|
||
| test("toast notifications initialize, stack, dismiss, and render safely", async ({ | ||
| page, | ||
| }) => { | ||
| await page.goto(`${BASE}/documentation.sql?component=toast#component`); | ||
|
|
||
| const automatic = page.locator("#toast-auto"); | ||
| await expect(automatic).toBeVisible(); | ||
| await expect(page.locator(".toast.show")).toHaveCount(1); | ||
|
|
||
| const stackOne = page.locator("#toast-stack-one"); | ||
| const stackTwo = page.locator("#toast-stack-two"); | ||
| await expect(stackOne).toBeHidden(); | ||
| await page.getByRole("link", { name: "Show queued notifications" }).click(); | ||
| await expect(stackOne).toBeVisible(); | ||
| await expect(stackTwo).toBeVisible(); | ||
| const stackContainer = stackOne.locator("xpath=.."); | ||
| await expect(stackContainer).toHaveAttribute( | ||
| "data-sqlpage-toast-position", | ||
| "top-end", | ||
| ); | ||
| expect( | ||
| await stackTwo | ||
| .locator("xpath=..") | ||
| .getAttribute("data-sqlpage-toast-position"), | ||
| ).toBe("top-end"); | ||
| const firstBox = await stackOne.boundingBox(); | ||
| const secondBox = await stackTwo.boundingBox(); | ||
| expect(firstBox).not.toBeNull(); | ||
| expect(secondBox).not.toBeNull(); | ||
| expect(secondBox?.y).toBeGreaterThanOrEqual( | ||
| (firstBox?.y ?? 0) + (firstBox?.height ?? 0), | ||
| ); | ||
|
|
||
| const temporary = page.locator("#toast-short"); | ||
| await expect(temporary).toBeVisible(); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
The Useful? React with 👍 / 👎. |
||
| await expect(temporary).toBeHidden({ timeout: 5000 }); | ||
| await expect(stackOne).toBeVisible(); | ||
|
|
||
| const dismissible = page.locator("#toast-dismissible"); | ||
| await page.getByRole("link", { name: "Show dismissible error" }).click(); | ||
| await expect( | ||
| dismissible.getByRole("button", { name: "Close notification" }), | ||
| ).toBeVisible(); | ||
| await dismissible.getByRole("button", { name: "Close notification" }).click(); | ||
| await expect(dismissible).toBeHidden(); | ||
| await page.getByRole("link", { name: "Show dismissible error" }).click(); | ||
| await expect(dismissible).toBeVisible(); | ||
| await dismissible.getByRole("button", { name: "Close notification" }).click(); | ||
| await page.getByRole("link", { name: "Show non-dismissible status" }).click(); | ||
| await expect( | ||
| page.locator("#toast-nondismissible").getByRole("button"), | ||
| ).toHaveCount(0); | ||
|
|
||
| await page.getByRole("link", { name: "Show rich notifications" }).click(); | ||
| await expect(page.locator("#toast-markdown strong")).toHaveText("2.0"); | ||
| await expect(page.locator("#toast-markdown a")).toHaveAttribute( | ||
| "href", | ||
| "https://example.com/releases", | ||
| ); | ||
| await expect(page.locator("#toast-plain strong")).toHaveCount(0); | ||
| await expect(page.locator("#toast-plain")).toContainText( | ||
| "<strong>Plain text stays escaped</strong>", | ||
| ); | ||
|
|
||
| const bottomContainer = page.locator( | ||
| '[data-sqlpage-toast-position="bottom-center"]', | ||
| ); | ||
| await page.getByRole("link", { name: "Show bottom notification" }).click(); | ||
| await expect(page.locator("#toast-bottom-center")).toBeVisible(); | ||
| await expect(bottomContainer).toHaveClass(/\bbottom-0\b/); | ||
| await expect(bottomContainer).toHaveClass(/\bstart-50\b/); | ||
| await expect(bottomContainer).toHaveClass(/\btranslate-middle-x\b/); | ||
| }); | ||
|
|
||
| test("form example", async ({ page }) => { | ||
| await page.goto(`${BASE}/examples/multistep-form`); | ||
| // Single selection matching the value or label | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| SELECT 'toast' AS component, 'Default toast' AS title, 'It works !' AS description; | ||
| SELECT 'toast' AS component, 'Persistent' AS title, 'It works !' AS description, 0 AS duration, 1 AS dismissible; | ||
| SELECT 'toast' AS component, 'No close control' AS title, 'It works !' AS description, 0 AS dismissible; | ||
| SELECT 'toast' AS component, 'Colored' AS title, 'It works !' AS description, 'check' AS icon, 'green' AS color; | ||
| SELECT 'toast' AS component, '<strong>Escaped</strong> It works !' AS description; | ||
| SELECT 'toast' AS component, '**Markdown** [link](https://example.com) It works !' AS description_md; | ||
| SELECT 'toast' AS component, 'Alternate position' AS title, 'It works !' AS description, 'bottom-center' AS position; | ||
| SELECT 'toast' AS component, 'Hash-triggered' AS title, 'It works !' AS description, 'notice' AS id, 'notice' AS trigger; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When a trigger contains spaces or non-ASCII characters, such as
trigger = 'café'with a link to#café, browsers exposewindow.location.hashin percent-encoded form (#caf%C3%A9) whiledata-toast-triggerremains decoded. This comparison therefore never matches and the documented fragment-triggered toast does not open; normalize both sides to the same encoded or decoded representation.Useful? React with 👍 / 👎.