|
| 1 | +import React from 'react'; |
| 2 | +import { ArrowRightIcon } from '@heroicons/react/16/solid'; |
| 3 | +import cn from 'src/utilities/cn'; |
| 4 | +import { IconSize } from 'src/components/Icon/types'; |
| 5 | +import LinkButton from './LinkButton'; |
| 6 | +import { ProductName, products } from './ProductTile/data'; |
| 7 | +import ProductIcon from './ProductTile/ProductIcon'; |
| 8 | +import ProductLabel from './ProductTile/ProductLabel'; |
| 9 | +import ProductDescription from './ProductTile/ProductDescription'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Props for the ProductTile component. |
| 13 | + */ |
| 14 | +export type ProductTileProps = { |
| 15 | + /** |
| 16 | + * The name of the product. |
| 17 | + */ |
| 18 | + name: ProductName; |
| 19 | + |
| 20 | + /** |
| 21 | + * Indicates if the product tile is selected. If `undefined`, the product tile is not selectable. |
| 22 | + * @default false |
| 23 | + */ |
| 24 | + selected?: boolean; |
| 25 | + |
| 26 | + /** |
| 27 | + * Indicates if the product tile is on the "current" page. Changes CTA copy. |
| 28 | + * @default false |
| 29 | + */ |
| 30 | + currentPage?: boolean; |
| 31 | + |
| 32 | + /** |
| 33 | + * Additional CSS class names to apply to the product tile outer container. |
| 34 | + */ |
| 35 | + className?: string; |
| 36 | + |
| 37 | + /** |
| 38 | + * Additional CSS class names to apply to the product description container. |
| 39 | + */ |
| 40 | + descriptionClassName?: string; |
| 41 | + |
| 42 | + /** |
| 43 | + * Additional CSS class names to apply to the product name / label container. |
| 44 | + */ |
| 45 | + labelClassName?: string; |
| 46 | + |
| 47 | + /** |
| 48 | + * Additional CSS class names to apply to the product icon. |
| 49 | + */ |
| 50 | + iconClassName?: string; |
| 51 | + |
| 52 | + /** |
| 53 | + * Callback function to handle click events on the product tile. |
| 54 | + */ |
| 55 | + |
| 56 | + onClick?: () => void; |
| 57 | + |
| 58 | + /** |
| 59 | + * Indicates if the product description should be shown. |
| 60 | + * @default true |
| 61 | + */ |
| 62 | + showDescription?: boolean; |
| 63 | + |
| 64 | + /** |
| 65 | + * Indicates if the product label should be shown. |
| 66 | + * @default true |
| 67 | + */ |
| 68 | + showLabel?: boolean; |
| 69 | + |
| 70 | + /** |
| 71 | + * The size of the product icon. |
| 72 | + * @default "40px" |
| 73 | + */ |
| 74 | + size?: IconSize; |
| 75 | + |
| 76 | + /** |
| 77 | + * Indicates if the product icons should be animated. |
| 78 | + * @default false |
| 79 | + */ |
| 80 | + animateIcons?: boolean; |
| 81 | +}; |
| 82 | + |
| 83 | +const CONTAINER_GAP_RATIO = 3; |
| 84 | + |
| 85 | +const ProductTile = ({ |
| 86 | + name, |
| 87 | + selected, |
| 88 | + currentPage, |
| 89 | + className, |
| 90 | + onClick, |
| 91 | + showDescription = true, |
| 92 | + showLabel = true, |
| 93 | + size = '40px', |
| 94 | + animateIcons = false, |
| 95 | + descriptionClassName, |
| 96 | + labelClassName, |
| 97 | + iconClassName, |
| 98 | +}: ProductTileProps) => { |
| 99 | + const { icon, hoverIcon, label, description, link, unavailable } = products[name] ?? {}; |
| 100 | + const numericalSize = parseInt(size, 10); |
| 101 | + const containerPresent = showDescription || showLabel; |
| 102 | + |
| 103 | + return ( |
| 104 | + <div |
| 105 | + className={cn( |
| 106 | + 'transition-colors group/product-tile', |
| 107 | + { 'flex flex-col p-3 rounded-lg gap-2': containerPresent }, |
| 108 | + { 'bg-neutral-1300 dark:bg-neutral-000': selected }, |
| 109 | + { |
| 110 | + 'bg-neutral-000 dark:bg-neutral-1300': !selected, |
| 111 | + }, |
| 112 | + { |
| 113 | + 'hover:bg-neutral-100 dark:hover:bg-neutral-1200': selected === false && !unavailable, |
| 114 | + }, |
| 115 | + { 'cursor-pointer': selected !== undefined && !unavailable }, |
| 116 | + { 'pointer-events-none': unavailable }, |
| 117 | + { [`${className}`]: className }, |
| 118 | + )} |
| 119 | + aria-hidden={unavailable} |
| 120 | + onClick={onClick} |
| 121 | + onKeyDown={ |
| 122 | + onClick |
| 123 | + ? (e) => { |
| 124 | + if (e.key === 'Enter' || e.key === ' ') { |
| 125 | + e.preventDefault(); |
| 126 | + onClick(); |
| 127 | + } |
| 128 | + } |
| 129 | + : undefined |
| 130 | + } |
| 131 | + role={onClick ? 'button' : undefined} |
| 132 | + tabIndex={onClick ? 0 : undefined} |
| 133 | + > |
| 134 | + <div |
| 135 | + className={cn('items-center', { flex: containerPresent }, { 'inline-flex': !containerPresent })} |
| 136 | + style={{ |
| 137 | + gap: containerPresent ? numericalSize / CONTAINER_GAP_RATIO : 0, |
| 138 | + }} |
| 139 | + > |
| 140 | + <ProductIcon |
| 141 | + size={numericalSize} |
| 142 | + name={icon} |
| 143 | + hoverName={animateIcons ? hoverIcon : undefined} |
| 144 | + selected={selected} |
| 145 | + unavailable={!!unavailable} |
| 146 | + className={iconClassName} |
| 147 | + /> |
| 148 | + <ProductLabel |
| 149 | + label={label} |
| 150 | + selected={selected} |
| 151 | + unavailable={!!unavailable} |
| 152 | + numericalSize={numericalSize} |
| 153 | + showLabel={showLabel} |
| 154 | + className={labelClassName} |
| 155 | + /> |
| 156 | + </div> |
| 157 | + <ProductDescription |
| 158 | + description={description} |
| 159 | + selected={selected} |
| 160 | + unavailable={!!unavailable} |
| 161 | + showDescription={showDescription} |
| 162 | + className={descriptionClassName} |
| 163 | + /> |
| 164 | + {selected && link ? ( |
| 165 | + <LinkButton |
| 166 | + variant="secondary" |
| 167 | + size="xs" |
| 168 | + className="mt-2 !text-neutral-000 dark:!text-neutral-1300" |
| 169 | + rightIcon={<ArrowRightIcon aria-hidden />} |
| 170 | + iconColor="text-orange-600" |
| 171 | + href={link} |
| 172 | + > |
| 173 | + {currentPage ? 'View docs' : 'Explore'} |
| 174 | + </LinkButton> |
| 175 | + ) : null} |
| 176 | + </div> |
| 177 | + ); |
| 178 | +}; |
| 179 | + |
| 180 | +export default ProductTile; |
0 commit comments