-
-
Notifications
You must be signed in to change notification settings - Fork 317
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Set up ModalMultiplexer and ModalVisibilityStore
- Loading branch information
1 parent
8e022b3
commit 279aaa9
Showing
6 changed files
with
137 additions
and
44 deletions.
There are no files selected for viewing
This file contains 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 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
28 changes: 28 additions & 0 deletions
28
mathesar_ui/src/component-library/modal/ModalMultiplexer.ts
This file contains 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,28 @@ | ||
import { writable } from 'svelte/store'; | ||
import { ModalVisibilityStore } from './ModalVisibilityStore'; | ||
|
||
/** | ||
* Opens/closes modals, ensuring that only one modal is open at a time. | ||
*/ | ||
export class ModalMultiplexer { | ||
openModalId = writable<number | undefined>(undefined); | ||
|
||
private maxId = 0; | ||
|
||
private getId(): number { | ||
this.maxId += 1; | ||
return this.maxId; | ||
} | ||
|
||
open(modalId: number): void { | ||
this.openModalId.set(modalId); | ||
} | ||
|
||
close(): void { | ||
this.openModalId.set(undefined); | ||
} | ||
|
||
createVisibilityStore(): ModalVisibilityStore { | ||
return new ModalVisibilityStore({ id: this.getId(), multiplexer: this }); | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
mathesar_ui/src/component-library/modal/ModalVisibilityStore.ts
This file contains 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,65 @@ | ||
import type { | ||
Subscriber, Unsubscriber, Writable, Updater, | ||
} from 'svelte/store'; | ||
import type { ModalMultiplexer } from './ModalMultiplexer'; | ||
|
||
/** | ||
* This store acts somewhat like a radio button interface to the | ||
* ModalMultiplexer. You can read from this store to determine whether a | ||
* specific modal is open. And when you write to this store, that write will get | ||
* passed up to the ModalMultiplexer which ultimately sets the value, ensuring | ||
* that only one modal is open at a time. | ||
*/ | ||
export class ModalVisibilityStore implements Writable<boolean> { | ||
id: number; | ||
|
||
multiplexer: ModalMultiplexer; | ||
|
||
constructor({ | ||
id, | ||
multiplexer, | ||
}: { | ||
id: number, | ||
multiplexer: ModalMultiplexer, | ||
}) { | ||
this.id = id; | ||
this.multiplexer = multiplexer; | ||
} | ||
|
||
subscribe(subscription: Subscriber<boolean>): Unsubscriber { | ||
return this.multiplexer.openModalId.subscribe((openModalId) => { | ||
subscription(openModalId === this.id); | ||
}); | ||
} | ||
|
||
update(updater: Updater<boolean>): void { | ||
this.multiplexer.openModalId.update((openModalId) => { | ||
const isCurrentlyVisible = this.id === openModalId; | ||
const shouldBecomeVisible = updater(isCurrentlyVisible); | ||
if (shouldBecomeVisible) { | ||
return this.id; | ||
} | ||
return undefined; | ||
}); | ||
} | ||
|
||
set(isVisible: boolean): void { | ||
if (isVisible) { | ||
this.multiplexer.open(this.id); | ||
} else { | ||
this.multiplexer.close(); | ||
} | ||
} | ||
|
||
open(): void { | ||
this.set(true); | ||
} | ||
|
||
close(): void { | ||
this.set(false); | ||
} | ||
|
||
toggle(): void { | ||
this.update((isVisible) => !isVisible); | ||
} | ||
} |
This file contains 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 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 @@ | ||
export type ModalCloseAction = 'button' | 'esc' | 'overlay'; |