use dioxus::prelude::*;
use super::shared::{js_number, js_round, merge_style};
use crate::class::compose;
const PARALLAX_DEFAULT_FROM: f64 = -6.0;
const PARALLAX_DEFAULT_TO: f64 = 6.0;
const PARALLAX_MAX_OFFSET: f64 = 50.0;
const PARALLAX_MAX_ZOOM: f64 = 1.5;
fn round_two(value: f64) -> f64 {
js_round(value * 100.0) / 100.0
}
pub(crate) fn normalize_parallax_offset(value: Option<f64>, fallback: f64) -> f64 {
match value {
Some(value) if value.is_finite() => {
round_two(value.clamp(-PARALLAX_MAX_OFFSET, PARALLAX_MAX_OFFSET))
}
_ => fallback,
}
}
pub(crate) fn normalize_parallax_zoom(value: Option<f64>) -> f64 {
match value {
Some(value) if value.is_finite() => round_two(value.clamp(1.0, PARALLAX_MAX_ZOOM)),
_ => 1.0,
}
}
pub(crate) fn parallax_properties(from: Option<f64>, to: Option<f64>, zoom: Option<f64>) -> String {
let from = normalize_parallax_offset(from, PARALLAX_DEFAULT_FROM);
let to = normalize_parallax_offset(to, PARALLAX_DEFAULT_TO);
let zoom = normalize_parallax_zoom(zoom);
let offsets = format!(
"--ui-parallax-from: {}%; --ui-parallax-to: {}%",
js_number(from),
js_number(to)
);
if (zoom - 1.0).abs() > f64::EPSILON {
format!("{offsets}; --ui-parallax-zoom: {}", js_number(zoom))
} else {
offsets
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ParallaxProps {
#[props(default)]
pub clip: bool,
#[props(into)]
pub from: Option<f64>,
#[props(into)]
pub to: Option<f64>,
#[props(into)]
pub zoom: Option<f64>,
#[props(into)]
pub style: Option<String>,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn Parallax(props: ParallaxProps) -> Element {
let class = compose([
Some("ui-parallax"),
props.clip.then_some("ui-parallax--clip"),
props.class.as_deref(),
]);
let style = merge_style(
props.style.as_deref(),
¶llax_properties(props.from, props.to, props.zoom),
);
rsx! {
div { class, style, ..props.attributes, {props.children} }
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ViewProgressRange {
#[default]
Cover,
Contain,
Entry,
Exit,
}
impl ViewProgressRange {
pub fn as_str(self) -> &'static str {
match self {
Self::Cover => "cover",
Self::Contain => "contain",
Self::Entry => "entry",
Self::Exit => "exit",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct ViewProgressProps {
#[props(default)]
pub range: ViewProgressRange,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn ViewProgress(props: ViewProgressProps) -> Element {
let range = format!("ui-view-progress--{}", props.range.as_str());
let class = compose([
Some("ui-view-progress"),
Some(range.as_str()),
props.class.as_deref(),
]);
rsx! {
div { class, ..props.attributes, {props.children} }
}
}
#[cfg(test)]
mod tests {
use super::{normalize_parallax_offset, normalize_parallax_zoom, parallax_properties};
#[test]
fn offsets_clamp_and_round_like_lumen_core() {
assert!((normalize_parallax_offset(Some(80.0), 6.0) - 50.0).abs() < f64::EPSILON);
assert!((normalize_parallax_offset(Some(-80.0), 6.0) + 50.0).abs() < f64::EPSILON);
assert!((normalize_parallax_offset(Some(1.234_56), 6.0) - 1.23).abs() < 1e-9);
assert!((normalize_parallax_offset(Some(f64::NAN), 6.0) - 6.0).abs() < f64::EPSILON);
assert!((normalize_parallax_offset(None, -6.0) + 6.0).abs() < f64::EPSILON);
}
#[test]
fn zoom_clamps_to_its_range() {
assert!((normalize_parallax_zoom(Some(3.0)) - 1.5).abs() < f64::EPSILON);
assert!((normalize_parallax_zoom(Some(0.2)) - 1.0).abs() < f64::EPSILON);
assert!((normalize_parallax_zoom(None) - 1.0).abs() < f64::EPSILON);
}
#[test]
fn properties_omit_a_resting_zoom() {
assert_eq!(
parallax_properties(None, None, None),
"--ui-parallax-from: -6%; --ui-parallax-to: 6%"
);
assert_eq!(
parallax_properties(Some(-0.0), Some(12.5), Some(1.2)),
"--ui-parallax-from: 0%; --ui-parallax-to: 12.5%; --ui-parallax-zoom: 1.2"
);
}
}