diff --git a/CHANGELOG.md b/CHANGELOG.md index c71095307..02fcd3518 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -36,6 +36,8 @@ - Keyboard users can now Tab to a dismissible alert’s Close button and press Enter or Space to dismiss it. ([#1612](https://github.com/sqlpage/SQLPage/issues/1612)) - The search field of the `shell` now uses `search_placeholder` as its accessible name, so screen readers announce the customized text instead of the fixed English "Search". ([#1628](https://github.com/sqlpage/SQLPage/issues/1628)) +- Modals can set a header `color`, and `visible` opens that dialog once when the page loads. The page address stays unchanged, and closing it does not open it again when later content loads. + ## v0.48.0 (2026-11-12) - Updated Rust and browser dependencies, including ApexCharts 8.8.1, Tabler 1.7.4, Tabler icons 3.51, and Tom Select 2.7. Chart series names and labels now display HTML as text, fixing upstream script injection in legends and tooltips. diff --git a/examples/official-site/reference/components/modal/05-colored-header.sql b/examples/official-site/reference/components/modal/05-colored-header.sql new file mode 100644 index 000000000..639a7a7d0 --- /dev/null +++ b/examples/official-site/reference/components/modal/05-colored-header.sql @@ -0,0 +1,20 @@ +/* +A modal with a colored header + +A modal with a colored header, opened by a button. The header uses the color and a contrasting foreground; the body stays in the normal text color. +*/ +select + 'modal' as "component", + 'colored_modal' as "id", + 'Saved' as "title", + 'green' as "color", + 'Close' as "close"; + +select + 'The header uses the chosen color.' as "contents_md"; + +select 'button' as "component"; + +select + 'Open a colored modal' as "title", + '#colored_modal' as "link"; diff --git a/frontend/src/sqlpage.ts b/frontend/src/sqlpage.ts index e616f050b..c05c82036 100644 --- a/frontend/src/sqlpage.ts +++ b/frontend/src/sqlpage.ts @@ -533,12 +533,38 @@ function sqlpage_modal(root: InitRoot) { // .page instead of the viewport. The modal then scrolls with the page // content and ends up behind its own backdrop, so its buttons cannot be // clicked. Moving modals to keeps them viewport-fixed. - for (const modal of select_all(root, "body .page .modal", HTMLElement)) { - document.body.appendChild(modal); - // The modal leaves the original root; announce its subtree separately so - // every initializer, including later-loaded bundles, can still see it. + const modals = select_all(root, "body .page .modal", HTMLElement); + for (const modal of modals) document.body.appendChild(modal); + // The modals left the original root; announce each subtree separately so + // every initializer, including later-loaded bundles, can still see it. The + // event runs this function again, so move them all first: a dialog opened + // below must not be moved afterwards. + for (const modal of modals) { modal.dispatchEvent(new CustomEvent("fragment-loaded", { bubbles: true })); } + + // `visible` opens the last such dialog once. A hash that already names a + // different modal keeps that dialog. Clearing every marker stops a later + // pass from opening an earlier dialog or reopening one the user closed. + const visible_modals = document.querySelectorAll( + ".modal[data-modal-visible]", + ); + const modal = visible_modals[visible_modals.length - 1]; + if (!modal) return; + const hash = window.location.hash.substring(1); + const hashed = hash ? document.getElementById(hash) : null; + const hash_names_other_modal = + hashed?.classList.contains("modal") === true && hashed !== modal; + if (!hash_names_other_modal) { + const Modal = page_bootstrap().Modal; + const bootstrap_modal = Modal.getOrCreateInstance(modal); + if (!(bootstrap_modal instanceof Modal)) + throw new Error("Invalid modal instance"); + bootstrap_modal.show(); + } + for (const visible of visible_modals) { + visible.removeAttribute("data-modal-visible"); + } } add_init_fn(sqlpage_table); diff --git a/sqlpage/templates/modal.handlebars b/sqlpage/templates/modal.handlebars index 1c559beac..d1875e55a 100644 --- a/sqlpage/templates/modal.handlebars +++ b/sqlpage/templates/modal.handlebars @@ -11,6 +11,7 @@ Modals are closed by default. Open one from a button or link targeting its ID. - **class** (TEXT): Class attribute added to the container in HTML. It can be used to apply custom styling to this item through css. - **close** (TEXT): The text to display in the Close button. - **close_label** (TEXT): The same as `close`. Use it on databases where `close` is a reserved word, such as SQL Server. + - **color** (COLOR): Optional Tabler color for the modal header. The header uses that background and the same contrasting foreground as a colored toast, with white mapped to the light foreground. The body and footer stay as they are. - **embed** (TEXT): Embed remote content in an iframe. - **embed_mode** (TEXT): Use "iframe" to display embedded content within an iframe. - **height** (INTEGER): Height of the modal content, in pixels. Overflow content scrolls. @@ -19,6 +20,7 @@ Modals are closed by default. Open one from a button or link targeting its ID. - **scrollable** (BOOLEAN): Create a scrollable modal that allows scroll the modal body. - **small** (BOOLEAN): Indicates that the modal box has a reduced width. - **style** (TEXT): Applies CSS styles to the embedded content. + - **visible** (BOOLEAN): Opens this modal once on load when true, without an inline script and without changing the URL hash. If several modals set this, only the last one opens. A URL hash that names a different modal takes precedence. ## Row-level parameters @@ -29,11 +31,11 @@ Modals are closed by default. Open one from a button or link targeting its ID. id="{{id}}" tabindex="-1" aria-hidden="false" - aria-labelledby="{{id}}-title"> + aria-labelledby="{{id}}-title"{{~#if visible}} data-modal-visible{{/if}}>