2024-08-18 17:17:06 -04:00
use crate ::utils ::get_header ;
use default_struct_builder ::DefaultBuilder ;
2024-08-22 17:08:21 +01:00
use leptos ::prelude ::* ;
use std ::sync ::Arc ;
2024-08-18 17:17:06 -04:00
/// Reactive [reduced motions preference](https://developer.mozilla.org/en-US/docs/Web/CSS/@media/prefers-reduced-motion).
///
/// ## Demo
///
/// [Link to Demo](https://github.com/Synphonyte/leptos-use/tree/main/examples/use_prefers_reduced_motion)
///
/// ## Usage
///
/// ```
2024-08-22 17:08:21 +01:00
/// # use leptos::prelude::*;
2024-08-18 17:17:06 -04:00
/// # use leptos_use::use_prefers_reduced_motion;
2024-08-18 18:43:35 -04:00
/// # use leptos_use::docs::BooleanDisplay;
2024-08-18 17:17:06 -04:00
/// #
/// # #[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
///
/// On the server this will try to read the
/// [`Sec-CH-Prefers-Reduced-Motion` header](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-Prefers-Reduced-Motion)
/// 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 `axum` you have to enable the `"axum"` feature in your Cargo.toml.
/// > In case it's `actix-web` enable the feature `"actix"`, for `spin` enable `"spin"`.
///
/// ### Bring your own header
///
/// In case you're neither using Axum, Actix nor Spin, 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
///
/// * [`fn@crate::use_media_query`]
/// * [`fn@crate::use_preferred_contrast`]
/// * [`fn@crate::use_preferred_dark`]
pub fn use_prefers_reduced_motion ( ) -> Signal < bool > {
use_prefers_reduced_motion_with_options ( UsePrefersReducedMotionOptions ::default ( ) )
}
/// Version of [`fn@crate::use_prefers_reduced_motion`] that takes a `UsePrefersReducedMotionOptions`. See [`fn@crate::use_prefers_reduced_motion`] for how to use.
2024-08-18 18:57:04 -04:00
pub fn use_prefers_reduced_motion_with_options (
options : UsePrefersReducedMotionOptions ,
) -> Signal < bool > {
2024-08-18 17:17:06 -04:00
#[ cfg(not(feature = " ssr " )) ]
{
let _ = options ;
crate ::use_media_query ( " (prefers-reduced-motion: reduce) " )
}
#[ cfg(feature = " ssr " ) ]
{
Signal ::derive ( move | | ( options . ssr_motion_header_getter ) ( ) = = Some ( " reduce " . to_string ( ) ) )
}
}
/// Options for [`fn@crate::use_prefers_reduced_motion_with_options`].
#[ derive(DefaultBuilder) ]
pub struct UsePrefersReducedMotionOptions {
/// Getter function to return the string value of the
/// [`Sec-CH-Prefers-Reduced-Motion`](https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Sec-CH-Prefers-Reduced-Motion)
/// header.
/// When you use one of the features `"axum"`, `"actix"` or `"spin"` there's a valid default
/// implementation provided.
#[ allow(dead_code) ]
2024-08-22 17:08:21 +01:00
pub ( crate ) ssr_motion_header_getter : Arc < dyn Fn ( ) -> Option < String > + Send + Sync > ,
2024-08-18 17:17:06 -04:00
}
impl Default for UsePrefersReducedMotionOptions {
fn default ( ) -> Self {
Self {
2024-08-22 17:08:21 +01:00
ssr_motion_header_getter : Arc ::new ( move | | {
2024-08-18 17:17:06 -04:00
get_header! (
HeaderName ::from_static ( " sec-ch-prefers-reduced-motion " ) ,
use_locale ,
ssr_motion_header_getter
)
} ) ,
}
}
}