2.2.30 • Published 2 years ago

@eightfeet/modal v2.2.30

Weekly downloads
15
License
ISC
Repository
github
Last release
2 years ago

modal

弹窗模块,需要promise polyfill demo

npm i @eightfeet/modal -S
import Modal from '@eightfeet/modal';

const myModal = new Modal({
    style: {
        article: {
            borderRadius: '0.5em',
            backgroundColor: '#fff',
        }
	}
});

myModal.create({
    article: '这是一个弹窗'
}).then(() => console.log('弹窗已打开'));

parame

参数说明是否必填备注类型
id所创建弹窗的id不传可自动生成id(modal + 时间戳 + 100以内的随机数)String
zIndexmodal的样式层级关系默认100Number
emBaseem单位的基准像素默认自动计算(emBase = document.clientWidth/24)Number
parentId所挂载的父级ID用于做局部弹窗默认挂在body下面,指定父级dom时将挂载在父级dom下,配合css实现局部弹窗String
closable是否可关闭默认trueBoolean
shouldCloseOnOverlayClick是否点击蒙层关闭弹窗默认falseBoolean
style弹窗样式定义modal样式 { overlay: 覆盖层, wrap: 模块包裹层, content: 内容区, header: 头部, article: 内容区, close: 关闭按钮, modify: 修饰器}, modify修饰器 是一个数组,每个数组元素对应会创建一个 基于弹窗的绝对定位div层,用于修饰弹窗(参考case)Object
animation弹窗动画{ form: 弹窗动画形式,参考animation附表, duration:持续时长}Object
onCancel关闭弹窗关闭弹窗时的回调Function
animation附表
参数说明备注
fadeInUp从底部向上淡入
fadeInDown从顶部向下淡入
fadeInLeft从左向右淡入
fadeInRight从右向左淡入
zoomIn从中心放大淡入默认
zoomInUp从底部向上放大淡入
zoomInDown从顶部向下放大淡入
zoomInLeft从左向右放大淡入
zoomInRight从右向左放大淡入

options

  1. create: ƒ (elements, doNotRemove) 创建弹窗
    /**
      * @description 创建弹窗
      * @param {{
      *         header: InnerHTML;
      *         article: InnerHTML;
      *         footer: InnerHTML;
      *     }} elements 弹窗元素
      * @param {boolean} doNotRemove 是否移除弹窗
      * @memberof Modal
      */
  2. hide: ƒ (doNotRemove) 隐藏弹窗
    /**
      * @description 移除弹窗
      * @memberof Modal
      */
  3. remove: ƒ () 移除弹窗

    移除,将弹窗从body中移除。

  4. show: ƒ () 显示弹窗

    显示页面弹窗,如果创建的弹窗隐藏时,调用此方法显示弹窗。

createInlineStyles

createInlineStyles: 创建行内样式、 
    import {createInlineStyles} from '@eightfeet/modal';

saferInnerHtml

InnerHtml 具有一定安全隐患,saferInnerHtml用于过滤模版中的危险脚本,让你安全使用InnerHtml
避免XSS 攻击,默认InnerHtml都有通过saferInnerHtml转译为安全html
    import {saferInnerHtml} from '@eightfeet/modal';
    const htmlStr = saferInnerHtml('<img src="x" onerror="alert(1)" />')
    console.log(htmlStr); // <img src="x" />

case

import Modal from '@eightfeet/modal';

const newModal = new Modal({
        id: 'modalId', // 所创建弹窗的id 不传可自动生成id(modal + 时间戳 + 100以内的随机数)
        zIndex: 100, // modal的层级关系,默认100
        emBase: 12, // 基准像素 parent Dom’s fontsize
        animation: {
           from: 'bottom',
           duration: '2s'
        }, // 启用动画 默认true
        parentId: 'indom', // modal的创建位置,默认创建在body下
        closable: false, // modal是否可关闭 默认true
        shouldCloseOnOverlayClick: true, // 点击背景层关闭弹窗 默认false
        style: { // 定义modal样式 {overlay: 覆盖层, content: 内容区, close: 关闭按钮} 
            overlay: {
                backgroundColor: 'rgba(0,0,0,0.5)'
            },
            content: {
                backgroundColor: 'rgba(100, 100, 100, 0.2)',
                width: '19em',
                // padding: '120px',
                // 设置内容的层级关系
                zIndex: 107
            },
            close: {
                backgroundColor: 'rgba(0, 0, 0, 1)',
                width: '1em',
                height: '1em',
                top: '-3em',
                left: '50%'
            },
            // modify(修饰层)层级按照zIndex(modal的层级)以2为步值递增
            modify: [
                {
                    backgroundColor: 'rgba(0, 0, 255, 0.4)',
                    width: '120%',
                    left: '-10%',
                    height: '200px',
                    border: '1px solid rgba(0, 0, 255, 0.6)',
                    top: '-5em'
                },
                {
                    backgroundColor: 'rgba(0, 0, 255, 0.4)',
                    width: '130%',
                    left: '-15%',
                    height: '200px',
                    border: '1px solid rgba(0, 0, 255, 0.6)',
                    top: '-4em'
                }
            ]
        }
    });

    const btn = document.getElementById('exampleBtn');

    btn.onclick = function(){ 
        return newModal.create({
            header:'<div style="position:relative; z-index: 90;background-color: yellow;">头部</div>',
            article: `<div style="background-color: #fff;">
                        这是一段内容
                        <input id="inp" type="text" />
                    </div>`,
            footer: `<div style="background-color: white">
                        <button id="close" style="border:1px solid #aaa; padding: 1em">确定</button>
                        <br>
                        脚部
                    </div>`
        }, true).then(function(){
            document.getElementById('close').onclick = function(){
                console.log(document.getElementById('inp').value);
                return newModal.hide(true);
            };
        });
    }
    

react

Modal组件最佳实践

demo

    <Modal 
        visible={visible}
        onCancel={() => setVisible(false)}
        overlayStyle={{background: 'rgba(0, 255, 0, 0.5)'}}
    >
        <div>这是一个弹窗</div>
        <button onClick={() => setVisible(false)}>关闭弹窗</button>
    </Modal>

react hook

  • case react useModal hook

    import { useRef, useCallback, useMemo, useEffect } from "react";
    import Modal, { ModalParameters } from "@eightfeet/modal";

    const useModal = (parameters: ModalParameters) => {
    const ref = useRef<Modal>();
    useMemo(() => {
        ref.current = new Modal(parameters);
    }, [parameters]);

    useEffect(() => {
        return () => {
        if (ref.current) {
            const previousModal = ref.current;
            if (document.getElementById(previousModal.state.id as string)) {
            previousModal.remove();
            }
        }
        }
    }, [])

    const createModal = useCallback<Modal['create']>((data) => {
        return ref.current?.create(data) || Promise.reject('modal is not ready yet!');
    }, []);

    const hideModal = useCallback<Modal["hide"]>((doNotRemove) => {
        return ref.current?.hide(doNotRemove) || Promise.reject('modal is not ready yet!');
    }, []);

    return { createModal, hideModal, modal: ref.current }
    };

    export default useModal;
  • useModal

    import React from "react";
    import useModal from "./hooks/useModal";

    function App() {
        const { createModal, hideModal } = useModal({
            style: {
                content: {
                    width: '301px'
                }
            }
        });

        return (
            <div className="App">
            <button
                onClick={() =>
                createModal({ article: "<div>messages!</div>" }).then(() =>
                    setTimeout(hideModal, 1000)
                )
                }
            >
                弹窗
            </button>
            </div>
        );
    }

    export default App;
2.2.30

2 years ago

2.2.28

2 years ago

2.2.29

2 years ago

2.2.27

2 years ago

2.2.26

2 years ago

2.2.25

2 years ago

2.2.24

2 years ago

2.2.23

3 years ago

2.2.21

3 years ago

2.2.19

3 years ago

2.2.20

3 years ago

2.2.17

3 years ago

2.2.18

3 years ago

2.2.15

3 years ago

2.2.16

3 years ago

2.2.13

3 years ago

2.2.14

3 years ago

2.2.12

3 years ago

2.2.11

3 years ago

2.2.10

3 years ago

2.2.9

3 years ago

2.2.8

3 years ago

2.2.7

3 years ago

2.2.6

3 years ago

2.2.5

3 years ago

2.2.4

3 years ago

2.2.3

3 years ago

2.2.1

3 years ago

2.2.0

3 years ago

2.2.2

3 years ago

2.1.9

3 years ago

2.1.8

4 years ago

2.1.7

4 years ago

2.1.6

4 years ago

2.1.5

4 years ago

2.1.4

4 years ago

2.1.3

4 years ago

2.1.2

4 years ago

2.1.1

4 years ago

2.1.0

4 years ago

2.0.0

5 years ago

1.2.5

5 years ago

1.2.4

5 years ago

1.2.3

5 years ago

1.2.2

5 years ago

1.2.1

5 years ago

1.2.0

5 years ago

1.1.1

5 years ago

1.1.0

5 years ago

1.0.19

5 years ago

1.0.18

5 years ago

1.0.17

5 years ago

1.0.15

5 years ago

1.0.14

5 years ago

1.0.13

5 years ago

1.0.12

5 years ago

1.0.11

5 years ago

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago