提交 76d7230a 编写于 作者: inkwalk's avatar inkwalk

add: match-media component

上级 a4e1dbb5
...@@ -40,5 +40,6 @@ module.exports = { ...@@ -40,5 +40,6 @@ module.exports = {
textarea: ['app-plus', 'mp-weixin', 'h5'], textarea: ['app-plus', 'mp-weixin', 'h5'],
video: ['app-plus', 'mp-weixin', 'h5'], video: ['app-plus', 'mp-weixin', 'h5'],
view: ['app-plus', 'mp-weixin', 'h5'], view: ['app-plus', 'mp-weixin', 'h5'],
'web-view': ['app-plus', 'mp-weixin'] 'web-view': ['app-plus', 'mp-weixin'],
'match-media':['app-plus', 'mp-weixin', 'h5']
} }
...@@ -44,7 +44,8 @@ const TAGS = [ ...@@ -44,7 +44,8 @@ const TAGS = [
'textarea', 'textarea',
'video', 'video',
'view', 'view',
'web-view' 'web-view',
'match-media'
] ]
const EVENTS = { const EVENTS = {
......
...@@ -47,7 +47,8 @@ const tags = [ ...@@ -47,7 +47,8 @@ const tags = [
'video', 'video',
'view', 'view',
'web-view', 'web-view',
'editor' 'editor',
'match-media'
] ]
const baseCompiler = { const baseCompiler = {
...@@ -127,4 +128,4 @@ module.exports = function getCompilerOptions (platform) { ...@@ -127,4 +128,4 @@ module.exports = function getCompilerOptions (platform) {
baseCompiler, baseCompiler,
require(id + '/lib/uni.compiler.js') require(id + '/lib/uni.compiler.js')
) )
} }
...@@ -136,7 +136,8 @@ if (process.env.UNI_USING_V3) { ...@@ -136,7 +136,8 @@ if (process.env.UNI_USING_V3) {
'textarea', 'textarea',
'video', 'video',
'view', 'view',
'web-view' 'web-view',
'match-media'
] ]
const BG_PROPS = [ const BG_PROPS = [
......
module.exports = [ module.exports = [
'uni-app', 'uni-app',
'uni-layout', 'uni-layout',
'uni-content', 'uni-content',
'uni-main', 'uni-main',
'uni-left-window', 'uni-left-window',
'uni-right-window', 'uni-right-window',
'uni-tabbar', 'uni-tabbar',
'uni-page', 'uni-page',
'uni-page-head', 'uni-page-head',
...@@ -14,7 +14,7 @@ module.exports = [ ...@@ -14,7 +14,7 @@ module.exports = [
'uni-actionsheet', 'uni-actionsheet',
'uni-modal', 'uni-modal',
'uni-toast', 'uni-toast',
'uni-resize-sensor', 'uni-resize-sensor',
'uni-shadow-root', 'uni-shadow-root',
'uni-ad', 'uni-ad',
...@@ -57,5 +57,6 @@ module.exports = [ ...@@ -57,5 +57,6 @@ module.exports = [
'uni-textarea', 'uni-textarea',
'uni-video', 'uni-video',
'uni-view', 'uni-view',
'uni-web-view' 'uni-web-view',
'uni-match-media'
] ]
<template>
<uni-match-media
v-show="MediaQueryListRes"
v-on="$listeners"
>
<slot />
</uni-match-media>
</template>
<script>
export default {
name: 'MatchMedia',
props: {
width: {
type: Number,
default: 0
},
minWidth: {
type: Number,
default: 0
},
maxWidth: {
type: Number,
default: 0
},
height: {
default: 0,
type: Number
},
minHeight: {
type: Number,
default: 0
},
maxHeight: {
type: Number,
default: 0
},
orientation: {
type: String,
default: ''
}
},
data () {
return {
MediaQueryListRes: true,
mql: null
}
},
computed: {
handleMediaQueryStr () {
let mediaQueryStr = []
const { $props } = this
for (const item in $props) {
if (item !== ('orientation' || 'animations') && $props[item] > 0) {
mediaQueryStr.push(`(${this.humpToLine(item)}: ${this[item]}px)`)
}
if (item === 'orientation' && $props[item]) {
mediaQueryStr.push(`(${this.humpToLine(item)}: ${this[item]})`)
}
}
mediaQueryStr = mediaQueryStr.join(' and ')
return mediaQueryStr
}
},
watch: {
handleMediaQueryStr: 'replaceListener'
},
mounted () {
this.mql = window.matchMedia(this.handleMediaQueryStr)
this.handleMediaQuery(this.mql)
this.mql.addListener(this.handleMediaQuery)
},
beforeDestroy () {
this.mql.removeListener(this.handleMediaQuery)
},
methods: {
handleMediaQuery (e) {
if (e.matches) {
this.MediaQueryListRes = true
} else {
this.MediaQueryListRes = false
}
},
humpToLine (name) {
return name.replace(/([A-Z])/g, '-$1').toLowerCase()
},
replaceListener () {
this.mql.removeListener(this.handleMediaQuery)
this.mql = window.matchMedia(this.handleMediaQueryStr)
this.handleMediaQuery(this.mql)
this.mql.addListener(this.handleMediaQuery)
}
}
}
</script>
<style>
uni-match-media {
display: block;
}
uni-match-media[hidden] {
display: none;
}
</style>
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册