-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathindex.js
More file actions
70 lines (60 loc) · 1.96 KB
/
Copy pathindex.js
File metadata and controls
70 lines (60 loc) · 1.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var html = require('nanohtml')
var Nanocomponent = require('nanocomponent')
var inlineStyle = require('inline-style')
module.exports = class MonoContextual extends Nanocomponent {
constructor () {
super()
this.handleResize = () => this.rerender()
}
load (element) {
if (this.props.disabled) return
this.handleResize()
window.addEventListener('resize', this.handleResize)
}
unload () {
window.removeEventListener('resize', this.handleResize)
}
getInnerDimensions (element) {
var dimensions = {}
var styles = getComputedStyle(element)
dimensions.width = element.clientWidth
- parseFloat(styles.paddingLeft)
- parseFloat(styles.paddingRight)
dimensions.height = element.clientHeight
- parseFloat(styles.paddingTop)
- parseFloat(styles.paddingBottom)
dimensions.ratio = (dimensions.height / dimensions.width) * 100
return dimensions
}
// 1. get parent dimensions
// 2. check width or height basis
// 3. return inline styles
inlineStyles () {
if (this.props.disabled) return 'width:100%'
if (this.element) {
this.element.removeAttribute('style')
var parent = this.element.parentNode
var parentD = this.getInnerDimensions(parent)
var styles = {}
if (parentD.ratio > this.props.ratio) {
styles.width = parentD.width + 'px'
var elHeight = parentD.width * (this.props.ratio / 100)
styles.height = elHeight + 'px'
styles.marginTop = (parentD.height - elHeight) / 2 + 'px'
} else {
styles.height = parentD.height + 'px'
var elWidth = parentD.height / (this.props.ratio / 100)
styles.width = elWidth + 'px'
styles.marginLeft = (parentD.width - elWidth) / 2 + 'px'
}
return inlineStyle(styles)
}
}
update () {
return true
}
createElement (props, children) {
this.props = props
return html`<div style="${this.inlineStyles()}">${children}</div>`
}
}