Skip to content

Commit

Permalink
👌 IMPROVE: Add link-alt to fix card link accessibility (#89)
Browse files Browse the repository at this point in the history
This PR adds the `link-alt` option to `card` (and `grid-item-card`) directives,
in order to assign a discernable name to the link (for screen readers).

As noted in #78, it may be ideal to add `aria-label/`aria-hidden` to the actual link `<a>` HTML element.
However, this would entail having to override aspects of the sphinx HTML builder.
Instead, we include the link alt text, but add a `sd-hide-link-text` CSS class, to set `font-size: 0`, to hide the text.
This solution was taken from https://stackoverflow.com/questions/471510/hide-text-using-css, and seemed to be the simplest solution.

fixes #78
  • Loading branch information
chrisjsewell authored Aug 18, 2022
1 parent f309aee commit adbcdf2
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
5 changes: 5 additions & 0 deletions docs/cards.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,13 +135,15 @@ Try hovering over then clicking on the cards below:

:::{card} Clickable Card (external)
:link: https://example.com
:link-alt: example

The entire card can be clicked to navigate to <https://example.com>.
:::

:::{card} Clickable Card (internal)
:link: cards-clickable
:link-type: ref
:link-alt: cards-clickable

The entire card can be clicked to navigate to the `cards-clickable` reference target.
:::
Expand Down Expand Up @@ -261,6 +263,9 @@ link
link-type
: Type of link: `url` (default), `ref`, `doc`, `any`.

link-alt
: Alternative text for the link (that will be used by screen-readers).

shadow
: The size of the shadow below the card: `none`, `sm` (default), `md`, `lg`.

Expand Down
17 changes: 13 additions & 4 deletions sphinx_design/cards.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ class CardDirective(SphinxDirective):
"img-background": directives.uri,
"link": directives.uri,
"link-type": make_choice(["url", "any", "ref", "doc"]),
"link-alt": directives.unchanged,
"shadow": make_choice(["none", "sm", "md", "lg"]),
"class-card": directives.class_option,
"class-header": directives.class_option,
Expand Down Expand Up @@ -159,25 +160,33 @@ def create_card(

if "link" in options:
link_container = PassthroughTextElement()
_classes = ["sd-stretched-link"]
_rawtext = options.get("link-alt") or ""
if options.get("link-alt"):
_classes.append("sd-hide-link-text")
if options.get("link-type", "url") == "url":
link = nodes.reference(
"",
_rawtext,
"",
refuri=options["link"],
classes=["sd-stretched-link"],
classes=_classes,
)
if options.get("link-alt"):
link.append(nodes.inline(_rawtext, _rawtext))
else:
options = {
# TODO the presence of classes raises an error if the link cannot be found
"classes": ["sd-stretched-link"],
"classes": _classes,
"reftarget": options["link"],
"refdoc": inst.env.docname,
"refdomain": "" if options["link-type"] == "any" else "std",
"reftype": options["link-type"],
"refexplicit": True,
"refwarn": True,
}
link = addnodes.pending_xref("", nodes.inline(), **options)
link = addnodes.pending_xref(
_rawtext, nodes.inline(_rawtext, _rawtext), **options
)
inst.set_source_info(link)
link_container += link
container.append(link_container)
Expand Down
2 changes: 1 addition & 1 deletion sphinx_design/compiled/style.min.css

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions sphinx_design/grids.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ class GridItemCardDirective(SphinxDirective):
"img-bottom": directives.uri,
"link": directives.uri,
"link-type": make_choice(["url", "any", "ref", "doc"]),
"link-alt": directives.unchanged,
"shadow": make_choice(["none", "sm", "md", "lg"]),
"class-item": directives.class_option,
"class-card": directives.class_option,
Expand Down Expand Up @@ -262,6 +263,7 @@ def run(self) -> List[nodes.Node]:
"img-bottom",
"link",
"link-type",
"link-alt",
"shadow",
"class-card",
"class-body",
Expand Down
4 changes: 4 additions & 0 deletions style/_button.scss
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@
z-index: 1;
content: "";
}

.sd-hide-link-text {
font-size: 0;
}

0 comments on commit adbcdf2

Please sign in to comment.