Skip to content
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
7 changes: 6 additions & 1 deletion e2e/uiAccessibility.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,12 @@ const PAGES = [
path: "#/explorePlants",
loaded: ".plant-grid > li",
},
{ name: "MyMatches", path: "#/myMatches", loaded: ".matches-grid" },
{
name: "MyMatches",
path: "#/myMatches",
// The status line stops being busy when the matches are loaded
loaded: '.my-matches-count[aria-busy="false"]',
},
{ name: "Profile", path: "#/profile", loaded: "main.profile" },
{
name: "PlantOwnerTest",
Expand Down
2 changes: 1 addition & 1 deletion e2e/uiMatchAndRemovePlant.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ test("user can like a plant in PlantMatch, find it in MyMatches and remove the m

await matchCards.first().locator(".remove-match-button").click();
await expect(matchCards).toHaveCount(0);
await expect(page.locator(".matches-grid")).toContainText(
await expect(page.locator(".my-matches-count")).toContainText(
/no matches|ingen matcher/i,
);
});
Expand Down
45 changes: 45 additions & 0 deletions frontend/src/components/sidebar/Sidebar.css
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,51 @@
border-color: transparent;
}

/* The number of new matches, on My Matches */
.sidebar__badge {
display: inline-flex;
align-items: center;
justify-content: center;
align-self: center;
min-width: 1.6rem;
height: 1.6rem;
margin-left: auto;
padding: 0 0.45rem;
box-sizing: border-box;
border-radius: 999px;
background: var(--dark-pink);
color: var(--text-light);
font-size: 0.9rem;
font-weight: 700;
line-height: 1;
animation: sidebar-badge-pop 0.3s ease;
}

@keyframes sidebar-badge-pop {
from {
transform: scale(0.4);
opacity: 0;
}
}

@media (prefers-reduced-motion: reduce) {
.sidebar__badge {
animation: none;
}
}

.sidebar__visually-hidden {
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}

.sidebar__link:focus-visible {
outline: 2px solid var(--dark-green);
outline-offset: 2px;
Expand Down
14 changes: 14 additions & 0 deletions frontend/src/components/sidebar/Sidebar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { NavLink } from "react-router-dom";
import LanguageSwitcher from "../sidebarButtons/LanguageSwitcher";
import ThemeToggle from "../sidebarButtons/ThemeToggle";
import { ROUTES } from "../../AppRoutes";
import { useNewMatches } from "../../hooks/useNewMatches";
import "./Sidebar.css";

type SidebarProps = {
Expand All @@ -15,6 +16,7 @@ type SidebarProps = {
export default function Sidebar({ isOpen, onCloseSidebar, ref }: SidebarProps) {
const { t } = useTranslation();
const closeButtonRef = useRef<HTMLButtonElement>(null);
const newMatches = useNewMatches();
// The title is "I <3 Plants". The heart is put in its own span so it can be pink.
const [titleStart, titleEnd] = t("sidebar.title").split("<3");

Expand Down Expand Up @@ -87,6 +89,18 @@ export default function Sidebar({ isOpen, onCloseSidebar, ref }: SidebarProps) {
onClick={onCloseSidebar}
>
{t("sidebar.myMatches")}
{newMatches > 0 && (
<>
{/* The number is for sighted users, the hidden text is read
by screen readers as part of the link */}
<span className="sidebar__badge" aria-hidden="true">
{newMatches}
</span>
<span className="sidebar__visually-hidden">
{t("sidebar.newMatches", { count: newMatches })}
</span>
</>
)}
</NavLink>
</li>

Expand Down
33 changes: 33 additions & 0 deletions frontend/src/hooks/useNewMatches.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { useState } from "react";
import { useLocation } from "react-router-dom";
import { useUser } from "../context/UserContext";
import { ROUTES } from "../AppRoutes";

// The number of plants matched since My Matches was last opened, shown as a
// badge on My Matches in the sidebar.
// The matches that have been "seen" are only kept in memory, as the user id is
// the only user data kept in the browser (see UserContext). So the matches the
// user already has when the app opens count as seen, and only matches made
// while the app is open are counted as new.
export function useNewMatches() {
const { user } = useUser();
const { pathname } = useLocation();
const matchedPlantIds = user?.matchedPlantIds;
const onMyMatches = pathname === ROUTES.myMatches;

const [seenIds, setSeenIds] = useState<string[] | null>(null);

// Everything counts as seen when the user is first loaded, and whenever
// My Matches is open. Updated during render (not in an effect), so the badge
// never shows for a moment before it is cleared.
if (
matchedPlantIds &&
seenIds !== matchedPlantIds &&
(seenIds === null || onMyMatches)
) {
setSeenIds(matchedPlantIds);
}

if (!matchedPlantIds || !seenIds || onMyMatches) return 0;
return matchedPlantIds.filter((id) => !seenIds.includes(id)).length;
}
4 changes: 3 additions & 1 deletion frontend/src/i18n/locales/en/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"plantOwnerTest": "Plant Owner Test",
"openSidebar": "Open sidebar",
"closeMenu": "Close menu",
"navigationLabel": "Sidebar navigation"
"navigationLabel": "Sidebar navigation",
"newMatches_one": ", {{count}} new match",
"newMatches_other": ", {{count}} new matches"
},
"plantMatchPage": {
"skipPlant": "Skip plant",
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/i18n/locales/no/translation.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
"plantOwnerTest": "Planteeiertest",
"openSidebar": "Åpne sidemeny",
"closeMenu": "Lukk menyen",
"navigationLabel": "Navigasjon i sidemenyen"
"navigationLabel": "Navigasjon i sidemenyen",
"newMatches_one": ", {{count}} ny match",
"newMatches_other": ", {{count}} nye matcher"
},
"plantMatchPage": {
"skipPlant": "Hopp over planten",
Expand Down
40 changes: 32 additions & 8 deletions frontend/src/pages/myMatches/MyMatchesPage.css
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,38 @@
gap: 30px;
}

/* The number of matches and the sort menu, on one line above the plants */
/* The sort menu, in the top right corner of the box with the plants it sorts */
.my-matches-header {
grid-column: 1 / -1;
display: flex;
flex-wrap: wrap;
justify-content: space-between;
align-items: center;
gap: 1rem;
justify-content: flex-end;
}

.my-matches-header .sort-menu {
align-self: center;
}

/* Same style as the number of results on Explore Plants */
/* The number of matches, centered under the page title as a summary of the
page. A pill with a pink border, like the preference chips on the cards. */
.my-matches-count {
margin: 0;
font-size: 1.25rem;
max-width: 40rem;
margin: -10px 0 0;
padding: 0.5rem 1.25rem;
border: 2px solid var(--dark-pink);
border-radius: 999px;
color: var(--pink-text);
font-size: 1.2rem;
font-weight: 600;
text-align: center;
}

.my-matches-count--message {
margin-top: 20px;
padding: 0;
border: none;
color: var(--text-dark);
font-weight: 400;
line-height: 1.5;
}

.matches-grid {
Expand Down Expand Up @@ -67,6 +80,17 @@
gap: 20px;
}

.my-matches-count {
margin-top: 0;
font-size: 1.05rem;
}

/* On mobile the sort menu takes the whole width */
.my-matches-header .sort-menu,
.my-matches-header .sort-button {
width: 100%;
}

.matches-grid {
grid-template-columns: 1fr;
gap: 20px;
Expand Down
70 changes: 40 additions & 30 deletions frontend/src/pages/myMatches/MyMatchesPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,21 +63,31 @@ function MyMatchesPage() {

return (
<main className="my-matches-page">
<section className="matches-grid">
<div className="my-matches-header">
{/* One status line that is always on the page, with aria-live, so
screen readers hear every change: the number of matches when
one is removed, and also "no matches" when the last one is
removed. While loading it says so, so "no matches" does not
flash before the matches are fetched. */}
<p className="my-matches-count" aria-live="polite">
{loading
? t("myMatchesPage.loading")
: totalCount > 0
? t("myMatchesPage.matchCount", { count: totalCount })
: t("myMatchesPage.noMatches")}
</p>
{totalCount > 0 && (
{/* Centered under the page title, as a summary of the page.
One status line that is always on the page, with aria-live, so
screen readers hear every change: the number of matches when
one is removed, and also "no matches" when the last one is
removed. While loading it says so, so "no matches" does not
flash before the matches are fetched. */}
<p
className={
!loading && totalCount > 0
? "my-matches-count"
: "my-matches-count my-matches-count--message"
}
aria-live="polite"
aria-busy={loading}
>
{loading
? t("myMatchesPage.loading")
: totalCount > 0
? t("myMatchesPage.matchCount", { count: totalCount })
: t("myMatchesPage.noMatches")}
</p>
{/* Only shown when there are matches, so there is no empty pink box */}
{totalCount > 0 && (
<section className="matches-grid">
<div className="my-matches-header">
<SortMenu
label={t("myMatchesPage.sort")}
value={sort}
Expand All @@ -87,22 +97,22 @@ function MyMatchesPage() {
}))}
onChange={setSort}
/>
)}
</div>

{plants.map((plant) => (
<div className="plant-card-match" key={plant.id}>
<PlantCard plant={plant} size="compact" />
{/* Every plant here is matched, so the button only removes */}
<MatchButton
isMatched
label={t("myMatchesPage.removeMatch")}
onClick={() => removeMatch(plant.id)}
className="remove-match-button"
/>
</div>
))}
</section>

{plants.map((plant) => (
<div className="plant-card-match" key={plant.id}>
<PlantCard plant={plant} size="compact" />
{/* Every plant here is matched, so the button only removes */}
<MatchButton
isMatched
label={t("myMatchesPage.removeMatch")}
onClick={() => removeMatch(plant.id)}
className="remove-match-button"
/>
</div>
))}
</section>
)}

{totalCount > 0 && (
<Pagination
Expand Down
Loading
Loading