Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

👌 IMPROVE: Add link-alt to fix card link accessibility #89

Merged
merged 2 commits into from
Aug 18, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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;
}