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

calling DOM methods #236

Closed
pekeler opened this issue Aug 27, 2016 · 6 comments
Closed

calling DOM methods #236

pekeler opened this issue Aug 27, 2016 · 6 comments

Comments

@pekeler
Copy link
Contributor

pekeler commented Aug 27, 2016

If an element gets rendered by a choo view, and I need to invoke a method on that element in response to an action, what is the proper way for making that call?

For example scrollIntoView(), focus(), normalize(), select(), etc.

In React, I'd use lifecycle methods for this. Would it make sense to add similar hooks to choo?

@pekeler
Copy link
Contributor Author

pekeler commented Aug 27, 2016

Seems like Elm has a similar issue: evancz/elm-architecture-tutorial#49.

@yoshuawuyts
Copy link
Member

Sounds like you probably want to do some finite state machinery around this; the onload and onunload hooks might be helpful here https://github.com/shama/bel#lifecycle-events

@pekeler
Copy link
Contributor Author

pekeler commented Aug 27, 2016

Thanks. Bel's hooks look useful for some use cases.

I'm thinking a generic view callback would cover all use cases. Would something like this be terrible:

const MyView(state, prev, send) => html`
  <div>
    ${tallThing1}
    ${tallThing2}
  </div>
`
MyView.domCallback = (state, prev, rootElement) => {
  // called every time after MyView has been called and morphed into the DOM
  // rootElement is the div in this example
  if (state.currentThing !== prev.currentThing) {
    rootElement.childNodes[state.currentThing].scrollIntoView();
  }
}

@yoshuawuyts
Copy link
Member

yoshuawuyts commented Aug 27, 2016

You could do that in a setTimeout(fn, 0) or process.nextTick(fn); DOM updates resolve synchronously afaik, so it should be good

edit: should def double check tho; not 100% sure, but think generally they do hmmmm

@pekeler
Copy link
Contributor Author

pekeler commented Aug 27, 2016

Oh, cool. The timeout callback doesn't give me the parentElement sugar but is good enough. Coming from React, I assumed DOM updates in Choo are also asynchronous.

@pekeler pekeler closed this as completed Aug 27, 2016
@timwis
Copy link
Member

timwis commented Aug 29, 2016

Note that you'll have to use query selectors in timeouts because once the view is patched onto the dom, there's no guarantee the element created by the view will still exist. Because choo uses morphdom to apply dom updates, if an element already exists in the dom, it simply gets updated rather than replaced. So if your view creates a <div> each time it runs, and you save a reference to that <div> inside your view and refer to it in a setTimeout callback, it will only work on the first render. Because future renders simply patch the existing <div> with the one they created.

Example: http://requirebin.com/?gist=7721c651eb58622669112c9391d9c1b8

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants