fluxtech-me / frontik

Frontend Development Libraries
https://frontik.js.org
MIT License
9 stars 0 forks source link

Reusable Modal Component #15

Open sargisvarderesyan opened 2 years ago

alexkharatyan commented 2 years ago
//FOR BODY OVERFLOW ISSUE

import React, {useEffect} from 'react';

const useModalOverflow = (isOpen = false, needToOverflow = true, style={}) => {
    useEffect(() => {
        if(needToOverflow) {
            const body = document.querySelector('body');
            const hasScrollableContent = body.scrollHeight > window.innerHeight;
            if(isOpen) {
                if (hasScrollableContent) {
                    body.classList.add('scroll-hidden');
                    for (const property in style) {
                        body.style[property] = style[property];
                    }
                }
            } else {
                body.classList.remove('scroll-hidden');
            }
        }
        return () => {
            if(needToOverflow) {
                const body = document.querySelector('body');
                body.classList.remove('scroll-hidden');
            }
        };
    }, [isOpen]);

};

export {useModalOverflow};