实现一个简单的弹窗组件

这篇博客介绍了如何在React中实现一个弹框(Drawer)组件,包括组件的创建、API设计以及父子组件间的通信。组件利用createPortal挂载到DOM根节点,实现了蒙层关闭、内容选择等功能。同时,文章探讨了React中通过props进行父子组件间的数据传递。
摘要由CSDN通过智能技术生成

弹框组件实现

一、写在前面

最近技术设计评审过程,架构师要求将其中一些共同的业务场景封装为组件,于是产生了这个组件,但是怎么说呢,API不算多,并且适用的场景也比较少,主要还是涉及的组件间传值可以看一下,也欢迎指出问题。

二、实现
import React, { useEffect, useState } from 'react';
import { createPortal } from 'react-dom';
import './index.less'
const Node = document.createElement("div")
const Drawer = (props) => {
    const [closeMantle, setCloseMantle] = useState(false)
    const [childName, setChildName] = useState(props.content[0])
    const [showCancel, setShowCancel] = useState(false)
    /* 点击蒙层关闭 */
    const closeModal = () => {
        if (props.maskClosable === true) setCloseMantle(props.maskClosable);
        else {
            setCloseMantle(false)
        }
    }
    const chooseName = (e, v) => {
        e.stopPropagation()
        props.onChange(v)
        setChildName(v)
        setCloseMantle(!closeMantle)
    }
    useEffect(() => {
        if (props.hideCancel == false) setShowCancel(false);
        else {
            setShowCancel(true)
        }
    }, [])
    const showMantle = () => {
        document.body.appendChild(Node)

        setCloseMantle(!closeMantle)
        // console.log('5648')
    }
    const onMiss = () => {
        props.onCancel()
    }
    return <span onTouchEnd={showMantle}>
        {props.children}
        {createPortal(<div className='modal-container' style={{ display: closeMantle ? '' : 'none' }} onTouchEnd={closeModal}>
            <div className={props.position === 'bottom' ? 'modal-babylist-bottom' : 'modal-babylist-top'}>
                {props.content.map((v, i) => {
                    return <div onTouchEnd={(e) => chooseName(e, v)} key={i} className={childName == v ? 'modal-baby modal-active-name-color' : 'modal-baby'}>{v}</div>
                })}
                <div className='modal-baby-cancel ' style={{ display: showCancel ? '' : 'none' }}><div className='modal-cancel' onTouchEnd={onMiss}>取消</div></div>
            </div>
        </div>, Node)}
    </span>

}
export default Drawer
在react中父子组件之间的传值,父传子通过props来进行接收,子传父的话通过回调函数来进行接收。另外对于一些样式的实现的话通过写入多个类名,设定某个样式对应的一些标识,只需要判别标识来进行展示不同样式,大概就这么多吧。
另外其中有一个Protal将创建的节点挂载到根节点下。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue3是一款流行的JavaScript框架,它在构建用户界面方面非常强大。以下是一个示例代码,用于封装一个基本的弹窗组件: ```javascript <template> <div> <button @click="showModal">打开弹窗</button> <div v-if="isOpen" class="modal"> <div class="modal-content"> <slot></slot> <button @click="closeModal">关闭弹窗</button> </div> </div> </div> </template> <script> import { ref } from 'vue'; export default { name: 'Modal', setup() { const isOpen = ref(false); const showModal = () => { isOpen.value = true; }; const closeModal = () => { isOpen.value = false; }; return { isOpen, showModal, closeModal }; }, }; </script> <style scoped> .modal { position: fixed; top: 0; left: 0; width: 100%; height: 100%; background-color: rgba(0, 0, 0, 0.5); display: flex; justify-content: center; align-items: center; } .modal-content { background-color: white; padding: 20px; } button { margin-top: 10px; } </style> ``` 这个弹窗组件包含了一个按钮和一个模态框。当点击按钮时,模态框会显示出来,并且渲染出插槽内容。关闭按钮可以用于关闭模态框。在模态框外部点击也可以关闭模态框。 这个组件使用Vue3的Composition API来定义逻辑。通过ref函数创建一个响应式引用isOpen,用于跟踪模态框的开启和关闭状态。showModal方法用于打开模态框,closeModal方法用于关闭模态框。使用slot插槽来动态渲染弹窗内容。 在样式上,将模态框设置为固定定位,并使用背景色来实现半透明遮罩效果。模态框内容使用白色背景并设置内边距。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值