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

Elm.<Module>.embed not replacing div #600

Open
benjamintanweihao opened this Issue May 12, 2016 · 9 comments

Comments

Projects
None yet
7 participants
@benjamintanweihao

benjamintanweihao commented May 12, 2016

Given

  • Main.elm:
module Main exposing (..)

import Html exposing (div, text)


main =
  div [] [ text "OHAI" ]
  • index.html
<DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <title>Welcome to Backpack!</title>
  </head>
  <body>
    <div id="main">Loading...</div>
    <script src="/app.js"></script>
  </body>
</html>
  • index.js
'use strict';

require('./index.html');

var Elm = require('./Main.elm');
var mountNode = document.getElementById('main');

// The third value on embed are the initial values for incomming ports into Elm

var app = Elm.Main.embed(mountNode);

I would expect that the <div id=main> be replaced, but the resulting HTML is

    <div id="main">Loading...
      <div>OHAI</div>
   </div>

In other words, I can still see "Loading ..." when it should have been replaced.

@sudhirvkumar

This comment has been minimized.

Show comment
Hide comment
@sudhirvkumar

sudhirvkumar May 12, 2016

https://github.com/elm-lang/virtual-dom/blob/master/src/Native/VirtualDom.js#L246

parent.appendChild(domNode);

Right now it appends instead of replacing the contents of the div.

sudhirvkumar commented May 12, 2016

https://github.com/elm-lang/virtual-dom/blob/master/src/Native/VirtualDom.js#L246

parent.appendChild(domNode);

Right now it appends instead of replacing the contents of the div.

@benjamintanweihao

This comment has been minimized.

Show comment
Hide comment
@benjamintanweihao

benjamintanweihao May 12, 2016

Awesome find Sudhir! Seems like a regression to me though.

On Fri, May 13, 2016 at 7:06 AM, Sudhir Kumar notifications@github.com
wrote:

https://github.com/elm-lang/virtual-dom/blob/master/src/Native/VirtualDom.js#L246

parent.appendChild(domNode);

Right now it appends instead of replacing the contents of the div.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/elm-lang/core/issues/600#issuecomment-218910783

benjamintanweihao commented May 12, 2016

Awesome find Sudhir! Seems like a regression to me though.

On Fri, May 13, 2016 at 7:06 AM, Sudhir Kumar notifications@github.com
wrote:

https://github.com/elm-lang/virtual-dom/blob/master/src/Native/VirtualDom.js#L246

parent.appendChild(domNode);

Right now it appends instead of replacing the contents of the div.


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/elm-lang/core/issues/600#issuecomment-218910783

@sudhirvkumar

This comment has been minimized.

Show comment
Hide comment
@sudhirvkumar

sudhirvkumar May 12, 2016

parent.innerHTML = "";
parent.appendChild(domNode);

would solve I guess.

Will have to wait and watch when @evancz handles it or may be not.

Also why do you need "Loading..." in HTML? can't you do it in Elm? I guess you are loading data in Elm and that takes time?

sudhirvkumar commented May 12, 2016

parent.innerHTML = "";
parent.appendChild(domNode);

would solve I guess.

Will have to wait and watch when @evancz handles it or may be not.

Also why do you need "Loading..." in HTML? can't you do it in Elm? I guess you are loading data in Elm and that takes time?

@benjamintanweihao

This comment has been minimized.

Show comment
Hide comment
@benjamintanweihao

benjamintanweihao May 13, 2016

I am following @sporto 's tutorial, that's why :).

On Fri, May 13, 2016 at 7:47 AM, Sudhir Kumar notifications@github.com
wrote:

parent.innerHTML = "";
parent.appendChild(domNode);

would solve I guess.

Will have to wait and watch when @evancz https://github.com/evancz
handles it or may be not.

Also why do you need "Loading..." in HTML? can't you do it in Elm? I guess
you are loading data in Elm and that takes time?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/elm-lang/core/issues/600#issuecomment-218917288

benjamintanweihao commented May 13, 2016

I am following @sporto 's tutorial, that's why :).

On Fri, May 13, 2016 at 7:47 AM, Sudhir Kumar notifications@github.com
wrote:

parent.innerHTML = "";
parent.appendChild(domNode);

would solve I guess.

Will have to wait and watch when @evancz https://github.com/evancz
handles it or may be not.

Also why do you need "Loading..." in HTML? can't you do it in Elm? I guess
you are loading data in Elm and that takes time?


You are receiving this because you authored the thread.
Reply to this email directly or view it on GitHub
https://github.com/elm-lang/core/issues/600#issuecomment-218917288

@sporto

This comment has been minimized.

Show comment
Hide comment
@sporto

sporto May 14, 2016

@benjamintanweihao the tutorial is for 0.16, I don't know if it is going to be very helpful with 0.17. Some stuff will just not work e.g. router.

sporto commented May 14, 2016

@benjamintanweihao the tutorial is for 0.16, I don't know if it is going to be very helpful with 0.17. Some stuff will just not work e.g. router.

@maxf

This comment has been minimized.

Show comment
Hide comment
@maxf

maxf Aug 17, 2016

Replacing the contents of the element would make it easier to have a no-javascript fallback:

<div id="fancy-elm-input">
   <input class="fallback" type="text" .../>
</div>

It's always possible to remove the contents with JS but the default should indeed be to replace (and add an option to embed to just append)

maxf commented Aug 17, 2016

Replacing the contents of the element would make it easier to have a no-javascript fallback:

<div id="fancy-elm-input">
   <input class="fallback" type="text" .../>
</div>

It's always possible to remove the contents with JS but the default should indeed be to replace (and add an option to embed to just append)

@jnmandal

This comment has been minimized.

Show comment
Hide comment
@jnmandal

jnmandal Nov 26, 2017

It would be nice if the contents of the div were replaced. I know server rendering is coming but still — for elm embeds that are only part of a larger app — this type of thing is needed to avoid FoUC.

jnmandal commented Nov 26, 2017

It would be nice if the contents of the div were replaced. I know server rendering is coming but still — for elm embeds that are only part of a larger app — this type of thing is needed to avoid FoUC.

@zwilias

This comment has been minimized.

Show comment
Hide comment
@zwilias

zwilias Nov 26, 2017

Member

With a little juggling, this can already be achieved like so: https://ellie-app.com/9rfjxgqKca1/0

The gist is rendering into a documentfragment and replacing the original content only after the Elm app has actually rendered, which can be detected with a port + requestAnimationFrame.

Member

zwilias commented Nov 26, 2017

With a little juggling, this can already be achieved like so: https://ellie-app.com/9rfjxgqKca1/0

The gist is rendering into a documentfragment and replacing the original content only after the Elm app has actually rendered, which can be detected with a port + requestAnimationFrame.

@jnmandal

This comment has been minimized.

Show comment
Hide comment
@jnmandal

jnmandal Nov 26, 2017

Thanks @zwilias for example using ports 🙏🏻. I opted to use non-JS to handle w/ SASS/SCSS by using the :empty pseudo selector like:

#embed-target {
  opacity: 1;
  transition: opacity .5s linear;
  &:empty {
    @extend .selector-class-for-div-to-mock;
    opacity: .5;
    &::before {
      content: "Initializing";
    }
  }

This way I get a little ease in animation too.

jnmandal commented Nov 26, 2017

Thanks @zwilias for example using ports 🙏🏻. I opted to use non-JS to handle w/ SASS/SCSS by using the :empty pseudo selector like:

#embed-target {
  opacity: 1;
  transition: opacity .5s linear;
  &:empty {
    @extend .selector-class-for-div-to-mock;
    opacity: .5;
    &::before {
      content: "Initializing";
    }
  }

This way I get a little ease in animation too.

@evancz evancz added the request label Mar 7, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment