Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

element can't downcast on onmounted #3177

Closed
hackartists opened this issue Nov 4, 2024 · 0 comments · Fixed by #3178
Closed

element can't downcast on onmounted #3177

hackartists opened this issue Nov 4, 2024 · 0 comments · Fixed by #3178
Labels
bug Something isn't working html Related to the html crate web relating to the web renderer for dioxus

Comments

@hackartists
Copy link
Contributor

hackartists commented Nov 4, 2024

Problem

Steps To Reproduce

Steps to reproduce the behavior:

        div {
            class: "flex flex-col w-full h-full",
            div { class: "flex rounded-xl justify-center items-center bg-gray-200 flex-1 p-4 lg:px-8",
                div {
                    class: "w-full flex-[0_0_2%] aspect-square hidden lg:block",
                    onclick: prev_slide,
                    ChevronLeft { width: "100%", height: "100%" }
                }
                div { class: "flex-1 overflow-hidden h-full",
                    div {
                        id: "carousel-scroll-area",
                        class: "overflow-x-scroll h-full snap-mandatory snap-x flex scroll-smooth",
                        onmounted: move |cx| {
                            #[cfg(feature = "web")]
                            {
                                use dioxus_web::WebEventExt;
                                tracing::debug!("mounted data {:?}", cx);
                                let web_sys_element = cx.try_as_web_event();
                                tracing::debug!("web_sys_element {:?}", web_sys_element);
                                element.set(web_sys_element);
                            }
                        },
                        for (i, collection) in collections().iter().enumerate() {
                            CarouselItem {
                                key: "carousel_slide_{i}",
                                collection: collection.clone(),
                            }
                        }
                    }
                }
                div {
                    class: "w-full flex-[0_0_2%] aspect-square hidden lg:block",
                    onclick: next_slide,
                    ChevronRight { width: "100%", height: "100%" }
                }
            }
            div {
                class: "flex gap-3 justify-center",
                for num in 0..len {
                    Indicator {
                        key: "carousel_indicator_{num}",
                        current_index: index(),
                        target_index: num,
                        onclick: move |_| {
                            index.set(num);
                        }
                    }
                }
            }
        }

Expected behavior

At v0.6.0-alpha.3, this code does work properly.
After v0.6.0-alpha.4 it does not work.

In this code, try_as_web_event() returns None.

If I change it to as_web_event(), it causes panic with below error

/home/hackartist/.cargo/registry/src/index.crates.io-6f17d22bba15001f/dioxus-web-0.6.0-alpha.4/src/events/mod.rs:214:13:
Error downcasting to `web-sys`, event should be a web_sys::features::gen_Element::Element.

Stack:

Error
    at http://localhost:8080/wasm/dagit.js:816:21
    at logError (http://localhost:8080/wasm/dagit.js:215:18)
    at imports.wbg.__wbg_new_abda76e883ba8a5f (http://localhost:8080/wasm/dagit.js:815:66)
    at dagit-e066f91dc1f7dcfd.wasm.__wbg_new_abda76e883ba8a5f externref shim (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[17587]:0xb74603)
    at dagit-e066f91dc1f7dcfd.wasm.console_error_panic_hook::hook::h50e127993bd66b78 (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[3165]:0x75f380)
    at dagit-e066f91dc1f7dcfd.wasm.core::ops::function::Fn::call::hf3ca7ff900b4ae6b (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[19962]:0xb7f41e)
    at dagit-e066f91dc1f7dcfd.wasm.std::panicking::rust_panic_with_hook::he5c089ac7305193e (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[11127]:0xad1519)
    at dagit-e066f91dc1f7dcfd.wasm.std::panicking::begin_panic_handler::{{closure}}::h010c94f3a1c5c766 (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[12513]:0xb11fd4)
    at dagit-e066f91dc1f7dcfd.wasm.std::sys::backtrace::__rust_end_short_backtrace::hbe714695da4edadc (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[19822]:0xb7eee1)
    at dagit-e066f91dc1f7dcfd.wasm.rust_begin_unwind (http://localhost:8080/wasm/dagit_bg.wasm:wasm-function[14294]:0xb48e6f)

Environment:

  • Dioxus version: v0.6.0-alpha.4
  • Rust version: rustc 1.82.0 (f6e511eec 2024-10-15)
  • OS info: Linux hackartist-archlinux 6.11.6-arch1-1 SMP PREEMPT_DYNAMIC Fri, 01 Nov 2024 03:30:41 +0000 x86_64 GNU/Linux
  • App platform: fullstack
  • web-sys: 0.3.72

Questionnaire
If there is another recommendation to move scroll, let me know alternatives of the below:

            if let Some(web_sys_element) = element.try_as_web_event() {
                tracing::debug!("web_sys_element {:?}", web_sys_element);
                let slide_width = web_sys_element.get_bounding_client_rect().width().trunc() as i32;
                web_sys_element.set_scroll_left(slide_width * index() as i32);
            }

@ealmloff ealmloff added bug Something isn't working web relating to the web renderer for dioxus html Related to the html crate labels Nov 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working html Related to the html crate web relating to the web renderer for dioxus
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants