¶Ô±ÈÐÂÎļþ |
| | |
| | | <template> |
| | | <view class="u-popup"> |
| | | <u-overlay |
| | | :show="show" |
| | | @click="overlayClick" |
| | | v-if="overlay" |
| | | :duration="overlayDuration" |
| | | :customStyle="overlayStyle" |
| | | :opacity="overlayOpacity" |
| | | ></u-overlay> |
| | | <u-transition |
| | | :show="show" |
| | | :customStyle="transitionStyle" |
| | | :mode="position" |
| | | :duration="duration" |
| | | @afterEnter="afterEnter" |
| | | @click="clickHandler" |
| | | > |
| | | <view |
| | | class="u-popup__content" |
| | | :style="[contentStyle]" |
| | | @tap.stop="noop" |
| | | > |
| | | <u-status-bar v-if="safeAreaInsetTop"></u-status-bar> |
| | | <slot></slot> |
| | | <view |
| | | v-if="closeable" |
| | | @tap.stop="close" |
| | | class="u-popup__content__close" |
| | | :class="['u-popup__content__close--' + closeIconPos]" |
| | | hover-class="u-popup__content__close--hover" |
| | | hover-stay-time="150" |
| | | > |
| | | <u-icon |
| | | name="close" |
| | | color="#909399" |
| | | size="18" |
| | | bold |
| | | ></u-icon> |
| | | </view> |
| | | <u-safe-bottom v-if="safeAreaInsetBottom"></u-safe-bottom> |
| | | </view> |
| | | </u-transition> |
| | | </view> |
| | | </template> |
| | | |
| | | <script> |
| | | import props from './props.js'; |
| | | |
| | | /** |
| | | * popup å¼¹çª |
| | | * @description å¼¹åºå±å®¹å¨ï¼ç¨äºå±ç¤ºå¼¹çªãä¿¡æ¯æç¤ºçå
å®¹ï¼æ¯æä¸ãä¸ãå·¦ãå³åä¸é¨å¼¹åºãç»ä»¶åªæä¾å®¹å¨ï¼å
é¨å
容ç±ç¨æ·èªå®ä¹ |
| | | * @tutorial https://www.uviewui.com/components/popup.html |
| | | * @property {Boolean} show æ¯å¦å±ç¤ºå¼¹çª (é»è®¤ false ) |
| | | * @property {Boolean} overlay æ¯å¦æ¾ç¤ºé®ç½© ï¼é»è®¤ true ï¼ |
| | | * @property {String} mode å¼¹åºæ¹åï¼é»è®¤ 'bottom' ï¼ |
| | | * @property {String | Number} duration å¨ç»æ¶é¿ï¼åä½ms ï¼é»è®¤ 300 ï¼ |
| | | * @property {String | Number} overlayDuration é®ç½©å±å¨ç»æ¶é¿ï¼åä½ms ï¼é»è®¤ 350 ï¼ |
| | | * @property {Boolean} closeable æ¯å¦æ¾ç¤ºå
³é徿 ï¼é»è®¤ false ï¼ |
| | | * @property {Object | String} overlayStyle èªå®ä¹é®ç½©çæ ·å¼ |
| | | * @property {String | Number} overlayOpacity é®ç½©éæåº¦ï¼0-1ä¹é´ï¼é»è®¤ 0.5ï¼ |
| | | * @property {Boolean} closeOnClickOverlay ç¹å»é®ç½©æ¯å¦å
³éå¼¹çª ï¼é»è®¤ true ï¼ |
| | | * @property {String | Number} zIndex å±çº§ ï¼é»è®¤ 10075 ï¼ |
| | | * @property {Boolean} safeAreaInsetBottom æ¯å¦ä¸ºiPhoneXçåºåºé¨å®å
¨è·ç¦» ï¼é»è®¤ true ï¼ |
| | | * @property {Boolean} safeAreaInsetTop æ¯å¦çåºé¡¶é¨å®å
¨è·ç¦»ï¼ç¶ææ é«åº¦ï¼ ï¼é»è®¤ false ï¼ |
| | | * @property {String} closeIconPos èªå®ä¹å
³é徿 ä½ç½®ï¼é»è®¤ 'top-right' ï¼ |
| | | * @property {String | Number} round åè§å¼ï¼é»è®¤ 0ï¼ |
| | | * @property {Boolean} zoom å½mode=centeræ¶ æ¯å¦å¼å¯ç¼©æ¾ï¼é»è®¤ true ï¼ |
| | | * @property {Object} customStyle ç»ä»¶çæ ·å¼ï¼å¯¹è±¡å½¢å¼ |
| | | * @event {Function} open å¼¹åºå±æå¼ |
| | | * @event {Function} close å¼¹åºå±æ¶èµ· |
| | | * @example <u-popup v-model="show"><text>åºæ·¤æ³¥èä¸æï¼æ¿¯æ¸
æ¶èä¸å¦</text></u-popup> |
| | | */ |
| | | export default { |
| | | name: 'u-popup', |
| | | mixins: [uni.$u.mpMixin, uni.$u.mixin, props], |
| | | data() { |
| | | return { |
| | | overlayDuration: this.duration + 50 |
| | | } |
| | | }, |
| | | watch: { |
| | | show(newValue, oldValue) { |
| | | if (newValue === true) { |
| | | // #ifdef MP-WEIXIN |
| | | const children = this.$children |
| | | this.retryComputedComponentRect(children) |
| | | // #endif |
| | | } |
| | | } |
| | | }, |
| | | computed: { |
| | | transitionStyle() { |
| | | const style = { |
| | | zIndex: this.zIndex, |
| | | position: 'fixed', |
| | | display: 'flex', |
| | | } |
| | | style[this.mode] = 0 |
| | | if (this.mode === 'left') { |
| | | return uni.$u.deepMerge(style, { |
| | | bottom: 0, |
| | | top: 0, |
| | | }) |
| | | } else if (this.mode === 'right') { |
| | | return uni.$u.deepMerge(style, { |
| | | bottom: 0, |
| | | top: 0, |
| | | }) |
| | | } else if (this.mode === 'top') { |
| | | return uni.$u.deepMerge(style, { |
| | | left: 0, |
| | | right: 0 |
| | | }) |
| | | } else if (this.mode === 'bottom') { |
| | | return uni.$u.deepMerge(style, { |
| | | left: 0, |
| | | right: 0, |
| | | }) |
| | | } else if (this.mode === 'center') { |
| | | return uni.$u.deepMerge(style, { |
| | | alignItems: 'center', |
| | | 'justify-content': 'center', |
| | | top: 0, |
| | | left: 0, |
| | | right: 0, |
| | | bottom: 0 |
| | | }) |
| | | } |
| | | }, |
| | | contentStyle() { |
| | | const style = {} |
| | | // éè¿è®¾å¤ä¿¡æ¯çsafeAreaInsets弿¥å¤ææ¯å¦éè¦é¢çé¡¶é¨ç¶ææ ååºé¨å®å
¨å±çä½ç½® |
| | | // ä¸ä½¿ç¨cssæ¹æ¡ï¼æ¯å 为nvue䏿¯æcssçiPhoneXå®å
¨åºæ¥è¯¢å±æ§ |
| | | const { |
| | | safeAreaInsets |
| | | } = uni.$u.sys() |
| | | if (this.mode !== 'center') { |
| | | style.flex = 1 |
| | | } |
| | | // èæ¯è²ï¼ä¸è¬ç¨äºè®¾ç½®ä¸ºtransparentï¼å»é¤é»è®¤çç½è²èæ¯ |
| | | if (this.bgColor) { |
| | | style.backgroundColor = this.bgColor |
| | | } |
| | | if(this.round) { |
| | | const value = uni.$u.addUnit(this.round) |
| | | if(this.mode === 'top') { |
| | | style.borderBottomLeftRadius = value |
| | | style.borderBottomRightRadius = value |
| | | } else if(this.mode === 'bottom') { |
| | | style.borderTopLeftRadius = value |
| | | style.borderTopRightRadius = value |
| | | } else if(this.mode === 'center') { |
| | | style.borderRadius = value |
| | | } |
| | | } |
| | | return uni.$u.deepMerge(style, uni.$u.addStyle(this.customStyle)) |
| | | }, |
| | | position() { |
| | | if (this.mode === 'center') { |
| | | return this.zoom ? 'fade-zoom' : 'fade' |
| | | } |
| | | if (this.mode === 'left') { |
| | | return 'slide-left' |
| | | } |
| | | if (this.mode === 'right') { |
| | | return 'slide-right' |
| | | } |
| | | if (this.mode === 'bottom') { |
| | | return 'slide-up' |
| | | } |
| | | if (this.mode === 'top') { |
| | | return 'slide-down' |
| | | } |
| | | }, |
| | | }, |
| | | methods: { |
| | | // ç¹å»é®ç½© |
| | | overlayClick() { |
| | | if (this.closeOnClickOverlay) { |
| | | this.$emit('close') |
| | | } |
| | | }, |
| | | close(e) { |
| | | this.$emit('close') |
| | | }, |
| | | afterEnter() { |
| | | this.$emit('open') |
| | | }, |
| | | clickHandler() { |
| | | // ç±äºä¸é¨å¼¹åºæ¶ï¼å
¶u-transitionå æ®äºæ´ä¸ªé¡µé¢ç¸å½äºé®ç½©ï¼æ¤æ¶éè¦ååºé®ç½©ç¹å»äºä»¶ï¼æ¯å¦æ æ³éè¿ç¹å»é®ç½©å
³éå¼¹çª |
| | | if(this.mode === 'center') { |
| | | this.overlayClick() |
| | | } |
| | | this.$emit('click') |
| | | }, |
| | | // #ifdef MP-WEIXIN |
| | | retryComputedComponentRect(children) { |
| | | // ç»ä»¶å
é¨éè¦è®¡ç®èç¹çç»ä»¶ |
| | | const names = ['u-calendar-month', 'u-album', 'u-collapse-item', 'u-dropdown', 'u-index-item', 'u-index-list', |
| | | 'u-line-progress', 'u-list-item', 'u-rate', 'u-read-more', 'u-row', 'u-row-notice', 'u-scroll-list', |
| | | 'u-skeleton', 'u-slider', 'u-steps-item', 'u-sticky', 'u-subsection', 'u-swipe-action-item', 'u-tabbar', |
| | | 'u-tabs', 'u-tooltip' |
| | | ] |
| | | // åéææçåç»ä»¶èç¹ |
| | | for (let i = 0; i < children.length; i++) { |
| | | const child = children[i] |
| | | // æ¿å°åç»ä»¶çåç»ä»¶ |
| | | const grandChild = child.$children |
| | | // 夿妿å¨éè¦éæ°åå§åçç»ä»¶æ°ç»ä¸åä¸ï¼å¹¶ä¸åå¨initæ¹æ³çè¯ï¼åæ§è¡ |
| | | if (names.includes(child.$options.name) && typeof child?.init === 'function') { |
| | | // éè¦è¿è¡ä¸å®çå»¶æ¶ï¼å 为åå§å页é¢éè¦æ¶é´ |
| | | uni.$u.sleep(50).then(() => { |
| | | child.init() |
| | | }) |
| | | } |
| | | // 妿åç»ä»¶è¿æåç»ä»¶ï¼è¿è¡éå½åé |
| | | if (grandChild.length) { |
| | | this.retryComputedComponentRect(grandChild) |
| | | } |
| | | } |
| | | } |
| | | // #endif |
| | | } |
| | | } |
| | | </script> |
| | | |
| | | <style lang="scss" scoped> |
| | | @import "../../libs/css/components.scss"; |
| | | $u-popup-flex:1 !default; |
| | | $u-popup-content-background-color: #fff !default; |
| | | |
| | | .u-popup { |
| | | flex: $u-popup-flex; |
| | | |
| | | &__content { |
| | | background-color: $u-popup-content-background-color; |
| | | position: relative; |
| | | |
| | | &--round-top { |
| | | border-top-left-radius: 0; |
| | | border-top-right-radius: 0; |
| | | border-bottom-left-radius: 10px; |
| | | border-bottom-right-radius: 10px; |
| | | } |
| | | |
| | | &--round-left { |
| | | border-top-left-radius: 0; |
| | | border-top-right-radius: 10px; |
| | | border-bottom-left-radius: 0; |
| | | border-bottom-right-radius: 10px; |
| | | } |
| | | |
| | | &--round-right { |
| | | border-top-left-radius: 10px; |
| | | border-top-right-radius: 0; |
| | | border-bottom-left-radius: 10px; |
| | | border-bottom-right-radius: 0; |
| | | } |
| | | |
| | | &--round-bottom { |
| | | border-top-left-radius: 10px; |
| | | border-top-right-radius: 10px; |
| | | border-bottom-left-radius: 0; |
| | | border-bottom-right-radius: 0; |
| | | } |
| | | |
| | | &--round-center { |
| | | border-top-left-radius: 10px; |
| | | border-top-right-radius: 10px; |
| | | border-bottom-left-radius: 10px; |
| | | border-bottom-right-radius: 10px; |
| | | } |
| | | |
| | | &__close { |
| | | position: absolute; |
| | | |
| | | &--hover { |
| | | opacity: 0.4; |
| | | } |
| | | } |
| | | |
| | | &__close--top-left { |
| | | top: 15px; |
| | | left: 15px; |
| | | } |
| | | |
| | | &__close--top-right { |
| | | top: 15px; |
| | | right: 15px; |
| | | } |
| | | |
| | | &__close--bottom-left { |
| | | bottom: 15px; |
| | | left: 15px; |
| | | } |
| | | |
| | | &__close--bottom-right { |
| | | right: 15px; |
| | | bottom: 15px; |
| | | } |
| | | } |
| | | } |
| | | </style> |