Skip to content

Commit

Permalink
Improve accessibility for external links by adding aria-label (#1494)
Browse files Browse the repository at this point in the history
* Improve accessibility for external links by adding aria-label

* Add aria label for warning dialog
  • Loading branch information
ibrahimjaved12 authored Jan 28, 2025
1 parent a5ad7d6 commit 9dd3760
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 5 deletions.
13 changes: 9 additions & 4 deletions base-theme/layouts/partials/external_resource_link.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,14 @@
{{- $hasWarning := default true .has_external_license_warning -}}
{{- $className := printf "%s %s" "external-link" (default "" .class) -}}
{{- $onClick := "" -}}
{{- $ariaLabel := "" -}}
{{- if $hasWarning -}}
{{- $className = printf "%s %s" "external-link-warning" $className -}}
{{- $ariaLabel = printf "%s (opens warning dialog)" $text -}}
{{/* Prevents external links from being clicked before corresponding JS is fully loaded. */}}
{{- $onClick = "event.preventDefault()" -}}
{{- else -}}
{{- $ariaLabel = printf "%s (opens in a new tab)" $text -}}
{{- end -}}
{{- if not (in $href "ocw.mit.edu") -}}
{{- partial "link" (dict
Expand All @@ -15,14 +19,15 @@
"class" $className
"onClick" $onClick
"target" "_blank"
"ariaLabel" $ariaLabel
)
-}}
{{- else -}}
{{- partial "link" (dict
"href" $href
"text" $text
"class" .class
"onClick" ""
"href" $href
"text" $text
"class" .class
"onClick" ""
)
-}}
{{- end -}}
3 changes: 2 additions & 1 deletion base-theme/layouts/partials/link.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
{{- $text := .text | default "" -}}
{{- $target := .target | default "" -}}
{{- $onClick := .onClick | default "" -}}
{{- $ariaLabel := .ariaLabel | default "" -}}
{{- $stripLinkOffline := .stripLinkOffline | default false -}}
{{- $hideLinkOffline := .hideLinkOffline | default false -}}
{{- if $stripLinkOffline -}}
Expand All @@ -13,7 +14,7 @@
{{- $class = print $class " hide-offline" -}}
{{- end -}}
{{- if or $href $name -}}
<a {{ if $name }} name="{{ $name }}"{{ end }} {{ if $class }} class="{{ $class }}" {{ end }} {{ if $target }} target="{{ $target }}" {{ end }} {{ if $href }} href="{{ $href }}"{{ end }} {{ if $onClick }} onclick="{{ $onClick | safeJS }}" {{ end }} >{{ $text }}</a>
<a {{ if $name }} name="{{ $name }}"{{ end }} {{ if $class }} class="{{ $class }}" {{ end }} {{ if $target }} target="{{ $target }}" {{ end }} {{ if $href }} href="{{ $href }}"{{ end }} {{ if $ariaLabel }} aria-label="{{ $ariaLabel }}" {{ end }} {{ if $onClick }} onclick="{{ $onClick | safeJS }}" {{ end }}>{{ $text }}</a>
{{- else -}}
<span>{{ $text }}</span>
{{- end -}}

0 comments on commit 9dd3760

Please sign in to comment.