question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support Page Visibility API

See original GitHub issue

I 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jediecommented, Feb 16, 2022

This this should be added as a example in the docs, isn’it ?

EDIT: In the end i make this:

hx-trigger="every 2s [document.visibilityState === 'visible'], visibilitychange[document.visibilityState === 'visible'] from:document"

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

0reactions
adamchainzcommented, Jul 13, 2021

I can confirm this is working now in 1.5.0, with the syntax: hx-trigger="visibilitychange[document.visibilityState === 'visible'] from:document" 🥳

Read more comments on GitHub >

github_iconTop Results From Across the Web

Page Visibility | Can I use... Support tables for HTML5, CSS3, etc
"Can I use" provides up-to-date browser support tables for support of front-end web technologies on desktop and mobile web browsers.
Read more >
Page Visibility API
The Page Visibility API defines a means for site developers to programmatically determine the current visibility of a document and be notified of...
Read more >
Getting Started with the Page Visibility API - Section.io
The Page Visibility API is a browser API that provides a way to determine which browser tabs are currently active or idle.
Read more >
Using the Page Visibility API - web.dev
The Page Visibility API performs a simple but important function – it lets your application know when a page is visible to the...
Read more >
JavaScript API — Page Visibility API | by Rajan V - codeburst
Detect if current page is visible or in focus or not. The API is really simple. ... console.log("Browser doesn't supports the Page Visibility...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found