Code Monkey home page Code Monkey logo

Comments (5)

David-OConnor avatar David-OConnor commented on August 12, 2024

Something like this in Angular?

I was able to add a focus-selector to the counter example with the addition of the following code:

use wasm_bindgen::JsCast;   // Not required once seed::to_html_el is live

// in Msg:
    Focus(&'static str),

// (in update):
        Msg::Focus(el_id) => {
            let document = web_sys::window().unwrap().document().unwrap();
            let input = document.get_element_by_id(el_id).unwrap();
            let input = input.dyn_into::<web_sys::HtmlElement>().unwrap();
            // let input = seed::to_html_el(&input);
            input.focus().unwrap();
            model
        }
/// In bottom of view func:
        input![ attrs!{"id" => "A"} ],
        input![ attrs!{"id" => "B"} ],
        input![ attrs!{"id" => "C"} ],
        button![ "Focus A", simple_ev("click", Msg::Focus("A")) ],
        button![ "Focus B", simple_ev("click", Msg::Focus("B")) ],
        button![ "Focus C", simple_ev("click", Msg::Focus("C")) ],

Thoughts: This approach is probably prone to bugs in more complicated cases, eg if the input els are destroyed/recreated. Could add a more convenient way to access the window document, since what I posted is verbose. Added seed::to_html_el, to avoid having the user import JsCast, and to simplify syntax. (eg commented code above) web_sys::Element doesn't have focus(); only web_sys::HtmlElement.

More importantly, while this seems to work for your specific example, something more flexible that can be triggered by diff events (destroy, create etc) is needed.

from seed.

rsaccon avatar rsaccon commented on August 12, 2024

thanks for your thoughts and the code example which illustrates how to interact with the underlying domNode.

About the life cycle hooks, I was thinking of react componentDidMount, componentWillUnmount, (there are more). I guess most virtual dom based frameworks have something like that. One common use case: integrating third party JS widgets.

from seed.

David-OConnor avatar David-OConnor commented on August 12, 2024

Adding this to the to-do list. Top priority is the Fetch (Get/Post data from server) API; will probably prioritize this over routing.

from seed.

David-OConnor avatar David-OConnor commented on August 12, 2024

@rsaccon Implemented. Check out the docs, and examples/counter with version 0.1.11. I'm ill-acquainted with the concept, so let me know if this is correct. If so, I'll close this issue.

from seed.

rsaccon avatar rsaccon commented on August 12, 2024

This looks great! According to my understanding, this is correct. Perfectly documented and I look forward to build a more complex example, eventually I would like to do animations (but I don't know yet how exactly ...)

from seed.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.