ReactNative 抽屉组件 侧滑栏 react-native-side-menu

示例图:

这里写图片描述

安装

npm install react-native-side-menu --save

使用

引入组件
import SideMenu from 'react-native-side-menu';
使用组件:
render() {
        const menu = <Text style={{marginTop: 22}}>aaa</Text>;
        return (

            <SideMenu
                menu={menu}                    //抽屉内的组件
            >
                <View style={styles.container}>
                    <Text style={styles.welcome} onPress={() => {
                        this.setState({
                            isOpen: true
                        })
                    }}>
                        Open Draw!
                    </Text>
                    <Text style={styles.instructions}>
                        To get started, edit App.js
                    </Text>

                </View>
            </SideMenu>

        );

menu 为 抽屉内部的组件 , 展示在抽屉上的内容 .
ContentView 为主页面视图 , 是抽屉关闭时页面上展示的内容 .

组件的属性
属性默认值类型描述
menu空的ViewReact.Component一个组件
isOpenfalseBoolean抽屉打开/关闭
openMenuOffset屏幕宽度的2/3Number抽屉打开时的宽度
hiddenMenuOffsetnoneNumber抽屉关闭状态时,显示多少宽度 默认0 抽屉完全隐藏
edgeHitWidthnoneNumber距离屏幕多少距离可以滑出抽屉,默认60
toleranceXnoneNumberX 轴的偏移量
toleranceYnoneNumberY 轴的偏移量
disableGesturesfalseBool是否禁用手势滑动抽屉 默认false 允许手势滑动
onStartShouldSetResponderCapturenoneFunctionFunction that accepts event as an argument and specify if side-menu should react on the touch or not. Check https://facebook.github.io/react-native/docs/gesture-responder-system.html for more details
onChangenoneFunction抽屉状态变化的监听函数
onMovenoneFunction抽屉移动时的监听函数 , 参数为抽屉拉出来的距离. 抽屉在左侧时参数为正,右侧则为负
onSlidingnoneFunctionCallback when menu is sliding. It returns a decimal from 0 to 1 which represents the percentage of menu offset between hiddenMenuOffset and openMenuOffset.
menuPositionleftString抽屉在左侧还是右侧 参数为 ‘left’/’right’
animationFunctionnone(Function -> Object)Function that accept 2 arguments (prop, value) and return an object:
- prop you should use at the place you specify parameter to animate
- value you should use to specify the final value of prop
animationStylenone(Function -> Object)Function that accept 1 argument (value) and return an object:
- value you should use at the place you need current value of animated parameter (left offset of content view)
bounceBackOnOverdrawtruebooleanwhen true, content view will bounce back to openMenuOffset when dragged further
autoClosingtrueboolean如果为true 一旦有事件发生抽屉就会关闭
以上为博主实践并根据源文档总结的. 
英文未翻译的为没有确定的,欢迎大神补充.
demo代码

最后送上本人使用时的demo代码
新建项目,安装好 react-native-side-menu库 , 替换APP.js中的内容运行即可

/**
 * Sample React Native App
 * https://github.com/facebook/react-native
 * @flow
 * zhuoyuan93@gmail.com
 *
 */

import React, {Component} from 'react';
import {
    Platform,
    StyleSheet,
    Text,
    View,
    Dimensions
} from 'react-native';
import SideMenu from 'react-native-side-menu';

const instructions = Platform.select({
    ios: 'Press Cmd+R to reload,\n' +
    'Cmd+D or shake for dev menu',
    android: 'Double tap R on your keyboard to reload,\n' +
    'Shake or press menu button for dev menu',
});
const {width, heihgt} = Dimensions.get('window');
type Props = {};
export default class App extends Component<Props> {

    constructor(props) {
        super(props);
        this.state = {
            isOpen: false
        }
    }

    render() {
        const menu = <Text style={{marginTop: 22}} onPress={() => alert('点击了aaa')}>aaa</Text>;
        return (

            <SideMenu
                menu={menu}                    //抽屉内的组件
                isOpen={this.state.isOpen}     //抽屉打开/关闭
                openMenuOffset={width / 2}     //抽屉的宽度
                hiddenMenuOffset={20}          //抽屉关闭状态时,显示多少宽度 默认0 抽屉完全隐藏
                edgeHitWidth={60}              //距离屏幕多少距离可以滑出抽屉,默认60
                disableGestures={false}        //是否禁用手势滑动抽屉 默认false 允许手势滑动
                /*onStartShouldSetResponderCapture={
                    () => console.log('开始滑动')}*/
                onChange={                   //抽屉状态变化的监听函数
                    (isOpen) => {
                        isOpen ? console.log('抽屉当前状态为开着')
                            :
                            console.log('抽屉当前状态为关着')

                    }}

                onMove={                     //抽屉移动时的监听函数 , 参数为抽屉拉出来的距离 抽屉在左侧时参数为正,右侧则为负
                    (marginLeft) => {
                        console.log(marginLeft)
                    }}

                menuPosition={'left'}     //抽屉在左侧还是右侧
                autoClosing={false}         //默认为true 如果为true 一有事件发生抽屉就会关闭
            >
                <View style={styles.container}>
                    <Text style={styles.welcome} onPress={() => {
                        this.setState({
                            isOpen: true
                        })
                    }}>
                        Open Draw!
                    </Text>
                    <Text style={styles.instructions}>
                        To get started, edit App.js
                    </Text>
                    <Text style={styles.instructions}>
                        {instructions}
                    </Text>
                </View>
            </SideMenu>

        );
    }
}


const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF',
        marginTop: 22
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10,
    },
    instructions: {
        textAlign: 'center',
        color: '#333333',
        marginBottom: 5,
    },
});

该库的github地址

React 中的插槽抽屉是一种常见的 UI 组件,用于在页面上创建一个可以展开或折叠的侧边或底部面板。它通常用于显示额外的内容,例如菜单、设置选项或通知。 在 React 中实现插槽抽屉可以通过以下步骤: 1. 创建一个 React 组件来表示抽屉,并在组件的状态中跟踪抽屉的展开/折叠状态。 2. 在组件的渲染方法中,根据抽屉的状态决定是否渲染抽屉内容。可以使用条件渲染或 CSS 类控制显示/隐藏样式。 3. 在页面的其他部分添加一个触发器,例如按钮或链接,用于切换抽屉的展开/折叠状态。这个触发器可以调用组件中的一个方法来更新状态。 下面是一个简单的示例代码,展示了如何实现一个基本的插槽抽屉: ```jsx import React, { useState } from "react"; const Drawer = () => { const [isOpen, setIsOpen] = useState(false); const toggleDrawer = () => { setIsOpen(!isOpen); }; return ( <div> <button onClick={toggleDrawer}>Toggle Drawer</button> {isOpen && <div className="drawer-content">Drawer Content</div>} </div> ); }; export default Drawer; ``` 在上面的代码中,`isOpen` 状态用于跟踪抽屉的展开/折叠状态。当点击按钮时,`toggleDrawer` 方法会更新 `isOpen` 状态的值,从而控制抽屉的显示与隐藏。根据 `isOpen` 的值,决定是否渲染抽屉内容。 你可以根据自己的需求来扩展这个示例,并添加适当的样式和动画效果。希望这个示例对你有帮助!如果需要更多的信息或代码示例,请随时提问。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值