Support Page Visibility API
See original GitHub issueI wanted to create an element that fetches whenever the tab comes into focus, as detectable with the page visibility API. I tried:
hx-trigger="visibilitychange[visibilityState === 'visible'] from:document"
but this did not seem to work, since document
is not a valid CSS selector.
Supporting from:document
or similar might be all that’s needed to support this API, but it strikes me as something that htmx could abstract a little.
It could be useful to have virtual events for “tab became visible” and “tab became hidden”. Then the repetitive filter [visibilityState === 'visible']
could be avoided, and the events could work when allowEval
is false
.
Also there could be event modifiers for tab visible / tab hidden, so we could specify “poll only while the tab is visible”. Polling only while visible would even make a sensible default…
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:6 (5 by maintainers)
This this should be added as a example in the docs, isn’it ?
EDIT: In the end i make this:
See: https://github.com/bigskysoftware/htmx/discussions/821
But i think it’s should be easier to setup this, isn’t it? Think i will open a new issues for this.
EDIT2: https://github.com/bigskysoftware/htmx/issues/824
I can confirm this is working now in 1.5.0, with the syntax:
hx-trigger="visibilitychange[document.visibilityState === 'visible'] from:document"
🥳