-
Notifications
You must be signed in to change notification settings - Fork 26
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
1 parent
3aea541
commit 5955375
Showing
8 changed files
with
283 additions
and
2 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 |
---|---|---|
@@ -0,0 +1,63 @@ | ||
MaskLayer 遮罩层 | ||
--- | ||
|
||
用于模态对话框,中的遮罩层。 | ||
|
||
### 基础示例 | ||
|
||
<!--DemoStart--> | ||
```jsx | ||
import React, { Component, Fragment } from 'react'; | ||
import { View, Text, Alert, SafeAreaView } from 'react-native'; | ||
import { Modal, Button, MaskLayer } from '@uiw/react-native'; | ||
|
||
export default () => { | ||
const [visible, setVisible] = useState(true); | ||
return ( | ||
<Fragment> | ||
<MaskLayer | ||
visible={visible} | ||
onDismiss={() => { | ||
setVisible(false); | ||
}}> | ||
<Text style={{color: '#fff'}}>展示内容</Text> | ||
</MaskLayer> | ||
<Button | ||
onPress={() => { | ||
setVisible(true); | ||
}}> | ||
{visible ? '隐藏模态框' : '显示模态框'} | ||
</Button> | ||
</Fragment> | ||
); | ||
} | ||
``` | ||
<!--End--> | ||
|
||
## Props | ||
|
||
继承原生 Modal 属性 [`ModalProps`](https://facebook.github.io/react-native/docs/modal.html#props) | ||
|
||
```typescript | ||
interface MaskLayerProps extends RNModalProps { | ||
/** | ||
* 遮罩层是否禁止点击 | ||
* defult: `true` | ||
*/ | ||
maskClosable?: boolean; | ||
/** | ||
* 是否隐藏 | ||
*/ | ||
visible?: boolean; | ||
/** | ||
* 遮罩层透明度 | ||
* defult: `0.6` | ||
*/ | ||
opacity?: number; | ||
/** | ||
* 隐藏消除回调事件 | ||
*/ | ||
onDismiss?: () => void; | ||
children?: JSX.Element; | ||
} | ||
``` |
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,134 @@ | ||
import React, {useState, useMemo} from 'react'; | ||
import { | ||
Modal, | ||
ModalProps as RNModalProps, | ||
Animated, | ||
TouchableOpacity, | ||
StyleSheet, | ||
} from 'react-native'; | ||
import {usePrevious} from '../utils'; | ||
|
||
const styles = StyleSheet.create({ | ||
position: { | ||
position: 'absolute', | ||
backgroundColor: 'transparent', | ||
top: 0, | ||
bottom: 0, | ||
left: 0, | ||
right: 0, | ||
zIndex: 9998, | ||
}, | ||
backdrop: { | ||
backgroundColor: '#000', | ||
}, | ||
content: { | ||
backgroundColor: '#fff', | ||
position: 'absolute', | ||
}, | ||
}); | ||
|
||
export interface MaskLayerProps extends RNModalProps { | ||
/** | ||
* 遮罩层是否禁止点击 | ||
* defult: `true` | ||
*/ | ||
maskClosable?: boolean; | ||
/** | ||
* 是否隐藏 | ||
*/ | ||
visible?: boolean; | ||
/** | ||
* 遮罩层透明度 | ||
* defult: `0.6` | ||
*/ | ||
opacity?: number; | ||
/** | ||
* 隐藏消除回调事件 | ||
*/ | ||
onDismiss?: () => void; | ||
animatedParallelShow?: Animated.CompositeAnimation[]; | ||
animatedParallelHide?: Animated.CompositeAnimation[]; | ||
children?: JSX.Element; | ||
} | ||
|
||
export default (props: MaskLayerProps = {}) => { | ||
const { | ||
maskClosable = true, | ||
children, | ||
// eslint-disable-next-line @typescript-eslint/no-unused-vars | ||
visible: _vis, | ||
opacity = 0.6, | ||
onDismiss, | ||
animatedParallelShow = [], | ||
animatedParallelHide = [], | ||
...otherProps | ||
} = props; | ||
const [visible, setVisible] = useState(!!props.visible); | ||
const preVisible = usePrevious<boolean | undefined>(props.visible); | ||
const [visibleModal, setVisibleModal] = useState(false); | ||
const [bgOpacity] = useState(new Animated.Value(0)); | ||
useMemo(() => { | ||
if (preVisible !== props.visible && props.visible) { | ||
setVisible(!!props.visible); | ||
setVisibleModal(false); | ||
Animated.parallel([ | ||
Animated.spring(bgOpacity, { | ||
toValue: opacity, | ||
overshootClamping: true, | ||
useNativeDriver: true, | ||
}), | ||
...animatedParallelShow, | ||
]).start(); | ||
} else if (preVisible !== props.visible && !props.visible) { | ||
Animated.parallel([ | ||
Animated.spring(bgOpacity, { | ||
toValue: 0, | ||
overshootClamping: true, | ||
useNativeDriver: true, | ||
}), | ||
...animatedParallelHide, | ||
]).start(() => { | ||
setVisible(!!props.visible); | ||
setVisibleModal(true); | ||
}); | ||
} | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [props.visible]); | ||
const backdropContent = ( | ||
<Animated.View | ||
style={[styles.position, styles.backdrop, {opacity: bgOpacity}]} | ||
/> | ||
); | ||
let backdrop = ( | ||
<TouchableOpacity | ||
activeOpacity={1} | ||
style={[styles.position]} | ||
onPress={() => onDismiss && onDismiss()}> | ||
{backdropContent} | ||
</TouchableOpacity> | ||
); | ||
let isTrue = true; | ||
|
||
if (!visible && visibleModal) { | ||
isTrue = false; | ||
} | ||
return ( | ||
<Modal | ||
transparent={true} | ||
animationType="none" | ||
{...otherProps} | ||
visible={isTrue}> | ||
{maskClosable ? backdrop : backdropContent} | ||
{children && | ||
React.Children.toArray(children).map((child) => { | ||
if (!React.isValidElement(child)) { | ||
return; | ||
} | ||
return React.cloneElement(child, { | ||
...child.props, | ||
...{style: [{zIndex: 9999}, child.props.style]}, | ||
}); | ||
})} | ||
</Modal> | ||
); | ||
}; |
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 |
---|---|---|
|
@@ -2,3 +2,4 @@ import color from 'color'; | |
import * as colors from './colors'; | ||
|
||
export {color, colors}; | ||
export * from './hooks'; |
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,72 @@ | ||
import React, {useState} from 'react'; | ||
import {Text, View, StyleSheet} from 'react-native'; | ||
import Layout, {Container} from '../../Layout'; | ||
import {Button, MaskLayer} from '../../../components'; | ||
import {ComProps} from '../../typings'; | ||
|
||
const {Header, Body, Card, Footer} = Layout; | ||
|
||
export interface MenuDropdownProps extends ComProps {} | ||
|
||
export default function MenuDropdownView(props: MenuDropdownProps) { | ||
const {route} = props || {}; | ||
const description = route.params.description; | ||
const title = route.params.title; | ||
const [visible, setVisible] = useState(true); | ||
return ( | ||
<Container> | ||
<Layout> | ||
<Header title={title} description={description} /> | ||
<Body> | ||
<Card title="基础实例" style={styles.card}> | ||
<MaskLayer | ||
visible={visible} | ||
onDismiss={() => { | ||
setVisible(false); | ||
}}> | ||
<View style={styles.top}> | ||
<Button | ||
type="danger" | ||
onPress={() => { | ||
setVisible(false); | ||
}}> | ||
<Text style={styles.color}>隐藏</Text> | ||
</Button> | ||
<Text style={styles.color}>内容</Text> | ||
</View> | ||
</MaskLayer> | ||
<Button | ||
onPress={() => { | ||
setVisible(true); | ||
}}> | ||
{String(true)} | ||
显示模态框 | ||
</Button> | ||
<Button | ||
onPress={() => { | ||
setVisible(false); | ||
}}> | ||
{String(false)} | ||
隐藏模态框 | ||
</Button> | ||
</Card> | ||
</Body> | ||
<Footer /> | ||
</Layout> | ||
</Container> | ||
); | ||
} | ||
|
||
const styles = StyleSheet.create({ | ||
card: { | ||
backgroundColor: '#fff', | ||
paddingLeft: 20, | ||
paddingRight: 20, | ||
}, | ||
color: { | ||
color: '#fff', | ||
}, | ||
top: { | ||
top: 100, | ||
}, | ||
}); |
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