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

Add tabindex to highlight template func markup #7194

Closed
gpanders opened this issue Apr 20, 2020 · 7 comments · Fixed by #8568
Closed

Add tabindex to highlight template func markup #7194

gpanders opened this issue Apr 20, 2020 · 7 comments · Fixed by #8568

Comments

@gpanders
Copy link

gpanders commented Apr 20, 2020

Code blocks generated through the {{ highlight }} shortcode or through the use of Markdown code fences should have the tabindex="0" attribute, allowing them to be tab focusable. This is good for accessibility; see here.

I've searched through the docs and I cannot find any way to set this attribute myself in my own site, since the highlight shortcode is (apparently) built-in to Hugo, so I don't think there is any way to do this outside of a change to Hugo itself.

@gpanders gpanders changed the title Scrollable highlighted code blocks should be focusable Scrollable highlighted code blocks should be tab focusable Apr 20, 2020
@bep bep added Enhancement and removed Proposal labels Apr 21, 2020
@bep bep changed the title Scrollable highlighted code blocks should be tab focusable Add tabindex to highlight shortcode markup Apr 21, 2020
@bep
Copy link
Member

bep commented Apr 21, 2020

so I don't think there is any way to do this outside of a change to Hugo itself.

You can copy this into your project:

https://github.com/gohugoio/hugo/blob/master/tpl/tplimpl/embedded/templates/shortcodes/highlight.html

@bep
Copy link
Member

bep commented Apr 21, 2020

Never mind that comment, I see that the shortcode is just delegating to the internal template func.

@bep bep added this to the v0.70 milestone Apr 21, 2020
@bep bep changed the title Add tabindex to highlight shortcode markup Add tabindex to highlight template func markup Apr 21, 2020
@bep bep modified the milestones: v0.70, v0.71 Apr 28, 2020
@bep bep modified the milestones: v0.71, v0.72 May 18, 2020
@bep bep modified the milestones: v0.72, v0.73 May 31, 2020
@bep bep modified the milestones: v0.73, v0.74 Jun 23, 2020
@bep bep modified the milestones: v0.74, v0.75 Jul 13, 2020
@bep bep modified the milestones: v0.75, v0.76 Sep 14, 2020
@bep bep modified the milestones: v0.76, v0.77 Oct 6, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@faraixyz
Copy link

faraixyz commented Dec 1, 2020

The internal function wraps the code in a div tag.

fmt.Fprint(w, `<div class="highlight">`)
if err := formatter.Format(w, style, iterator); err != nil {
return "", err
}
fmt.Fprint(w, `</div>`)

Would it be possible to move this to the short code?

{{ if len .Params | eq 2 }}{{ highlight (trim .Inner "\n\r") (.Get 0) (.Get 1) }}{{ else }}{{ highlight (trim .Inner "\n\r") (.Get 0) "" }}{{ end }}

That way users have more flexibility in wrapping the generated pre block.

@faraixyz
Copy link

faraixyz commented Mar 4, 2021

Hey again, I was thinking of this again since #8280 with attribute lists for code fences. Would it be possible to have a way to specify default attributes for code fences?

@bep bep modified the milestones: v0.78, v0.83 Apr 23, 2021
@sephore
Copy link

sephore commented Jun 8, 2021

Browsers like Firefox will allow scrollable elements to be tab focusable by default, so maybe it's a browser-level issue? I've been injecting tabindexes with replaceRE as a temporary workaround for Chrome users and etc, but it really sounds like a hack.

Edit: Oh yeah. And while using tabindex, Firefox is the one complaining about accessibility. 🤣 (Even tho it works fine.)

By forcing tabindex in ALL elements (even if not scrollable), the issue becomes "Focusable elements should have interactive semantics".

@rhymes
Copy link
Contributor

rhymes commented Jun 8, 2021

@sephore I have opened a PR to fix it here #8568 - I used Firexo and generated a demo Hugo site with highlighting and I wasn't able to focus on <pre>, you need that for accessibility purposes, see the documentation in the PR

@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep closed this as completed in #8568 Jul 15, 2021
bep pushed a commit that referenced this issue Jul 15, 2021
Currently the generated `<pre>` element isn't fully accessible as it can't be focused by keyboard users.
To make this fully accessible, the attribute `tabindex="0"` should be added to the `<pre>` tag.

Closes #7194
@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 17, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants