Skip to content

Commit dc693ea

Browse files
authored
Visible alt text width and fallback alt text (#385)
* implement css props for visible alt text * and fallback label * limit width for visible alt text on some grid items
1 parent a322523 commit dc693ea

File tree

4 files changed

+32
-10
lines changed

4 files changed

+32
-10
lines changed

src/components/GridItem/Example.astro

+4-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,12 @@ const { item, lazyLoad } = Astro.props;
2020
alt={item.data.featuredImageAlt}
2121
widths={[325, 400]}
2222
sizes={`325px, (min-width: 768px) 400px`}
23-
class="w-3/5"
2423
loading={lazyLoad ? "lazy" : "eager"}
24+
class="w-3/5"
25+
visibleAltTextClass="max-w-[calc(60%-30px)]"
2526
/>
27+
<!-- visible alt text class keeps the alt text within
28+
the narrower image width given in class prop -->
2629
<p class="text-body mt-xs break-words break-keep group-hover:underline">
2730
{item.data.title}
2831
</p>

src/components/GridItem/Library.astro

+3
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,10 @@ if (!/[.!]\)?$/.test(description)) {
3434
alt={item.data.featuredImageAlt}
3535
width="400"
3636
class="w-3/5"
37+
visibleAltTextClass="max-w-[calc(60%-30px)]"
3738
/>
39+
<!-- visible alt text class keeps the alt text within
40+
the narrower image width given in class prop -->
3841
<p
3942
class="text-xl mt-xs text-wrap break-words break-keep group-hover:underline"
4043
>

src/components/Image/index.astro

+24-9
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
---
2+
import { getCurrentLocale, getUiTranslator } from "@/src/i18n/utils";
23
import type { ComponentProps } from "astro/types";
34
import { Image } from "astro:assets";
45
type Props = ComponentProps<typeof Image> & {
56
/** Defaults to 'photo' aspect ratio */
67
aspectRatio?: "photo" | "square" | "none";
78
caption?: string;
89
containerClass?: string;
10+
visibleAltTextClass?: string;
911
};
1012
const { props } = Astro;
13+
14+
const currentLocale = getCurrentLocale(Astro.url.pathname);
15+
const t = await getUiTranslator(currentLocale);
16+
const noAltText = t("No alt text");
1117
---
1218

1319
<div class={props.containerClass || "relative w-fit h-fit"}>
@@ -16,19 +22,28 @@ const { props } = Astro;
1622
<Image
1723
{...props}
1824
class=""
19-
class:list={(props.class,
20-
{
21-
"object-cover": props.aspectRatio !== "none",
22-
"aspect-square": props.aspectRatio === "square",
23-
"aspect-photo":
24-
props.aspectRatio === "photo" || props.aspectRatio === undefined,
25-
})}
25+
class:list={[
26+
props.class,
27+
{
28+
"object-cover": props.aspectRatio !== "none",
29+
"aspect-square": props.aspectRatio === "square",
30+
"aspect-photo":
31+
props.aspectRatio === "photo" || props.aspectRatio === undefined,
32+
},
33+
]}
2634
/>
2735
<p
2836
aria-hidden
29-
class="renderable-alt bg-bg-gray-40 line-clamp-3 absolute top-0 mt-sm mx-sm px-[7.5px] pb-[2.5px] rounded-xl text-body-caption text-ellipsis"
37+
class:list={[
38+
"renderable-alt",
39+
"text-body-caption",
40+
"bg-bg-gray-40 line-clamp-3 absolute top-0 mt-sm mx-sm px-[7.5px] pb-[2.5px] rounded-xl text-ellipsis",
41+
props.visibleAltTextClass,
42+
]}
3043
>
31-
{props.alt}
44+
<!-- It shouldn't be possible to pass no alt text, but if its missing
45+
show a '?' so the component doesn't look broken -->
46+
{props.alt || noAltText}
3247
</p>
3348
{props.caption && <p class="text-body-caption mt-xs">{props.caption}</p>}
3449
</div>

src/content/ui/en.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ Show Code: Show Code
4242
Donate to p5.js: Donate to p5.js
4343
Download p5.js: Download p5.js
4444
No Results: No entries found for that search.
45+
No alt text: No alt text
4546
briefPageDescriptions:
4647
Reference: Find easy explanations for every piece of p5.js code.
4748
Examples: Explore the possibilities of p5.js with short examples.

0 commit comments

Comments
 (0)