Skip to content

Commit

Permalink
Merge pull request #112 from FrameMuse/86-if-the-same-window-is-open-…
Browse files Browse the repository at this point in the history
…it-duplicates

Fixed when opening the same modals in a row isn't filtered
  • Loading branch information
FrameMuse authored Apr 17, 2023
2 parents fecbf32 + e7ff283 commit 558ad48
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/ModalContainer.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,20 @@ describe("ModalContainer", () => {
expect(modalContainerElement).not.toEqual(savedElement)
})

it("should render the correct component when open the same window and closed", () => {
act(() => {
Modal.open(PopupExample, { test: "2" })
Modal.open(PopupExample, { test: "2" })
})

act(() => {
modalContainerElement?.querySelector("button")?.click()
})

expect(modalContainerElement?.className).toBe("modal")
expect(modalContainerElement).toMatchSnapshot()
})

it("should render the correct component when closed (not weak)", () => {
act(() => {
Modal.open(PopupExample)
Expand Down
26 changes: 26 additions & 0 deletions src/__tests__/__snapshots__/ModalContainer.spec.tsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,32 @@ exports[`ModalContainer should render the correct component when open multiple a
</div>
`;

exports[`ModalContainer should render the correct component when open the same window and closed 1`] = `
<div
aria-hidden="true"
aria-modal="true"
class="modal"
>
<div
class="modal__container"
>
<div>
<span
id="test"
>
2
</span>
<h1>
Popup Example
</h1>
<button>
Close
</button>
</div>
</div>
</div>
`;

exports[`ModalContainer should render the correct component when replaced (closed) 1`] = `
<div
aria-hidden="true"
Expand Down
8 changes: 8 additions & 0 deletions src/controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ export class ModalController {
* - Controls whether the order of windows.
*/
private add(modalWindow: ModalWindow) {
// Skip adding to queue if the window is already in the beginning of the queue.
const lastWindow = [...this.windows].at(-1)
if (lastWindow?.id === modalWindow.id) {
this.isOpen = true
this.events.emit("update")
return
}

// If there are temporary modal windows, clear.
if (this.isOpen === false && this.windows.size > 0) {
this.windows.clear()
Expand Down

0 comments on commit 558ad48

Please sign in to comment.