use std::rc::Rc;
use dioxus::prelude::*;
use super::shared::{js_number, merge_style, use_motion_support};
use crate::class::compose;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum VisualEffectVariant {
#[default]
Mesh,
Aurora,
Spotlight,
Grain,
Border,
Draw,
Depth,
}
impl VisualEffectVariant {
pub fn as_str(self) -> &'static str {
match self {
Self::Mesh => "mesh",
Self::Aurora => "aurora",
Self::Spotlight => "spotlight",
Self::Grain => "grain",
Self::Border => "border",
Self::Draw => "draw",
Self::Depth => "depth",
}
}
}
pub(crate) fn normalize_effect_intensity(value: f64) -> f64 {
if value.is_finite() {
value.clamp(0.0, 1.0)
} else {
0.5
}
}
pub(crate) fn spotlight_offset(pointer: f64, start: f64, size: f64) -> String {
let size = if size.is_finite() { size.max(0.0) } else { 0.0 };
let offset = (pointer - start).max(0.0).min(size);
format!("{}px", js_number(offset))
}
type Bounds = (f64, f64, f64, f64);
#[derive(Props, Clone, PartialEq)]
pub struct VisualEffectProps {
#[props(default)]
pub animated: bool,
#[props(default = 0.5, into)]
pub intensity: f64,
#[props(default)]
pub variant: VisualEffectVariant,
#[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 VisualEffect(props: VisualEffectProps) -> Element {
let support = use_motion_support();
let mut root = use_hook(|| CopyValue::new(None::<Rc<MountedData>>));
let mut bounds = use_hook(|| CopyValue::new(None::<Bounds>));
let mut spotlight = use_signal(|| None::<(String, String)>);
let tracks = props.variant == VisualEffectVariant::Spotlight;
let refresh_bounds = move || {
if let Some(element) = root.peek().clone() {
spawn(async move {
if let Ok(rect) = element.get_client_rect().await {
bounds.set(Some((
rect.origin.x,
rect.origin.y,
rect.size.width,
rect.size.height,
)));
}
});
}
};
let mut reset = move || {
if tracks {
spotlight.set(Some(("50%".to_string(), "50%".to_string())));
}
};
let intensity = js_number(normalize_effect_intensity(props.intensity));
let own = match spotlight() {
Some((x, y)) => format!(
"--ui-effect-intensity: {intensity}; --ui-spotlight-x: {x}; --ui-spotlight-y: {y}"
),
None => format!("--ui-effect-intensity: {intensity}"),
};
let style = merge_style(props.style.as_deref(), &own);
let class = compose([Some("ui-visual-effect"), props.class.as_deref()]);
rsx! {
div {
class,
"data-ui-effect-animated": "{props.animated}",
"data-ui-visual-effect": props.variant.as_str(),
style,
onmounted: move |event| root.set(Some(event.data())),
onpointerenter: move |_| {
if tracks {
refresh_bounds();
}
},
onpointermove: move |event| {
if !tracks || event.pointer_type() == "touch" || support.peek().reduced() {
return;
}
let point = event.client_coordinates();
match *bounds.peek() {
Some((left, top, width, height)) => spotlight.set(Some((
spotlight_offset(point.x, left, width),
spotlight_offset(point.y, top, height),
))),
None => refresh_bounds(),
}
},
onpointerleave: move |_| reset(),
onfocusout: move |_| reset(),
..props.attributes,
{props.children}
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum MotionGroupDuration {
Fast,
#[default]
Standard,
Slow,
}
impl MotionGroupDuration {
pub fn as_str(self) -> &'static str {
match self {
Self::Fast => "fast",
Self::Standard => "standard",
Self::Slow => "slow",
}
}
}
#[derive(Props, Clone, PartialEq)]
pub struct MotionGroupProps {
#[props(default)]
pub duration: MotionGroupDuration,
#[props(default = true)]
pub enter_exit: bool,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn MotionGroup(props: MotionGroupProps) -> Element {
let class = compose([Some("ui-motion-group"), props.class.as_deref()]);
rsx! {
div {
class,
"data-ui-motion-group": "true",
..props.attributes,
{props.children}
}
}
}
#[cfg(test)]
mod tests {
use super::{normalize_effect_intensity, spotlight_offset};
#[test]
fn intensity_clamps_like_lumen_core() {
assert!((normalize_effect_intensity(3.0) - 1.0).abs() < f64::EPSILON);
assert!(normalize_effect_intensity(-1.0).abs() < f64::EPSILON);
assert!((normalize_effect_intensity(f64::INFINITY) - 0.5).abs() < f64::EPSILON);
}
#[test]
fn spotlight_stays_inside_the_box() {
assert_eq!(spotlight_offset(150.0, 100.0, 200.0), "50px");
assert_eq!(spotlight_offset(50.0, 100.0, 200.0), "0px");
assert_eq!(spotlight_offset(500.0, 100.0, 200.0), "200px");
assert_eq!(spotlight_offset(500.0, 100.0, f64::NAN), "0px");
}
}