Skip to content

Commit 032972b

Browse files
update indicator style to have visibility:hidden for screen readers (#3361)
* update indicator style to have visibility:hidden for screen readers * spelling
1 parent b0c87bf commit 032972b

2 files changed

Lines changed: 22 additions & 16 deletions

File tree

src/htmx.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5060,12 +5060,14 @@ var htmx = (function() {
50605060
function insertIndicatorStyles() {
50615061
if (htmx.config.includeIndicatorStyles !== false) {
50625062
const nonceAttribute = htmx.config.inlineStyleNonce ? ` nonce="${htmx.config.inlineStyleNonce}"` : ''
5063+
const indicator = htmx.config.indicatorClass
5064+
const request = htmx.config.requestClass
50635065
getDocument().head.insertAdjacentHTML('beforeend',
5064-
'<style' + nonceAttribute + '>\
5065-
.' + htmx.config.indicatorClass + '{opacity:0}\
5066-
.' + htmx.config.requestClass + ' .' + htmx.config.indicatorClass + '{opacity:1; transition: opacity 200ms ease-in;}\
5067-
.' + htmx.config.requestClass + '.' + htmx.config.indicatorClass + '{opacity:1; transition: opacity 200ms ease-in;}\
5068-
</style>')
5066+
`<style${nonceAttribute}>` +
5067+
`.${indicator}{opacity:0;visibility: hidden} ` +
5068+
`.${request} .${indicator}, .${request}.${indicator}{opacity:1;visibility: visible;transition: opacity 200ms ease-in}` +
5069+
'</style>'
5070+
)
50695071
}
50705072
}
50715073

www/content/attributes/hx-indicator.md

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,17 +43,18 @@ image. The image also has the `htmx-indicator` class on it, which defines an op
4343
that will show the spinner:
4444

4545
```css
46-
.htmx-indicator{
47-
opacity:0;
48-
transition: opacity 500ms ease-in;
46+
.htmx-indicator {
47+
opacity: 0;
48+
visibility: hidden;
4949
}
50-
.htmx-request .htmx-indicator{
51-
opacity:1;
52-
}
53-
.htmx-request.htmx-indicator{
54-
opacity:1;
50+
.htmx-request .htmx-indicator,
51+
.htmx-request.htmx-indicator {
52+
opacity: 1;
53+
visibility: visible;
54+
transition: opacity 200ms ease-in;
5555
}
5656
```
57+
This default `htmx-indicator` CSS also sets the visibility to hidden for better screen reader accessibility and does a quick fade in of the opacity.
5758

5859
If you would prefer a different effect for showing the spinner you could define and use your own indicator
5960
CSS. Here is an example that uses `display` rather than opacity (Note that we use `my-indicator` instead of `htmx-indicator`):
@@ -62,9 +63,7 @@ CSS. Here is an example that uses `display` rather than opacity (Note that we u
6263
.my-indicator{
6364
display:none;
6465
}
65-
.htmx-request .my-indicator{
66-
display:inline;
67-
}
66+
.htmx-request .my-indicator,
6867
.htmx-request.my-indicator{
6968
display:inline;
7069
}
@@ -102,3 +101,8 @@ This simulates what a spinner might look like in that situation:
102101
```html
103102
<meta name="htmx-config" content='{"includeIndicatorStyles": false}'>
104103
```
104+
* the `htmx-indicator` CSS added when this config is not disabled uses an inline style tag which may need you to set `inlineStyleNonce` config if you have a strict nonce based CSP policy for `style-src`
105+
```html
106+
<meta name="htmx-config" content='{"inlineStyleNonce": "random-nonce"}'>
107+
```
108+
* If your CSP needs to block all inline style tags then disable `includeIndicatorStyles` and host your own CSS file with a copy of your preferred `htmx-indicator` style from above

0 commit comments

Comments
 (0)