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

Is there a way for the use to be considered idle when the web page loses focus? ⚡️ #385

Open
2 tasks
mircealungu opened this issue Jan 27, 2024 · 7 comments
Assignees
Labels
enhancement A new feature or improvement to an existing feature. triage Has not been reviewed yet and should not be worked on.

Comments

@mircealungu
Copy link

What problem does your feature request solve?

from the app's POV, the fact that the user is not on the page anymore it is the same as being idle. Or?

Describe the solution you are proposing.

stop the counter on blur event?

Describe alternatives you have considered.

implementing it on my own, but it's less nice.

Importance

I Need It

Additional Context

  • This missing feature presents as a bug.
  • This missing feature is forcing me to consider alternatives.
@mircealungu mircealungu added enhancement A new feature or improvement to an existing feature. triage Has not been reviewed yet and should not be worked on. labels Jan 27, 2024
@SupremeTechnopriest
Copy link
Owner

Just add the blur event to the events prop.

@mircealungu
Copy link
Author

Ah, I see. Cool!

I tried with this code:

    useIdleTimer({
        onIdle,
        onActive,
        events: "blur",
        timeout: 30_000,
        throttle: 500,
    });

The problem is that on the page I would like to give 30seconds before becoming idle. However, when one defocuses the app, I would want that it goes automatically in idle. How would you do taht?

@SupremeTechnopriest
Copy link
Owner

The events prop needs to be an array. Try this as the value.

import { DEFAULT_EVENTS, useIdleTimer } from 'react-idle-timer'

...
useIdleTimer({
  onIdle,
  onActive,
  events: DEFAULT_EVENTS.concat('blur'),
  timeout: 30_000,
  throttle: 500,
});

@SupremeTechnopriest
Copy link
Owner

Oh you want them to immediately go idle? You can use immediateEvents

useIdleTimer({
  onIdle,
  onActive,
  immediateEvents: ['blur'],
  timeout: 30_000,
  throttle: 500,
});

@mircealungu
Copy link
Author

Nice! So basically, it will timeout after 30s of inactivity while in focus, or immediately on blur. This is what I wanted indeed!

@SupremeTechnopriest
Copy link
Owner

That's correct! Feel free to close if this solves your issue!

@mircealungu
Copy link
Author

The following does not work :(

    useIdleTimer({
        onIdle,
        onActive,
        timeout: 30_000,
        immediateEvents: ['blur'],
        throttle: 500,
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement A new feature or improvement to an existing feature. triage Has not been reviewed yet and should not be worked on.
Projects
None yet
Development

No branches or pull requests

2 participants