File size: 1,810 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import useIsBrowser from '@docusaurus/useIsBrowser';
import { Card, CardBody, CardMedia } from '@wordpress/components';
import clsx from 'clsx';
import { useId } from 'react';
import styles from './style.module.scss';

// TODO: Replace with actual image URL.
const DEFAULT_IMAGE =
	'https://raw.githubusercontent.com/Automattic/wp-calypso/2e973a7d95304c88ecd795e903ba28f47a6e714e/apps/design-system-docs/docs/components/ds/form-controls/button/button.png';

/**
 * A card that links to a page.
 */
export const LinkCard = ( {
	className,
	href,
	label,
	description,
	image = DEFAULT_IMAGE,
}: {
	className?: string;
	href: string;
	label: string;
	description?: string;
	/**
	 * Optional image to display in the card.
	 * Should only be decorative, as it will be hidden from screen readers.
	 */
	image?: string;
} ) => {
	const descriptionId = useId();
	const labelId = useId();

	const isBrowser = useIsBrowser();

	// Just for SSG (SEO). Can be removed when `Card` is forked and converted away from Emotion.
	if ( ! isBrowser ) {
		return (
			<>
				<a href={ href } aria-describedby={ descriptionId }>
					{ label }
				</a>
				<p id={ descriptionId }>{ description }</p>
			</>
		);
	}

	return (
		<Card className={ clsx( styles[ 'link-card' ], className ) }>
			<a
				href={ href }
				aria-describedby={ descriptionId }
				aria-labelledby={ labelId }
				className={ styles[ 'link-card__click-target' ] }
			></a>
			<CardMedia className={ styles[ 'link-card__image' ] }>
				<img src={ image } alt="" />
			</CardMedia>
			<CardBody>
				<span id={ labelId } className={ styles[ 'link-card__label' ] } aria-hidden="true">
					{ label }
				</span>
				<span id={ descriptionId } className={ styles[ 'link-card__description' ] }>
					{ description }
				</span>
			</CardBody>
		</Card>
	);
};