-
Notifications
You must be signed in to change notification settings - Fork 36
feat(evo-marko): alert & confirm dialog #622
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
Open
LuLaValva
wants to merge
5
commits into
main
Choose a base branch
from
alert-confirm-dialog
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
926eacd
feat(evo-marko): alert & confirm dialog
LuLaValva af446dd
chore: add changeset
LuLaValva c92779d
Apply suggestions from code review
LuLaValva 67c5437
fix(evo-marko): confirm & alert dialog comments
LuLaValva 23381d1
fix(evo-confirm-dialog): onCancel triggers from cancel button
LuLaValva 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| "@evo-web/marko": patch | ||
| --- | ||
|
|
||
| Confirm & Alert Dialog |
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,18 @@ | ||
| <h1 style='display: flex; justify-content: space-between; align-items: center;'> | ||
| <span> | ||
| evo-alert-dialog | ||
| </span> | ||
| <span style='font-weight: normal; font-size: medium; margin-bottom: -15px;'> | ||
| DS vBETA | ||
| </span> | ||
| </h1> | ||
|
|
||
| An alert dialog that forces the user to acknowledge a message before continuing. The dialog can only be dismissed by clicking the confirm button -- Escape and backdrop clicks are blocked. | ||
|
|
||
| Uses a native `<dialog>` element with `role="alertdialog"` and `closedby="none"`. | ||
|
|
||
| ## Examples and Documentation | ||
|
|
||
| - [Storybook](https://ebay.github.io/evo-web/evo-marko/?path=/story/navigation-disclosure-evo-alert-dialog) | ||
| - [Storybook Docs](https://ebay.github.io/evo-web/evo-marko/?path=/docs/navigation-disclosure-evo-alert-dialog) | ||
| - [Code Examples](https://github.com/eBay/evo-web/tree/main/packages/evo-marko/src/tags/evo-alert-dialog/examples) |
58 changes: 58 additions & 0 deletions
58
packages/evo-marko/src/tags/evo-alert-dialog/alert-dialog.stories.ts
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,58 @@ | ||
| import { buildExtensionTemplate } from "../../common/storybook/utils"; | ||
| import { type Meta } from "@storybook/marko"; | ||
| import Readme from "./README.md"; | ||
| import Component, { type Input } from "./index.marko"; | ||
| import DefaultTemplate from "./examples/default.marko"; | ||
| import DefaultCode from "./examples/default.marko?raw"; | ||
|
|
||
| export default { | ||
| title: "navigation & disclosure/evo-alert-dialog", | ||
| component: Component, | ||
| parameters: { | ||
| docs: { | ||
| description: { | ||
| component: Readme, | ||
| }, | ||
| }, | ||
| }, | ||
|
|
||
| argTypes: { | ||
| open: { | ||
| type: "boolean", | ||
| controllable: true, | ||
| description: "Whether the alert dialog is open", | ||
| table: { defaultValue: { summary: "false" } }, | ||
| }, | ||
| header: { | ||
| description: | ||
| "The header content rendered inside the dialog title (required)", | ||
| "@": { | ||
| as: { | ||
| type: "string", | ||
| description: | ||
| "The heading element to use for the title. Defaults to `h2`", | ||
| }, | ||
| ["<h2> attributes" as any]: { | ||
| description: | ||
| "All attributes and event handlers from the heading element will be passed through", | ||
| }, | ||
| }, | ||
| }, | ||
| confirm: { | ||
| description: | ||
| "The confirm/acknowledge button (required). Render body is the button label text", | ||
| "@": { | ||
| ["<button> attributes" as any]: { | ||
| description: | ||
| "All attributes and event handlers from [the native HTML `<button>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/button) will be passed through", | ||
| }, | ||
| }, | ||
| }, | ||
| ["<dialog> attributes" as any]: { | ||
| description: | ||
| "All attributes and event handlers from [the native HTML `<dialog>` tag](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Elements/dialog) will be passed through", | ||
| }, | ||
| }, | ||
| } satisfies Meta<Input>; | ||
|
|
||
| export const Default = buildExtensionTemplate(DefaultTemplate, DefaultCode); |
14 changes: 14 additions & 0 deletions
14
packages/evo-marko/src/tags/evo-alert-dialog/examples/default.marko
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,14 @@ | ||
| import { type Input as AlertDialogInput } from "<evo-alert-dialog>"; | ||
| export interface Input extends AlertDialogInput {} | ||
|
|
||
| <let/open:=input.open> | ||
|
|
||
| <evo-button onClick() { open = true; }> | ||
| Open Alert Dialog | ||
| </evo-button> | ||
|
|
||
| <evo-alert-dialog ...input open:=open> | ||
| <@header>Alert!</@header> | ||
| <@confirm>OK</@confirm> | ||
| <p>You must acknowledge this alert to continue.</p> | ||
| </evo-alert-dialog> |
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,79 @@ | ||
| import { type Input as ButtonInput } from "<evo-button>"; | ||
|
|
||
| export interface Input extends Omit<Marko.HTML.Dialog, "open" | "closedby" | "role" | "aria-labelledby"> { | ||
| open?: boolean; | ||
| openChange?: (o: boolean) => void; | ||
| header: Marko.AttrTag<Marko.HTML.H2 & { as?: string }>; | ||
| confirm: Marko.AttrTag<ButtonInput>; | ||
| } | ||
|
|
||
| <const/{ | ||
| open: inputOpen, | ||
| openChange, | ||
| class: inputClass, | ||
| header, | ||
| confirm, | ||
| content, | ||
| onCancel, | ||
| onAnimationEnd, | ||
| ...htmlInput | ||
| }=input> | ||
|
|
||
| <let/open:=input.open> | ||
| <id/headerId=header.id> | ||
|
|
||
| <script> | ||
| if (open && !$dialog().open) { | ||
| $dialog().showModal(); | ||
| } | ||
| </script> | ||
|
|
||
| <dialog/$dialog | ||
| ...htmlInput | ||
| open=null // tell Marko it's not a part of the spread because the browser does DOM manipulation | ||
| role="alertdialog" | ||
| closedby="none" | ||
| aria-labelledby=headerId | ||
| class=[ | ||
| "dialog", | ||
| "dialog--narrow", | ||
| !open && "dialog--close", | ||
| inputClass, | ||
| ] | ||
| onCancel(e, el) { | ||
| e.preventDefault(); | ||
| onCancel && onCancel(e, el); | ||
| } | ||
| onAnimationEnd(e, el) { | ||
| if (!open) { | ||
| el.close(); | ||
| } | ||
| onAnimationEnd && onAnimationEnd(e, el); | ||
| } | ||
| > | ||
| <div class="dialog__header"> | ||
| <const/{ as: headerAs = "h2", ...headerInput }=header> | ||
| <${headerAs} | ||
| ...headerInput | ||
| id=headerId | ||
| class=["dialog__title", headerInput.class] | ||
| /> | ||
| </div> | ||
| <id/mainId> | ||
| <div id=mainId class="dialog__main"> | ||
| <${content}/> | ||
| </div> | ||
| <div class="dialog__footer"> | ||
| <const/{ onClick: confirmOnClick, ...confirmInput }=confirm> | ||
| <evo-button | ||
| ...confirmInput | ||
| priority="primary" | ||
| autofocus | ||
| aria-describedby=mainId | ||
| onClick(e, el) { | ||
| open = false; | ||
| confirmOnClick && confirmOnClick(e, el); | ||
| } | ||
| /> | ||
| </div> | ||
| </dialog> | ||
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 @@ | ||
| import "@ebay/skin/dialog"; |
137 changes: 137 additions & 0 deletions
137
packages/evo-marko/src/tags/evo-alert-dialog/test/test.browser.ts
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,137 @@ | ||
| import { | ||
| afterAll, | ||
| afterEach, | ||
| beforeAll, | ||
| beforeEach, | ||
| describe, | ||
| it, | ||
| expect, | ||
| } from "vitest"; | ||
| import { render, fireEvent, waitFor, cleanup } from "@marko/testing-library"; | ||
LuLaValva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| import { userEvent } from "vitest/browser"; | ||
| import { composeStories } from "@storybook/marko"; | ||
| import { fastAnimations } from "../../../common/test-utils/index"; | ||
| import * as stories from "../alert-dialog.stories"; | ||
|
|
||
| const { Default } = composeStories(stories); | ||
|
|
||
| beforeAll(() => fastAnimations.start()); | ||
| afterAll(() => fastAnimations.stop()); | ||
| afterEach(cleanup); | ||
|
|
||
| let component: Awaited<ReturnType<typeof render>>; | ||
|
|
||
| describe("evo-alert-dialog", () => { | ||
| describe("given the dialog is in the default (closed) state", () => { | ||
| beforeEach(async () => { | ||
| component = await render(Default); | ||
| }); | ||
|
|
||
| it("should render with a dialog element", () => { | ||
| expect(component.container.querySelector("dialog")).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should have the dialog class", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog")).toBe(true); | ||
| }); | ||
|
|
||
| it("should have dialog--close class when not open", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog--close")).toBe(true); | ||
| }); | ||
|
|
||
| it("should have dialog--narrow class", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog--narrow")).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("given the dialog is in the open state", () => { | ||
| beforeEach(async () => { | ||
| component = await render(Default, { open: true }); | ||
| }); | ||
|
|
||
| it("should render the dialog element", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should not have dialog--close class when open", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog")).toBe(true); | ||
| expect(dialog?.classList.contains("dialog--close")).toBe(false); | ||
| }); | ||
|
|
||
| it("should have role alertdialog", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.getAttribute("role")).toBe("alertdialog"); | ||
| }); | ||
|
|
||
| it("should have aria-modal set to true", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.getAttribute("aria-modal")).toBe("true"); | ||
| }); | ||
|
|
||
| it("should have closedby set to none", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.getAttribute("closedby")).toBe("none"); | ||
| }); | ||
|
|
||
| it("should render header with h2 by default", () => { | ||
| const title = component.container.querySelector(".dialog__title"); | ||
| expect(title).toBeTruthy(); | ||
| expect(title?.tagName.toLowerCase()).toBe("h2"); | ||
| }); | ||
|
|
||
| it("should link dialog to header via aria-labelledby", () => { | ||
| const dialog = component.container.querySelector("dialog"); | ||
| const title = component.container.querySelector(".dialog__title"); | ||
| const titleId = title?.id; | ||
| expect(titleId).toBeTruthy(); | ||
| expect(dialog?.getAttribute("aria-labelledby")).toBe(titleId); | ||
| }); | ||
|
|
||
| it("should render the confirm button", () => { | ||
| const button = component.getByRole("button", { name: "OK" }); | ||
| expect(button).toBeTruthy(); | ||
| }); | ||
|
|
||
| it("should have aria-describedby on the confirm button referencing main content", () => { | ||
| const button = component.getByRole("button", { name: "OK" }); | ||
| const describedBy = button.getAttribute("aria-describedby"); | ||
| expect(describedBy).toBeTruthy(); | ||
| const mainEl = document.getElementById(describedBy!); | ||
| expect(mainEl).toBeTruthy(); | ||
| expect(mainEl?.textContent).toContain( | ||
| "You must acknowledge this alert to continue.", | ||
| ); | ||
| }); | ||
|
|
||
| it("should render main content area", () => { | ||
| const main = component.container.querySelector(".dialog__main"); | ||
| expect(main).toBeTruthy(); | ||
| }); | ||
|
|
||
| describe("when the confirm button is clicked", () => { | ||
| it("should close the dialog", async () => { | ||
| await fireEvent.click( | ||
| component.getByRole("button", { name: "OK" }), | ||
| ); | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog--close")).toBe(true); | ||
| }); | ||
| }); | ||
|
|
||
| describe("when Escape key is pressed", () => { | ||
| it("should not close the dialog", async () => { | ||
| const user = userEvent.setup(); | ||
| const button = component.getByRole("button", { name: "OK" }); | ||
| button.focus(); | ||
| await user.keyboard("{Escape}"); | ||
| const dialog = component.container.querySelector("dialog"); | ||
| expect(dialog?.classList.contains("dialog--close")).toBe(false); | ||
| }); | ||
| }); | ||
| }); | ||
| }); | ||
16 changes: 16 additions & 0 deletions
16
packages/evo-marko/src/tags/evo-alert-dialog/test/test.server.ts
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,16 @@ | ||
| import { describe, it } from "vitest"; | ||
| import { composeStories } from "@storybook/marko"; | ||
| import { snapshotHTML } from "../../../common/test-utils/snapshots"; | ||
| import * as stories from "../alert-dialog.stories"; | ||
|
|
||
| const { Default } = composeStories(stories); | ||
|
|
||
| describe("evo-alert-dialog SSR", () => { | ||
| it("renders default (closed)", async () => { | ||
| await snapshotHTML(Default); | ||
| }); | ||
|
|
||
| it("renders in open state", async () => { | ||
| await snapshotHTML(Default, { open: true }); | ||
| }); | ||
| }); |
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,18 @@ | ||
| <h1 style='display: flex; justify-content: space-between; align-items: center;'> | ||
| <span> | ||
| evo-confirm-dialog | ||
| </span> | ||
| <span style='font-weight: normal; font-size: medium; margin-bottom: -15px;'> | ||
| DS vBETA | ||
| </span> | ||
| </h1> | ||
|
|
||
| A confirm dialog that forces the user to make a choice to either confirm or reject. The dialog can only be dismissed by clicking one of the two buttons. Pressing Escape triggers the reject action. | ||
|
|
||
| Uses a native `<dialog>` element with `role="alertdialog"` and `closedby="none"`. | ||
LuLaValva marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ## Examples and Documentation | ||
|
|
||
| - [Storybook](https://ebay.github.io/evo-web/evo-marko/?path=/story/navigation-disclosure-evo-confirm-dialog) | ||
| - [Storybook Docs](https://ebay.github.io/evo-web/evo-marko/?path=/docs/navigation-disclosure-evo-confirm-dialog) | ||
| - [Code Examples](https://github.com/eBay/evo-web/tree/main/packages/evo-marko/src/tags/evo-confirm-dialog/examples) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.