Skip to content

Commit

Permalink
Add transform visual test
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielleHuisman committed Apr 3, 2024
1 parent 2cd2b31 commit f9991b3
Show file tree
Hide file tree
Showing 4 changed files with 224 additions and 7 deletions.
5 changes: 3 additions & 2 deletions packages/leptos/tests/playwright.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::{env, fs, path::Path, process::Command};

const IMPLEMENTED_TESTS: [&str; 10] = [
const IMPLEMENTED_TESTS: [&str; 11] = [
"arrow",
"autoPlacement",
"autoUpdate",
Expand All @@ -11,6 +11,7 @@ const IMPLEMENTED_TESTS: [&str; 10] = [
"relative",
"scroll",
"table",
"transform",
];

#[test]
Expand Down Expand Up @@ -111,7 +112,7 @@ pub fn playwright() {

let config_content = fs::read_to_string(repository_playwright_config_path.clone())
.expect("Reading Playwright config file failed.")
.replace("retries: 3,", "retries: 0,")
.replace("retries: 3,", "retries: 0,\n timeout: 10 * 1000,")
.replace(
"command: 'pnpm run dev',",
&format!(
Expand Down
19 changes: 14 additions & 5 deletions packages/leptos/tests/visual/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use crate::spec::placement::Placement;
use crate::spec::relative::Relative;
use crate::spec::scroll::Scroll;
use crate::spec::table::Table;
use crate::spec::transform::Transform;
use crate::utils::new::New;

const ROUTES: [&str; 23] = [
Expand Down Expand Up @@ -97,19 +98,27 @@ pub fn App() -> impl IntoView {
<Route path="new" view=New />
<Route path="placement" view=Placement />
<Route path="relative" view=Relative />
// <Route path="transform" view=Transform />
<Route path="transform" view=Transform />
<Route path="border" view=Border />
<Route path="scroll" view=Scroll />

// <Route path="decimal-size" view=DecimalSize />
<Route path="table" view=Table />

// <Route path="scrollbars" view=Scrollbars />
// <Route path="shift" view=Shift />
// <Route path="flip" view=Flip />
// <Route path="size" view=Size />
<Route path="arrow" view=Arrow />
<Route path="offset" view=Offset />
// <Route path="hide" view=Hide />
<Route path="autoPlacement" view=AutoPlacement />

// <Route path="inline" view=Inline />
<Route path="autoUpdate" view=AutoUpdate />

// <Route path="shadow-DOM" view=ShadowDom />
<Route path="containing-block" view=ContainingBlock />
// <Route path="virtual-element" view=VirtualElement />
// <Route path="perf" view=Perf />
// <Route path="iframe" view=Iframe />
// <Route path="top-layer" view=TopLayer />

<Route path="/*any" view=|| view! { <h1>"Not Found"</h1> }/>
</Route>
Expand Down
1 change: 1 addition & 0 deletions packages/leptos/tests/visual/src/spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ pub mod placement;
pub mod relative;
pub mod scroll;
pub mod table;
pub mod transform;
206 changes: 206 additions & 0 deletions packages/leptos/tests/visual/src/spec/transform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,206 @@
use std::ops::Deref;

use convert_case::{Case, Casing};
use floating_ui_leptos::{
use_floating, MiddlewareVec, Shift, ShiftOptions, UseFloatingOptions, UseFloatingReturn,
};
use leptos::{html::Div, *};
use wasm_bindgen::JsCast;

#[derive(Copy, Clone, Debug, PartialEq)]
enum Node {
None,
Reference,
Floating,
Body,
Html,
OffsetParent,
OffsetParent3d,
OffsetParentInverse,
OffsetParentReference,
Virtual,
Inline,
}

const ALL_NODES: [Node; 11] = [
Node::None,
Node::Reference,
Node::Floating,
Node::Body,
Node::Html,
Node::OffsetParent,
Node::OffsetParent3d,
Node::OffsetParentInverse,
Node::OffsetParentReference,
Node::Virtual,
Node::Inline,
];

#[component]
pub fn Transform() -> impl IntoView {
let reference_ref = create_node_ref::<Div>();
let floating_ref = create_node_ref::<Div>();
let offset_parent_ref = create_node_ref::<Div>();

let (node, set_node) = create_signal(Node::None);

let middleware: MiddlewareVec = vec![Box::new(Shift::new(
ShiftOptions::default().cross_axis(true),
))];

let UseFloatingReturn {
x,
y,
strategy,
update,
..
} = use_floating(
reference_ref,
floating_ref,
UseFloatingOptions::default()
.middleware(middleware.into())
.while_elements_mounted_auto_update(),
);

create_effect(move |_| {
let element = match node() {
Node::Html => document()
.document_element()
.map(|element| element.unchecked_into::<web_sys::HtmlElement>()),
Node::Body => document().body(),
Node::OffsetParent
| Node::OffsetParent3d
| Node::OffsetParentInverse
| Node::OffsetParentReference
| Node::Virtual
| Node::Inline => offset_parent_ref
.get()
.map(|offset_parent| offset_parent.deref().clone().into()),
_ => None,
};

if let Some(element) = element {
let transform = match node() {
Node::OffsetParent3d => "scale3d(0.5, 0.2, 0.7) translate3d(2rem, -2rem, 0)",
Node::OffsetParentInverse | Node::Virtual => "scale(0.5)",
_ => "scale(0.5) translate(2rem, -2rem)",
};

element
.style()
.set_property("transform", transform)
.expect("Style should be updated.");

if node() == Node::Virtual {
let _virtual_context = document().get_element_by_id("virtual-context");
// TODO: change reference ref to virtual element
}
}

update();
});

on_cleanup(move || {
let element = match node() {
Node::Html => document()
.document_element()
.map(|element| element.unchecked_into::<web_sys::HtmlElement>()),
Node::Body => document().body(),
Node::OffsetParent
| Node::OffsetParent3d
| Node::OffsetParentInverse
| Node::OffsetParentReference
| Node::Virtual
| Node::Inline => offset_parent_ref
.get()
.map(|offset_parent| offset_parent.deref().clone().into()),
_ => None,
};

if let Some(element) = element {
element
.style()
.remove_property("transform")
.expect("Style should be updated.");
}
});

view! {
<h1>Transform</h1>
<p>
The floating element should be positioned correctly on the bottom when a certain node has been transformed.
</p>
<div
_ref=offset_parent_ref
class="container"
style:overflow="hidden"
style:position=move || match node() {
Node::OffsetParent => "relative",
_ => ""
}
>
<span style:position=move || match node() {
Node::Inline => "relative",
_ => ""
}>
<Show when=move || node() == Node::Virtual>
<div
id="virtual-context"
style:width="50px"
style:height="50px"
style:background="black"
/>
</Show>
<div
_ref=reference_ref
class="reference"
style:transform=move || match node() {
Node::Reference | Node::OffsetParentReference => "scale(1.25) translate(2rem, -2rem)",
_ => ""
}
>
Reference
</div>
<div
_ref=floating_ref
class="floating"
style:position=move || format!("{:?}", strategy()).to_lowercase()
style:top=move || format!("{}px", y())
style:left=move || format!("{}px", x())
style:transform=move || match node() {
Node::Floating => "scale(1.25)",
_ => ""
}
style:transform-origin="top"
>
Floating
</div>
</span>
</div>

<div class="controls">
<For
each=|| ALL_NODES
key=|local_node| format!("{:?}", local_node)
children=move |local_node| view! {
<button
data-testid=move || format!("transform-{}", match local_node {
Node::None => "null".into(),
Node::OffsetParent3d => "offsetParent-3d".into(),
Node::OffsetParentInverse => "offsetParent-inverse".into(),
Node::OffsetParentReference => "offsetParent-reference".into(),
_ => format!("{:?}", local_node).to_case(Case::Camel)
})
style:background-color=move || match node() == local_node {
true => "black",
false => ""
}
on:click=move |_| set_node(local_node)
>
{format!("{:?}", local_node).to_case(Case::Camel)}
</button>
}
/>
</div>
}
}

0 comments on commit f9991b3

Please sign in to comment.