use_idle
Tracks whether the user is being inactive.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_idle::{use_idle, UseIdleReturn};
#[component]
fn Demo() -> impl IntoView {
let UseIdleReturn {
idle, last_active, ..
} = use_idle(5 * 60 * 1000); // 5 minutes
log!("{}", idle.get()); // true or false
view! { }
}
Programatically resetting:
use std::time::Duration;
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use_idle::{use_idle, UseIdleReturn};
#[component]
fn Demo() -> impl IntoView {
let UseIdleReturn {
idle, last_active, reset
} = use_idle(5 * 60 * 1000); // 5 minutes
reset(); // restarts the idle timer. Does not change the `last_active` value.
view! { }
}
SendWrapped Return
The returned closure reset is a sendwrapped function. It can
only be called from the same thread that called use_idle.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this will always return static signals
#![allow(unused)]
fn main() {
UseIdleReturn{
idle: Signal(initial_state),
last_active: Signal(now),
reset: || {}
}
}
Installation
cargo add leptos-use-idle
Options
UseIdleOptions
Options for use_idle_with_options.
Every option is set with a builder method of the same name, e.g. UseIdleOptions::default().events(...). See also the API docs.
events
Type: Vec<String>
Event names to listen to on window for detected user activity.
Default: vec!["mousemove", "mousedown", "resize", "keydown", "touchstart", "wheel"].
listen_for_visibility_change
Type: bool
Whether to listen for document visibility change.
Defaults to true.
initial_state
Type: bool
Initial state of the returned idle.
Defaults to false.
filter
Type: FilterOptions
Allows to debounce or throttle the event listener that is called for
every event (from events). Defaults to a throttle by 50ms.