-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add modal URL router (#modal=name&tab=key) #3924
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,159 @@ | ||
| /** | ||
| * ModalRouter — two-way sync between `#modal=<name>&tab=<key>&...` and modals. | ||
| * | ||
| * URL → modal: parse hash, find registered modal, call `modal.open(args)`. | ||
| * Modal → URL: when a router-managed modal opens, closes, or switches tabs, | ||
| * update the URL via `history.replaceState` (no history entries). | ||
| * | ||
| * Lobby modals (join/host) and matchmaking are intentionally not registered: | ||
| * they have their own URL state (path-based) or none at all. | ||
| */ | ||
|
|
||
| interface RegistryEntry { | ||
| /** Custom element tag, e.g. "store-modal". */ | ||
| tag: string; | ||
| /** | ||
| * Optional page-content element id (e.g. "page-item-store"). When set, the | ||
| * router calls `window.showPage(pageId)` for inline modals so the page-content | ||
| * container becomes visible. For popup-style modals, omit. | ||
| */ | ||
| pageId?: string; | ||
| } | ||
|
|
||
| /** Modals that the router can drive via the URL. */ | ||
| interface RoutableModal extends HTMLElement { | ||
| open(args?: Record<string, unknown>): void; | ||
| close(args?: Record<string, unknown>): void; | ||
| } | ||
|
|
||
| class ModalRouter { | ||
| private registry = new Map<string, RegistryEntry>(); | ||
| /** Name of the modal currently reflected in the URL, if any. */ | ||
| private currentName: string | null = null; | ||
| /** True while we're routing from the URL (suppress modal→URL sync). */ | ||
| private routingFromUrl = false; | ||
|
|
||
| register(name: string, entry: RegistryEntry): void { | ||
| this.registry.set(name, entry); | ||
| } | ||
|
|
||
| /** | ||
| * Parse `window.location.hash` for `#modal=<name>&...`. If present and | ||
| * registered, open the modal with the remaining keys as args. Returns true | ||
| * if the hash was a recognized modal route (the caller can skip other | ||
| * hash handlers). The open itself happens asynchronously after the custom | ||
| * element is upgraded. | ||
| */ | ||
| routeFromHash(): boolean { | ||
| const hash = window.location.hash; | ||
| if (!hash.startsWith("#")) return false; | ||
| const params = new URLSearchParams(hash.slice(1)); | ||
| const name = params.get("modal"); | ||
| if (!name) return false; | ||
|
|
||
| const entry = this.registry.get(name); | ||
| if (!entry) { | ||
| // Unknown modal — strip the hash silently. | ||
| this.replaceHash(""); | ||
| return true; | ||
| } | ||
|
|
||
| params.delete("modal"); | ||
| const args: Record<string, unknown> = {}; | ||
| params.forEach((value, key) => { | ||
| args[key] = value; | ||
| }); | ||
|
|
||
| void this.openRegistered(name, entry, args); | ||
| return true; | ||
| } | ||
|
|
||
| private async openRegistered( | ||
| name: string, | ||
| entry: RegistryEntry, | ||
| args: Record<string, unknown>, | ||
| ): Promise<void> { | ||
| // The custom element may not be upgraded yet (e.g. routed on initial load | ||
| // before its module has finished evaluating). Wait so el.open is defined. | ||
| await customElements.whenDefined(entry.tag); | ||
|
|
||
| this.routingFromUrl = true; | ||
| try { | ||
| this.currentName = name; | ||
| if (entry.pageId) { | ||
| // Inline modal: showPage reveals the page-content container and calls | ||
| // .open() on the inline modal element automatically. We then call | ||
| // .open(args) so the args reach onOpen. | ||
| window.showPage?.(entry.pageId); | ||
| } | ||
| const el = document.querySelector(entry.tag) as RoutableModal | null; | ||
| el?.open(args); | ||
| } finally { | ||
| this.routingFromUrl = false; | ||
| } | ||
| } | ||
|
|
||
| /** Called by BaseModal.open() when a router-managed modal opens. */ | ||
| syncOpened(name: string, args?: Record<string, unknown>): void { | ||
| if (this.routingFromUrl) return; // we're driving the modal from the URL; don't loop | ||
| if (!this.registry.has(name)) return; | ||
| this.currentName = name; | ||
| this.writeHash(name, args); | ||
| } | ||
|
|
||
| /** Called by BaseModal.close() when a router-managed modal closes. */ | ||
| syncClosed(name: string): void { | ||
| if (this.routingFromUrl) return; | ||
| if (this.currentName !== name) return; // not the active routed modal | ||
| this.currentName = null; | ||
| this.replaceHash(""); | ||
| } | ||
|
|
||
| /** Called by BaseModal.setActiveTab() when a router-managed modal switches tabs. */ | ||
| syncTab(name: string, tab: string): void { | ||
| if (this.routingFromUrl) return; | ||
| if (this.currentName !== name) return; | ||
| const params = this.currentHashParams(); | ||
| params.set("modal", name); | ||
| if (tab) { | ||
| params.set("tab", tab); | ||
| } else { | ||
| params.delete("tab"); | ||
| } | ||
| this.replaceHash("#" + params.toString()); | ||
| } | ||
|
|
||
| /** True if the current hash is `#modal=...`. */ | ||
| isHashRouted(): boolean { | ||
| const hash = window.location.hash; | ||
| if (!hash.startsWith("#")) return false; | ||
| return new URLSearchParams(hash.slice(1)).has("modal"); | ||
| } | ||
|
|
||
| private currentHashParams(): URLSearchParams { | ||
| const hash = window.location.hash; | ||
| if (!hash.startsWith("#")) return new URLSearchParams(); | ||
| return new URLSearchParams(hash.slice(1)); | ||
| } | ||
|
|
||
| private writeHash(name: string, args?: Record<string, unknown>): void { | ||
| const params = new URLSearchParams(); | ||
| params.set("modal", name); | ||
| if (args) { | ||
| for (const [key, value] of Object.entries(args)) { | ||
| if (key === "modal") continue; | ||
| if (value === undefined || value === null) continue; | ||
| if (typeof value === "object") continue; | ||
| params.set(key, String(value)); | ||
| } | ||
| } | ||
| this.replaceHash("#" + params.toString()); | ||
| } | ||
|
|
||
| private replaceHash(hash: string): void { | ||
| const url = window.location.pathname + window.location.search + hash; | ||
| history.replaceState(history.state, "", url); | ||
| } | ||
| } | ||
|
|
||
| export const modalRouter = new ModalRouter(); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Potential double-open fragility for inline modals.
For inline modals with
pageId, Line 73 callswindow.showPage(pageId), which (per BaseModal comments) "calls.open()on the inline modal element automatically." Then Line 76 explicitly callsel?.open(args)again.BaseModal handles this by checking
wasOpenand returning early at Line 174, butonOpen(args?)still runs twice—once with no args, once with args. This relies on BaseModal's specificwasOpenlogic and assumesonOpentolerates being called twice.Consider a more explicit contract:
showPageif it supports them, orshowPagereturn a reference and skip the secondopen()call, orRoutableModalinterfaceThis pattern is fragile if
showPageor BaseModal internals change.🤖 Prompt for AI Agents