Last active
October 28, 2018 06:23
-
-
Save justinfagnani/07685707ce9f6c9c3f41c2ac1ac90a7c to your computer and use it in GitHub Desktop.
the-platform with the platform
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import {LitElement, html} from 'lit-element'; | |
import {UseWindowSize} from './useWindowSize.js'; | |
class extends UseWindowSize()(LitElement) { | |
render() { | |
return html` | |
<p> | |
width: ${window.innerWidth} | |
height: ${window.innerHeight} | |
</p> | |
`; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import throttle from 'lodash/throttle'; | |
const onResize = Symbol(); | |
export const UseWindowSize = (options = {}) => (base) => { | |
const { throttleMs = 100 } = options; | |
return class exrends base { | |
[onResize] = throttle(() => { | |
// We don't have a standard platform way to request a re-render, | |
// but any base class implementing requestUpdate() will work | |
this.requestUpdate(); | |
}, throttleMs);); | |
connectedCallback() { | |
if (super.connectedCallback) super.connectedCallback(); | |
window.addEventListener('resize', this[onResize]); | |
} | |
disconnectedCallback() { | |
if (super.connectedCallback) super.disconnectedCallback(); | |
window.removeEventListener('resize', this[onResize]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
typo in useWIndowsSize L21, should be:
I like the example a lot!