updated docs and changelog

This commit is contained in:
Maccesch 2024-08-28 02:57:26 +01:00
parent 04bcb6b319
commit 074685acab
13 changed files with 107 additions and 39 deletions

View file

@ -3,20 +3,27 @@
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
## [Unreleased] - ## [0.13.0] - 2024-08-28
### New Functions 🚀 ### New Functions 🚀
- `use_toggle` - `use_toggle`
- `use_prefers_reduced_motion` - `use_prefers_reduced_motion` (thanks to @hcandelaria)
### Breaking Change 🛠 ### Breaking Changes 🛠
- `use_websocket` now supports different types for sending and receiving messages - `use_websocket` now supports different types for sending and receiving messages
- `SyncSignalOptions` now can take now either transformations or assignment functions but not both.
### Change 🔥 ### Fixes 🍕
- There is now a feature for almost every function to get better compile and rust-analyzer times. - `use_websocket` fixed 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_notification` now supports the `vibrate` option (thanks to @hcandelaria).
- `UseDocument` now supports a whole bunch of methods more from `document` (thanks to @luckynumberke7in).
## [0.12.0] - 2024-08-14 ## [0.12.0] - 2024-08-14

View file

@ -1,6 +1,6 @@
[package] [package]
name = "leptos-use" name = "leptos-use"
version = "0.12.0" version = "0.13.0"
edition = "2021" edition = "2021"
authors = ["Marc-Stefan Cassola"] authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"] categories = ["gui", "web-programming"]

View file

@ -13,7 +13,7 @@
<a href="https://crates.io/crates/leptos-use"><img src="https://img.shields.io/crates/v/leptos-use.svg?label=&color=%232C1275" alt="Crates.io"/></a> <a href="https://crates.io/crates/leptos-use"><img src="https://img.shields.io/crates/v/leptos-use.svg?label=&color=%232C1275" alt="Crates.io"/></a>
<a href="https://leptos-use.rs/server_side_rendering.html"><img src="https://img.shields.io/badge/-SSR-%236a214b" alt="SSR"></a> <a href="https://leptos-use.rs/server_side_rendering.html"><img src="https://img.shields.io/badge/-SSR-%236a214b" alt="SSR"></a>
<a href="https://leptos-use.rs"><img src="https://img.shields.io/badge/-docs%20%26%20demos-%239A233F" alt="Docs & Demos"></a> <a href="https://leptos-use.rs"><img src="https://img.shields.io/badge/-docs%20%26%20demos-%239A233F" alt="Docs & Demos"></a>
<a href="https://leptos-use.rs"><img src="https://img.shields.io/badge/-82%20functions-%23EF3939" alt="82 Functions" /></a> <a href="https://leptos-use.rs"><img src="https://img.shields.io/badge/-84%20functions-%23EF3939" alt="84 Functions" /></a>
</p> </p>
<br/> <br/>
@ -87,9 +87,9 @@ This will create the function file in the src directory, scaffold an example dir
## Leptos compatibility ## Leptos compatibility
| Crate version | Compatible Leptos version | | Crate version | Compatible Leptos version |
|------------------|---------------------------| |---------------|---------------------------|
| <= 0.3 | 0.3 | | <= 0.3 | 0.3 |
| 0.4, 0.5, 0.6 | 0.4 | | 0.4, 0.5, 0.6 | 0.4 |
| 0.7, 0.8, 0.9 | 0.5 | | 0.7, 0.8, 0.9 | 0.5 |
| 0.10, 0.11, 0.12 | 0.6 | | 0.10 0.13 | 0.6 |

View file

@ -12,6 +12,6 @@
<a href="https://crates.io/crates/leptos-use"><img src="https://img.shields.io/crates/v/leptos-use.svg?label=&color=%232C1275" alt="Crates.io"/></a> <a href="https://crates.io/crates/leptos-use"><img src="https://img.shields.io/crates/v/leptos-use.svg?label=&color=%232C1275" alt="Crates.io"/></a>
<a href="https://leptos-use.rs/server_side_rendering.html"><img src="https://img.shields.io/badge/-SSR-%236a214b" alt="SSR"></a> <a href="https://leptos-use.rs/server_side_rendering.html"><img src="https://img.shields.io/badge/-SSR-%236a214b" alt="SSR"></a>
<a href="./get_started.html"><img src="https://img.shields.io/badge/-docs%20%26%20demos-%239A233F" alt="Docs & Demos"></a> <a href="./get_started.html"><img src="https://img.shields.io/badge/-docs%20%26%20demos-%239A233F" alt="Docs & Demos"></a>
<a href="./functions.html"><img src="https://img.shields.io/badge/-82%20functions-%23EF3939" alt="82 Functions" /></a> <a href="./functions.html"><img src="https://img.shields.io/badge/-84%20functions-%23EF3939" alt="84 Functions" /></a>
</p> </p>
</div> </div>

View file

@ -28,8 +28,6 @@ mod is_ok;
mod is_some; mod is_some;
#[cfg(feature = "on_click_outside")] #[cfg(feature = "on_click_outside")]
mod on_click_outside; mod on_click_outside;
#[cfg(feature = "use_toggle")]
mod use_toggle;
#[cfg(feature = "signal_debounced")] #[cfg(feature = "signal_debounced")]
mod signal_debounced; mod signal_debounced;
#[cfg(feature = "signal_throttled")] #[cfg(feature = "signal_throttled")]
@ -136,6 +134,8 @@ mod use_timeout_fn;
mod use_timestamp; mod use_timestamp;
#[cfg(feature = "use_to_string")] #[cfg(feature = "use_to_string")]
mod use_to_string; mod use_to_string;
#[cfg(feature = "use_toggle")]
mod use_toggle;
#[cfg(feature = "use_user_media")] #[cfg(feature = "use_user_media")]
mod use_user_media; mod use_user_media;
#[cfg(feature = "use_web_notification")] #[cfg(feature = "use_web_notification")]
@ -169,8 +169,6 @@ pub use is_ok::*;
pub use is_some::*; pub use is_some::*;
#[cfg(feature = "on_click_outside")] #[cfg(feature = "on_click_outside")]
pub use on_click_outside::*; pub use on_click_outside::*;
#[cfg(feature = "use_toggle")]
pub use use_toggle::*;
#[cfg(feature = "signal_debounced")] #[cfg(feature = "signal_debounced")]
pub use signal_debounced::*; pub use signal_debounced::*;
#[cfg(feature = "signal_throttled")] #[cfg(feature = "signal_throttled")]
@ -277,6 +275,8 @@ pub use use_timeout_fn::*;
pub use use_timestamp::*; pub use use_timestamp::*;
#[cfg(feature = "use_to_string")] #[cfg(feature = "use_to_string")]
pub use use_to_string::*; pub use use_to_string::*;
#[cfg(feature = "use_toggle")]
pub use use_toggle::*;
#[cfg(feature = "use_user_media")] #[cfg(feature = "use_user_media")]
pub use use_user_media::*; pub use use_user_media::*;
#[cfg(feature = "use_web_notification")] #[cfg(feature = "use_web_notification")]

View file

@ -7,8 +7,8 @@ use std::rc::Rc;
/// ///
/// > Note: Please consider first if you can achieve your goals with the /// > Note: Please consider first if you can achieve your goals with the
/// > ["Good Options" described in the Leptos book](https://book.leptos.dev/reactivity/working_with_signals.html#making-signals-depend-on-each-other) /// > ["Good Options" described in the Leptos book](https://book.leptos.dev/reactivity/working_with_signals.html#making-signals-depend-on-each-other)
/// > firstly. Only if you really have to, use this function. This is in effect the /// > Only if you really have to, use this function. This is, in effect, the
/// > ["If you really must..."](https://book.leptos.dev/reactivity/working_with_signals.html#if-you-really-must). /// > ["If you really must..." option](https://book.leptos.dev/reactivity/working_with_signals.html#if-you-really-must).
/// ///
/// ## Demo /// ## Demo
/// ///
@ -112,9 +112,10 @@ use std::rc::Rc;
/// let stop = sync_signal_with_options( /// let stop = sync_signal_with_options(
/// (a, set_a), /// (a, set_a),
/// (b, set_b), /// (b, set_b),
/// SyncSignalOptions::default() /// SyncSignalOptions::with_transforms(
/// .transform_ltr(|left| *left * 2) /// |left| *left * 2,
/// .transform_rtl(|right| *right / 2) /// |right| *right / 2,
/// ),
/// ); /// );
/// ///
/// logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 10, b: 20 /// logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 10, b: 20
@ -129,20 +130,60 @@ use std::rc::Rc;
/// ///
/// #### Different Types /// #### Different Types
/// ///
/// `SyncSignalOptions::default()` is only defined if the two signal types are identical or /// `SyncSignalOptions::default()` is only defined if the two signal types are identical.
/// implement `From` for each other. Otherwise, you have to initialize the options with /// Otherwise, you have to initialize the options with `with_transforms` or `with_assigns` instead
/// `with_transforms` instead of `default`. /// of `default`.
/// ///
/// ``` /// ```
/// # use leptos_use::SyncSignalOptions; /// # use leptos::*;
/// # use leptos_use::{sync_signal_with_options, SyncSignalOptions};
/// # use std::str::FromStr; /// # use std::str::FromStr;
/// # /// #
/// let options = SyncSignalOptions::with_transforms( /// # #[component]
/// |left: &String| i32::from_str(left).unwrap_or_default(), /// # fn Demo() -> impl IntoView {
/// |right: &i32| right.to_string(), /// let (a, set_a) = create_signal("10".to_string());
/// let (b, set_b) = create_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::*;
/// # 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) = create_signal(Foo { bar: 10 });
/// let (b, set_b) = create_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! { }
/// # }
/// ```
pub fn sync_signal<T>( pub fn sync_signal<T>(
left: impl Into<UseRwSignal<T>>, left: impl Into<UseRwSignal<T>>,
right: impl Into<UseRwSignal<T>>, right: impl Into<UseRwSignal<T>>,
@ -166,7 +207,7 @@ where
let SyncSignalOptions { let SyncSignalOptions {
immediate, immediate,
direction, direction,
transforms transforms,
} = options; } = options;
let (assign_ltr, assign_rtl) = transforms.assigns(); let (assign_ltr, assign_rtl) = transforms.assigns();
@ -228,13 +269,19 @@ pub enum SyncDirection {
pub type AssignFn<T, S> = Rc<dyn Fn(&mut T, &S)>; pub type AssignFn<T, S> = Rc<dyn Fn(&mut T, &S)>;
/// Transforms or assigns for syncing.
pub enum SyncTransforms<L, R> { pub enum SyncTransforms<L, R> {
/// Transform the signal into each other by calling the transform functions.
/// The values are then simply assigned.
Transforms { Transforms {
/// Transforms the left signal into the right signal. /// Transforms the left signal into the right signal.
ltr: Rc<dyn Fn(&L) -> R>, ltr: Rc<dyn Fn(&L) -> R>,
/// Transforms the right signal into the left signal. /// Transforms the right signal into the left signal.
rtl: Rc<dyn Fn(&R) -> L>, rtl: Rc<dyn Fn(&R) -> L>,
}, },
/// Assign the signals to each other. Instead of using `=` to assign the signals,
/// these functions are called.
Assigns { Assigns {
/// Assigns the left signal to the right signal. /// Assigns the left signal to the right signal.
ltr: AssignFn<R, L>, ltr: AssignFn<R, L>,
@ -256,13 +303,16 @@ where
} }
impl<L, R> SyncTransforms<L, R> impl<L, R> SyncTransforms<L, R>
where L: 'static, R: 'static where
L: 'static,
R: 'static,
{ {
/// Returns assign functions for both directions that respect the value of this enum.
pub fn assigns(&self) -> (AssignFn<R, L>, AssignFn<L, R>) { pub fn assigns(&self) -> (AssignFn<R, L>, AssignFn<L, R>) {
match self { match self {
SyncTransforms::Transforms { ltr, rtl } => { SyncTransforms::Transforms { ltr, rtl } => {
let ltr = Rc::clone(&ltr); let ltr = Rc::clone(ltr);
let rtl = Rc::clone(&rtl); let rtl = Rc::clone(rtl);
( (
Rc::new(move |right, left| *right = ltr(left)), Rc::new(move |right, left| *right = ltr(left)),
Rc::new(move |left, right| *left = rtl(right)), Rc::new(move |left, right| *left = rtl(right)),
@ -285,13 +335,15 @@ pub struct SyncSignalOptions<L, R> {
direction: SyncDirection, direction: SyncDirection,
/// How to transform or assign the values to each other /// How to transform or assign the values to each other
/// Defaults to identity. /// If `L` and `R` are identical this defaults to the simple `=` operator. If the types are
/// not the same, then you have to choose to either use [`SyncSignalOptions::with_transforms`]
/// or [`SyncSignalOptions::with_assigns`].
#[builder(skip)] #[builder(skip)]
transforms: SyncTransforms<L, R>, transforms: SyncTransforms<L, R>,
} }
impl<L, R> SyncSignalOptions<L, R> { impl<L, R> SyncSignalOptions<L, R> {
/// Initializes options with transforms /// Initializes options with transforms functions that convert the signals into each other.
pub fn with_transforms( pub fn with_transforms(
transform_ltr: impl Fn(&L) -> R + 'static, transform_ltr: impl Fn(&L) -> R + 'static,
transform_rtl: impl Fn(&R) -> L + 'static, transform_rtl: impl Fn(&R) -> L + 'static,
@ -306,6 +358,7 @@ impl<L, R> SyncSignalOptions<L, R> {
} }
} }
/// Initializes options with assign functions that replace the default `=` operator.
pub fn with_assigns( pub fn with_assigns(
assign_ltr: impl Fn(&mut R, &L) + 'static, assign_ltr: impl Fn(&mut R, &L) + 'static,
assign_rtl: impl Fn(&mut L, &R) + 'static, assign_rtl: impl Fn(&mut L, &R) + 'static,

View file

@ -4,6 +4,7 @@ use leptos::ev::{copy, cut};
use leptos::*; use leptos::*;
/// Reactive [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API). /// Reactive [Clipboard API](https://developer.mozilla.org/en-US/docs/Web/API/Clipboard_API).
///
/// Provides the ability to respond to clipboard commands (cut, copy, and paste) /// 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. /// as well as to asynchronously read from and write to the system clipboard.
/// Access to the contents of the clipboard is gated behind the /// Access to the contents of the clipboard is gated behind the

View file

@ -2,6 +2,7 @@ use cfg_if::cfg_if;
use leptos::*; use leptos::*;
/// Reactive [DeviceOrientationEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent). /// Reactive [DeviceOrientationEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent).
///
/// Provide web developers with information from the physical orientation of /// Provide web developers with information from the physical orientation of
/// the device running the web page. /// the device running the web page.
/// ///

View file

@ -13,6 +13,7 @@ cfg_if! { if #[cfg(not(feature = "ssr"))] {
}} }}
/// Use EventListener with ease. /// Use EventListener with ease.
///
/// Register using [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) on mounted, /// Register using [addEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener) on mounted,
/// and [removeEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) automatically on cleanup. /// and [removeEventListener](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) automatically on cleanup.
/// ///

View file

@ -3,6 +3,7 @@ use default_struct_builder::DefaultBuilder;
use leptos::*; use leptos::*;
/// Reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API). /// Reactive [Geolocation API](https://developer.mozilla.org/en-US/docs/Web/API/Geolocation_API).
///
/// It allows the user to provide their location to web applications if they so desire. For privacy reasons, /// 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. /// the user is asked for permission to report location information.
/// ///

View file

@ -1,5 +1,5 @@
use leptos::*;
use crate::core::MaybeRwSignal; use crate::core::MaybeRwSignal;
use leptos::*;
/// A boolean switcher with utility functions. /// A boolean switcher with utility functions.
/// ///
@ -25,7 +25,9 @@ use crate::core::MaybeRwSignal;
/// ///
/// * [`fn@crate::use_cycle_list`] /// * [`fn@crate::use_cycle_list`]
// #[doc(cfg(feature = "use_toggle"))] // #[doc(cfg(feature = "use_toggle"))]
pub fn use_toggle(initial_value: impl Into<MaybeRwSignal<bool>>) -> UseToggleReturn<impl Fn() + Clone + 'static> { pub fn use_toggle(
initial_value: impl Into<MaybeRwSignal<bool>>,
) -> UseToggleReturn<impl Fn() + Clone + 'static> {
let initial_value = initial_value.into(); let initial_value = initial_value.into();
let (value, set_value) = initial_value.into_signal(); let (value, set_value) = initial_value.into_signal();
@ -52,4 +54,4 @@ where
pub value: Signal<bool>, pub value: Signal<bool>,
/// Sets the current value to the given value. /// Sets the current value to the given value.
pub set_value: WriteSignal<bool>, pub set_value: WriteSignal<bool>,
} }

View file

@ -151,6 +151,7 @@ async fn create_media(video: bool, audio: bool) -> Result<web_sys::MediaStream,
} }
/// Options for [`use_user_media_with_options`]. /// Options for [`use_user_media_with_options`].
///
/// Either or both constraints must be specified. /// Either or both constraints must be specified.
/// If the browser cannot find all media tracks with the specified types that meet the constraints given, /// If the browser cannot find all media tracks with the specified types that meet the constraints given,
/// then the returned promise is rejected with `NotFoundError` /// then the returned promise is rejected with `NotFoundError`

View file

@ -359,6 +359,7 @@ impl From<&UseWebNotificationOptions> for web_sys::NotificationOptions {
} }
/// Options for [`UseWebNotificationReturn::show`]. /// Options for [`UseWebNotificationReturn::show`].
///
/// This can be used to override options passed to [`use_web_notification`]. /// This can be used to override options passed to [`use_web_notification`].
/// See [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/notification) for more info. /// See [MDN Docs](https://developer.mozilla.org/en-US/docs/Web/API/notification) for more info.
/// ///