Add alt text to OutfitCard images

This commit is contained in:
Emi Matchu 2021-01-08 00:35:56 -08:00
parent b478f35103
commit fe799a9bbc

View file

@ -1,5 +1,6 @@
import React from "react"; import React from "react";
import { Box, Center, Flex, Wrap, WrapItem } from "@chakra-ui/react"; import { Box, Center, Flex, Wrap, WrapItem } from "@chakra-ui/react";
import { ClassNames } from "@emotion/react";
import gql from "graphql-tag"; import gql from "graphql-tag";
import { useQuery } from "@apollo/client"; import { useQuery } from "@apollo/client";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
@ -109,6 +110,8 @@ function UserOutfitsPageContent() {
function OutfitCard({ outfit }) { function OutfitCard({ outfit }) {
const image = ( const image = (
<ClassNames>
{({ css }) => (
<Box <Box
as="img" as="img"
src={buildOutfitThumbnailUrl( src={buildOutfitThumbnailUrl(
@ -117,8 +120,23 @@ function OutfitCard({ outfit }) {
)} )}
width={150} width={150}
height={150} height={150}
alt="Outfit thumbnail" alt={buildOutfitAltText(outfit)}
// Firefox shows alt text as a fallback for images it can't show yet.
// Show our alt text clearly if the image failed to load... but hide
// it if it's still loading. It's normal for these to take a second
// to load on a new device, and the flash of text is unhelpful.
color="white"
fontSize="xs"
padding="2"
overflow="auto"
className={css`
&:-moz-loading {
visibility: hidden;
}
`}
/> />
)}
</ClassNames>
); );
return ( return (
@ -178,6 +196,26 @@ function buildOutfitThumbnailUrl(petAppearance, itemAppearances) {
return `/api/outfitImage?size=${size}&layerUrls=${layerUrls.join(",")}`; return `/api/outfitImage?size=${size}&layerUrls=${layerUrls.join(",")}`;
} }
function buildOutfitAltText(outfit) {
const { petAppearance, wornItems } = outfit;
const { species, color } = petAppearance;
let altText = "";
const petDescription = `${color.name} ${species.name}`;
altText += petDescription;
if (wornItems.length > 0) {
const itemNames = wornItems
.map((item) => item.name)
.sort()
.join(", ");
altText += ` wearing ${itemNames}`;
}
return altText;
}
/** /**
* getBestImageSize returns the right image size to render at 150x150, for the * getBestImageSize returns the right image size to render at 150x150, for the
* current device. * current device.