Get Started
Installation
cargo add leptos-use
Examples
Usage Example
Simply import the functions you need from leptos-use
use leptos::prelude::*;
use leptos_use::{use_mouse, UseMouseReturn};
#[component]
fn Demo() -> impl IntoView {
let UseMouseReturn { x, y, .. } = use_mouse();
view! { cx,
{x} " x " {y}
}
}
Please refer to the functions list for more details.
Stable Rust
Just like leptos this library can be safely run on stable rust.
In the Getting Started section
of the leptos docs you can read more about what this means.
Options
Most functions in Leptos-Use come with a version ..._with_options. For example use_css_var has a
version use_css_var_with_options. As the name suggests, you can provide additional options to those versions of the
functions.
These options are defined as structs with the corresponding PascalCase name. For our example use_css_var_with_options
the name of the struct is UseCssVarOptions. Every option struct implements Default and the builder pattern to
make it easy to change only the values needed. This can look like the following example.
#![allow(unused)] fn main() { let (color, set_color) = use_css_var_with_options( "--color", UseCssVarOptions::default() .target(el) .initial_value("#eee"), ); }
Here only the values target and initial_value are changed and everything else is left to default.
TODO : automatic conversion like Fn and Option
Element Parameters
Many functions in this library operate on HTML/SVG elements. For example, the
function use_element_size returns the width and height of an element:
#![allow(unused)] fn main() { use leptos::{*, html::Div}; use leptos_use::{use_element_size, UseElementSizeReturn}; #[component] pub fn Component() -> impl IntoView { let el = NodeRef::<Div>::new(); let UseElementSizeReturn { width, height } = use_element_size(el); view! { <div node_ref=el></div> } } }
In the example above we used a Leptos NodeRef to pass into the function. But that is not
the only way you can do that. All of these work as well:
#![allow(unused)] fn main() { use_element_size(window().body()); // Option<web_sys::Element> use_element_size(window().body().unwrap()); // web_sys::Element use_element_size("div > p.some-class"); // &str or String intepreted as CSS selector pub fn some_directive(el: HtmlElement<AnyElement>) { use_element_size(el); // leptos::html::HtmlElement<T> } }
Signal of Strings: Signal<String>, ReadSignal<String>, RwSignal<String>, Memo<String>; also works with &str:
#![allow(unused)] fn main() { let (str_signal, set_str_signal) = signal("div > p.some-class"); use_element_size(str_signal); }
Signals of
Elements: Signal<web_sys::Element>, ReadSignal<web_sys::Element>, RwSignal<web_sys::Element>, Memo<web_sys::Element>;
also works with Option<web_sys::Element> and SendWrapper<web_sys::Element>:
#![allow(unused)] fn main() { let (el_signal, set_el_signal) = signal(document().query_selector("div > p.some-class").unwrap()); use_element_size(el_signal); }
How it works
Looking at the source code of use_element_size you'll find sth like
#![allow(unused)] fn main() { pub fn use_element_size(el: Into<ElementMaybeSignal<...>>) -> UseElementSizeReturn {} }
All the above code works because there are From implementations for all of these
types for ElementMaybeSignal.
ElementsMaybeSignal
Some functions work on one or more elements. Take use_resize_observer for example.
This works very much the same way as described above but instead of Into<ElementMaybeSignal>
it takes an Into<ElementsMaybeSignal> (note the plural). This means you can use it exactly in
the same ways as you saw with the singular ElementMaybeSignal. Only this time, when you use
String or &str it will be interpreted as CSS selector with query_selector_all.
But you can also use it with containers.
#![allow(unused)] fn main() { // Array of Option<web_sys::Element> use_resize_observer([window().body(), document().query_selector("div > p.some-class").unsrap()]); // Vec of &str. All of them will be interpreted as CSS selectors with query_selector_all() and the // results will be merged into one Vec. use_resize_observer(vec!["div > p.some-class", "p.some-class"]); // Slice of NodeRef let node_ref1 = NodeRef::<Div>::new(); let node_ref2 = NodeRef::<Div>::new(); use_resize_observer(vec![node_ref1, node_ref2].as_slice()); }
Usage in Options
Some functions have options that take Element(s)MaybeSignal.
They can be used in the same way.
#![allow(unused)] fn main() { use_mouse_with_options( UseMouseOptions::default().target("div > p.some-class") ); }
See also "Excluding Elements" in on_click_outside.
Server-Side Rendering
When using together with server-side rendering (SSR) you have to enable the feature ssr similar to
how you do it for leptos.
In your Cargo.toml file enable Leptos-Use's ssr feature only from your project's ssr feature:
[dependencies]
leptos-use = "0.10" # do NOT enable the "ssr" feature here
...
[features]
hydrate = [
"leptos/hydrate",
...
]
ssr = [
...
"leptos/ssr",
...
"leptos-use/ssr" # <== add this
]
...
Please see the ssr example in the examples folder
for a simple working demonstration.
Many functions work differently on the server and on the client. If that's the case you will find information about these differences in their respective docs under the section "Server-Side Rendering". If you don't find that section, it means that the function works exactly the same on both, the client and the server.
Do not enable the
ssrfeature directly!Don't do the following.
[dependencies] leptos-use = { version = "0.10", features = ["ssr"] } # this is wrong
The ssr feature is used to select which version of the functions are built.
Effectively it means your application is built two times: with ssr enabled to
build the server executable, and with ssr disabled to build the client's WASM
binary module.
So if you enable leptos-use's ssr feature globally, you will get the server
version of the functions in the client.
By adding "leptos-use/ssr" to the ssr feature of your project, it will only
be enabled when your project is built with ssr, and you will get the server
functions server-side, and the client functions client-side.
WASM on the server
If you enable ssr in your project on a wasm32 target architecture, you will get
a compile-time warning in the console because it is a common mistake that users enable ssr globally.
If you're using wasm32 on the server however you can safely disable this warning by
enabling the wasm_ssr feature together with ssr.
Functions with Target Elements
A lot of functions like use_resize_observer and use_element_size are only useful when a target HTML/SVG element is
available. This is not always the case on the server. If you use them with NodeRefs they will just work in SSR.
But what if you want to use them with window() or document()?
To enable that we provide the helper functions use_window()
and use_document() which return
a new-type-wrapped Option<web_sys::Window> or Option<web_sys::Document> respectively. These can be
used safely on the server. The following code works on both the client and the server:
#![allow(unused)] fn main() { use leptos::prelude::*; use leptos::ev::keyup; use leptos_use::{use_event_listener, use_window}; use_event_listener(use_window(), keyup, | evt| { ... }); }
There are some convenience methods provided as well, like use_document().body() which
just propagate a None on the server.
Encoding and Decoding Data
Several functions encode and decode data for storing it and/or sending it over the network. To do this, codecs
from the crate codee are used. They
implement the traits Encoder with the
method encode and Decoder with the
method decode.
There are two types of codecs: One that encodes as binary data (Vec[u8]) and another type that encodes as
strings (String). There is also an adapter
Base64 that can be used to
wrap a binary codec and make it a string codec by representing the binary data as a base64 string.
Please check the documentation of codee for more details and a list of all
available codecs.
Example
In this example, a codec is given to use_cookie that stores data as a string in the JSON
format. Since cookies can only store strings, we have to use string codecs here.
use leptos::*;
use leptos_use::use_cookie;
use serde::{Deserialize, Serialize};
use codee::string::JsonCodec;
#[component]
pub fn App(cx: Scope) -> impl IntoView {
#[derive(Serialize, Deserialize, Clone)]
struct MyState {
chicken_count: i32,
egg_count: i32,
}
let (cookie, set_cookie) = use_cookie::<MyState, JsonCodec>("my-state-cookie");
view! {}
}
Custom Codecs
If you don't find a suitable codec for your needs, you can implement your own; it's straightforward!
If you want to create a string codec, you can look at
JsonSerdeCodec.
In case it's a binary codec, have a look at
BincodeSerdeCodec.
Versioning
For a discussion on how to implement versioning please refer to the
relevant section in the docs for codee.
Changelog
The format is based on Keep a Changelog, and this project adheres to Semantic Versioning.
[0.16.3] - 2025-09-30
Change
- Implemented
PartialEqforcore::Position
Fixes π
- Fixed effects running simultaneously in
use_cookie(thanks to @ cbishopvelti) - Fixed link to Leptos Getting Started in the book (thanks to @mbergkvist)
- Fixed SSR in
use_interval_fn(thanks to @johansmitsnl)
Special thanks to our sponsor
- @benwis
[0.16.2] - 2025-06-18
Fixes π
use_color_modefrontend-only logic behind feature gate to fixSendWrapperissues with SSR (thanks to @BakerNet)
Special thanks to our sponsor
- @benwis
[0.16.1] - 2025-06-16
Dependency Fixes π
- Removed version pin for
chrono - Made
chronooptional inCargo.toml(thanks to @emilbonnek)
Special thanks to our sponsor
- @benwis
[0.16.0] - 2025-06-12
Breaking Change π
- Updated dependency Leptos to version
0.8.0(thanks to @DerKnerd, @jaystile, @ifiokjr)
New Features π
- Added
month_by_datetouse_calendar(thanks to @tomaszkubacki) - Added
target_offsettouse_draggable(thanks to @theredfish) UseRwSignalcan now be converted from anything that implements the respective reactive Leptos traits. This includes Signals, Memos, Store fields, ... meaning thesync_signalnow works with everything.ElementMaybeSignalcan be converted fromSignal<SendWrapper<web_sys::Element>>improving interop with other crates.
Fixes π
use_cookiewith SSR has been fixed. This fixes theuse_color_modeSSR issues as well.use_event_sourcewith infinity reconnect limit has been fixed.- Conversion of
ElementMaybeSignalfrom&strhas been fixed.
Special thanks to our sponsor
- @benwis
[0.16.0-beta2] - 2025-03-20
Breaking Change π
- Updated dependency Leptos to version
0.8.0(thanks to @DerKnerd, @jaystile)
New Feature π
- Added
target_offsettouse_draggableoptions for more flexible positioning (thanks to @theredfish)
Special thanks to our sponsors
- @benwis
- @spencewenski
[0.16.0-beta] - 2025-03-20
Breaking Change π
- Updated dependency Leptos to version
0.8.0-beta(thanks to @ifiokjr)
New Feature π
- Added
month_by_datetouse_calendar(thanks to @tomaszkubacki)
Special thanks to our sponsors
- @benwis
- @spencewenski
[0.16.0-alpha] - 2025-03-17
Breaking Changes π
- Removed the feature
spinand it's backend integration dependencyleptos-spin(thanks to @ifiokjr) - Updated dependency Leptos to version
0.8.0-alpha(thanks to @ifiokjr) - Updated dependency getrandom to version
0.3 - Updated dependency rand to version
0.9
Special thanks to our sponsor
- @spencewenski
[0.15.8] - 2025-06-12
New Features π
- Added
month_by_datetouse_calendar(thanks to @tomaszkubacki) - Added
target_offsettouse_draggable(thanks to @theredfish) UseRwSignalcan now be converted from anything that implements the respective reactive Leptos traits. This includes Signals, Memos, Store fields, ... meaning thesync_signalnow works with everything.ElementMaybeSignalcan be converted fromSignal<SendWrapper<web_sys::Element>>improving interop with other crates.
Fixes π
use_cookiewith SSR has been fixed. This fixes theuse_color_modeSSR issues as well.use_event_sourcewith infinity reconnect limit has been fixed.- Conversion of
ElementMaybeSignalfrom&strhas been fixed.
Special thanks to our sponsor
- @benwis
[0.15.7] - 2025-03-17
Change
- Updated codee version to 0.3.0 in line with the latest 0.7 Leptos versions
Special thanks to our sponsor
- @spencewenski
[0.15.6] - 2025-02-08
Fix π
- Fixed
use_storageoverwriting stored values with SSR (thanks to @BakerNet).
Special thanks to our sponsor
- @spencewenski
[0.15.5] - 2025-01-15
Fix π
sync_signalwithimmediate = truenow syncs the signals on the server once initially. This fixesuse_color_modewith cookies enabled to give wrong results during SSR.
Special thanks to our sponsor
- @spencewenski
[0.15.4] - 2025-01-15
Fixes π
- downgraded codee to 0.2.0 to be compatible with Leptos 0.7
- fixed
use_mutation_observer(thanks to @bpwarner)
Changes π₯
- improxed DX: implemented
Clone + CopyforUseDropZoneReturn(thanks to @mahmoud-eltahawy)
Special thanks to our sponsor
- @spencewenski
[0.15.3] - 2025-01-08 (yanked)
New Function π
use_calendar
Fix π
- added
DebugtoSize(thanks to @Ahlman)
Special thanks to our sponsor
- @spencewenski
[0.15.2] - 2025-01-03
Fixes π
- Fixed path of
use_color_modecookie ElementMaybeSignalandElementsMaybeSignalare now properlyCloneandCopy
Special thanks to our sponsor
- @spencewenski
[0.15.1] - 2024-12-31
Fixes π
- Fixed
use_element_hovernot properly cancelling it's timeout (thanks to @jcold). - Fixed
use_storagenot writing default values. - Fixed unidirectional
sync_signalnot syncing properly.
Special thanks to our sponsors
- @spencewenski
- LeftClick
[0.15.0] - 2024-12-17
New Functions π
signal_throttledandsignal_debouncednow have..._localvariants (thanks to @bicarlsen)
Breaking Changes π
use_storagenow accepts a Signal as it'skeyparameter (thanks to LeftClick)use_websocketnow supports sending heartbeats (thanks to LeftClick)
Fix π
- Fixed
use_storageto actually remove the key whenremoveis called (thanks to @flaviopezzini)
Special thanks to our sponsors
- @spencewenski
- LeftClick
[0.14.0] - 2024-12-01
Highlights since 0.13
- Updated to Leptos 0.7
- Refactored
ElementMaybeSignalandElementsMaybeSignalto have a simpler implementation. For the vast majority of cases this should continue to work as before. - Almost everything returned from functions is now
Send + Sync.
Changes since 0.14.0-rc5
- Updated Leptos to use stable 0.7 version
- Updated wasm-bindgen to 0.2.96
- Updated web-sys 0.3.73
Special thanks to our sponsor:
- @spencewenski
[0.14.0-rc5] - 2024-11-27
- fixed error messages for get_header
- added Send + Sync to storage return closure
Special thanks to our sponsor:
- @spencewenski
[0.14.0-rc4] - 2024-11-26
- Updated to Leptos 0.7.0-rc2
- Fixed WASM on the serverside (thanks to @GuillaumeDelorme)
- Fixed
use_storage. - Made all returned closures
Send + Sync.
Special thanks to our sponsor:
- @spencewenski
[0.14.0-rc3] - 2024-11-14
- Fixed potential SSR panic in use_locale(s) (thanks to @veigaribo)
- Make
use_localeprioritize user preferred locales over app preferred ones
[0.14.0-rc2] - 2024-11-10
- Updated to Leptos 0.7.0-rc1
- Updated to web-sys 0.3.72 and unpinned version (thanks to @sabify)
- Added dependabot (thanks to @sabify)
- Reverted use_user_media to have video enabled by default
- Fixed exponential increase on websocket reconnects
[0.14.0-rc1] - 2024-11-06
- Fixed MediaTrackConstraints dependency (thanks to @mollymorphous)
- Fixed warnings and tests (thanks to @SleeplessOne1917 and @jheuel)
- Unpinned the wasm-bindgen version (thanks to @jheuel)
[0.14.0-rc0] - 2024-11-03
- Latest changes up to version 0.13.7 ported
- Updated to Leptos 0.7.0-rc0
[0.14.0-gamma2] - 2024-10-16
- Updated to Leptos 0.7.0-gamma3 by using
Signalinstead ofMaybeSignal
[0.14.0-gamma1] - 2024-10-10
- Adapted to the latest changes in Leptos (thanks to @BakerNet and @nikessel)
- Fixed all the examples
use_active_elementporteduse_drop_zonenow returnsSignal<Vec<SendSignal<web_sys::File>>>instead ofSignal<Vec<web_sys::File>, LocalStorage>to make it easier to use with<For>
[0.14.0-beta4] - 2024-09-15
- Latest changes from version 0.13.4 and 0.13.5 ported
[0.14.0-beta3] - 2024-09-02
Breaking Changes π
- Refactored
ElementMaybeSignalandElementsMaybeSignalto have a simpler implementation. For the vast majority of cases this should continue to work as before.
[0.14.0-beta2] - 2024-09-09
Change π₯
- Latest Leptos 0.7 beta changed the trigger trait method (thanks to @BakerNet)
- Latest changes from version 0.13.3 ported
[0.14.0-beta1] - 2024-09-02
Ported everything to Leptos 0.7 Some example don't run yet.
[0.13.12] - 2025-01-03
- Fixed path of
use_color_modecookie
Thanks to our generous sponsor:
- @spencewenski
[0.13.11] - 2024-11-22
- Updated web-sys version to 0.3.72
Thanks to our generous sponsor:
- @spencewenski
[0.13.10] - 2024-11-14
- Fixed potential SSR crash in
use_locale(s)(thanks to @veigaribo)
[0.13.9] - 2024-11-10
- Reverted use_user_media to have video enabled by default
- Fixed exponential increase on websocket reconnects
[0.13.8] - 2024-11-06
- Backported fixes from 0.14.0-rc1
[0.13.7] - 2024-10-20
- Added video and audio options to
use_user_media(thanks to @sauloco). - Fixed cookies in SSR (thanks to @jim-taylor-business).
[0.13.6] - 2024-10-20
- Updated leptos-spin version to 0.2 (thanks to @tqq1994516).
[0.13.5] - 2024-09-15
New Function π
use_textarea_autosize
[0.13.4] - 2024-09-05
Fix π
use_websocketnow returns a signal for the websocket instance so the user can actually use it. Before it always returnedNone.
[0.13.3] - 2024-09-02
Fix π
- Fixed
use_color_modewith cookies enabled
[0.13.2] - 2024-09-02
Fix π
- Fixed web-sys
unstable_apisflag foruse_web_lock
[0.13.1] - 2024-09-01
New Functions π
use_web_lockuse_window_size
Change π₯
UseWebsocket::protocolsnow supports a signal. It is read right beforeopenis called. (thanks to @zakstucke)
[0.13.0] - 2024-08-28
New Functions π
use_toggleuse_prefers_reduced_motion(thanks to @hcandelaria)
Breaking Changes π
use_websocketnow supports different types for sending and receiving messagesSyncSignalOptionsnow can take now either transformations or assignment functions but not both.- updated to
codeeversion 0.2.0
Fixes π
use_websocketfixed error with cleanup and reconnect (thanks to @BakerNet).
New Features π
- There is now a feature for almost every function to get better compile and rust-analyzer times.
use_web_notificationnow supports thevibrateoption (thanks to @hcandelaria).UseDocumentnow supports a whole bunch of methods more fromdocument(thanks to @luckynumberke7in).
[0.12.0] - 2024-08-14
Make sure you also update
cargo-leptosto the latest version if you use that.
Breaking Changes π
- Updated to web_sys 0.3.70 which unfortunately is breaking some things.
use_clipboarddoesn't need the unstable flags anymore.use_localenow usesunic_langid::LanguageIdentifierand proper locale matching (thanks to @mondeja).- Removed
UseMouseEventExtractorDefaultand reworkedUseMouseCoordType(thanks to @carloskiki) use_preferred_darkanduse_color_modenow try to read theSec-CH-Prefers-Color-Schemeheader in SSR. This brings the necessity to enable an additional feature for them (axum/actix/spin).
Fixes π
- Fixed the codec chapter in the book to refer to crate
codee.
[0.11.4] - 2024-08-12
New Features π
use_web_notificationnow supports the optionsrenotify,silentandimage(thanks to @hcandelaria).sync_signalno supports the optionsassign_ltrandassign_rtl.
[0.11.3] - 2024-07-31
Fix π
- Made
use_timeout_fnSSR-safe
[0.11.2] - 2024-07-30
Change π₯
use_localehas now a supported locale list.
(yanked) [0.11.1] - 2024-07-28
New Functions π
use_locale(thanks to @BrandonDyer64)use_locales(thanks to @BrandonDyer64)headerβ Standard implementations for reading a header on the server.
[0.11.0] - 2024-07-27
New Functions π
use_user_media
New Features π
-
Codecs:
- All codecs now live in their own crate
codee - There are now binary codecs in addition to string codecs.
FromToBytesCodecWebpackSerdeCodecBincodeSerdeCodecProstCodec(see also the section "Breaking Changes π " below)
- Every binary codec can be used as a string codec with the
Base64wrapper which encodes the binary data as a base64 string.- This required feature
base64 - It can be wrapped for example like this:
Base64<WebpackSerdeCodec>.
- This required feature
- There is now an
OptionCodecwrapper that allows to wrap any string codec that encodesTto encodeOption<T>.- Use it like this:
OptionCodec<FromToStringCodec<f64>>.
- Use it like this:
- All codecs now live in their own crate
-
ElementMaybeSignalis now implemented forwebsys::HtmlElement(thanks to @blorbb). -
UseStorageOptionsnow hasdelay_during_hydrationwhich has to be used when you conditionally show parts of the DOM controlled by a value from storage. This leads to hydration errors which can be fixed by setting this new option totrue. -
cookie::SameSiteis now re-exported -
Changing the signal returned by
use_cookienow tries and changes the headers during SSR. -
New book chapter about codecs
-
The macro
use_derive_signal!is now exported (thanks to @mscofield0).
Breaking Changes π
UseStorageOptionsandUseEventSourceOptionsno longer accept acodecvalue because this is already provided as a generic parameter to the respective function calls.- Codecs have been refactored. There are now two traits that codecs implement:
EncoderandDecoder. The traitStringCodecis gone. The methods are now associated methods and their params now always take references.JsonCodechas been renamed toJsonSerdeCodec.- The feature to enable this codec is now called
json_serdeinstead of justserde. ProstCodecnow encodes as binary data. If you want to keep using it with string data you can wrap it like this:Base64<ProstCodec>.- All of these structs, traits and features now live in their own crate called
codee - A bunch of new codecs are available. Have a look at the docs for crate
codee.
use_websocket:UseWebsocketOptionshas been renamed toUseWebSocketOptions(uppercase S) to be consistent with the return type.UseWebSocketOptions::reconnect_limitandUseEventSourceOptions::reconnect_limitis nowReconnectLimitinstead ofu64. UseReconnectLimit::Infinitefor infinite retries orReconnectLimit::Limited(...)for limited retries.use_websocketnow uses codecs to send typed messages over the network.- When calling you have give type parameters for the message type and the
codec:
use_websocket::<String, WebpackSerdeCodec> - You can use binary or string codecs.
- The
UseWebSocketReturn::sendclosure now takes a&Twhich is encoded using the codec. - The
UseWebSocketReturn::messagesignal now returns anOption<T>which is decoded using the codec. UseWebSocketReturn::send_bytesandUseWebSocketReturn::message_bytesare gone.UseWebSocketOptions::on_messageandUseWebSocketOptions::on_message_byteshave been renamed toon_message_rawandon_message_raw_bytes.- The new
UseWebSocketOptions::on_messagetakes a&T. UseWebSocketOptions::on_errornow takes aUseWebSocketErrorinstead of aweb_sys::Event.
- When calling you have give type parameters for the message type and the
codec:
use_storagenow always saves the default value to storage if the key doesn't exist yet.- Renamed
BreakpointsSematictoBreakpointsSemanticandbreakpoints_sematictobreakpoints_semantic(note then) (thanks to @mondeja).
Fixes π
- Fixed auto-reconnect in
use_websocket - Fixed typo in compiler error messages in
use_cookie(thanks to @SleeplessOne1917). - Fixed potential signal out of scope issue with
use_raf_fn
Other Changes π₯
- Better links in docs that work both in the book and in rustdoc (thanks to @mondeja).
- Better CI/CD (thanks to @EstebanBorai).
[0.10.10] - 2024-05-10
Change π₯
- Added compile-time warning when you use
ssrfeature withwasm32. You can enablewasm_ssrto remove the warning.
[0.10.9] - 2024-04-27
Fixes π
- Fixed
use_color_modewithout cookies and make cookies sync properly with local storage - Fixed
use_infinite_scrolledge case bug with disposed signals
[0.10.8] - 2024-04-19
Change π₯
use_cookienow supports Spin out of the box (thanks to @javierEd).
[0.10.7] - 2024-04-10
New Function π
sync_signal
Change π₯
use_color_modenow supports cookies.
[0.10.6] - 2024-04-02
Fixes π
- Corrected docs of
use_cookie'smax-ageunit to milliseconds (thanks to @sify21). - Fixed setting multiple cookies in the browser (thanks to @sbking).
[0.10.5] - 2024-03-12
Fix π
- Fixed SSR detection from an url query parameter for
use_color_mode(thanks to @mondeja).
[0.10.4] - 2024-03-05
New Functions π
use_event_source
Changes π₯
- Wrapped callbacks in a non-reactive zone to remove potential warnings.
- Updated SSR chapter in the book to make it more clear and beginner-friendly (thanks to @flupke).
[0.10.3] - 2024-02-23
New Functions π
use_oruse_anduse_not
Fix π
- Removed signal warnings from
use_websocket'ssend...methods.
Changes π₯
use_color_modenow supports detection from an url query parameter. (thanks to @mondeja)
[0.10.2] - 2024-02-09
New Functions π
use_permissionuse_clipboarduse_timeout_fn
[0.10.1] - 2024-01-31
Fix π
- Fixed docs.rs build
[0.10.0] - 2024-01-31
New Functions π
use_broadcast_channeluse_cookie(thanks to @rakshith-ravi)use_mouse_in_elementuse_device_orientation(thanks to @mondeja)use_device_pixel_ratio(thanks to @mondeja)use_element_bounding
Breaking Changes π
- The
leptosversion is now 0.6 - The trait
Codechas been renamed toStringCodecand has been moved toutil::StringCodec.- The struct
StringCodechas been renamed toFromToStringCodecand has been moved toutil::FromToStringCodec. - The structs
JsonCodecandProstCodechave been moved toutilas well.
- The struct
- The function
use_storagenow requires type parameters for the stored type and the codec like all the other...storage...functions.
Fixes π
- Fixed
use_geolocationSSR compile issue - Fixed
use_intl_number_formatmaximum fraction digits option
Changes π₯
- The
UseMouseReturnsignalsx,y, andsource_typeare now of typeSignal<f64>instead ofReadSignal<f64>. - You can now convert
leptos::html::HtmlElement<T>intoElement(s)MaybeSignal. This should make functions a lot easier to use in directives. - There's now a chapter in the book especially for
Element(s)MaybeSignal. - Throttled or debounced callbacks (in watch__ or __fn) no longer are called after the containing scope was cleaned up.
- The document returned from
use_documentnow supports the methodsquery_selectorandquery_selector_all.
[0.9.0] - 2023-12-06
New Functions π
use_display_media(thanks to @seanaye)
Breaking Changes π
- (@feral-dot-io) The use
use_<type>_storagefunctions have been rewritten to useCodecs instead of always requiringserde.- This also removes the feature
storage - By default the
StringCodecis used which relies on types implementingFromString + ToString - If you want to use
JsonCodecyou have to enable the featureserde - If you want to use
ProstCodec(new!) you have to enable the featureprost.
- This also removes the feature
- (@feral-dot-io) The Rust flag
--cfg=web_sys_unstable_apisis not needed anymore since relevantweb_sysAPIs are now stable. This affects in particularuse_element_sizeuse_resize_observer
Fixes π
use_raf_fnanduse_timestampno longer spam warnings because ofgetting signals outside of reactive contexts.use_infinite_scrollno longer calls the callback twice for the same eventuse_scrollnow usestry_get_untrackedin the debounced callback to avoid panics if the context has been destroyed while the callback was waiting to be called.use_idleworks properly now (no more idles too early).use_web_notificationdoesn't panic on the server anymore.
[0.8.2] - 2023-11-09
Fixes π
- Fixed SSR for
- use_timestamp
- use_raf_fn
- use_idle
[0.8.1] - 2023-10-28
Fixes π
- Using strings for
ElementMaybeSignalandElementsMaybeSignalis now SSR safe.- This fixes specifically
use_color_modeto work on the server.
- This fixes specifically
[0.8.0] - 2023-10-24
New Functions π
use_web_notification(thanks to @centershocks44)use_infinite_scrolluse_service_worker(thanks to @lpotthast)
Breaking Changes π
use_scrollreturnsimpl Fn(T) + Cloneinstead ofBox<dyn Fn(T)>.
Other Changes π₯
UseScrollReturnis now documented
[0.7.2] - 2023-10-21
Fixes π
- Some functions still used
window()which could lead to panics in SSR. This is now fixed. Specifically foruse_draggable.
[0.7.1] - 2023-10-02
New Function π
use_sorted
[0.7.0] - 2023-09-30
New Functions π
use_timestampuse_idleuse_documentuse_windowuse_geolocationsignal_debouncedsignal_throttled
Breaking Changes π
- Leptos version is now 0.5
- No
cx: Scopeparams are supported/needed anymore because of the changes in Leptos. Please check the release notes of Leptos 0.5 for how to upgrade. watchis now deprecated in favor ofleptos::watchand will be removed in a future release.watch_with_optionswill continue to exist.use_event_listener_with_optionsnow takes aUseEventListenerOptionsinstead of aweb_sys::AddEventListenerOptions.use_mutation_observer_with_optionsnow takes aUseMutationObserverOptionsinstead of aweb_sys::MutationObserverInit.use_websocket:- takes now a
&strinstead of aStringas itsurlparameter. - same for the returned
sendmethod. - The
ready_statereturn type is now renamed toConnectionReadyStateinstead ofUseWebSocketReadyState. - The returned signals
ready_state,message,message_byteshave now the typeSignal<...>instead ofReadSignal<...>to make them more consistent with other functions. - The options
reconnect_limitandreconnect_intervalnow take au64instead ofOption<u64>to improve DX. - The option
manualhas been renamed toimmediateto make it more consistent with other functions. To port please note thatimmediateis the inverse ofmanual(immediate=!manual). - Added documentation how pass it ergonomically as context.
- takes now a
use_color_mode:- The optional
on_changedhandler parameters have changed slightly. Please refer to the docs for more details.
- The optional
- Throttled or debounced functions cannot be
FnOnceanymore. - All traits
ClonableFn...have been removed.
Other Changes π₯
use_websocketcan use relative urls now- Callbacks in options don't require to be cloneable anymore
- Callback in
use_raf_fndoesn't require to be cloneable anymore - All (!) functions can now be safely called on the server. Specifically this includes the following that before
panicked on the server:
use_scrolluse_event_listeneruse_element_hoveron_click_outsideuse_drop_zoneuse_element_sizeuse_element_visibilityuse_resize_observeruse_intersection_observeruse_mutation_observer
Fixes π
use_element_visibilitydidn't work in some cases on Chrome properly. This has been fixed.
[0.6.3] - 2023-08-12
Fixes π
use_websocketpanicked after unmount
[0.6.2] - 2023-08-03
Fixes π
use_event_listener_with_optionsremoves the handlers now correctly.
[0.6.1] - 2023-08-03
Fixes π
use_storagenow uses.get_untracked()to avoid warnings.
[0.6.0] - 2023-07-17
New Functions π
use_draggableuse_to_stringis_erris_okis_noneis_someuse_raf_fn
Breaking Changes π
- The following functions now accept a
MaybeRwSignalas their initial/default value which means you can use a synchronizedRwSignalin those places.use_color_modeuse_cycle_listuse_faviconuse_storageuse_local_storageuse_session_storage
- Instead of returning
ReadSignal, the following functions now returnSignal.use_color_modeuse_faviconuse_storageuse_local_storageuse_session_storage
Fixes π
use_drop_zonenow uses.get_untracked()in event handlers
[0.5.0] - 2023-07-15
New Functions π
use_drop_zoneuse_websocket(thanks @sectore)use_intl_number_format
Changes π₯
- Crate is ready for Server-Side Rendering. Enable feature
ssrlike you do forleptos.
[0.4.1] - 2023-07-07
New Functions π
use_window_focususe_window_scrolluse_document_visibility
[0.4.0] - 2023-07-03
Breaking Changes π
- Required
leptosversion is now 0.4 - Following the changes in
leptosthere is no longer astablecrate feature required in order to use this library with a stable toolchain. If you want to use it with a nightly toolchain you have to enable thenightlyfeature only onleptosdirectly. No change is required forleptos-useitself.
[0.3.3] - 2023-06-24
New Functions π
use_color_modeuse_cycle_listuse_active_element
Changes π₯
- You can now use this crate with the
stabletoolchain (thanks @lpotthast) - Set leptos dependency to
default-features = falsein order to enable SSR.
[0.3.2] - 2023-06-17
New Functions π
use_css_varuse_element_hover
[0.3.1] - 2023-06-15
New Functions π
use_interval_fnuse_interval
[0.3.0] - 2023-06-13
Breaking Changes π
use_event_listenerno longer returns aBox<dyn Fn()>but aimpl Fn() + Clone
Changes π₯
- You can now specify a
&strorSignal<String>with CSS selectors wherever a node ref is accepted - Callbacks of the following functions no longer require
Cloneuse_resize_observeruse_intersection_observer
- These functions now also accept multiple target elements in addition to a single one:
use_resize_observeruse_intersection_observer
New Functions π
wheneveruse_mutation_observeruse_abson_click_outside
[0.2.1] - 2023-06-11
New Functions
use_intersection_observeruse_element_visibility
[0.2.0] - 2023-06-11
Breaking Changes
watchdoesn't acceptimmediateas a direct argument anymore. This is only provided by the option variant.watchhas now variantwatch_with_optionswhich allows for debouncing and throttling.
New Functions
use_storageuse_local_storageuse_session_storagewatch_debouncedwatch_throttledwatch_pausableuse_ceiluse_rounduse_media_queryuse_preferred_darkuse_preferred_contrastuse_faviconuse_breakpoints
Other Changes
- Function count badge in readme
[0.1.8/9] - 2023-06-05
- Fixed documentation and doc tests running for functions behind
#[cfg(web_sys_unstable_apis)]
[0.1.7] - 2023-06-05
New Function
use_element_size
[0.1.6] - 2023-06-03
Changes
- Fixed documentation so all feature are documented
[0.1.5] - 2023-06-03
New Functions
use_flooruse_maxuse_min
Changes
- New feature:
maththat has to be activated in order to use the math functions.
[0.1.4] - 2023-06-02
New Functions
use_supporteduse_resize_observerwatchuse_mouse
Changes
- Use the crate
default-struct-builderto provide ergonimic function options.
[0.1.3] - 2023-05-28
New Functions
use_scrolluse_debounce_fn
Other Changes
- Better and more beautiful demo integration into the guide.
Functions
Storage
- use_local_storage β Reactive LocalStorage.
- use_session_storage β Reactive SessionStorage.
- use_storage β Reactive Storage.
Elements
- use_active_element β Reactive
document.activeElement - use_document β SSR safe
document(). - use_document_visibility β Reactively track
document.visibilityState - use_draggable β Make elements draggable.
- use_drop_zone β Create a zone where files can be dropped.
- use_element_bounding β Reactive bounding box of an HTML element
- use_element_size β Reactive size of an HTML element.
- use_element_visibility β Tracks the visibility of an element within the viewport.
- use_intersection_observer β Reactive IntersectionObserver.
- use_mouse_in_element β Reactive mouse position related to an element.
- use_mutation_observer β Reactive MutationObserver.
- use_resize_observer β Reports changes to the dimensions of an Element's content or the border-box.
- use_window β SSR safe
window(). - use_window_focus β Reactively track window focus
- use_window_scroll β Reactive window scroll.
- use_window_size β Reactive window size.
Browser
- use_breakpoints β Reactive viewport breakpoints.
- use_broadcast_channel β Reactive BroadcastChannel API.
- use_clipboard β Reactive Clipboard API.
- use_color_mode β Reactive color mode (dark / light / customs) with auto data persistence.
- use_cookie β SSR-friendly and reactive cookie access.
- use_css_var β Manipulate CSS variables.
- use_display_media β Reactive
mediaDevices.getDisplayMediastreaming. - use_event_listener β Use EventListener with ease.
- use_favicon β Reactive favicon.
- use_media_query β Reactive Media Query.
- use_permission β Reactive Permissions API.
- use_preferred_contrast β Reactive prefers-contrast media query.
- use_preferred_dark β Reactive dark theme preference.
- use_prefers_reduced_motion β Reactive reduced motions preference.
- use_service_worker β Reactive ServiceWorker API.
- use_textarea_autosize β Automatically update the height of a textarea depending on the content.
- use_user_media β Reactive
mediaDevices.getUserMediastreaming. - use_web_lock β Rustified Web Locks API.
- use_web_notification β Reactive Notification API.
Intl
- use_intl_number_format β Reactive
Intl.NumberFormat. - use_locale β Reactive locale matching.
- use_locales β Reactive locales.
Sensors
- on_click_outside β Listen for clicks outside an element.
- use_device_orientation β Reactive DeviceOrientationEvent.
- use_device_pixel_ratio β Reactive
window.devicePixelRatio - use_element_hover β Reactive element's hover state.
- use_geolocation β Reactive Geolocation API.
- use_idle β Tracks whether the user is being inactive.
- use_infinite_scroll β Infinite scrolling of the element.
- use_mouse β Reactive mouse position
- use_scroll β We have to check if the scroll amount is close enough to some threshold in order to
Network
- use_event_source β Reactive EventSource
- use_websocket β Creating and managing a Websocket connection.
Animation
- use_interval β Reactive counter increases on every interval.
- use_interval_fn β Wrapper for
set_intervalwith controls. - use_raf_fn β Call function on every requestAnimationFrame.
- use_timeout_fn β Wrapper for
setTimeoutwith controls. - use_timestamp β Reactive current timestamp.
Watch
- watch_debounced β A debounced version of [
watch]. - watch_pausable β Pausable [
watch]. - watch_throttled β A throttled version of
leptos::watch. - watch_with_options β A version of
leptos::watchbut with additional options. - whenever β Shorthand for watching a signal to be
true.
Utilities
Math
- use_abs β Reactive
abs(). - use_and β Reactive
ANDcondition. - use_ceil β Reactive
ceil(). - use_floor β Reactive
floor(). - use_max β Reactive
max(). - use_min β Reactive
min(). - use_not β Reactive
NOTcondition. - use_or β Reactive
ORcondition. - use_round β Reactive
round().
use_local_storage
Reactive LocalStorage.
LocalStorage stores data in the browser with no expiration time. Access is given to all pages from the same origin (e.g., all pages from "https://example.com" share the same origin). While data doesn't expire the user can view, modify and delete all data stored. Browsers allow 5MB of data to be stored.
This is in contrast to use_session_storage which clears data when the page session ends and is not shared.
Usage
See use_storage for more details on how to use.
Feature
This function is only available if the crate feature
storageis enabled
Source
use_session_storage
Reactive SessionStorage.
SessionStorages stores data in the browser that is deleted when the page session ends. A page session ends when the browser closes the tab. Data is not shared between pages. While data doesn't expire the user can view, modify and delete all data stored. Browsers allow 5MB of data to be stored.
Use use_local_storage to store data that is shared amongst all pages with the same origin and persists between page sessions.
Usage
See use_storage for more details on how to use.
Feature
This function is only available if the crate feature
storageis enabled
Source
use_storage
Reactive Storage.
The function returns a triplet (read_signal, write_signal, delete_from_storage_fn).
Demo
Usage
Pass a StorageType to determine the kind of key-value browser storage to use.
The specified key is where data is stored. All values are stored as UTF-16 strings which
is then encoded and decoded via the given *Codec. This value is synced with other calls using
the same key on the same page and across tabs for local storage.
See UseStorageOptions to see how behavior can be further customised.
Values are (en)decoded via the given codec. You can use any of the string codecs or a
binary codec wrapped in Base64.
Please check the codec chapter to see what codecs are available and what feature flags they require.
Example
use leptos::prelude::*;
use leptos_use::storage::{StorageType, use_local_storage, use_session_storage, use_storage};
use serde::{Deserialize, Serialize};
use codee::string::{FromToStringCodec, JsonSerdeCodec, Base64};
use codee::binary::ProstCodec;
#[component]
pub fn Demo() -> impl IntoView {
// Binds a struct:
let (state, set_state, _) = use_local_storage::<MyState, JsonSerdeCodec>("my-state");
// Binds a bool, stored as a string:
let (flag, set_flag, remove_flag) = use_session_storage::<bool, FromToStringCodec>("my-flag");
// Binds a number, stored as a string:
let (count, set_count, _) = use_session_storage::<i32, FromToStringCodec>("my-count");
// Binds a number, stored in JSON:
let (count, set_count, _) = use_session_storage::<i32, JsonSerdeCodec>("my-count-kept-in-js");
// Bind string with SessionStorage stored in ProtoBuf format:
let (id, set_id, _) = use_storage::<String, Base64<ProstCodec>>(
StorageType::Session,
"my-id",
);
view! { }
}
// Data stored in JSON must implement Serialize, Deserialize.
// And you have to add the feature "serde" to your project's Cargo.toml
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct MyState {
pub hello: String,
pub greeting: String,
}
// Default can be used to implement initial or deleted values.
// You can also use a signal via UseStorageOptions::default_value`
impl Default for MyState {
fn default() -> Self {
Self {
hello: "hi".to_string(),
greeting: "Hello".to_string()
}
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned signals will just read/manipulate the initial_value without persistence.
Hydration bugs and use_cookie
If you use a value from storage to control conditional rendering you might run into issues with hydration.
use leptos::prelude::*;
use leptos_use::storage::use_session_storage;
use codee::string::FromToStringCodec;
#[component]
pub fn Example() -> impl IntoView {
let (flag, set_flag, _) = use_session_storage::<bool, FromToStringCodec>("my-flag");
view! {
<Show when=move || flag.get()>
<div>Some conditional content</div>
</Show>
}
}
You can see hydration warnings in the browser console and the conditional parts of
the app might never show up when rendered on the server and then hydrated in the browser. The
reason for this is that the server has no access to storage and therefore will always use
initial_value as described above. So on the server your app is always rendered as if
the value from storage was initial_value. Then in the browser the actual stored value is used
which might be different, hence during hydration the DOM looks different from the one rendered
on the server which produces the hydration warnings.
The recommended way to avoid this is to use use_cookie instead because values stored in cookies
are available on the server as well as in the browser.
If you still want to use storage instead of cookies you can use the delay_during_hydration
option that will use the initial_value during hydration just as on the server and delay loading
the value from storage by an animation frame. This gets rid of the hydration warnings and makes
the app correctly render things. Some flickering might be unavoidable though.
use leptos::prelude::*;
use leptos_use::storage::{use_local_storage_with_options, UseStorageOptions};
use codee::string::FromToStringCodec;
#[component]
pub fn Example() -> impl IntoView {
let (flag, set_flag, _) = use_local_storage_with_options::<bool, FromToStringCodec>(
"my-flag",
UseStorageOptions::default().delay_during_hydration(true),
);
view! {
<Show when=move || flag.get()>
<div>Some conditional content</div>
</Show>
}
}
Feature
This function is only available if the crate feature
storageis enabled
Types
Source
use_active_element
Reactive document.activeElement
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::use_active_element;
#[component]
fn Demo() -> impl IntoView {
let active_element = use_active_element();
Effect::new(move || {
log!("focus changed to {:?}", active_element.get());
});
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that always contains the value None.
Feature
This function is only available if the crate feature
use_active_elementis enabled
Source
use_document
SSR safe document().
This returns just a new-type wrapper around Option<Document>.
Calling this amounts to None on the server and Some(Document) on the client.
It provides some convenient methods for working with the document like body().
Usage
use leptos::prelude::*;
use leptos_use::use_document;
#[component]
fn Demo() -> impl IntoView {
let document = use_document();
// Returns `None` on the server but will not panic.
let body = document.body();
view! { }
}
Feature
This function is only available if the crate feature
use_documentis enabled
Types
Source
use_document_visibility
Reactively track document.visibilityState
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_document_visibility;
#[component]
fn Demo() -> impl IntoView {
let visibility = use_document_visibility();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that always contains the value web_sys::VisibilityState::Hidden.
Feature
This function is only available if the crate feature
use_document_visibilityis enabled
Source
use_draggable
Make elements draggable.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_draggable_with_options, UseDraggableOptions, UseDraggableReturn};
use leptos_use::core::Position;
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
// `style` is a helper string "left: {x}px; top: {y}px;"
let UseDraggableReturn {
x,
y,
style,
..
} = use_draggable_with_options(
el,
UseDraggableOptions::default().initial_value(Position { x: 40.0, y: 40.0 }),
);
view! {
<div node_ref=el style=move || format!("position: fixed; {}", style.get())>
Drag me! I am at { x }, { y }
</div>
}
}
Feature
This function is only available if the crate feature
use_draggableis enabled
Types
Source
use_drop_zone
Create a zone where files can be dropped.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_drop_zone_with_options, UseDropZoneOptions, UseDropZoneReturn};
#[component]
fn Demo() -> impl IntoView {
let drop_zone_el = NodeRef::<Div>::new();
let on_drop = |event| {
// called when files are dropped on zone
};
let UseDropZoneReturn {
is_over_drop_zone,
..
} = use_drop_zone_with_options(
drop_zone_el,
UseDropZoneOptions::default().on_drop(on_drop)
);
view! {
<div node_ref=drop_zone_el>
"Drop files here"
</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned file signal always contains an empty Vec and
is_over_drop_zone contains always false
Feature
This function is only available if the crate feature
use_drop_zoneis enabled
Types
Source
use_element_bounding
Reactive bounding box of an HTML element
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_element_bounding, UseElementBoundingReturn};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let UseElementBoundingReturn {
x, y, top,right,bottom,left, width, height, ..
} = use_element_bounding(el);
view! { <div node_ref=el></div> }
}
SendWrapped Return
The returned closure update is a sendwrapped function. It can
only be called from the same thread that called use_element_bounding.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned signals always are 0.0 and update is a no-op.
Feature
This function is only available if the crate feature
use_element_boundingis enabled
Types
Source
use_element_size
Reactive size of an HTML element.
Please refer to ResizeObserver on MDN for more details.
Demo
Usage
use leptos::{html::Div, prelude::*};
use leptos_use::{use_element_size, UseElementSizeReturn};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let UseElementSizeReturn { width, height } = use_element_size(el);
view! {
<div node_ref=el>
"Width: " {width}
"Height: " {height}
</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned signals always contain the value of the initial_size option.
See also
Feature
This function is only available if the crate feature
use_element_sizeis enabled
Types
Source
use_element_visibility
Tracks the visibility of an element within the viewport.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::use_element_visibility;
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let is_visible = use_element_visibility(el);
view! {
<div node_ref=el>
<h1>{is_visible}</h1>
</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that always contains the value false.
See also
Feature
This function is only available if the crate feature
use_element_visibilityis enabled
Types
Source
use_intersection_observer
Reactive IntersectionObserver.
Detects that a target element's visibility inside the viewport.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::use_intersection_observer;
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let (is_visible, set_visible) = signal(false);
use_intersection_observer(
el,
move |entries, _| {
set_visible.set(entries[0].is_intersecting());
},
);
view! {
<div node_ref=el>
<h1>"Hello World"</h1>
</div>
}
}
SendWrapped Return
The returned closures pause, resume and stop are sendwrapped functions. They can
only be called from the same thread that called use_intersection_observer.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this amounts to a no-op.
See also
Feature
This function is only available if the crate feature
use_intersection_observeris enabled
Types
Source
use_mouse_in_element
Reactive mouse position related to an element.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_mouse_in_element, UseMouseInElementReturn};
#[component]
fn Demo() -> impl IntoView {
let target = NodeRef::<Div>::new();
let UseMouseInElementReturn { x, y, is_outside, .. } = use_mouse_in_element(target);
view! {
<div node_ref=target>
<h1>Hello world</h1>
</div>
}
}
SendWrapped Return
The returned closure stop is a sendwrapped function. It can
only be called from the same thread that called use_mouse_in_element.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns simple Signals with the initial_value for x and y,
no-op for stop, is_outside = true and 0.0 for the rest of the signals.
Feature
This function is only available if the crate feature
use_mouse_in_elementis enabled
Types
Source
use_mutation_observer
Reactive MutationObserver.
Watch for changes being made to the DOM tree.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Pre;
use leptos_use::{use_mutation_observer_with_options, UseMutationObserverOptions};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Pre>::new();
let (text, set_text) = signal("".to_string());
use_mutation_observer_with_options(
el,
move |mutations, _| {
if let Some(mutation) = mutations.first() {
set_text.update(|text| *text = format!("{text}\n{:?}", mutation.attribute_name()));
}
},
UseMutationObserverOptions::default().attributes(true),
);
view! {
<pre node_ref=el>{ text }</pre>
}
}
SendWrapped Return
The returned closure stop is a sendwrapped function. It can
only be called from the same thread that called use_mouse_in_element.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this amounts to a no-op.
Feature
This function is only available if the crate feature
use_mutation_observeris enabled
Types
Source
use_resize_observer
Reports changes to the dimensions of an Element's content or the border-box.
Please refer to ResizeObserver on MDN for more details.
Demo
Usage
use leptos::{html::Div, prelude::*};
use leptos_use::use_resize_observer;
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let (text, set_text) = signal("".to_string());
use_resize_observer(
el,
move |entries, observer| {
let rect = entries[0].content_rect();
set_text.set(format!("width: {}\nheight: {}", rect.width(), rect.height()));
},
);
view! {
<div node_ref=el>{ move || text.get() }</div>
}
}
SendWrapped Return
The returned closure stop is a sendwrapped function. It can
only be called from the same thread that called use_resize_observer.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this amounts to a no-op.
See also
Feature
This function is only available if the crate feature
use_resize_observeris enabled
Types
Source
use_window
SSR safe window().
This returns just a new-type wrapper around Option<Window>.
Calling this amounts to None on the server and Some(Window) on the client.
It provides some convenient methods for working with the window like document() and navigator().
These will all return None on the server.
Usage
use leptos::prelude::*;
use leptos_use::use_window;
#[component]
fn Demo() -> impl IntoView {
let window = use_window();
// Returns `None` on the server but will not panic.
let navigator = window.navigator();
view! { }
}
Feature
This function is only available if the crate feature
use_windowis enabled
Types
Source
use_window_focus
Reactively track window focus
with window.onfocus and window.onblur events.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_window_focus;
#[component]
fn Demo() -> impl IntoView {
let focused = use_window_focus();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that is always true.
Feature
This function is only available if the crate feature
use_window_focusis enabled
Source
use_window_scroll
Reactive window scroll.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_window_scroll;
#[component]
fn Demo() -> impl IntoView {
let (x, y) = use_window_scroll();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns Signals that are always 0.0.
Feature
This function is only available if the crate feature
use_window_scrollis enabled
Source
use_window_size
Reactive window size.
Demo
Usage
use leptos::*;
use leptos_use::{use_window_size, UseWindowSizeReturn};
#[component]
fn Demo() -> impl IntoView {
let UseWindowSizeReturn { width, height } = use_window_size();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the width and height are always initial_size which defaults to
Size { width: INFINITY, height: INFINITY }.
Feature
This function is only available if the crate feature
use_window_sizeis enabled
Types
Source
use_breakpoints
Reactive viewport breakpoints.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_breakpoints, BreakpointsTailwind, breakpoints_tailwind};
#[component]
fn Demo() -> impl IntoView {
let screen_width = use_breakpoints(breakpoints_tailwind());
use BreakpointsTailwind::*;
let sm_and_larger = screen_width.ge(Sm);
let larger_than_sm = screen_width.gt(Sm);
let lg_and_smaller = screen_width.le(Lg);
let smaller_than_lg = screen_width.lt(Lg);
view! { }
}
Breakpoints
There are many predefined breakpoints for major UI frameworks. The following are provided.
breakpoints_tailwindbreakpoints_bootstrap_v5breakpoints_materialbreakpoints_ant_designbreakpoints_quasarbreakpoints_semanticbreakpoints_master_css
You can also provide your own breakpoints.
use std::collections::HashMap;
use leptos::prelude::*;
use leptos_use::use_breakpoints;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
enum MyBreakpoints {
Tablet,
Laptop,
Desktop,
}
fn my_breakpoints() -> HashMap<MyBreakpoints, u32> {
use MyBreakpoints::*;
HashMap::from([
(Tablet, 640),
(Laptop, 1024),
(Desktop, 1280),
])
}
#[component]
fn Demo() -> impl IntoView {
let screen_width = use_breakpoints(my_breakpoints());
use MyBreakpoints::*;
let laptop = screen_width.between(Laptop, Desktop);
view! { }
}
Non-reactive methods
For every reactive method there is also a non-reactive variant that is prefixed with is_
use leptos::prelude::*;
use leptos_use::{use_breakpoints, BreakpointsTailwind, breakpoints_tailwind};
#[component]
fn Demo() -> impl IntoView {
let screen_width = use_breakpoints(breakpoints_tailwind());
use BreakpointsTailwind::*;
let sm_and_larger = screen_width.is_ge(Sm);
let larger_than_sm = screen_width.is_gt(Sm);
let lg_and_smaller = screen_width.is_le(Lg);
let smaller_than_lg = screen_width.is_lt(Lg);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Since internally this uses fn@crate::use_media_query, which returns always false on the server,
the returned methods also will return false.
Feature
This function is only available if the crate feature
use_breakpointsis enabled
Types
struct UseBreakpointsReturnenum BreakpointsTailwindenum BreakpointsBootstrapV5enum BreakpointsMaterialenum BreakpointsAntDesignenum BreakpointsQuasarenum BreakpointsSemanticenum BreakpointsMasterCss
Source
use_broadcast_channel
Reactive BroadcastChannel API.
Closes a broadcast channel automatically when the component is cleaned up.
Demo
Usage
The BroadcastChannel interface represents a named channel that any browsing context of a given origin can subscribe to. It allows communication between different documents (in different windows, tabs, frames, or iframes) of the same origin.
Messages are broadcasted via a message event fired at all BroadcastChannel objects listening to the channel.
use leptos::prelude::*;
use leptos_use::{use_broadcast_channel, UseBroadcastChannelReturn};
use codee::string::FromToStringCodec;
#[component]
fn Demo() -> impl IntoView {
let UseBroadcastChannelReturn {
is_supported,
message,
post,
error,
close,
..
} = use_broadcast_channel::<bool, FromToStringCodec>("some-channel-name");
post(&true);
close();
view! { }
}
Values are (en)decoded via the given codec. You can use any of the string codecs or a
binary codec wrapped in Base64.
Please check the codec chapter to see what codecs are available and what feature flags they require.
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
use leptos_use::use_broadcast_channel;
use codee::string::JsonSerdeCodec;
// Data sent in JSON must implement Serialize, Deserialize:
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct MyState {
pub playing_lego: bool,
pub everything_is_awesome: String,
}
#[component]
fn Demo() -> impl IntoView {
use_broadcast_channel::<MyState, JsonSerdeCodec>("everyting-is-awesome");
view! { }
}
SendWrapped Return
The returned closures post and close are sendwrapped functions. They can
only be called from the same thread that called use_broadcast_channel.
Feature
This function is only available if the crate feature
use_broadcast_channelis enabled
Types
Source
use_clipboard
Reactive Clipboard API.
Provides the ability to respond to clipboard commands (cut, copy, and paste) as well as to asynchronously read from and write to the system clipboard. Access to the contents of the clipboard is gated behind the Permissions API. Without user permission, reading or altering the clipboard contents is not permitted.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_clipboard, UseClipboardReturn};
#[component]
fn Demo() -> impl IntoView {
let UseClipboardReturn { is_supported, text, copied, copy } = use_clipboard();
view! {
<Show
when=move || is_supported.get()
fallback=move || view! { <p>Your browser does not support Clipboard API</p> }
>
<button on:click={
let copy = copy.clone();
move |_| copy("Hello!")
}>
<Show when=move || copied.get() fallback=move || "Copy">
"Copied!"
</Show>
</button>
</Show>
}
}
SendWrapped Return
The returned closures copy is a sendwrapped function. It can
only be called from the same thread that called use_clipboard.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returnd text signal will always be None and copy is a no-op.
Feature
This function is only available if the crate feature
use_clipboardis enabled
Types
Source
use_color_mode
Reactive color mode (dark / light / customs) with auto data persistence.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_color_mode, UseColorModeReturn};
#[component]
fn Demo() -> impl IntoView {
let UseColorModeReturn {
mode, // Signal<ColorMode::dark | ColorMode::light>
set_mode,
..
} = use_color_mode();
view! { }
}
By default, it will match with users' browser preference using fn@crate::use_preferred_dark (a.k.a. ColorMode::Auto).
When reading the signal, it will by default return the current color mode (ColorMode::Dark, ColorMode::Light or
your custom modes ColorMode::Custom("some-custom")). The ColorMode::Auto variant can
be included in the returned modes by enabling the emit_auto option and using use_color_mode_with_options.
When writing to the signal (set_mode), it will trigger DOM updates and persist the color mode to local
storage (or your custom storage). You can pass ColorMode::Auto to set back to auto mode.
use leptos::prelude::*;
use leptos_use::{ColorMode, use_color_mode, UseColorModeReturn};
#[component]
fn Demo() -> impl IntoView {
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode();
mode.get(); // ColorMode::Dark or ColorMode::Light
set_mode.set(ColorMode::Dark); // change to dark mode and persist
set_mode.set(ColorMode::Auto); // change to auto mode
view! { }
}
Options
use leptos::prelude::*;
use leptos_use::{use_color_mode_with_options, UseColorModeOptions, UseColorModeReturn};
#[component]
fn Demo() -> impl IntoView {
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options(
UseColorModeOptions::default()
.attribute("theme") // instead of writing to `class`
.custom_modes(vec![
// custom colors in addition to light/dark
"dim".to_string(),
"cafe".to_string(),
]),
); // Signal<ColorMode::Dark | ColorMode::Light | ColorMode::Custom("dim") | ColorMode::Custom("cafe")>
view! { }
}
Cookie
To persist color mode in a cookie, use use_cookie_with_options and specify .cookie_enabled(true).
Note: To work with SSR you have to add the
axumoractixfeature as described infn@crate::use_cookie.
use leptos::prelude::*;
use leptos_meta::*;
use leptos_use::{use_color_mode_with_options, UseColorModeOptions, UseColorModeReturn};
#[component]
fn Demo() -> impl IntoView {
let UseColorModeReturn { mode, set_mode, .. } = use_color_mode_with_options(
UseColorModeOptions::default()
.cookie_enabled(true),
);
// This adds the color mode class to the `<html>` element even with SSR
view! {
<Html {..} class=move || mode.get().to_string()/>
}
}
For a working example please check out the ssr example.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this will try to read the
Sec-CH-Prefers-Color-Scheme header
to determine the color mode. If the header is not present it will return ColorMode::Light.
Please have a look at the linked documentation above for that header to see browser support
as well as potential server requirements.
If you're using
axumyou have to enable the"axum"feature in your Cargo.toml. In case it'sactix-webenable the feature"actix".
Bring your own header
In case you're neither using Axum nor Actix or the default implementation is not to your liking,
you can provide your own way of reading the color scheme header value using the option
crate::UseColorModeOptions::ssr_color_header_getter.
Cookie
If cookie_enabled is set to true, a cookie will be used and if present this value will be used
on the server as well as on the client. Please note that you have to add the axum or actix
feature as described in fn@crate::use_cookie.
See also
Feature
This function is only available if the crate feature
use_color_modeis enabled
Types
Source
use_cookie
SSR-friendly and reactive cookie access.
You can use this function multiple times for the same cookie and their signals will synchronize
(even across windows/tabs). But there is no way to listen to changes to document.cookie directly so in case
something outside of this function changes the cookie, the signal will not be updated.
When the options max_age or expire is given then the returned signal will
automatically turn to None after that time.
Demo
Usage
The example below creates a cookie called counter. If the cookie doesn't exist, it is initially set to a random value.
Whenever we update the counter variable, the cookie will be updated accordingly.
use leptos::prelude::*;
use leptos_use::use_cookie;
use codee::string::FromToStringCodec;
use rand::random;
#[component]
fn Demo() -> impl IntoView {
let (counter, set_counter) = use_cookie::<u32, FromToStringCodec>("counter");
let reset = move || set_counter.set(Some(random()));
if counter.get().is_none() {
reset();
}
let increase = move || {
set_counter.set(counter.get().map(|c| c + 1));
};
view! {
<p>Counter: {move || counter.get().map(|c| c.to_string()).unwrap_or("β".to_string())}</p>
<button on:click=move |_| reset()>Reset</button>
<button on:click=move |_| increase()>+</button>
}
}
Values are (en)decoded via the given codec. You can use any of the string codecs or a
binary codec wrapped in Base64.
Please check the codec chapter to see what codecs are available and what feature flags they require.
Cookie attributes
As part of the options when you use use_cookie_with_options you can specify cookie attributes.
use cookie::SameSite;
use leptos::prelude::*;
use leptos_use::{use_cookie_with_options, UseCookieOptions};
use codee::string::FromToStringCodec;
#[component]
fn Demo() -> impl IntoView {
let (cookie, set_cookie) = use_cookie_with_options::<bool, FromToStringCodec>(
"user_info",
UseCookieOptions::default()
.max_age(3600_000) // one hour
.same_site(SameSite::Lax)
);
view! {}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
This works equally well on the server or the client.
On the server this function reads the cookie from the HTTP request header and writes it back into
the HTTP response header according to options (if provided).
The returned WriteSignal may not affect the cookie headers on the server! It will try and write
the headers but if this happens after the headers have already been streamed to the client then
this will have no effect.
If you're using
axumyou have to enable the"axum"feature in your Cargo.toml. In case it'sactix-webenable the feature"actix"..
Bring your own header
In case you're neither using Axum nor Actix or the default implementation is not to your liking, you can provide your own way of reading and writing the cookie header value.
use cookie::Cookie;
use leptos::prelude::*;
use serde::{Deserialize, Serialize};
use leptos_use::{use_cookie_with_options, UseCookieOptions};
use codee::string::JsonSerdeCodec;
#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
pub struct Auth {
pub username: String,
pub token: String,
}
#[component]
fn Demo() -> impl IntoView {
use_cookie_with_options::<Auth, JsonSerdeCodec>(
"auth",
UseCookieOptions::default()
.ssr_cookies_header_getter(|| {
#[cfg(feature = "ssr")]
{
Some("Somehow get the value of the cookie header as a string".to_owned())
}
#[cfg(not(feature = "ssr"))]
None
})
.ssr_set_cookie(|cookie: &Cookie| {
#[cfg(feature = "ssr")]
{
// somehow insert the Set-Cookie header for this cookie
}
}),
);
view! {}
}
Feature
This function is only available if the crate feature
use_cookieis enabled
Types
Source
use_css_var
Manipulate CSS variables.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_css_var;
#[component]
fn Demo() -> impl IntoView {
let (color, set_color) = use_css_var("--color");
set_color.set("red".to_string());
view! { }
}
The variable name itself can be a Signal.
use leptos::prelude::*;
use leptos_use::use_css_var;
#[component]
fn Demo() -> impl IntoView {
let (key, set_key) = signal("--color".to_string());
let (color, set_color) = use_css_var(key);
view! { }
}
You can specify the element that the variable is applied to as well as an initial value in case the variable is not set yet. The option to listen for changes to the variable is also available.
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_css_var_with_options, UseCssVarOptions};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let (color, set_color) = use_css_var_with_options(
"--color",
UseCssVarOptions::default()
.target(el)
.initial_value("#eee")
.observe(true),
);
view! {
<div node_ref=el>"..."</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this simply returns signal(options.initial_value).
Feature
This function is only available if the crate feature
use_css_varis enabled
Types
Source
use_display_media
Reactive mediaDevices.getDisplayMedia streaming.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::{log, error};
use leptos_use::{use_display_media, UseDisplayMediaReturn};
#[component]
fn Demo() -> impl IntoView {
let video_ref = NodeRef::<leptos::html::Video>::new();
let UseDisplayMediaReturn { stream, start, .. } = use_display_media();
start();
Effect::new(move |_|
video_ref.get().map(|v| {
match stream.get() {
Some(Ok(s)) => v.set_src_object(Some(&s)),
Some(Err(e)) => error!("Failed to get media stream: {:?}", e),
None => log!("No stream yet"),
}
})
);
view! { <video node_ref=video_ref controls=false autoplay=true muted=true></video> }
}
SendWrapped Return
The returned closures start and stop are sendwrapped functions. They can
only be called from the same thread that called use_display_media.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server calls to start or any other way to enable the stream will be ignored
and the stream will always be None.
Feature
This function is only available if the crate feature
use_display_mediais enabled
Types
Source
use_event_listener
Use EventListener with ease.
Register using addEventListener on mounted, and removeEventListener automatically on cleanup.
Usage
use leptos::prelude::*;
use leptos::ev::visibilitychange;
use leptos::logging::log;
use leptos_use::{use_document, use_event_listener};
#[component]
fn Demo() -> impl IntoView {
use_event_listener(use_document(), visibilitychange, |evt| {
log!("{:?}", evt);
});
view! { }
}
You can also pass a NodeRef as the event target, use_event_listener will unregister the previous event and register
the new one when you change the target.
use leptos::prelude::*;
use leptos::ev::click;
use leptos::logging::log;
use leptos_use::use_event_listener;
#[component]
fn Demo() -> impl IntoView {
let element = NodeRef::new();
use_event_listener(element, click, |evt| {
log!("click from element {:?}", event_target::<web_sys::HtmlDivElement>(&evt));
});
let (cond, set_cond) = signal(true);
view! {
<Show
when=move || cond.get()
fallback=move || view! { <div node_ref=element>"Condition false"</div> }
>
<div node_ref=element>"Condition true"</div>
</Show>
}
}
You can also call the returned to unregister the listener.
use leptos::prelude::*;
use leptos::ev::keydown;
use leptos::logging::log;
use web_sys::KeyboardEvent;
use leptos_use::use_event_listener;
#[component]
fn Demo() -> impl IntoView {
let cleanup = use_event_listener(document().body(), keydown, |evt: KeyboardEvent| {
log!("{}", &evt.key());
});
cleanup();
view! { }
}
SendWrapped Return
The returned closure is a sendwrapped function. It can
only be called from the same thread that called use_event_listener.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this amounts to a noop.
Feature
This function is only available if the crate feature
use_event_listeneris enabled
Types
Source
use_favicon
Reactive favicon.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_favicon;
#[component]
fn Demo() -> impl IntoView {
let (icon, set_icon) = use_favicon();
set_icon.set(Some("dark.png".to_string())); // change current icon
view! { }
}
Passing a Source Signal
You can pass a Signal to use_favicon_with_options. Change from the source signal will be
reflected in your favicon automatically.
use leptos::prelude::*;
use leptos_use::{use_favicon_with_options, UseFaviconOptions, use_preferred_dark};
#[component]
fn Demo() -> impl IntoView {
let is_dark = use_preferred_dark();
let (icon, _) = use_favicon_with_options(
UseFaviconOptions::default().new_icon(
Signal::derive(move || {
Some((if is_dark.get() { "dark.png" } else { "light.png" }).to_string())
}),
)
);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server only the signals work but no favicon will be changed obviously.
Feature
This function is only available if the crate feature
use_faviconis enabled
Types
Source
use_media_query
Reactive Media Query.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_media_query;
#[component]
fn Demo() -> impl IntoView {
let is_large_screen = use_media_query("(min-width: 1024px)");
let is_dark_preferred = use_media_query("(prefers-color-scheme: dark)");
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this functions returns a Signal that is always false.
See also
Feature
This function is only available if the crate feature
use_media_queryis enabled
Source
use_permission
Reactive Permissions API.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_permission;
#[component]
fn Demo() -> impl IntoView {
let microphone_access = use_permission("microphone");
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned signal will always be PermissionState::Unknown.
Feature
This function is only available if the crate feature
use_permissionis enabled
Types
Source
use_preferred_contrast
Reactive prefers-contrast media query.
Usage
use leptos::prelude::*;
use leptos_use::use_preferred_contrast;
#[component]
fn Demo() -> impl IntoView {
let preferred_contrast = use_preferred_contrast();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that always contains the value PreferredContrast::NoPreference.
See also
Feature
This function is only available if the crate feature
use_preferred_contrastis enabled
Types
Source
use_preferred_dark
Reactive dark theme preference.
Usage
use leptos::prelude::*;
use leptos_use::use_preferred_dark;
#[component]
fn Demo() -> impl IntoView {
let is_dark_preferred = use_preferred_dark();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this will try to read the
Sec-CH-Prefers-Color-Scheme header
to determine the color mode. If the header is not present it will return ColorMode::Light.
Please have a look at the linked documentation above for that header to see browser support
as well as potential server requirements.
If you're using
axumyou have to enable the"axum"feature in your Cargo.toml. In case it'sactix-webenable the feature"actix".
Bring your own header
In case you're neither using Axum nor Actix or the default implementation is not to your liking,
you can provide your own way of reading the color scheme header value using the option
crate::UsePreferredDarkOptions::ssr_color_header_getter.
See also
Feature
This function is only available if the crate feature
use_preferred_darkis enabled
Types
Source
use_prefers_reduced_motion
Reactive reduced motions preference.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_prefers_reduced_motion;
#[cfg(feature = "docs")]
use leptos_use::docs::BooleanDisplay;
#[component]
fn Demo() -> impl IntoView {
let is_reduced_motion_preferred = use_prefers_reduced_motion();
view! {
<div>
<p>Prefers reduced motions: <BooleanDisplay value=is_reduced_motion_preferred/></p>
<p>
Update reduce motion preference
<a href="https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion#user_preferences">
documentation.
</a>
</p>
</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this will try to read the
Sec-CH-Prefers-Reduced-Motion header
to indicate the preference for animations to be displayed with reduced motion.
Please have a look at the linked documentation above to see browser support
as well as potential serve requirements.
If you're using
axumyou have to enable the"axum"feature in your Cargo.toml. In case it'sactix-webenable the feature"actix".
Bring your own header
In case you're neither using Axum nor Actix or the default implementation is not to your
liking, you can provide your own way of reading the reduced motion header value using the option
crate::UsePrefersReducedMotionOptions::ssr_motion_header_getter.
See also
Feature
This function is only available if the crate feature
use_prefers_reduced_motionis enabled
Types
Source
use_service_worker
Reactive ServiceWorker API.
Please check the working example.
Usage
use leptos::prelude::*;
use leptos_use::{use_service_worker_with_options, UseServiceWorkerOptions, UseServiceWorkerReturn};
#[component]
fn Demo() -> impl IntoView {
let UseServiceWorkerReturn {
registration,
installing,
waiting,
active,
skip_waiting,
check_for_update,
} = use_service_worker_with_options(UseServiceWorkerOptions::default()
.script_url("service-worker.js")
.skip_waiting_message("skipWaiting"),
);
view! { }
}
SendWrapped Return
The returned closures check_for_update and skip_waiting are sendwrapped functions. They can
only be called from the same thread that called use_service_worker.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
This function does not support SSR. Call it inside a create_effect.
Feature
This function is only available if the crate feature
use_service_workeris enabled
Types
Source
use_textarea_autosize
Automatically update the height of a textarea depending on the content.
Demo
Usage
Simple example
use leptos::prelude::*;
use leptos::html::Textarea;
use leptos_use::{use_textarea_autosize, UseTextareaAutosizeReturn};
#[component]
fn Demo() -> impl IntoView {
let textarea = NodeRef::new();
let UseTextareaAutosizeReturn {
content,
set_content,
trigger_resize
} = use_textarea_autosize(textarea);
view! {
<textarea
prop:value=content
on:input=move |evt| set_content.set(event_target_value(&evt))
node_ref=textarea
class="resize-none"
placeholder="What's on your mind?"
/>
}
}
Make sure that you set
box-sizing: border-boxon the textarea element.It's also recommended to reset the scrollbar styles for the textarea element to avoid incorrect height values for large amounts of text.
textarea {
-ms-overflow-style: none;
scrollbar-width: none;
}
textarea::-webkit-scrollbar {
display: none;
}
With rows attribute
If you need support for the rows attribute on a textarea element, then you should set the
style_prop option to "min-height".
use leptos::prelude::*;
use leptos::html::Textarea;
use leptos_use::{use_textarea_autosize_with_options, UseTextareaAutosizeOptions, UseTextareaAutosizeReturn};
#[component]
fn Demo() -> impl IntoView {
let textarea = NodeRef::new();
let UseTextareaAutosizeReturn {
content,
set_content,
..
} = use_textarea_autosize_with_options(
textarea,
UseTextareaAutosizeOptions::default().style_prop("min-height"),
);
view! {
<textarea
prop:value=content
on:input=move |evt| set_content.set(event_target_value(&evt))
node_ref=textarea
class="resize-none"
placeholder="What's on your mind?"
rows="3"
/>
}
}
SendWrapped Return
The returned closure trigger_resize is a sendwrapped function. It can
only be called from the same thread that called use_textarea_autosize.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this will always return an empty string as Β΄contentand a no-optrigger_resize`.
Feature
This function is only available if the crate feature
use_textarea_autosizeis enabled
Types
Source
use_user_media
Reactive mediaDevices.getUserMedia streaming.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::{log, error};
use leptos_use::{use_user_media, UseUserMediaReturn};
#[component]
fn Demo() -> impl IntoView {
let video_ref = NodeRef::<leptos::html::Video>::new();
let UseUserMediaReturn { stream, start, .. } = use_user_media();
start();
Effect::new(move |_|
video_ref.get().map(|v| {
match stream.get() {
Some(Ok(s)) => v.set_src_object(Some(&s)),
Some(Err(e)) => error!("Failed to get media stream: {:?}", e),
None => log!("No stream yet"),
}
})
);
view! { <video node_ref=video_ref controls=false autoplay=true muted=true></video> }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server calls to start or any other way to enable the stream will be ignored
and the stream will always be None.
Feature
This function is only available if the crate feature
use_user_mediais enabled
Types
struct UseUserMediaOptionsstruct UseUserMediaReturnenum ConstraintExactIdealenum ConstraintRangeenum FacingModeenum AudioConstraintsenum VideoConstraintsstruct VecMarkerstruct AudioTrackConstraintsstruct VideoTrackConstraints
Source
use_web_lock
Rustified Web Locks API.
The Web Locks API allows scripts running in one tab or worker to asynchronously acquire a lock, hold it while work is performed, then release it. While held, no other script executing in the same origin can acquire the same lock, which allows a web app running in multiple tabs or workers to coordinate work and the use of resources.
This function requires
--cfg=web_sys_unstable_apisto be activated as described in the wasm-bindgen guide.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_web_lock;
async fn my_process(_lock: web_sys::Lock) -> i32 {
// do sth
42
}
#[component]
fn Demo() -> impl IntoView {
leptos::task::spawn_local(async {
let res = use_web_lock("my_lock", my_process).await;
assert!(matches!(res, Ok(42)));
});
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns Err(UseWebLockError::Server) and the task is not executed.
Feature
This function is only available if the crate feature
use_web_lockis enabled
Types
Source
use_web_notification
Reactive Notification API.
The Web Notification interface of the Notifications API is used to configure and display desktop notifications to the user.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_web_notification_with_options, UseWebNotificationOptions, ShowOptions, UseWebNotificationReturn, NotificationDirection};
#[component]
fn Demo() -> impl IntoView {
let UseWebNotificationReturn {
show,
close,
..
} = use_web_notification_with_options(
UseWebNotificationOptions::default()
.direction(NotificationDirection::Auto)
.language("en")
.renotify(true)
.tag("test"),
);
show(ShowOptions::default().title("Hello World from leptos-use"));
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
This function is basically ignored on the server. You can safely call show but it will do nothing.
Feature
This function is only available if the crate feature
use_web_notificationis enabled
Types
enum NotificationDirectionstruct UseWebNotificationOptionsstruct ShowOptionsenum NotificationPermissionstruct UseWebNotificationReturn
Source
on_click_outside
Listen for clicks outside an element. Useful for modals or dropdowns.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos::html::Div;
use leptos_use::on_click_outside;
#[component]
fn Demo() -> impl IntoView {
let target = NodeRef::<Div>::new();
on_click_outside(target, move |event| { log!("{:?}", event); });
view! {
<div node_ref=target>"Hello World"</div>
<div>"Outside element"</div>
}
}
This function uses Event.composedPath() which is not supported by IE 11, Edge 18 and below. If you are targeting these browsers, we recommend you to include this code snippet on your project.
SendWrapped Return
The return value of this function is a sendwrapped function to remove all event listeners. It can
only be called from the same thread that called on_click_outside.
Excluding Elements
Use this to ignore clicks on certain elements.
use leptos::prelude::*;
use leptos::logging::log;
use leptos::html::Div;
use leptos_use::{on_click_outside_with_options, OnClickOutsideOptions};
#[component]
fn Demo() -> impl IntoView {
let target = NodeRef::<Div>::new();
on_click_outside_with_options(
target,
move |event| { log!("{:?}", event); },
OnClickOutsideOptions::default().ignore(["input", "#some-id"]),
);
view! {
<div node_ref=target>"Hello World"</div>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this amounts to a no-op.
Feature
This function is only available if the crate feature
on_click_outsideis enabled
Types
Source
use_device_orientation
Reactive DeviceOrientationEvent.
Provide web developers with information from the physical orientation of the device running the web page.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_device_orientation, UseDeviceOrientationReturn};
#[component]
fn Demo() -> impl IntoView {
let UseDeviceOrientationReturn {
is_supported,
absolute,
alpha,
beta,
gamma,
} = use_device_orientation();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this function returns values as if the orientation capabilties were not supported by the device.
Feature
This function is only available if the crate feature
use_device_orientationis enabled
Types
Source
use_device_pixel_ratio
Reactive window.devicePixelRatio
NOTE: there is no event listener for
window.devicePixelRatiochange. So this function uses the same mechanism as described in this example.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_device_pixel_ratio;
#[component]
fn Demo() -> impl IntoView {
let pixel_ratio = use_device_pixel_ratio();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this function returns a Signal that is always 1.0.
Feature
This function is only available if the crate feature
use_device_pixel_ratiois enabled
Source
use_element_hover
Reactive element's hover state.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Button;
use leptos_use::use_element_hover;
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Button>::new();
let is_hovered = use_element_hover(el);
view! {
<button node_ref=el>{ move || format!("{:?}", is_hovered.get()) }</button>
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns a Signal that always contains the value false.
Feature
This function is only available if the crate feature
use_element_hoveris enabled
Types
Source
use_geolocation
Reactive Geolocation API.
It allows the user to provide their location to web applications if they so desire. For privacy reasons, the user is asked for permission to report location information.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_geolocation, UseGeolocationReturn};
#[component]
fn Demo() -> impl IntoView {
let UseGeolocationReturn {
coords,
located_at,
error,
resume,
pause,
} = use_geolocation();
view! { }
}
SendWrapped Return
The returned closures pause and resume are sendwrapped functions. They can
only be called from the same thread that called use_geolocation.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server all signals returns will always contain None and the functions do nothing.
Feature
This function is only available if the crate feature
use_geolocationis enabled
Types
Source
use_idle
Tracks whether the user is being inactive.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{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::{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
UseIdleReturn{
idle: Signal(initial_state),
last_active: Signal(now),
reset: || {}
}
Feature
This function is only available if the crate feature
use_idleis enabled
Types
Source
use_infinite_scroll
Infinite scrolling of the element.
Demo
Usage
use leptos::prelude::*;
use leptos::html::Div;
use leptos_use::{use_infinite_scroll_with_options, UseInfiniteScrollOptions};
#[component]
fn Demo() -> impl IntoView {
let el = NodeRef::<Div>::new();
let (data, set_data) = signal(vec![1, 2, 3, 4, 5, 6]);
let _ = use_infinite_scroll_with_options(
el,
move |_| async move {
let len = data.with(|d| d.len());
set_data.update(|data| *data = (1..len+6).collect());
},
UseInfiniteScrollOptions::default().distance(10.0),
);
view! {
<div node_ref=el>
<For each=move || data.get() key=|i| *i let:item>{ item }</For>
</div>
}
}
The returned signal is true while new data is being loaded.
Feature
This function is only available if the crate feature
use_infinite_scrollis enabled
Types
Source
use_mouse
Reactive mouse position
Demo
Basic Usage
use leptos::prelude::*;
use leptos_use::{use_mouse, UseMouseReturn};
#[component]
fn Demo() -> impl IntoView {
let UseMouseReturn {
x, y, source_type, ..
} = use_mouse();
view! { }
}
Touch is enabled by default. To only detect mouse changes, set touch to false.
The dragover event is used to track mouse position while dragging.
use leptos::prelude::*;
use leptos_use::{use_mouse_with_options, UseMouseOptions, UseMouseReturn};
#[component]
fn Demo() -> impl IntoView {
let UseMouseReturn {
x, y, ..
} = use_mouse_with_options(
UseMouseOptions::default().touch(false)
);
view! { }
}
Custom Extractor
It's also possible to provide a custom extractor to get the position from the events.
use leptos::prelude::*;
use leptos::html::Div;
use web_sys::MouseEvent;
use leptos_use::{use_mouse_with_options, UseMouseOptions, UseMouseReturn, UseMouseEventExtractor, UseMouseCoordType};
#[derive(Clone)]
struct MyExtractor;
impl UseMouseEventExtractor for MyExtractor {
fn extract_mouse_coords(&self, event: &MouseEvent) -> Option<(f64, f64)> {
Some((event.offset_x() as f64, event.offset_y() as f64))
}
// don't implement fn extract_touch_coords to ignore touch events
}
#[component]
fn Demo() -> impl IntoView {
let element = NodeRef::<Div>::new();
let UseMouseReturn {
x, y, source_type, ..
} = use_mouse_with_options(
UseMouseOptions::default()
.target(element)
.coord_type(UseMouseCoordType::Custom(MyExtractor))
);
view! { <div node_ref=element></div> }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns simple Signals with the initial_values.
Feature
This function is only available if the crate feature
use_mouseis enabled
Types
Source
use_scroll
We have to check if the scroll amount is close enough to some threshold in order to more accurately calculate arrivedState. This is because scrollTop/scrollLeft are non-rounded numbers, while scrollHeight/scrollWidth and clientHeight/clientWidth are rounded. https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollHeight#determine_if_an_element_has_been_totally_scrolled
Feature
This function is only available if the crate feature
use_scrollis enabled
Types
Source
use_event_source
Reactive EventSource
An EventSource or Server-Sent-Events instance opens a persistent connection to an HTTP server, which sends events in text/event-stream format.
Usage
Values are decoded via the given decoder. You can use any of the string codecs or a
binary codec wrapped in Base64.
Please check the codec chapter to see what codecs are available and what feature flags they require.
use leptos::prelude::*;
use leptos_use::{use_event_source, UseEventSourceReturn};
use codee::string::JsonSerdeCodec;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Clone, PartialEq)]
pub struct EventSourceData {
pub message: String,
pub priority: u8,
}
#[component]
fn Demo() -> impl IntoView {
let UseEventSourceReturn {
ready_state, data, error, close, ..
} = use_event_source::<EventSourceData, JsonSerdeCodec>("https://event-source-url");
view! { }
}
Named Events
You can define named events when using use_event_source_with_options.
use leptos::prelude::*;
use leptos_use::{use_event_source_with_options, UseEventSourceReturn, UseEventSourceOptions};
use codee::string::FromToStringCodec;
#[component]
fn Demo() -> impl IntoView {
let UseEventSourceReturn {
ready_state, data, error, close, ..
} = use_event_source_with_options::<String, FromToStringCodec>(
"https://event-source-url",
UseEventSourceOptions::default()
.named_events(["notice".to_string(), "update".to_string()])
);
view! { }
}
Immediate
Auto-connect (enabled by default).
This will call open() automatically for you, and you don't need to call it by yourself.
Auto-Reconnection
Reconnect on errors automatically (enabled by default).
You can control the number of reconnection attempts by setting reconnect_limit and the
interval between them by setting reconnect_interval.
use leptos::prelude::*;
use leptos_use::{use_event_source_with_options, UseEventSourceReturn, UseEventSourceOptions, ReconnectLimit};
use codee::string::FromToStringCodec;
#[component]
fn Demo() -> impl IntoView {
let UseEventSourceReturn {
ready_state, data, error, close, ..
} = use_event_source_with_options::<bool, FromToStringCodec>(
"https://event-source-url",
UseEventSourceOptions::default()
.reconnect_limit(ReconnectLimit::Limited(5)) // at most 5 attempts
.reconnect_interval(2000) // wait for 2 seconds between attempts
);
view! { }
}
SendWrapped Return
The returned closures open and close are sendwrapped functions. They can
only be called from the same thread that called use_event_source.
To disable auto-reconnection, set reconnect_limit to 0.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server-side, use_event_source will always return ready_state as ConnectionReadyState::Closed,
data, event and error will always be None, and open and close will do nothing.
Feature
This function is only available if the crate feature
use_event_sourceis enabled
Types
Source
use_websocket
Creating and managing a Websocket connection.
Demo
Usage
Values are (en)decoded via the given codec. You can use any of the codecs, string or binary.
Please check the codec chapter to see what codecs are available and what feature flags they require.
use leptos::prelude::*;
use codee::string::FromToStringCodec;
use leptos_use::{use_websocket, UseWebSocketReturn};
use leptos_use::core::ConnectionReadyState;
#[component]
fn Demo() -> impl IntoView {
let UseWebSocketReturn {
ready_state,
message,
send,
open,
close,
..
} = use_websocket::<String, String, FromToStringCodec>("wss://echo.websocket.events/");
let send_message = move |_| {
send(&"Hello, world!".to_string());
};
let status = move || ready_state.get().to_string();
let connected = move || ready_state.get() == ConnectionReadyState::Open;
let open_connection = move |_| {
open();
};
let close_connection = move |_| {
close();
};
view! {
<div>
<p>"status: " {status}</p>
<button on:click=send_message disabled=move || !connected()>"Send"</button>
<button on:click=open_connection disabled=connected>"Open"</button>
<button on:click=close_connection disabled=move || !connected()>"Close"</button>
<p>"Receive message: " {move || format!("{:?}", message.get())}</p>
</div>
}
}
Here is another example using msgpack for encoding and decoding. This means that only binary
messages can be sent or received. For this to work you have to enable the msgpack_serde feature flag.
use leptos::*;
use codee::binary::MsgpackSerdeCodec;
use leptos_use::{use_websocket, UseWebSocketReturn};
use serde::{Deserialize, Serialize};
#[component]
fn Demo() -> impl IntoView {
#[derive(Serialize, Deserialize)]
struct SomeData {
name: String,
count: i32,
}
let UseWebSocketReturn {
message,
send,
..
} = use_websocket::<SomeData, SomeData, MsgpackSerdeCodec>("wss://some.websocket.server/");
let send_data = move || {
send(&SomeData {
name: "John Doe".to_string(),
count: 42,
});
};
view! {}
}
Heartbeats
Heartbeats can be configured by the heartbeat option. You have to provide a heartbeat
type, that implements the Default trait and an Encoder for it. This encoder doesn't have
to be the same as the one used for the other websocket messages.
use leptos::*;
use codee::string::FromToStringCodec;
use leptos_use::{use_websocket_with_options, UseWebSocketOptions, UseWebSocketReturn};
use serde::{Deserialize, Serialize};
#[component]
fn Demo() -> impl IntoView {
#[derive(Default)]
struct Heartbeat;
// Simple example for usage with `FromToStringCodec`
impl std::fmt::Display for Heartbeat {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "<Heartbeat>")
}
}
let UseWebSocketReturn {
send,
message,
..
} = use_websocket_with_options::<String, String, FromToStringCodec, _, _>(
"wss://echo.websocket.events/",
UseWebSocketOptions::default()
// Enable heartbeats every 10 seconds. In this case we use the same codec as for the
// other messages. But this is not necessary.
.heartbeat::<Heartbeat, FromToStringCodec>(10_000),
);
view! {}
}
Relative Paths
If the provided url is relative, it will be resolved relative to the current page.
Urls will be resolved like this the following. Please note that the protocol (http vs https) will
be taken into account as well.
| Current Page | Relative Url | Resolved Url |
|---|---|---|
| http://example.com/some/where | /api/ws | ws://example.com/api/ws |
| https://example.com/some/where | /api/ws | wss://example.com/api/ws |
| https://example.com/some/where | api/ws | wss://example.com/some/where/api/ws |
| https://example.com/some/where | //otherdomain.com/api/ws | wss://otherdomain.com/api/ws |
Usage with provide_context
The return value of use_websocket utilizes several type parameters which can make it
cumbersome to use with provide_context + expect_context.
The following example shows how to avoid type parameters with dynamic dispatch.
This sacrifices a little bit of performance for the sake of ergonomics. However,
compared to network transmission speeds this loss of performance is negligible.
First we define the struct that is going to be passed around as context.
use leptos::prelude::*;
use std::sync::Arc;
#[derive(Clone)]
pub struct WebsocketContext {
pub message: Signal<Option<String>>,
send: Arc<dyn Fn(&String)>, // use Arc to make it easily cloneable
}
impl WebsocketContext {
pub fn new(message: Signal<Option<String>>, send: Arc<dyn Fn(&String)>) -> Self {
Self {
message,
send,
}
}
// create a method to avoid having to use parantheses around the field
#[inline(always)]
pub fn send(&self, message: &str) {
(self.send)(&message.to_string())
}
}
Now you can provide the context like the following.
use leptos::prelude::*;
use codee::string::FromToStringCodec;
use leptos_use::{use_websocket, UseWebSocketReturn};
use std::sync::Arc;
#[derive(Clone)]
pub struct WebsocketContext {
pub message: Signal<Option<String>>,
send: Arc<dyn Fn(&String) + Send + Sync>,
}
impl WebsocketContext {
pub fn new(message: Signal<Option<String>>, send: Arc<dyn Fn(&String) + Send + Sync>) -> Self {
Self {
message,
send,
}
}
}
#[component]
fn Demo() -> impl IntoView {
let UseWebSocketReturn {
message,
send,
..
} = use_websocket::<String, String, FromToStringCodec>("ws:://some.websocket.io");
provide_context(WebsocketContext::new(message, Arc::new(send.clone())));
view! {}
}
Finally let's use the context:
use leptos::prelude::*;
use leptos_use::{use_websocket, UseWebSocketReturn};
use std::sync::Arc;
#[derive(Clone)]
pub struct WebsocketContext {
pub message: Signal<Option<String>>,
send: Arc<dyn Fn(&String)>,
}
impl WebsocketContext {
#[inline(always)]
pub fn send(&self, message: &str) {
(self.send)(&message.to_string())
}
}
#[component]
fn Demo() -> impl IntoView {
let websocket = expect_context::<WebsocketContext>();
websocket.send("Hello World!");
view! {}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the returned functions amount to no-ops.
Feature
This function is only available if the crate feature
use_websocketis enabled
Types
struct UseWebSocketOptionsstruct DummyEncoderstruct HeartbeatOptionsstruct UseWebSocketReturnenum UseWebSocketError
Source
use_interval
Reactive counter increases on every interval.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_interval, UseIntervalReturn};
#[component]
fn Demo() -> impl IntoView {
let UseIntervalReturn {
counter,
reset,
is_active,
pause,
resume
} = use_interval( 200 );
view! { }
}
SendWrapped Return
The returned closures pause, resume and reset are sendwrapped functions. They can
only be called from the same thread that called use_intersection_observer.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this function will simply be ignored.
Feature
This function is only available if the crate feature
use_intervalis enabled
Types
Source
use_interval_fn
Wrapper for set_interval with controls.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_interval_fn;
use leptos_use::utils::Pausable;
#[component]
fn Demo() -> impl IntoView {
let Pausable { pause, resume, is_active } = use_interval_fn(
|| {
// do something
},
1000,
);
view! { }
}
SendWrapped Return
The returned closures pause and resume are sendwrapped functions. They can
only be called from the same thread that called use_interval_fn.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this function will simply be ignored.
Feature
This function is only available if the crate feature
use_interval_fnis enabled
Types
Source
use_raf_fn
Call function on every requestAnimationFrame. With controls of pausing and resuming.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_raf_fn;
use leptos_use::utils::Pausable;
#[component]
fn Demo() -> impl IntoView {
let (count, set_count) = signal(0);
let Pausable { pause, resume, is_active } = use_raf_fn(move |_| {
set_count.update(|count| *count += 1);
});
view! { <div>Count: { count }</div> }
}
You can use use_raf_fn_with_options and set immediate to false. In that case
you have to call resume() before the callback is executed.
SendWrapped Return
The returned closures pause and resume are sendwrapped functions. They can
only be called from the same thread that called use_interval_fn.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this does basically nothing. The provided closure will never be called.
Feature
This function is only available if the crate feature
use_raf_fnis enabled
Types
Source
use_timeout_fn
Wrapper for setTimeout with controls.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_timeout_fn, UseTimeoutFnReturn};
#[component]
fn Demo() -> impl IntoView {
let UseTimeoutFnReturn { start, stop, is_pending, .. } = use_timeout_fn(
|i: i32| {
// do sth
},
3000.0
);
start(3);
view! { }
}
SendWrapped Return
The returned closures start and stop are sendwrapped functions. They can
only be called from the same thread that called use_timeout_fn.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the callback will never be run. The returned functions are all no-ops and
is_pending will always be false.
Feature
This function is only available if the crate feature
use_timeout_fnis enabled
Types
Source
use_timestamp
Reactive current timestamp.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_timestamp;
#[component]
fn Demo() -> impl IntoView {
let timestamp = use_timestamp();
view! { }
}
With controls:
use leptos::prelude::*;
use leptos_use::{use_timestamp_with_controls, UseTimestampReturn};
#[component]
fn Demo() -> impl IntoView {
let UseTimestampReturn {
timestamp,
is_active,
pause,
resume,
} = use_timestamp_with_controls();
view! { }
}
SendWrapped Return
The returned closures pause and resume of the ..._with_controls versions are
sendwrapped functions. They can only be called from the same thread that called
use_timestamp_with_controls.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this function will return a signal with the milliseconds since the Unix epoch.
But the signal will never update (as there's no request_animation_frame on the server).
Feature
This function is only available if the crate feature
use_timestampis enabled
Types
Source
use_calendar
Create bare-bone calendar data to use in your component.
See UseCalendarOptions for options and UseCalendarReturn for return values.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{use_calendar, UseCalendarReturn};
#[component]
fn Demo() -> impl IntoView {
let UseCalendarReturn {
dates,
weekdays,
previous_month,
today,
month_by_date,
next_month
} = use_calendar();
view! {
}
}
Use use_calendar_with_options to change the initial date and first day of the week.
use leptos::prelude::*;
use chrono::NaiveDate;
use leptos_use::{use_calendar_with_options, UseCalendarReturn, UseCalendarOptions};
#[component]
fn Demo() -> impl IntoView {
let initial_date = RwSignal::new(
Some(NaiveDate::from_ymd_opt(2022, 1, 1).unwrap())
);
let options = UseCalendarOptions::default()
.first_day_of_the_week(6)
.initial_date(initial_date);
let UseCalendarReturn {
dates,
weekdays,
previous_month,
today,
month_by_date,
next_month
} = use_calendar_with_options(options);
view! {
}
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Not tested yet.
Feature
This function is only available if the crate feature
use_calendaris enabled
Types
Source
watch_debounced
A debounced version of watch.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::watch_debounced;
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_debounced(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
);
view! { }
}
This really is only shorthand shorthand for watch_with_options(deps, callback, WatchOptions::default().debounce(ms)).
Please note that if the current component is cleaned up before the debounced callback is called, the debounced callback will not be called.
There's also watch_debounced_with_options where you can specify the other watch options (except filter).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_debounced_with_options, WatchDebouncedOptions};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_debounced_with_options(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
WatchDebouncedOptions::default().max_wait(Some(1000.0)),
);
view! { }
}
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the callback
will never be called except if you set immediate to true in which case the callback will be
called exactly once.
See also
leptos::watchfn@crate::watch_throttled
Feature
This function is only available if the crate feature
watch_debouncedis enabled
Types
Source
watch_pausable
Pausable watch.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_pausable, WatchPausableReturn};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal("foo".to_string());
let WatchPausableReturn {
stop,
pause,
resume,
..
} = watch_pausable(
move || source.get(),
|v, _, _| {
log!("Changed to {}", v);
},
);
set_source.set("bar".to_string()); // > "Changed to bar"
pause();
set_source.set("foobar".to_string()); // (nothing happens)
resume();
set_source.set("hello".to_string()); // > "Changed to hello"
view! { }
}
There's also watch_pausable_with_options which takes the same options as watch.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this works just fine except if you throttle or debounce in which case the callback
will never be called except if you set immediate to true in which case the callback will be
called exactly once.
See also
leptos::watch
Feature
This function is only available if the crate feature
watch_pausableis enabled
Types
Source
watch_throttled
A throttled version of leptos::watch.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::watch_throttled;
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_throttled(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
);
view! { }
}
This really is only shorthand shorthand for watch_with_options(deps, callback, WatchOptions::default().throttle(ms)).
Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
There's also watch_throttled_with_options where you can specify the other watch options (except filter).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{watch_throttled_with_options, WatchThrottledOptions};
pub fn Demo() -> impl IntoView {
let (source, set_source) = signal(0);
watch_throttled_with_options(
move || source.get(),
move |_, _, _| {
log!("changed!");
},
500.0,
WatchThrottledOptions::default().leading(true).trailing(false),
);
view! { }
}
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the callback
will never be called except if you set immediate to true in which case the callback will be
called exactly once.
See also
leptos::watchfn@crate::watch_debounced
Feature
This function is only available if the crate feature
watch_throttledis enabled
Types
Source
watch_with_options
whenever
Shorthand for watching a signal to be true.
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::whenever;
pub fn Demo() -> impl IntoView {
let (is_ready, set_ready) = signal(false);
whenever(move || is_ready.get(), |v, _, _| log!("{}", v));
view! { }
}
Callback Function
Same as fn@crate::watch, the callback will be called with callback(input, prev_input, prev_return).
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::whenever;
pub fn Demo() -> impl IntoView {
let (is_ready, set_ready) = signal(false);
whenever(move || is_ready.get(), |value, prev_value, _| {
log!("before: {prev_value:?}; now: {value}");
});
view! { }
}
Computed
Same as fn@crate::watch, you can pass a getter function to calculate on each change.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::whenever;
pub fn Demo() -> impl IntoView {
let (counter, set_counter) = signal(0);
whenever(
move || counter.get() == 7,
|_, _, _| log!("counter is 7 now!"),
);
view! { }
}
Options
Options and defaults are same as fn@watch_with_options.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{WatchOptions, whenever_with_options};
pub fn Demo() -> impl IntoView {
let (counter, set_counter) = signal(0);
whenever_with_options(
move || counter.get() == 7,
|_, _, _| log!("counter is 7 now!"),
WatchOptions::default().immediate(true),
);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this works just fine except if you throttle or debounce in which case the callback
will never be called except if you set immediate to true in which case the callback will be
called exactly once.
Feature
This function is only available if the crate feature
wheneveris enabled
Source
signal_debounced
Debounce changing of a Signal value.
Use *_local variants for values that are not Send + Sync.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{signal_debounced, signal_debounced_local};
use std::cell::RefCell;
#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let debounced: Signal<&'static str> = signal_debounced(input, 1000.0);
let (input_local, set_input_local) = signal_local(RefCell::new(0));
let debounced_local: Signal<RefCell<i32>, _> = signal_debounced_local(input_local, 1000.0);
view! { }
}
Options
The usual debounce option max_wait is available.
use leptos::prelude::*;
use leptos_use::{signal_debounced_with_options, signal_debounced_local_with_options, DebounceOptions};
use std::cell::RefCell;
#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let debounced: Signal<&'static str> = signal_debounced_with_options(
input,
1000.0,
DebounceOptions::default().max_wait(Some(500.0))
);
let (input_local, set_input_local) = signal_local(RefCell::new(0));
let debounced_local: Signal<RefCell<i32>, _> = signal_debounced_local_with_options(
input_local,
1000.0,
DebounceOptions::default().max_wait(Some(500.0))
);
view! { }
}
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Internally this uses setTimeout which is not supported on the server. So usually
a throttled signal on the server will simply be ignored.
Feature
This function is only available if the crate feature
signal_debouncedis enabled
Source
signal_throttled
Throttle changing of a Signal value.
Use *_local variants for values that are not Send + Sync.
Demo
Usage
use leptos::prelude::*;
use leptos_use::{signal_throttled, signal_throttled_local};
use std::cell::RefCell;
#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let throttled: Signal<&'static str> = signal_throttled(input, 1000.0);
let (input_local, set_input_local) = signal_local(RefCell::new(0));
let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local(input_local, 1000.0);
view! { }
}
Options
The usual throttle options leading and trailing are available.
use leptos::prelude::*;
use leptos_use::{signal_throttled_with_options, signal_throttled_local_with_options, ThrottleOptions};
use std::cell::RefCell;
#[component]
fn Demo() -> impl IntoView {
let (input, set_input) = signal("");
let throttled: Signal<&'static str> = signal_throttled_with_options(
input,
1000.0,
ThrottleOptions::default().leading(false).trailing(true)
);
let (input_local, set_input_local) = signal_local(RefCell::new(0));
let throttled_local: Signal<RefCell<i32>, _> = signal_throttled_local_with_options(
input_local,
1000.0,
ThrottleOptions::default().leading(false).trailing(true)
);
view! { }
}
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Internally this uses setTimeout which is not supported on the server. So usually
a throttled signal on the server will simply be ignored.
Feature
This function is only available if the crate feature
signal_throttledis enabled
Source
sync_signal
Two-way Signals synchronization.
Note: Please consider first if you can achieve your goals with the "Good Options" described in the Leptos book Only if you really have to, use this function. This is, in effect, the "If you really must..." option.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::sync_signal;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(1);
let (b, set_b) = signal(2);
let stop = sync_signal((a, set_a), (b, set_b));
log!("a: {}, b: {}", a.get(), b.get()); // a: 1, b: 1
set_b.set(3);
log!("a: {}, b: {}", a.get(), b.get()); // a: 3, b: 3
set_a.set(4);
log!("a: {}, b: {}", a.get(), b.get()); // a: 4, b: 4
view! { }
}
RwSignal
You can mix and match RwSignals and Signal-WriteSignal pairs.
use leptos::prelude::*;
use leptos_use::sync_signal;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(1);
let (b, set_b) = signal(2);
let c_rw = RwSignal::new(3);
let d_rw = RwSignal::new(4);
sync_signal((a, set_a), c_rw);
sync_signal(d_rw, (b, set_b));
sync_signal(c_rw, d_rw);
view! { }
}
One directional
You can synchronize a signal only from left to right or right to left.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{sync_signal_with_options, SyncSignalOptions, SyncDirection};
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(1);
let (b, set_b) = signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::default().direction(SyncDirection::LeftToRight)
);
set_b.set(3); // doesn't sync
log!("a: {}, b: {}", a.get(), b.get()); // a: 1, b: 3
set_a.set(4);
log!("a: {}, b: {}", a.get(), b.get()); // a: 4, b: 4
view! { }
}
Custom Transform
You can optionally provide custom transforms between the two signals.
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{sync_signal_with_options, SyncSignalOptions};
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(10);
let (b, set_b) = signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::with_transforms(
|left| *left * 2,
|right| *right / 2,
),
);
log!("a: {}, b: {}", a.get(), b.get()); // a: 10, b: 20
set_b.set(30);
log!("a: {}, b: {}", a.get(), b.get()); // a: 15, b: 30
view! { }
}
Different Types
SyncSignalOptions::default() is only defined if the two signal types are identical.
Otherwise, you have to initialize the options with with_transforms or with_assigns instead
of default.
use leptos::prelude::*;
use leptos_use::{sync_signal_with_options, SyncSignalOptions};
use std::str::FromStr;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal("10".to_string());
let (b, set_b) = signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::with_transforms(
|left: &String| i32::from_str(left).unwrap_or_default(),
|right: &i32| right.to_string(),
),
);
view! { }
}
use leptos::prelude::*;
use leptos_use::{sync_signal_with_options, SyncSignalOptions};
use std::str::FromStr;
#[derive(Clone)]
pub struct Foo {
bar: i32,
}
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(Foo { bar: 10 });
let (b, set_b) = signal(2);
let stop = sync_signal_with_options(
(a, set_a),
(b, set_b),
SyncSignalOptions::with_assigns(
|b: &mut i32, a: &Foo| *b = a.bar,
|a: &mut Foo, b: &i32| a.bar = *b,
),
);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server the signals are not continuously synced. If the option immediate is true, the
signals are synced once initially. If the option immediate is false, then this function
does nothing.
Feature
This function is only available if the crate feature
sync_signalis enabled
Types
Source
use_sorted
Reactive sort of iterable
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_sorted;
#[component]
fn Demo() -> impl IntoView {
let source = vec![10, 3, 5, 7, 2, 1, 8, 6, 9, 4];
let sorted: Signal<Vec<i32>> = use_sorted(source); // [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
view! { }
}
You can also sort by key or with a compare function.
use leptos::prelude::*;
use leptos_use::{use_sorted_by, use_sorted_by_key};
#[derive(Clone, PartialEq)]
pub struct Person {
pub name: String,
pub age: u16,
}
#[component]
fn Demo() -> impl IntoView {
let source = vec![
Person {
name: "John".to_string(),
age: 40,
},
Person {
name: "Jane".to_string(),
age: 20,
},
Person {
name: "Joe".to_string(),
age: 30,
},
Person {
name: "Jenny".to_string(),
age: 22,
},
];
// sort by key
let sorted: Signal<Vec<Person>> = use_sorted_by_key(
source.clone(),
|person: &Person| person.age,
);
// sort with compare function
let sorted: Signal<Vec<Person>> = use_sorted_by(
source,
|p1: &Person, p2: &Person| p1.age.cmp(&p2.age),
);
view! { }
}
Please note that these two ways of sorting are equivalent.
Feature
This function is only available if the crate feature
use_sortedis enabled
Source
header
Get the value of the header with the given name.
This function is only meant to be used on the server.
So it is only defined when the feature "ssr" is enabled together with one of the
features "axum" or "actix".
Example
use leptos_use::utils::header;
let content_len = header(http::header::CONTENT_LENGTH);
Feature
This function is only available if the crate feature
utils/headeris enabled
Source
is_err
Reactive Result::is_err().
Usage
use leptos::prelude::*;
use leptos_use::is_err;
#[component]
fn Demo() -> impl IntoView {
let (example, set_example) = signal(
if js_sys::Math::random() < 0.5 { Ok("Example") } else { Err(()) }
);
let is_error = is_err(example);
view! { }
}
Feature
This function is only available if the crate feature
is_erris enabled
Source
is_none
Reactive Option::is_none().
Usage
use leptos::prelude::*;
use leptos_use::is_none;
#[component]
fn Demo() -> impl IntoView {
let (example, set_example) = signal(
if js_sys::Math::random() < 0.5 { Some("Example") } else { None }
);
let is_empty = is_none::<_, &str>(example);
view! { }
}
Feature
This function is only available if the crate feature
is_noneis enabled
Source
is_ok
Reactive Result::is_ok().
Usage
use leptos::prelude::*;
use leptos_use::is_ok;
#[component]
fn Demo() -> impl IntoView {
let (example, set_example) = signal(
if js_sys::Math::random() < 0.5 { Ok("Example") } else { Err(()) }
);
let is_ok = is_ok(example);
view! { }
}
Feature
This function is only available if the crate feature
is_okis enabled
Source
is_some
Reactive Option::is_some().
Usage
use leptos::prelude::*;
use leptos_use::is_some;
#[component]
fn Demo() -> impl IntoView {
let (example, set_example) = signal(
if js_sys::Math::random() < 0.5 { Some("Example") } else { None }
);
let not_empty = is_some::<_, &str>(example);
view! { }
}
Feature
This function is only available if the crate feature
is_someis enabled
Source
use_cycle_list
Cycle through a list of items.
Demo
Usage
use leptos::prelude::*;
use leptos::logging::log;
use leptos_use::{use_cycle_list, UseCycleListReturn};
#[component]
fn Demo() -> impl IntoView {
let UseCycleListReturn { state, next, prev, .. } = use_cycle_list(
vec!["Dog", "Cat", "Lizard", "Shark", "Whale", "Dolphin", "Octopus", "Seal"]
);
log!("{}", state.get()); // "Dog"
prev();
log!("{}", state.get()); // "Seal"
view! { }
}
Feature
This function is only available if the crate feature
use_cycle_listis enabled
Types
Source
use_debounce_fn
Debounce execution of a function.
Debounce is an overloaded waiter: If you keep asking him your requests will be ignored until you stop and give him some time to think about your latest inquiry.
Demo
Usage
use leptos::prelude::*;
use leptos::ev::resize;
use leptos_use::use_debounce_fn;
#[component]
fn Demo() -> impl IntoView {
let mut debounced_fn = use_debounce_fn(
|| {
// do something
},
1000.0,
);
window_event_listener(resize, move |_| { debounced_fn(); });
view! { }
}
Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
You can also pass options to use_debounce_fn_with_options with a maximum wait time, similar to
lodash debounce.
use leptos::prelude::*;
use leptos::ev::resize;
use leptos_use::use_debounce_fn_with_options;
use leptos_use::utils::DebounceOptions;
#[component]
fn Demo() -> impl IntoView {
let mut debounced_fn = use_debounce_fn_with_options(
|| {
// do something
},
1000.0,
DebounceOptions::default()
.max_wait(Some(5000.0)),
);
window_event_listener(resize, move |_| { debounced_fn(); });
view! { }
}
Currently there is no way to use a function with a return value. Please open an issue if you need this.
If you want to throttle a function that takes an argument there are also the versions
use_debounce_fn_with_arg and use_debounce_fn_with_arg_and_options.
SendWrapped Return
The returned closure is a sendwrapped function. It can
only be called from the same thread that called use_debounce_....
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Internally this uses setTimeout which is not supported on the server. So usually calling
a debounced function on the server will simply be ignored.
Feature
This function is only available if the crate feature
use_debounce_fnis enabled
Source
use_derive_signal!
Macro to easily create helper functions that derive a signal using a piece of code.
See is_ok or use_to_string as examples.
use_supported
SSR compatibe is_supported
Usage
use leptos::prelude::*;
use leptos_use::{use_supported, js};
use wasm_bindgen::JsValue;
pub fn Demo() -> impl IntoView {
let is_supported = use_supported(
|| js!("getBattery" in &window().navigator())
);
if is_supported.get() {
// do something
}
view! { }
}
Feature
This function is only available if the crate feature
use_supportedis enabled
Source
use_throttle_fn
Throttle execution of a function. Especially useful for rate limiting execution of handlers on events like resize and scroll.
Throttle is a spring that throws balls: After a ball flies out it needs some time to shrink back, so it cannot throw any more balls until it's ready.
Demo
Usage
use leptos::prelude::*;
use leptos_use::use_throttle_fn;
#[component]
fn Demo() -> impl IntoView {
let mut throttled_fn = use_throttle_fn(
|| {
// do something, it will be called at most 1 time per second
},
1000.0,
);
view! {
<button on:click=move |_| { throttled_fn(); }>
"Smash me!"
</button>
}
}
Please note that if the current component is cleaned up before the throttled callback is called, the throttled callback will not be called.
You can provide options when you use use_throttle_fn_with_options.
use leptos::prelude::*;
use leptos_use::{ThrottleOptions, use_throttle_fn_with_options};
#[component]
fn Demo() -> impl IntoView {
let throttled_fn = use_throttle_fn_with_options(
|| {
// do something, it will be called at most 1 time per second
},
1000.0,
ThrottleOptions::default()
.leading(true)
.trailing(true),
);
view! { }
}
If you want to throttle a function that takes an argument there are also the versions
use_throttle_fn_with_arg and use_throttle_fn_with_arg_and_options.
SendWrapped Return
The returned closure is a sendwrapped function. It can
only be called from the same thread that called use_throttle_....
Recommended Reading
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Internally this uses setTimeout which is not supported on the server. So usually calling
a throttled function on the server will simply be ignored.
Feature
This function is only available if the crate feature
use_throttle_fnis enabled
Source
use_to_string
Reactive ToString::to_string().
Usage
use leptos::prelude::*;
use leptos_use::use_to_string;
#[component]
fn Demo() -> impl IntoView {
let (number, set_number) = signal(3.14_f64);
let str = use_to_string::<_, f64>(number);
view! { }
}
Feature
This function is only available if the crate feature
use_to_stringis enabled
Source
use_toggle
A boolean switcher with utility functions.
Demo
Usage
use leptos::*;
use leptos_use::{use_toggle, UseToggleReturn};
#[component]
fn Demo() -> impl IntoView {
let UseToggleReturn { toggle, value, set_value } = use_toggle(true);
view! { }
}
See also
Feature
This function is only available if the crate feature
use_toggleis enabled
Types
Source
use_intl_number_format
Reactive Intl.NumberFormat.
Demo
Usage
In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.
use leptos::prelude::*;
use leptos_use::{use_intl_number_format, UseIntlNumberFormatOptions};
#[component]
fn Demo() -> impl IntoView {
let (number, set_number) = signal(3500);
let number_format = use_intl_number_format(UseIntlNumberFormatOptions::default());
let formatted = number_format.format::<u16>(number); // "3,500" if in US English locale
view! { }
}
Using locales
This example shows some of the variations in localized number formats. In order to get the format
of the language used in the user interface of your application, make sure to specify that language
(and possibly some fallback languages) using the locales argument:
use leptos::prelude::*;
use leptos_use::{use_intl_number_format, UseIntlNumberFormatOptions};
#[component]
fn Demo() -> impl IntoView {
let number = 123456.789_f32;
// German uses comma as decimal separator and period for thousands
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default().locale("de-DE"),
);
let formatted = number_format.format::<f32>(number); // 123.456,789
// Arabic in most Arabic speaking countries uses real Arabic digits
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default().locale("ar-EG"),
);
let formatted = number_format.format::<f32>(number); // Ω‘Ω’Ω£Ω€Ω₯Ω¦Ω«Ω§Ω¨Ω©
// India uses thousands/lakh/crore separators
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default().locale("en-IN"),
);
let formatted = number_format.format::<f32>(number); // 1,23,456.789
// the nu extension key requests a numbering system, e.g. Chinese decimal
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default().locale("zh-Hans-CN-u-nu-hanidec"),
);
let formatted = number_format.format::<f32>(number); // δΈδΊδΈ,εδΊε
.δΈε
«δΉ
// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default().locales(vec!["ban".to_string(), "id".to_string()]),
);
let formatted = number_format.format::<f32>(number); // 123.456,789
view! { }
}
Using options
The results can be customized in multiple ways.
use leptos::prelude::*;
use leptos_use::{NumberStyle, UnitDisplay, use_intl_number_format, UseIntlNumberFormatOptions};
#[component]
fn Demo() -> impl IntoView {
let number = 123456.789_f64;
// request a currency format
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default()
.locale("de-DE")
.style(NumberStyle::Currency)
.currency("EUR"),
);
let formatted = number_format.format::<f64>(number); // 123.456,79 β¬
// the Japanese yen doesn't use a minor unit
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default()
.locale("ja-JP")
.style(NumberStyle::Currency)
.currency("JPY"),
);
let formatted = number_format.format::<f64>(number); // οΏ₯123,457
// limit to three significant digits
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default()
.locale("en-IN")
.maximum_significant_digits(3),
);
let formatted = number_format.format::<f64>(number); // 1,23,000
// Formatting with units
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default()
.locale("pt-PT")
.style(NumberStyle::Unit)
.unit("kilometer-per-hour"),
);
let formatted = number_format.format::<i32>(50); // 50 km/h
let number_format = use_intl_number_format(
UseIntlNumberFormatOptions::default()
.locale("en-GB")
.style(NumberStyle::Unit)
.unit("liter")
.unit_display(UnitDisplay::Long),
);
let formatted = number_format.format::<i32>(16); // 16 litres
view! { }
}
For an exhaustive list of options see UseIntlNumberFormatOptions.
Formatting ranges
Apart from the format method, the format_range method can be used to format a range of numbers.
Please see UseIntlNumberFormatReturn::format_range
for details.
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Since Intl.NumberFormat is a JavaScript API it is not available on the server. That's why
it falls back to a simple call to format!() on the server.
Feature
This function is only available if the crate feature
use_intl_number_formatis enabled
Types
enum CompactDisplayenum CurrencyDisplayenum CurrencySignenum LocaleMatcherenum Notationenum SignDisplayenum NumberStyleenum UnitDisplayenum NumberGroupingenum RoundingModeenum RoundingPriorityenum TrailingZeroDisplaystruct UseIntlNumberFormatOptions
Source
use_locale
Reactive locale matching.
Returns the first matching locale given by fn@crate::use_locales that is also found in
the supported list. In case there is no match, then the first locale in supported will be
returned.
If
supportedis empty, this function will panic!
Matching is done by using the fn@unic_langid::LanguageIdentifier::matches method.
Demo
Usage
use leptos::*;
use leptos_use::use_locale;
use unic_langid::langid_slice;
#[component]
fn Demo() -> impl IntoView {
let locale = use_locale(langid_slice!["en", "de", "fr"]);
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
Feature
This function is only available if the crate feature
use_localeis enabled
Source
use_locales
Reactive locales.
If called on the client-side this function returns the value of
navigator.languages
and listens for changes to that property.
See "Server-Side Rendering" below.
Demo
Usage
use leptos::*;
use leptos_use::use_locales;
#[component]
fn Demo() -> impl IntoView {
let locales = use_locales();
view! { }
}
Server-Side Rendering
Make sure you follow the instructions in Server-Side Rendering.
On the server this returns the parsed value of the accept-language header.
If you're using
axumyou have to enable the"axum"feature in your Cargo.toml. In case it'sactix-webenable the feature"actix".
Bring your own header
In case you're neither using Axum nor Actix or the default implementation is not to your liking,
you can provide your own way of reading the language header value using the option
crate::UseLocalesOptions::ssr_lang_header_getter.
Feature
This function is only available if the crate feature
use_localesis enabled
Types
Source
use_abs
Reactive abs().
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_abs;
#[component]
fn Demo() -> impl IntoView {
let (value, set_value) = signal(-32.25);
let result: Signal<f64> = use_abs(value); // 32.25
assert_eq!(result.get(), 32.25);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_and
Reactive AND condition.
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_and;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(true);
let (b, set_b) = signal(false);
let a_and_b = use_and(a, b);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_ceil
Reactive ceil().
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_ceil;
#[component]
fn Demo() -> impl IntoView {
let (value, set_value) = signal(44.15);
let result: Signal<f64> = use_ceil(value); // 45
assert_eq!(result.get(), 45.0);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_floor
Reactive floor().
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_floor;
#[component]
fn Demo() -> impl IntoView {
let (value, set_value) = signal(45.95);
let result: Signal<f64> = use_floor(value); // 45
assert_eq!(result.get(), 45.0);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_max
Reactive max().
Works with any container that implements IntoIterator (Vec, HashSet, ...)
with any elements that implement PartialOrd and Clone (floats, ints, strings, ...).
If the container is empty or only contains non comparable values like NaN, it returns None.
Otherwise it returns the Some(<largest value>) in the container.
Usage
use leptos::prelude::*;
use leptos_use::math::use_max;
#[component]
fn Demo() -> impl IntoView {
let (values, set_values) = signal(vec![1.0, 2.0, 3.0, f32::NAN, 4.0, 5.0]);
let result = use_max::<Vec<f32>, _, _>(values); // Some(5.0)
assert_eq!(result.get(), Some(5.0));
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_min
Reactive min().
Works with any container that implements IntoIterator (Vec, HashSet, ...)
with any elements that implement PartialOrd and Clone (floats, ints, strings, ...).
If the container is empty or only contains non comparable values like NaN, it returns None.
Otherwise it returns the Some(<smallest value>) in the container.
Usage
use leptos::prelude::*;
use leptos_use::math::use_min;
#[component]
fn Demo() -> impl IntoView {
let (values, set_values) = signal(vec![1.0, 2.0, 3.0, f32::NAN, 4.0, 5.0]);
let result = use_min::<Vec<f32>, _, _>(values); // Some(1.0)
assert_eq!(result.get(), Some(1.0));
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_not
Reactive NOT condition.
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_not;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(true);
let not_a = use_not(a);
view! { }
}
See also
Feature
This function is only available if the crate feature
mathis enabled
Source
use_or
Reactive OR condition.
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_or;
#[component]
fn Demo() -> impl IntoView {
let (a, set_a) = signal(true);
let (b, set_b) = signal(false);
let a_or_b = use_or(a, b);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled
Source
use_round
Reactive round().
Demo
Usage
use leptos::prelude::*;
use leptos_use::math::use_round;
#[component]
fn Demo() -> impl IntoView {
let (value, set_value) = signal(45.95);
let result: Signal<f64> = use_round(value); // 46
assert_eq!(result.get(), 46.0);
view! { }
}
Feature
This function is only available if the crate feature
mathis enabled