-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
348 additions
and
71 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
dist | ||
node_modules |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"typescript.tsdk": "node_modules/typescript/lib" | ||
} |
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
@import '../../settings.scss'; | ||
|
||
.popup { | ||
position: fixed; | ||
top: 0; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
display: flex; | ||
.mask { | ||
position: absolute; | ||
top: 0; | ||
left: 0; | ||
bottom: 0; | ||
right: 0; | ||
background: rgba(black, 0.6); | ||
} | ||
.content { | ||
position: relative; | ||
z-index: 1; | ||
} | ||
&.center { | ||
justify-content: center; | ||
align-items: center; | ||
} | ||
&.top { | ||
justify-content: center; | ||
align-items: flex-start; | ||
} | ||
&.bottom { | ||
justify-content: center; | ||
align-items: flex-end; | ||
} | ||
&.left { | ||
justify-content: flex-start; | ||
align-items: center; | ||
} | ||
&.right { | ||
justify-content: flex-end; | ||
align-items: center; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import Taro from '@tarojs/taro' | ||
import { View } from '@tarojs/components' | ||
import { noop } from 'vtils' | ||
import { component, RequiredProp } from '../component' | ||
import MTransition from '../Transition' | ||
import _ from './index.module.scss' | ||
|
||
type Position = 'center' | 'top' | 'bottom' | 'right' | 'left' | ||
|
||
const positionToTransitionName: { [key in Position]: MTransition['props']['name'] } = { | ||
center: 'fade', | ||
top: 'slideDown', | ||
bottom: 'slideUp', | ||
left: 'slideLeft', | ||
right: 'slideRight', | ||
} | ||
|
||
/** | ||
* 弹出层组件。 | ||
*/ | ||
class MPopup extends component({ | ||
props: { | ||
/** 弹出层是否可见 */ | ||
visible: false as any as RequiredProp<boolean>, | ||
/** 点击遮罩是否可关闭 */ | ||
maskClosable: true as boolean, | ||
/** 动画时长,单位: ms */ | ||
duration: 300 as number, | ||
/** 弹出内容位置 */ | ||
position: 'center' as Position, | ||
/** 可见性变化事件 */ | ||
onVisibleChange: noop as any as RequiredProp<(visible: boolean) => void>, | ||
}, | ||
state: { | ||
/** zIndex 值 */ | ||
zIndex: 0 as number, | ||
/** 弹出层是否显示 */ | ||
display: false as boolean, | ||
}, | ||
}) { | ||
/** 起始 zIndex 值 */ | ||
static startZIndex = 5000 | ||
|
||
/** 计数器 */ | ||
transitionEndCounter = 0 | ||
|
||
componentWillMount() { | ||
this.setState({ | ||
zIndex: MPopup.startZIndex++, | ||
display: this.props.visible, | ||
}) | ||
} | ||
|
||
componentWillReceiveProps(nextProps: MPopup['props']) { | ||
if (nextProps.visible !== this.props.visible) { | ||
this.setState({ | ||
display: true, | ||
}) | ||
} | ||
} | ||
|
||
handleTouchMove = (e: Event) => { | ||
e.stopPropagation() | ||
} | ||
|
||
handleMaskClick = () => { | ||
if (this.props.maskClosable) { | ||
this.props.onVisibleChange(false) | ||
} | ||
} | ||
|
||
handleTransitionEnd = () => { | ||
this.transitionEndCounter++ | ||
if (this.transitionEndCounter >= 2) { | ||
this.transitionEndCounter = 0 | ||
this.setState({ | ||
display: this.props.visible, | ||
}) | ||
} | ||
} | ||
|
||
render() { | ||
const { visible, duration, position } = this.props | ||
const { zIndex, display } = this.state | ||
return ( | ||
<View | ||
className={`${_.popup} ${_[position]}`} | ||
style={{ | ||
zIndex, | ||
...(display ? {} : { display: 'none' }), | ||
}} | ||
onTouchMove={this.handleTouchMove as any}> | ||
<MTransition | ||
name='fade' | ||
visible={visible} | ||
duration={duration} | ||
onTransitionEnd={this.handleTransitionEnd}> | ||
<View | ||
className={_.mask} | ||
onClick={this.handleMaskClick} | ||
/> | ||
</MTransition> | ||
<View className={_.content}> | ||
<MTransition | ||
name={positionToTransitionName[position]} | ||
visible={visible} | ||
duration={duration} | ||
onTransitionEnd={this.handleTransitionEnd}> | ||
{this.props.children} | ||
</MTransition> | ||
</View> | ||
</View> | ||
) | ||
} | ||
} | ||
|
||
export default MPopup |
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
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
Oops, something went wrong.