Skip to content

Commit

Permalink
updated docs and changelog
Browse files Browse the repository at this point in the history
  • Loading branch information
maccesch committed Aug 28, 2024
1 parent 04bcb6b commit 074685a
Show file tree
Hide file tree
Showing 13 changed files with 107 additions and 39 deletions.
17 changes: 12 additions & 5 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@
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).

## [Unreleased] -
## [0.13.0] - 2024-08-28

### New Functions 🚀

- `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
- `SyncSignalOptions` now can take now either transformations or assignment functions but not both.

### Change 🔥
### Fixes 🍕

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

Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "leptos-use"
version = "0.12.0"
version = "0.13.0"
edition = "2021"
authors = ["Marc-Stefan Cassola"]
categories = ["gui", "web-programming"]
Expand Down
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://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/-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>

<br/>
Expand Down Expand Up @@ -87,9 +87,9 @@ This will create the function file in the src directory, scaffold an example dir

## Leptos compatibility

| Crate version | Compatible Leptos version |
|------------------|---------------------------|
| <= 0.3 | 0.3 |
| 0.4, 0.5, 0.6 | 0.4 |
| 0.7, 0.8, 0.9 | 0.5 |
| 0.10, 0.11, 0.12 | 0.6 |
| Crate version | Compatible Leptos version |
|---------------|---------------------------|
| <= 0.3 | 0.3 |
| 0.4, 0.5, 0.6 | 0.4 |
| 0.7, 0.8, 0.9 | 0.5 |
| 0.10 0.13 | 0.6 |
2 changes: 1 addition & 1 deletion docs/book/src/introduction.md
Original file line number Diff line number Diff line change
Expand Up @@ -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://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="./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>
</div>
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@ mod is_ok;
mod is_some;
#[cfg(feature = "on_click_outside")]
mod on_click_outside;
#[cfg(feature = "use_toggle")]
mod use_toggle;
#[cfg(feature = "signal_debounced")]
mod signal_debounced;
#[cfg(feature = "signal_throttled")]
Expand Down Expand Up @@ -136,6 +134,8 @@ mod use_timeout_fn;
mod use_timestamp;
#[cfg(feature = "use_to_string")]
mod use_to_string;
#[cfg(feature = "use_toggle")]
mod use_toggle;
#[cfg(feature = "use_user_media")]
mod use_user_media;
#[cfg(feature = "use_web_notification")]
Expand Down Expand Up @@ -169,8 +169,6 @@ pub use is_ok::*;
pub use is_some::*;
#[cfg(feature = "on_click_outside")]
pub use on_click_outside::*;
#[cfg(feature = "use_toggle")]
pub use use_toggle::*;
#[cfg(feature = "signal_debounced")]
pub use signal_debounced::*;
#[cfg(feature = "signal_throttled")]
Expand Down Expand Up @@ -277,6 +275,8 @@ pub use use_timeout_fn::*;
pub use use_timestamp::*;
#[cfg(feature = "use_to_string")]
pub use use_to_string::*;
#[cfg(feature = "use_toggle")]
pub use use_toggle::*;
#[cfg(feature = "use_user_media")]
pub use use_user_media::*;
#[cfg(feature = "use_web_notification")]
Expand Down
89 changes: 71 additions & 18 deletions src/sync_signal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use std::rc::Rc;
///
/// > 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)
/// > firstly. 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).
/// > Only if you really have to, use this function. This is, in effect, the
/// > ["If you really must..." option](https://book.leptos.dev/reactivity/working_with_signals.html#if-you-really-must).
///
/// ## Demo
///
Expand Down Expand Up @@ -112,9 +112,10 @@ use std::rc::Rc;
/// let stop = sync_signal_with_options(
/// (a, set_a),
/// (b, set_b),
/// SyncSignalOptions::default()
/// .transform_ltr(|left| *left * 2)
/// .transform_rtl(|right| *right / 2)
/// SyncSignalOptions::with_transforms(
/// |left| *left * 2,
/// |right| *right / 2,
/// ),
/// );
///
/// logging::log!("a: {}, b: {}", a.get(), b.get()); // a: 10, b: 20
Expand All @@ -129,20 +130,60 @@ use std::rc::Rc;
///
/// #### Different Types
///
/// `SyncSignalOptions::default()` is only defined if the two signal types are identical or
/// implement `From` for each other. Otherwise, you have to initialize the options with
/// `with_transforms` instead of `default`.
/// `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_use::SyncSignalOptions;
/// # use leptos::*;
/// # use leptos_use::{sync_signal_with_options, SyncSignalOptions};
/// # use std::str::FromStr;
/// #
/// let options = SyncSignalOptions::with_transforms(
/// |left: &String| i32::from_str(left).unwrap_or_default(),
/// |right: &i32| right.to_string(),
/// # #[component]
/// # fn Demo() -> impl IntoView {
/// 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>(
left: impl Into<UseRwSignal<T>>,
right: impl Into<UseRwSignal<T>>,
Expand All @@ -166,7 +207,7 @@ where
let SyncSignalOptions {
immediate,
direction,
transforms
transforms,
} = options;

let (assign_ltr, assign_rtl) = transforms.assigns();
Expand Down Expand Up @@ -228,13 +269,19 @@ pub enum SyncDirection {

pub type AssignFn<T, S> = Rc<dyn Fn(&mut T, &S)>;

/// Transforms or assigns for syncing.
pub enum SyncTransforms<L, R> {
/// Transform the signal into each other by calling the transform functions.
/// The values are then simply assigned.
Transforms {
/// Transforms the left signal into the right signal.
ltr: Rc<dyn Fn(&L) -> R>,
/// Transforms the right signal into the left signal.
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 the left signal to the right signal.
ltr: AssignFn<R, L>,
Expand All @@ -256,13 +303,16 @@ where
}

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>) {
match self {
SyncTransforms::Transforms { ltr, rtl } => {
let ltr = Rc::clone(&ltr);
let rtl = Rc::clone(&rtl);
let ltr = Rc::clone(ltr);
let rtl = Rc::clone(rtl);
(
Rc::new(move |right, left| *right = ltr(left)),
Rc::new(move |left, right| *left = rtl(right)),
Expand All @@ -285,13 +335,15 @@ pub struct SyncSignalOptions<L, R> {
direction: SyncDirection,

/// 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)]
transforms: SyncTransforms<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(
transform_ltr: impl Fn(&L) -> R + 'static,
transform_rtl: impl Fn(&R) -> L + 'static,
Expand All @@ -306,6 +358,7 @@ impl<L, R> SyncSignalOptions<L, R> {
}
}

/// Initializes options with assign functions that replace the default `=` operator.
pub fn with_assigns(
assign_ltr: impl Fn(&mut R, &L) + 'static,
assign_rtl: impl Fn(&mut L, &R) + 'static,
Expand Down
1 change: 1 addition & 0 deletions src/use_clipboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use leptos::ev::{copy, cut};
use leptos::*;

/// 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)
/// as well as to asynchronously read from and write to the system clipboard.
/// Access to the contents of the clipboard is gated behind the
Expand Down
1 change: 1 addition & 0 deletions src/use_device_orientation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use cfg_if::cfg_if;
use leptos::*;

/// Reactive [DeviceOrientationEvent](https://developer.mozilla.org/en-US/docs/Web/API/DeviceOrientationEvent).
///
/// Provide web developers with information from the physical orientation of
/// the device running the web page.
///
Expand Down
1 change: 1 addition & 0 deletions src/use_event_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ cfg_if! { if #[cfg(not(feature = "ssr"))] {
}}

/// Use EventListener with ease.
///
/// 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.
///
Expand Down
1 change: 1 addition & 0 deletions src/use_geolocation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use default_struct_builder::DefaultBuilder;
use leptos::*;

/// 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,
/// the user is asked for permission to report location information.
///
Expand Down
8 changes: 5 additions & 3 deletions src/use_toggle.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use leptos::*;
use crate::core::MaybeRwSignal;
use leptos::*;

/// A boolean switcher with utility functions.
///
Expand All @@ -25,7 +25,9 @@ use crate::core::MaybeRwSignal;
///
/// * [`fn@crate::use_cycle_list`]
// #[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 (value, set_value) = initial_value.into_signal();

Expand All @@ -52,4 +54,4 @@ where
pub value: Signal<bool>,
/// Sets the current value to the given value.
pub set_value: WriteSignal<bool>,
}
}
1 change: 1 addition & 0 deletions src/use_user_media.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ async fn create_media(video: bool, audio: bool) -> Result<web_sys::MediaStream,
}

/// Options for [`use_user_media_with_options`].
///
/// Either or both constraints must be specified.
/// 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`
Expand Down
1 change: 1 addition & 0 deletions src/use_web_notification.rs
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,7 @@ impl From<&UseWebNotificationOptions> for web_sys::NotificationOptions {
}

/// Options for [`UseWebNotificationReturn::show`].
///
/// 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.
///
Expand Down

0 comments on commit 074685a

Please sign in to comment.