Skip to content
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

feat: modal overlay close and hide #31

Merged
merged 1 commit into from
Feb 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions workspaces/e2e/tests/modal-overlay/modal-overlay.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="/tests/reset.css" />
<link rel="stylesheet" href="/node_modules/@flows/js/css.min/flows.css" />
</head>
<body>
<button style="cursor: pointer">Click me</button>

<script type="module" src="./modal-overlay.ts"></script>
</body>
</html>
21 changes: 21 additions & 0 deletions workspaces/e2e/tests/modal-overlay/modal-overlay.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { expect, test } from "@playwright/test";

test("should show overlay without close on click by default", async ({ page }) => {
await page.goto("/modal-overlay/modal-overlay.html");
await expect(page.locator(".flows-modal")).toBeVisible();
await page.locator(".flows-modal-overlay").click({ position: { x: 100, y: 100 } });
await expect(page.locator(".flows-modal")).toBeVisible();
});

test("should close overlay on click", async ({ page }) => {
await page.goto("/modal-overlay/modal-overlay.html?closeOnOverlayClick=true");
await expect(page.locator(".flows-modal")).toBeVisible();
await page.locator(".flows-modal-overlay").click({ position: { x: 100, y: 100 } });
await expect(page.locator(".flows-modal")).toBeHidden();
});

test("should hide overlay", async ({ page }) => {
await page.goto("/modal-overlay/modal-overlay.html?hideOverlay=true");
await expect(page.locator(".flows-modal")).toBeVisible();
await expect(page.locator(".flows-modal-overlay")).toBeHidden();
});
24 changes: 24 additions & 0 deletions workspaces/e2e/tests/modal-overlay/modal-overlay.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { init } from "@flows/js/core";

const closeOnOverlayClick =
new URLSearchParams(window.location.search).get("closeOnOverlayClick") === "true";
const hideOverlay = new URLSearchParams(window.location.search).get("hideOverlay") === "true";

init({
flows: [
{
id: "flow",
location: "/",
steps: [
{
title: "Hello",
body: "Lorem ipsum dolor sit amet, consectetur adipiscing elit.",
hideOverlay,
closeOnOverlayClick,
},
],
},
],
});

document.querySelector("button")?.addEventListener("click", () => console.log("Hello!"));
4 changes: 2 additions & 2 deletions workspaces/e2e/tests/tooltip-overlay/tooltip-overlay.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ <h1 style="color: red">Tooltip overlay</h1>
<div>
<button class="another-btn">Another button</button>
</div>
<div style="margin: 40px; display: inline-block; padding: 20px" class="target">
<button>Ahoj</button>
<div style="margin: 40px; display: inline-block" class="target">
<button>Click me</button>
</div>

<script type="module" src="./tooltip-overlay.ts"></script>
Expand Down
5 changes: 5 additions & 0 deletions workspaces/e2e/tests/tooltip-overlay/tooltip-overlay.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ test("should close overlay on click", async ({ page }) => {
await page.locator(".flows-tooltip-overlay-click-layer").click();
await expect(page.locator(".flows-tooltip")).not.toBeVisible();
});

test("should match screenshot", async ({ page }) => {
await page.goto("/tooltip-overlay/tooltip-overlay.html");
await expect(page).toHaveScreenshot({ scale: "css" });
});
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 7 additions & 5 deletions workspaces/js/css/template.css
Original file line number Diff line number Diff line change
Expand Up @@ -186,19 +186,21 @@
/* Modal styles */

.flows-modal-overlay {
position: fixed;
inset: 0;
background-color: var(--flows-modal-overlayBackground);
}
.flows-modal-wrapper {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
inset: 0;
display: grid;
place-items: center;
padding: var(--flows-modal-padding);
overflow: auto;
pointer-events: none;
}

.flows-modal {
pointer-events: auto;
background-color: var(--flows-bg-default);
border: var(--flows-border);
color: var(--flows-fg-default);
Expand Down
11 changes: 9 additions & 2 deletions workspaces/js/src/core/render.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ const renderTooltip = ({

const overlayClickLayer = (
<div
className={`flows-tooltip-overlay-click-layer ${step.closeOnOverlayClick ? "flows-overlay-cancel" : ""}`}
className={`flows-tooltip-overlay-click-layer${step.closeOnOverlayClick ? " flows-overlay-cancel" : ""}`}
/>
);
root.appendChild(overlayClickLayer);
Expand Down Expand Up @@ -252,7 +252,7 @@ const renderModal = ({
state: FlowState;
}): void => {
const modal = (
<div className="flows-modal-overlay">
<div className="flows-modal-wrapper">
<div className="flows-modal">
{getStepHeader({ step })}
{step.body && (
Expand All @@ -262,6 +262,13 @@ const renderModal = ({
</div>
</div>
);
if (!step.hideOverlay) {
root.appendChild(
<div
className={`flows-modal-overlay${step.closeOnOverlayClick ? " flows-overlay-cancel" : ""}`}
/>,
);
}
root.appendChild(modal);
};

Expand Down
2 changes: 2 additions & 0 deletions workspaces/js/src/core/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ const TooltipStepStruct: Describe<FlowTooltipStep> = object({
const ModalStepStruct: Describe<FlowModalStep> = object({
title: string(),
body: optional(string()),
hideOverlay: optional(boolean()),
closeOnOverlayClick: optional(boolean()),
stepId: optional(string()),
hideClose: optional(boolean()),
hidePrev: optional(boolean()),
Expand Down
10 changes: 10 additions & 0 deletions workspaces/js/src/types/flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,16 @@ export interface FlowModalStep extends CommonStepProps {
* Body of the modal. Supports HTML.
*/
body?: string;
/**
* Hides modal overlay backdrop and makes rest .
* @defaultValue `false`
*/
hideOverlay?: boolean;
/**
* Cancel the flow when the overlay is clicked.
* @defaultValue `false`
*/
closeOnOverlayClick?: boolean;
/**
* Hide the close button. Without the close button the user will not be able to close the tooltip.
*/
Expand Down
Loading