Skip to content
Merged
12 changes: 12 additions & 0 deletions e2e/uiAccessibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,8 @@ const BOUNDARY_SELECTORS = [
".add-match-button",
".reload-button",
".sidebar-button",
".help-button",
".walkthrough__button",
".plant-preferences button",
".plant-owner-test-option",
".sort-button",
Expand Down Expand Up @@ -439,6 +441,16 @@ for (const theme of THEMES) {
await checkAccessibility(page);
});

test("Walkthrough open", async ({ page }) => {
await page.goto("");
await expect(page.locator(".plant-card-front")).toBeVisible();
await setTheme(page, theme);
await page.locator(".help-button").click();
await expect(page.getByRole("dialog")).toBeVisible();
// The grayed out page behind is not checked, only the walkthrough
await checkAccessibility(page, ".walkthrough");
});

test("Sidebar open, with the language and theme buttons reachable", async ({
page,
}) => {
Expand Down
65 changes: 65 additions & 0 deletions e2e/uiWalkthrough.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { test, expect } from "@playwright/test";

// The other UI tests start as users who have seen the walkthrough
// (playwright.config.ts). These start as new users, with empty storage.
test.use({ storageState: { cookies: [], origins: [] } });

test("the walkthrough opens for new users and not after it is closed", async ({
page,
}) => {
await page.goto("");
const dialog = page.getByRole("dialog");
await expect(dialog).toBeVisible();
await expect(page.locator(".walkthrough__progress")).toHaveText(
/1 (of|av) 7/,
);

await page.keyboard.press("Escape");
await expect(dialog).toBeHidden();

await page.reload();
await expect(page.locator(".plant-card-front")).toBeVisible();
await expect(dialog).toBeHidden();
});

test("the page behind is grayed out and can't be used while the walkthrough is open", async ({
page,
}) => {
await page.goto("");
await expect(page.getByRole("dialog")).toBeVisible();

const backdrop = await page
.locator(".walkthrough")
.evaluate((el) => getComputedStyle(el, "::backdrop").backgroundColor);
expect(backdrop).not.toBe("rgba(0, 0, 0, 0)");

// Tab never reaches the page behind. The browser may move focus to its own
// address bar after the last button, then the page reports body as focused.
for (let i = 0; i < 8; i++) {
await page.keyboard.press("Tab");
const focusOnPageBehind = await page.evaluate(() => {
const el = document.activeElement;
return !!el && el !== document.body && !el.closest(".walkthrough");
});
expect(focusOnPageBehind).toBe(false);
}
});

test("the help button opens the walkthrough at the current page, and Go to changes page", async ({
page,
}) => {
await page.goto("");
await page.keyboard.press("Escape");

await page.goto("#/explorePlants");
await page.locator(".help-button").click();
await expect(page.locator(".walkthrough__progress")).toHaveText(
/3 (of|av) 7/,
);

await page.keyboard.press("ArrowRight");
await page.locator(".walkthrough__go-to").click();

await expect(page.getByRole("dialog")).toBeHidden();
await expect(page).toHaveURL(/#\/myMatches$/);
});
59 changes: 59 additions & 0 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,29 @@ import Sidebar from "./components/sidebar/Sidebar";
import Header from "./components/header/Header";
import AppRoutes from "./AppRoutes.tsx";
import { ErrorMessageProvider } from "./context/ErrorMessageContext.tsx";
import Walkthrough from "./components/walkthrough/Walkthrough.tsx";
import { stepForPath } from "./components/walkthrough/walkthroughSteps.ts";

// Remembers that the user has seen the walkthrough, so it only opens by
// itself on the first visit
const WALKTHROUGH_SEEN_KEY = "walkthroughSeen";

function hasSeenWalkthrough() {
try {
return localStorage.getItem(WALKTHROUGH_SEEN_KEY) === "true";
} catch {
// Without storage the walkthrough would open on every page load
return true;
}
}

function markWalkthroughSeen() {
try {
localStorage.setItem(WALKTHROUGH_SEEN_KEY, "true");
} catch {
// The walkthrough still closes, it just isn't remembered
}
}

function App() {
const { t } = useTranslation();
Expand All @@ -26,6 +49,33 @@ function App() {
pageStartRef.current?.focus({ preventScroll: true });
}, [pathname]);

// The walkthrough opens by itself on the first visit, starting at the
// welcome step. The help button opens it at the step for the current page.
const [walkthrough, setWalkthrough] = useState(() => ({
isOpen: !hasSeenWalkthrough(),
startStep: 0,
}));
const helpButtonRef = useRef<HTMLButtonElement>(null);
const returnFocusToHelp = useRef(false);

const openWalkthrough = () => {
setWalkthrough({ isOpen: true, startStep: stepForPath(pathname) });
};

const closeWalkthrough = (returnFocus: boolean) => {
markWalkthroughSeen();
returnFocusToHelp.current = returnFocus;
setWalkthrough((current) => ({ ...current, isOpen: false }));
};

// Focus goes back to the help button when the walkthrough closes. Done in
// an effect, since the page can't get focus while the dialog is still open.
useEffect(() => {
if (walkthrough.isOpen || !returnFocusToHelp.current) return;
returnFocusToHelp.current = false;
helpButtonRef.current?.focus();
}, [walkthrough.isOpen]);

const sidebarRef = useRef<HTMLElement>(null);
const menuButtonRef = useRef<HTMLButtonElement>(null);

Expand Down Expand Up @@ -82,8 +132,17 @@ function App() {
menuButtonRef={menuButtonRef}
isSidebarOpen={isSidebarOpen}
onOpenSidebar={openSidebar}
onOpenWalkthrough={openWalkthrough}
helpButtonRef={helpButtonRef}
/>

{walkthrough.isOpen && (
<Walkthrough
startStep={walkthrough.startStep}
onClose={closeWalkthrough}
/>
)}

{/* tabIndex -1 lets the skip link focus it, without adding it to the tab order */}
<div
id="main-content"
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/errorMessage/ErrorMessage.css
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
border-radius: 8px;
background-color: var(--dark-pink);
color: var(--text-light);
font-family: var(--font-ui);
font-size: 1rem;
animation: error-message-in 0.2s ease-out;
}
Expand Down
1 change: 0 additions & 1 deletion frontend/src/components/filterSelect/FilterSelect.css
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,5 @@

.filter select:focus {
outline: none;
border-color: var(--dark-pink);
box-shadow: 0 0 0 2px var(--dark-pink);
}
50 changes: 49 additions & 1 deletion frontend/src/components/header/Header.css
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,40 @@
display: none;
}

/* Opens the walkthrough. Always in the top right corner of the header. */
.help-button {
position: absolute;
right: 30px;
display: flex;
align-items: center;
justify-content: center;
width: 48px;
height: 48px;
padding: 0;
border: 2px solid var(--dark-green);
border-radius: 50%;
background-color: var(--dark-green);
color: var(--text-on-green);
/* Georgia draws "?" curved and leaning, so the button uses a plain
sans-serif font to show it straight and centred */
font-family: Arial, Helvetica, sans-serif;
font-size: 26px;
font-weight: 700;
font-style: normal;
line-height: 1;
cursor: pointer;
transition: transform 0.2s ease;
}

.help-button:hover {
transform: scale(1.05);
}

.help-button:focus-visible {
outline: 2px solid var(--dark-pink);
outline-offset: 2px;
}

@media (max-width: 767px) {
.header {
height: 70px;
Expand All @@ -38,8 +72,15 @@
display: none;
}

/* Smaller on narrow screens, and kept clear of the menu button on the left
and the help button on the right. A very narrow screen wraps the title
instead of letting it go under the buttons. */
.header h1 {
font-size: 37px;
padding: 0 64px;
font-size: clamp(24px, 7vw, 32px);
line-height: 1.1;
text-align: center;
overflow-wrap: anywhere;
}

.menu-button {
Expand All @@ -52,4 +93,11 @@
cursor: pointer;
color: var(--text-dark);
}

.help-button {
right: 16px;
width: 40px;
height: 40px;
font-size: 22px;
}
}
17 changes: 17 additions & 0 deletions frontend/src/components/header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ type HeaderProps = {
isSidebarOpen: boolean;
onOpenSidebar: () => void;
menuButtonRef?: Ref<HTMLButtonElement>;
onOpenWalkthrough?: () => void;
helpButtonRef?: Ref<HTMLButtonElement>;
};

// Maps each route path to the translation key used for its title
Expand All @@ -23,6 +25,8 @@ export default function Header({
isSidebarOpen,
onOpenSidebar,
menuButtonRef,
onOpenWalkthrough,
helpButtonRef,
}: HeaderProps) {
const { t } = useTranslation();
const { pathname } = useLocation();
Expand All @@ -45,6 +49,19 @@ export default function Header({
</button>

<h1>{t(titleKey)}</h1>

{onOpenWalkthrough && (
<button
ref={helpButtonRef}
type="button"
className="help-button"
onClick={onOpenWalkthrough}
aria-label={t("walkthrough.open")}
aria-haspopup="dialog"
>
<span aria-hidden="true">?</span>
</button>
)}
</header>
);
}
8 changes: 4 additions & 4 deletions frontend/src/components/matchButton/MatchButton.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@
border-color: var(--text-light);
}

[data-theme="dark"] .add-match-button:focus-visible {
box-shadow: 0 0 0 3px var(--text-light);
}

.add-match-button:hover {
transform: scale(1.1);
}
Expand All @@ -69,6 +65,10 @@
box-shadow: 0 0 0 3px var(--dark-pink);
}

[data-theme="dark"] .add-match-button:focus-visible {
box-shadow: 0 0 0 3px var(--text-light);
}

@media (max-width: 767px) {
.add-match-button {
width: 35px;
Expand Down
18 changes: 12 additions & 6 deletions frontend/src/components/plantCard/PlantCard.css
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,10 @@
overflow: hidden;
}

[data-theme="dark"] .plant-card-back {
border: 2px solid var(--dark-green);
}

.plant-card-front {
background: var(--card-front);
}
Expand Down Expand Up @@ -235,13 +239,8 @@
margin: 0;
padding: 20px 18px 4px;

display: flex;
flex-wrap: wrap;
justify-content: flex-start;
align-items: center;
align-content: flex-start;

gap: 8px;
}

.plant-card-hint {
Expand All @@ -251,7 +250,6 @@
width: 100%;
border: none;
background: none;
font-family: inherit;
cursor: pointer;

margin: auto 0 0;
Expand Down Expand Up @@ -425,11 +423,19 @@
overflow-wrap: anywhere;
}

/* The chips are shown two by two, with the same width */
.plant-card-front .plant-card-preferences {
display: grid;
grid-template-columns: 1fr 1fr;
padding: 18px 12px 4px;
gap: 6px;
}

.plant-card-flipper .plant-preference-chip {
width: 100%;
text-align: center;
}

.plant-card-hint {
padding: 16px var(--card-hint-space) 26px 14px;
font-size: clamp(0.8rem, 4.8cqw, 0.9rem);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,6 @@
display: flex;
}

.plant-card-preferences .visually-hidden {
position: absolute;
width: 1px;
height: 1px;
overflow: hidden;
clip: rect(0 0 0 0);
white-space: nowrap;
}

.plant-preference-chip {
flex-shrink: 0;

Expand All @@ -42,6 +33,5 @@
.plant-preference-chip.is-match {
background: var(--dark-pink);
color: var(--text-light);
font-weight: 700;
transform: scale(1.05);
}
Loading
Loading