Skip to content

Commit 3841eaa

Browse files
ENH: Add img-alt for card and grid-item-card (#114)
Co-authored-by: Chris Holdgraf <choldgraf@berkeley.edu>
1 parent d19e589 commit 3841eaa

File tree

7 files changed

+24
-9
lines changed

7 files changed

+24
-9
lines changed

docs/cards.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ You can also add an image as the background of a card or at the top/bottom of th
8484
:::{card} Title
8585
:img-background: images/particle_background.jpg
8686
:class-card: sd-text-black
87+
:img-alt: your desired alt text
8788

8889
Text
8990
:::
@@ -92,6 +93,7 @@ Text
9293

9394
::::{grid-item-card} Title
9495
:img-top: images/particle_background.jpg
96+
:img-alt: your desired alt text
9597

9698
Header
9799
^^^
@@ -102,6 +104,7 @@ Footer
102104

103105
::::{grid-item-card} Title
104106
:img-bottom: images/particle_background.jpg
107+
:img-alt: your desired alt text
105108

106109
Header
107110
^^^
@@ -257,6 +260,9 @@ img-top
257260
img-bottom
258261
: A URI (relative file path or URL) to an image to be placed below the content.
259262

263+
img-alt
264+
: Alternative text for the image (that will be used by screen-readers).
265+
260266
link
261267
: Turn the entire card into a clickable link to an external/internal target.
262268

docs/snippets/myst/card-images.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
:::{card} Title
66
:img-background: images/particle_background.jpg
77
:class-card: sd-text-black
8+
:img-alt: my text
89

910
Text
1011
:::
@@ -13,6 +14,7 @@ Text
1314

1415
::::{grid-item-card} Title
1516
:img-top: images/particle_background.jpg
17+
:img-alt:
1618

1719
Header
1820
^^^

docs/snippets/rst/card-images.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@
55
.. card:: Title
66
:img-background: images/particle_background.jpg
77
:class-card: sd-text-black
8+
:img-alt: my text
89

910
Text
1011

1112
.. grid-item-card:: Title
1213
:img-top: images/particle_background.jpg
14+
:img-alt:
1315

1416
Header
1517
^^^

sphinx_design/cards.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class CardDirective(SphinxDirective):
5959
"img-top": directives.uri,
6060
"img-bottom": directives.uri,
6161
"img-background": directives.uri,
62+
"img-alt": directives.unchanged,
6263
"link": directives.uri,
6364
"link-type": make_choice(["url", "any", "ref", "doc"]),
6465
"link-alt": directives.unchanged,
@@ -96,13 +97,15 @@ def create_card(
9697
)
9798
inst.set_source_info(card)
9899

100+
img_alt = options.get("img-alt") or ""
101+
99102
container = card
100103
if "img-background" in options:
101104
card.append(
102105
nodes.image(
103106
uri=options["img-background"],
104107
classes=["sd-card-img"],
105-
alt="background image",
108+
alt=img_alt,
106109
)
107110
)
108111
overlay = create_component("card-overlay", ["sd-card-img-overlay"])
@@ -114,7 +117,7 @@ def create_card(
114117
image_top = nodes.image(
115118
"",
116119
uri=options["img-top"],
117-
alt="card-img-top",
120+
alt=img_alt,
118121
classes=["sd-card-img-top"] + options.get("class-img-top", []),
119122
)
120123
container.append(image_top)
@@ -156,7 +159,7 @@ def create_card(
156159
image_bottom = nodes.image(
157160
"",
158161
uri=options["img-bottom"],
159-
alt="card-img-bottom",
162+
alt=img_alt,
160163
classes=["sd-card-img-bottom"] + options.get("class-img-bottom", []),
161164
)
162165
container.append(image_bottom)

sphinx_design/grids.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ class GridItemCardDirective(SphinxDirective):
223223
"img-background": directives.uri,
224224
"img-top": directives.uri,
225225
"img-bottom": directives.uri,
226+
"img-alt": directives.unchanged,
226227
"link": directives.uri,
227228
"link-type": make_choice(["url", "any", "ref", "doc"]),
228229
"link-alt": directives.unchanged,
@@ -266,6 +267,7 @@ def run(self) -> List[nodes.Node]:
266267
"img-background",
267268
"img-top",
268269
"img-bottom",
270+
"img-alt",
269271
"link",
270272
"link-type",
271273
"link-alt",

tests/test_snippets/snippet_post_card-images.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<container classes="sd-row sd-row-cols-2 sd-row-cols-xs-2 sd-row-cols-sm-3 sd-row-cols-md-3 sd-row-cols-lg-4" design_component="grid-row" is_div="True">
77
<container classes="sd-col sd-d-flex-column" design_component="grid-item" is_div="True">
88
<container classes="sd-card sd-sphinx-override sd-mb-3 sd-shadow-sm sd-text-black" design_component="card" is_div="True">
9-
<image alt="background image" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img" uri="images/particle_background.jpg">
9+
<image alt="my text" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img" uri="images/particle_background.jpg">
1010
<container classes="sd-card-img-overlay" design_component="card-overlay" is_div="True">
1111
<container classes="sd-card-body" design_component="card-body" is_div="True">
1212
<container classes="sd-card-title sd-font-weight-bold" design_component="card-title" is_div="True">
@@ -16,7 +16,7 @@
1616
Text
1717
<container classes="sd-col sd-d-flex-row" design_component="grid-item" is_div="True">
1818
<container classes="sd-card sd-sphinx-override sd-w-100 sd-shadow-sm" design_component="card" is_div="True">
19-
<image alt="card-img-top" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-top" uri="images/particle_background.jpg">
19+
<image alt="" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-top" uri="images/particle_background.jpg">
2020
<container classes="sd-card-header" design_component="card-header" is_div="True">
2121
<paragraph classes="sd-card-text">
2222
Header
@@ -43,4 +43,4 @@
4343
<container classes="sd-card-footer" design_component="card-footer" is_div="True">
4444
<paragraph classes="sd-card-text">
4545
Footer
46-
<image alt="card-img-bottom" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-bottom" uri="images/particle_background.jpg">
46+
<image alt="" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-bottom" uri="images/particle_background.jpg">

tests/test_snippets/snippet_pre_card-images.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<container classes="sd-row sd-row-cols-2 sd-row-cols-xs-2 sd-row-cols-sm-3 sd-row-cols-md-3 sd-row-cols-lg-4" design_component="grid-row" is_div="True">
77
<container classes="sd-col sd-d-flex-column" design_component="grid-item" is_div="True">
88
<container classes="sd-card sd-sphinx-override sd-mb-3 sd-shadow-sm sd-text-black" design_component="card" is_div="True">
9-
<image alt="background image" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img" uri="images/particle_background.jpg">
9+
<image alt="my text" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img" uri="images/particle_background.jpg">
1010
<container classes="sd-card-img-overlay" design_component="card-overlay" is_div="True">
1111
<container classes="sd-card-body" design_component="card-body" is_div="True">
1212
<container classes="sd-card-title sd-font-weight-bold" design_component="card-title" is_div="True">
@@ -16,7 +16,7 @@
1616
Text
1717
<container classes="sd-col sd-d-flex-row" design_component="grid-item" is_div="True">
1818
<container classes="sd-card sd-sphinx-override sd-w-100 sd-shadow-sm" design_component="card" is_div="True">
19-
<image alt="card-img-top" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-top" uri="images/particle_background.jpg">
19+
<image alt="" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-top" uri="images/particle_background.jpg">
2020
<container classes="sd-card-header" design_component="card-header" is_div="True">
2121
<paragraph classes="sd-card-text">
2222
Header
@@ -43,4 +43,4 @@
4343
<container classes="sd-card-footer" design_component="card-footer" is_div="True">
4444
<paragraph classes="sd-card-text">
4545
Footer
46-
<image alt="card-img-bottom" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-bottom" uri="images/particle_background.jpg">
46+
<image alt="" candidates="{'*': 'images/particle_background.jpg'}" classes="sd-card-img-bottom" uri="images/particle_background.jpg">

0 commit comments

Comments
 (0)