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
5 changes: 5 additions & 0 deletions apps/web/src/components/recipe/RecipeCard/RecipeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ export default function RecipeCard({
size="sm"
aria-pressed={saved}
onClick={handleSaveButtonClick}
aria-label={
saved
? `Remove ${title} from My Cookbook`
: `Save ${title} to My Cookbook`
}
>
<Icon
icon={HeartIcon}
Expand Down
14 changes: 7 additions & 7 deletions apps/web/src/components/recipe/__tests__/RecipeCard.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ describe('RecipeCard', () => {
});

const saveButton = screen.getByRole('button', {
name: 'Save',
name: /^Save .* to My Cookbook$/,
});

await user.tab();
Expand All @@ -97,7 +97,7 @@ describe('RecipeCard', () => {
});

const saveButton = screen.getByRole('button', {
name: 'Save',
name: /^Save .* to My Cookbook$/,
});

expect(saveButton.getAttribute('aria-pressed')).toBe('false');
Expand All @@ -121,7 +121,7 @@ describe('RecipeCard', () => {
});

const savedButton = screen.getByRole('button', {
name: 'Saved',
name: /^Remove .* from My Cookbook$/,
});

expect(savedButton.getAttribute('aria-pressed')).toBe('true');
Expand Down Expand Up @@ -156,7 +156,7 @@ describe('RecipeCard', () => {

await user.click(
screen.getByRole('button', {
name: 'Saved',
name: /^Remove .* from My Cookbook$/,
}),
);

Expand All @@ -174,13 +174,13 @@ describe('RecipeCard', () => {

expect(
screen.queryByRole('button', {
name: 'Save',
name: /^Save .* to My Cookbook$/,
}),
).toBeNull();

expect(
screen.queryByRole('button', {
name: 'Saved',
name: /^Remove .* from My Cookbook$/,
}),
).toBeNull();
});
Expand All @@ -201,7 +201,7 @@ describe('RecipeCard', () => {

expect(
screen.queryByRole('button', {
name: 'Saved',
name: /^Remove .* from My Cookbook$/,
}),
).toBeNull();
});
Expand Down
24 changes: 24 additions & 0 deletions apps/web/src/components/ui/Select/Select.css
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
box-shadow var(--duration-fast) var(--ease-standard);
}

.select__control--with-leading-icon {
padding-inline-start: calc(
var(--space-3) + var(--icon-size-sm) + var(--space-2)
);
}

.select--full .select__control {
width: 100%;
}
Expand Down Expand Up @@ -118,6 +124,24 @@
cursor: not-allowed;
}

/* Leading icon */

.select__leading-icon {
position: absolute;
z-index: 1;
top: 50%;
left: var(--space-3);

width: var(--icon-size-sm);
height: var(--icon-size-sm);

color: var(--color-icon-secondary);

pointer-events: none;

transform: translateY(-50%);
}

/* Chevron */

.select__icon {
Expand Down
30 changes: 28 additions & 2 deletions apps/web/src/components/ui/Select/Select.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import type { ComponentPropsWithRef, ReactNode } from 'react';
import type { ComponentPropsWithRef, ComponentType, ReactNode } from 'react';

import { ChevronDownIcon } from '../../../icons/navigation';
import type { IconProps as SvgIconProps } from '../../../icons/types';

import { Icon } from '../Icon/Icon';
import { FieldError, FieldHelp, RequiredMarker } from '../internal/FieldText';
import { useFieldA11y } from '../internal/fieldA11y';
Expand All @@ -13,6 +15,8 @@ export type SelectProps = Omit<
'size' | 'width'
> & {
label: ReactNode;
labelVisuallyHidden?: boolean;
leadingIcon?: ComponentType<SvgIconProps>;
helpText?: ReactNode;
error?: ReactNode;
children: ReactNode;
Expand All @@ -21,6 +25,8 @@ export type SelectProps = Omit<

function Select({
label,
labelVisuallyHidden = false,
leadingIcon: LeadingIcon,
helpText,
error,
children,
Expand All @@ -40,8 +46,10 @@ function Select({
});

const wrapperClasses = ['select', `select--${width}`].join(' ');

const selectClasses = [
'select__control',
LeadingIcon && 'select__control--with-leading-icon',
error && 'select__control--error',
className,
]
Expand All @@ -50,12 +58,27 @@ function Select({

return (
<div className={wrapperClasses}>
<label className="select__label" htmlFor={controlId}>
<label
className={
labelVisuallyHidden ? 'select__label sr-only' : 'select__label'
}
htmlFor={controlId}
>
{label}
{required && <RequiredMarker className="select__required" />}
</label>

<div className="select__control-wrapper">
{LeadingIcon && (
<Icon
icon={LeadingIcon}
className="select__leading-icon"
size="sm"
tone="secondary"
aria-hidden="true"
/>
)}

<select
id={controlId}
className={selectClasses}
Expand All @@ -67,17 +90,20 @@ function Select({
>
{children}
</select>

<Icon
icon={ChevronDownIcon}
className="select__icon"
size="md"
stroke="thin"
aria-hidden="true"
/>
</div>

<FieldHelp id={helpId} className="select__help">
{helpText}
</FieldHelp>

<FieldError id={errorId} className="select__error">
{error}
</FieldError>
Expand Down
26 changes: 26 additions & 0 deletions apps/web/src/components/ui/__tests__/Select.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import userEvent from '@testing-library/user-event';
import { describe, expect, it, vi } from 'vitest';

import Select from '../Select/Select';
import { CookingTimeIcon } from '../../../icons/recipe';

describe('Select', () => {
it('connects its label, help text, error text, and invalid state', () => {
Expand Down Expand Up @@ -50,4 +51,29 @@ describe('Select', () => {
expect(select.value).toBe('dinner');
expect(onChange).toHaveBeenCalledTimes(1);
});

it('supports a visually hidden label and leading icon', () => {
const { container } = render(
<Select
label="Cooking time"
labelVisuallyHidden
leadingIcon={CookingTimeIcon}
defaultValue="any"
>
<option value="any">Cooking time</option>
<option value="30">30 minutes or less</option>
</Select>,
);

const select = screen.getByRole('combobox', {
name: 'Cooking time',
});

const label = container.querySelector('.select__label');
const leadingIcon = container.querySelector('.select__leading-icon');

expect(select).toBeTruthy();
expect(label?.classList.contains('sr-only')).toBe(true);
expect(leadingIcon).toBeTruthy();
});
});
12 changes: 12 additions & 0 deletions apps/web/src/data/recipes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Add the eggs and cook until set.' },
],
rating: { average: 4.8, count: 126 },
saveCount: 200,
visibility: 'public',
isSaved: true,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -109,6 +110,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Combine with pasta and season with lemon.' },
],
rating: { average: 4.6, count: 89 },
saveCount: 150,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -156,6 +158,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Blend until smooth and adjust seasoning.' },
],
rating: { average: 4.5, count: 37 },
saveCount: 20,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: true,
Expand Down Expand Up @@ -203,6 +206,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Layer chicken and rice, then steam until done.' },
],
rating: { average: 4.9, count: 204 },
saveCount: 45,
visibility: 'public',
isSaved: true,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -250,6 +254,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Assemble and serve with avocado dressing.' },
],
rating: { average: 4.4, count: 61 },
saveCount: 12,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -297,6 +302,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Cook in a greased waffle iron.' },
],
rating: { average: 4.7, count: 143 },
saveCount: 44,
visibility: 'public',
isSaved: true,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -352,6 +358,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Serve with lemon tahini dressing.' },
],
rating: { average: 4.3, count: 28 },
saveCount: 0,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -391,6 +398,7 @@ export const recipes: Recipe[] = [
{ step: 2, text: 'Blend until smooth and season to taste.' },
],
rating: { average: 0, count: 0 },
saveCount: 11,
visibility: 'private',
isSaved: false,
isCreatedByCurrentUser: true,
Expand Down Expand Up @@ -438,6 +446,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Arrange everything in serving bowls.' },
],
rating: { average: 4.7, count: 95 },
saveCount: 3353,
visibility: 'public',
isSaved: true,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -485,6 +494,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Bake until golden and cooked through.' },
],
rating: { average: 4.6, count: 19 },
saveCount: 35,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: true,
Expand Down Expand Up @@ -532,6 +542,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Simmer until tender and finish with basil.' },
],
rating: { average: 4.5, count: 73 },
saveCount: 35,
visibility: 'public',
isSaved: false,
isCreatedByCurrentUser: false,
Expand Down Expand Up @@ -578,6 +589,7 @@ export const recipes: Recipe[] = [
{ step: 3, text: 'Cook slowly until the lamb is tender.' },
],
rating: { average: 0, count: 0 },
saveCount: 466,
visibility: 'private',
isSaved: false,
isCreatedByCurrentUser: true,
Expand Down
Loading