Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
8 changes: 8 additions & 0 deletions apps/web/src/components/recipe/RecipeCard/RecipeCard.css
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,14 @@
background-color: var(--color-surface-muted);
}

/* Fills the media area so the whole image is clickable. */
.recipe-card__media-link {
display: block;

width: 100%;
height: 100%;
}

.recipe-card__image {
display: block;

Expand Down
47 changes: 32 additions & 15 deletions apps/web/src/components/recipe/RecipeCard/RecipeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,11 @@ export default function RecipeCard({

const recipePath = `/recipes/${encodeURIComponent(recipeId)}`;

// Lets the detail page return to the exact Explore/Cookbook URL.
const recipeLinkState = {
from: `${location.pathname}${location.search}${location.hash}`,
};

const usesBuiltInSaveAction = action == null && Boolean(onSaveChange);

const visibleTags = tags.slice(0, MAX_VISIBLE_TAGS);
Expand Down Expand Up @@ -183,18 +188,32 @@ export default function RecipeCard({
<>
<article className={classes} {...articleProps}>
<div className="recipe-card__media">
<img
className={[
'recipe-card__image',
!imageSrc && 'recipe-card__image--fallback',
]
.filter(Boolean)
.join(' ')}
src={imageSrc ?? fallbackRecipeImage}
alt={imageSrc ? imageAlt : ''}
loading="lazy"
decoding="async"
/>
{/*
* The image is a pointer/touch shortcut to the same page as the
* title link. It is removed from the tab order and hidden from
* assistive technology, so keyboard and screen-reader users still
* meet exactly one link per card (the title).
*/}
<Link
className="recipe-card__media-link"
to={recipePath}
state={recipeLinkState}
tabIndex={-1}
aria-hidden="true"
>
<img
className={[
'recipe-card__image',
!imageSrc && 'recipe-card__image--fallback',
]
.filter(Boolean)
.join(' ')}
src={imageSrc ?? fallbackRecipeImage}
alt={imageSrc ? imageAlt : ''}
loading="lazy"
decoding="async"
/>
</Link>
</div>

<div className="recipe-card__body">
Expand All @@ -203,9 +222,7 @@ export default function RecipeCard({
<Link
className="recipe-card__title-link"
to={recipePath}
state={{
from: `${location.pathname}${location.search}${location.hash}`,
}}
state={recipeLinkState}
>
{title}
</Link>
Expand Down
36 changes: 36 additions & 0 deletions apps/web/src/components/recipe/__tests__/RecipeCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,42 @@ describe('RecipeCard', () => {
);
});

it('opens the recipe when the image is clicked, keeping browse state', async () => {
const user = userEvent.setup();

const { container } = render(
<MemoryRouter initialEntries={['/?q=pasta&page=2']}>
<Routes>
<Route path="/" element={<RecipeCard {...baseProps} />} />

<Route path="/recipes/:id" element={<LocationStateProbe />} />
</Routes>
</MemoryRouter>,
);

const image = container.querySelector('.recipe-card__image');
expect(image).not.toBeNull();

await user.click(image!);

expect(screen.getByTestId('recipe-location-state').textContent).toBe(
JSON.stringify({ from: '/?q=pasta&page=2' }),
);
});

it('exposes only the title as a recipe link to keyboard and screen-reader users', () => {
const { container } = renderRecipeCard(baseProps);

// The image link is hidden from the accessibility tree...
expect(screen.getAllByRole('link')).toHaveLength(1);

// ...and from the tab order, but points to the same recipe.
const imageLink = container.querySelector('.recipe-card__media-link');
expect(imageLink?.getAttribute('href')).toBe('/recipes/carbonara');
expect(imageLink?.getAttribute('tabindex')).toBe('-1');
expect(imageLink?.getAttribute('aria-hidden')).toBe('true');
});

it('renders structured cooking time and servings metadata', () => {
renderRecipeCard(baseProps);

Expand Down