This repository has been archived by the owner on Apr 25, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
GridItem.vue
114 lines (107 loc) · 2.73 KB
/
GridItem.vue
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<template>
<div class="vfg-grid-item" :style="styleObject" v-show="!isSizeZero">
<slot></slot>
</div>
</template>
<script>
import reduceCSSCalc from 'reduce-css-calc'
import isUndefined from '../utils/is-undefined'
import initConfig from '../utils/init-config'
import initRWD from '../utils/init-rwd'
export default {
name: 'grid-item',
mixins: [initConfig, initRWD],
props: {
size: {
type: String,
validator (value) {
const fraction = value.split('/')
const denominator = +fraction[1]
return fraction.length === 2 && denominator !== 0
}
},
grow: [String, Number],
shrink: [String, Number]
},
created () {
this.bindMediaQueries((mediaQuery, gridItemSize) => {
if (mediaQuery.matches) {
this.gridItemSize = gridItemSize
}
mediaQuery.addListener(listener => {
if (listener.matches) {
this.gridItemSize = gridItemSize
} else {
this.gridItemSize = this.size
}
})
})
},
data () {
return {
fraction: this.size
}
},
computed: {
isSizeZero () {
return this.gridItemSize === '0/1'
},
styleObject () {
const stylePadding = {
paddingRight: this.horizontalPadding,
paddingLeft: this.horizontalPadding
}
if (this.hasSize) {
return Object.assign(stylePadding, {
width: this.percentageWidth,
flexBasis: this.percentageWidth,
maxWidth: this.percentageWidth
})
}
if (this.hasGrow) {
return Object.assign(stylePadding, {
flexGrow: +this.grow
})
}
if (this.hasShrink) {
return Object.assign(stylePadding, {
flexShrink: +this.shrink
})
}
return stylePadding
},
hasSize () {
return !isUndefined(this.size) && isUndefined(this.grow) && isUndefined(this.shrink)
},
hasGrow () {
return isUndefined(this.size) && !isUndefined(this.grow) && isUndefined(this.shrink)
},
hasShrink () {
return isUndefined(this.size) && isUndefined(this.grow) && !isUndefined(this.shrink)
},
horizontalPadding () {
const notFlatGridChild = isUndefined(this.$parent) || isUndefined(this.$parent.flat)
return notFlatGridChild
? reduceCSSCalc(`calc(${this.config.gutter} / 2)`) : 0
},
gridItemSize: {
get () {
return this.fraction
},
set (fraction) {
this.fraction = fraction
}
},
percentageWidth () {
const [numerator, denominator] = this.gridItemSize.split('/')
const value = (+numerator * 100 / +denominator).toFixed(4)
return `${value}%`
}
}
}
</script>
<style>
.vfg-grid-item {
box-sizing: border-box;
}
</style>