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

Support Page Visibility API #525

Closed
adamchainz opened this issue Jun 15, 2021 · 6 comments
Closed

Support Page Visibility API #525

adamchainz opened this issue Jun 15, 2021 · 6 comments

Comments

@adamchainz
Copy link
Sponsor Contributor

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...

@1cg
Copy link
Contributor

1cg commented Jun 15, 2021

seems like a great idea to me

could be a simple as adding a special case for document here:

function find(eltOrSelector, selector) {

are you interested in making this change? I can show you how to write tests, etc. for it...

@1cg
Copy link
Contributor

1cg commented Jul 6, 2021

I just added syntax for filtering polls:

40ed7b0

And it looks like the Document.hidden API could be used for this:

https://developer.mozilla.org/en-US/docs/Web/API/Document/hidden

With that, can I close this?

@adamchainz
Copy link
Sponsor Contributor Author

That doesn't quite allow making a request as soon as the tab gains focus, which would be useful for infrequently polled items.

Maybe the best solution is a little JS to fire custom events?

@1cg
Copy link
Contributor

1cg commented Jul 6, 2021

OK, I set it up so you can listen from:document now, so you can listen for the visibilitychange event on it, that seems like it would let you specify pretty much what you want

bbf2ae4

@adamchainz
Copy link
Sponsor Contributor Author

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

@jedie
Copy link

jedie commented 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: #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: #824

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants