use dioxus::prelude::*;
use super::util::js_number;
use crate::class::compose;
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeviceFrameDevice {
#[default]
Laptop,
Desktop,
Iphone,
Android,
Tablet,
MacbookPro,
MacbookAir,
Imac,
Pixel,
IpadPro,
}
impl DeviceFrameDevice {
pub fn as_str(self) -> &'static str {
match self {
Self::Laptop => "laptop",
Self::Desktop => "desktop",
Self::Iphone => "iphone",
Self::Android => "android",
Self::Tablet => "tablet",
Self::MacbookPro => "macbook-pro",
Self::MacbookAir => "macbook-air",
Self::Imac => "imac",
Self::Pixel => "pixel",
Self::IpadPro => "ipad-pro",
}
}
pub fn screen_size(self) -> (f64, f64) {
match self {
Self::Laptop | Self::MacbookPro | Self::MacbookAir => (1280.0, 800.0),
Self::Desktop => (1440.0, 900.0),
Self::Iphone => (390.0, 844.0),
Self::Android | Self::Pixel => (412.0, 915.0),
Self::Tablet => (820.0, 1180.0),
Self::Imac => (1440.0, 810.0),
Self::IpadPro => (834.0, 1194.0),
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum DeviceFrameOrientation {
Portrait,
Landscape,
}
impl DeviceFrameOrientation {
pub fn as_str(self) -> &'static str {
match self {
Self::Portrait => "portrait",
Self::Landscape => "landscape",
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum DeviceFrameTone {
Light,
#[default]
Dark,
}
impl DeviceFrameTone {
pub fn as_str(self) -> &'static str {
match self {
Self::Light => "light",
Self::Dark => "dark",
}
}
}
pub(crate) fn resolve_device_frame_color(color: &str) -> Option<&str> {
if color == "white" || color == "black" {
return Some(color);
}
let digits = color.strip_prefix('#')?;
([3, 4, 6, 8].contains(&digits.len()) && digits.bytes().all(|byte| byte.is_ascii_hexdigit()))
.then_some(color)
}
pub(crate) fn resolve_device_frame_size(
device: DeviceFrameDevice,
orientation: Option<DeviceFrameOrientation>,
width: Option<f64>,
height: Option<f64>,
) -> (f64, f64) {
let (default_width, default_height) = device.screen_size();
let dimension = |value: Option<f64>, fallback: f64| {
value
.filter(|value| value.is_finite() && (1.0..=16384.0).contains(value))
.unwrap_or(fallback)
};
let first = dimension(width, default_width);
let second = dimension(height, default_height);
match orientation {
None => (first, second),
Some(DeviceFrameOrientation::Landscape) => (first.max(second), first.min(second)),
Some(DeviceFrameOrientation::Portrait) => (first.min(second), first.max(second)),
}
}
#[derive(Props, Clone, PartialEq)]
pub struct DeviceFrameProps {
#[props(default)]
pub device: DeviceFrameDevice,
pub orientation: Option<DeviceFrameOrientation>,
#[props(default)]
pub tone: DeviceFrameTone,
#[props(into)]
pub color: Option<String>,
pub screen_width: Option<f64>,
pub screen_height: Option<f64>,
#[props(default = true)]
pub scroll: bool,
#[props(into)]
pub class: Option<String>,
#[props(extends = GlobalAttributes, extends = div)]
pub attributes: Vec<Attribute>,
pub children: Element,
}
#[component]
pub fn DeviceFrame(props: DeviceFrameProps) -> Element {
let (width, height) = resolve_device_frame_size(
props.device,
props.orientation,
props.screen_width,
props.screen_height,
);
let mut scale = use_signal(|| None::<f64>);
let color = props
.color
.as_deref()
.and_then(resolve_device_frame_color)
.map(|color| format!("--ui-device-color: {color}"));
let (width_text, height_text) = (js_number(width), js_number(height));
let scale_text = scale().map_or_else(String::new, |scale| {
format!("; --ui-device-frame-scale: {}", js_number(scale))
});
let screen_style = format!(
"--ui-device-frame-height: {height_text}px; --ui-device-frame-width: {width_text}px; aspect-ratio: {width_text} / {height_text}{scale_text}"
);
let class = compose([Some("ui-device-frame"), props.class.as_deref()]);
rsx! {
div {
class,
"data-device": props.device.as_str(),
"data-tone": props.tone.as_str(),
"data-orientation": props.orientation.map(DeviceFrameOrientation::as_str),
style: color,
..props.attributes,
div { class: "ui-device-frame__shell",
span { class: "ui-device-frame__hardware", aria_hidden: "true",
span { class: "ui-device-frame__action" }
span { class: "ui-device-frame__volume-up" }
span { class: "ui-device-frame__volume-down" }
span { class: "ui-device-frame__power" }
span { class: "ui-device-frame__rail-left" }
span { class: "ui-device-frame__rail-right" }
}
div { class: "ui-device-frame__glass",
span { aria_hidden: "true", class: "ui-device-frame__camera" }
div {
class: "ui-device-frame__screen",
"data-scroll": if props.scroll { "true" } else { "false" },
style: screen_style,
onresize: move |event: ResizeEvent| {
if let Ok(size) = event.get_content_box_size()
&& size.width > 0.0
{
scale.set(Some(size.width / width));
}
},
{props.children}
}
}
}
span { aria_hidden: "true", class: "ui-device-frame__base" }
}
}
}
#[cfg(test)]
mod tests {
use super::{
DeviceFrameDevice, DeviceFrameOrientation, resolve_device_frame_color,
resolve_device_frame_size,
};
#[test]
fn colors_are_bounded() {
assert_eq!(resolve_device_frame_color("white"), Some("white"));
assert_eq!(resolve_device_frame_color("#fff"), Some("#fff"));
assert_eq!(resolve_device_frame_color("#FF000080"), Some("#FF000080"));
assert_eq!(resolve_device_frame_color("#ff00"), Some("#ff00"));
assert_eq!(resolve_device_frame_color("#ff0"), Some("#ff0"));
assert_eq!(resolve_device_frame_color("#ff000"), None);
assert_eq!(resolve_device_frame_color("red"), None);
assert_eq!(resolve_device_frame_color("#ff0;x"), None);
assert_eq!(resolve_device_frame_color("#gggggg"), None);
}
#[test]
fn sizes_follow_lumen_core() {
let iphone = DeviceFrameDevice::Iphone;
assert_eq!(
resolve_device_frame_size(iphone, None, None, None),
(390.0, 844.0)
);
assert_eq!(
resolve_device_frame_size(iphone, Some(DeviceFrameOrientation::Landscape), None, None),
(844.0, 390.0)
);
assert_eq!(
resolve_device_frame_size(
DeviceFrameDevice::Tablet,
Some(DeviceFrameOrientation::Portrait),
Some(900.0),
Some(600.0)
),
(600.0, 900.0)
);
assert_eq!(
resolve_device_frame_size(DeviceFrameDevice::Laptop, None, Some(0.5), Some(f64::NAN)),
(1280.0, 800.0)
);
assert_eq!(
resolve_device_frame_size(
DeviceFrameDevice::Laptop,
None,
Some(20_000.0),
Some(16384.0)
),
(1280.0, 16384.0)
);
}
}