Skip to content

Instantly share code, notes, and snippets.

@back2dos
Created July 30, 2018 08:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save back2dos/4b6e1363c4061c4c603ccb4a7dd1ccd7 to your computer and use it in GitHub Desktop.
Save back2dos/4b6e1363c4061c4c603ccb4a7dd1ccd7 to your computer and use it in GitHub Desktop.
import coconut.ui.*;
import vdom.Attr;
import js.Browser.*;
typedef Bounds =
{
var top(default, never):Float;
var left(default, never):Float;
var bottom(default, never):Float;
var right(default, never):Float;
var width(default, never):Float;
var height(default, never):Float;
}
class BoundBasedView extends View
{
@:attribute function renderWithBounds(bounds:Bounds):Children;
@:attribute var className:vdom.Attr.ClassName = null;
@:state var bounds:Bounds = null;
function render() '
<div class=${className}>
<if ${bounds != null}>
${...renderWithBounds(bounds)}
</if>
</div>
';
function updateBounds()
this.bounds = toElement().getBoundingClientRect();
override function afterInit(_)
{
window.addEventListener('resize', updateBounds);
tink.core.Callback.defer(updateBounds);//not ideal, but because of how virtual-dom calls life-cycle hooks this must be deferred
}
override function beforeDestroy(_)
window.removeEventListener('resize', updateBounds);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment