All files / src/DotProvider DotProvider.js

100% Statements 19/19
100% Branches 13/13
100% Functions 3/3
100% Lines 19/19

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72          1x                                 50x 50x   50x 150x 3x 147x 144x   3x       50x 98x 98x 2x 96x 2x       50x                                 50x             1x 1x    
import React, { useRef, useCallback, forwardRef } from "react";
import { defaultProps } from './props/defaultProps';
import { propTypes } from './props/propTypes';
import useDotProvider from "./hooks/useDotProvider";
 
const DotProvider = forwardRef((props, ref) => {
    
    const { 
        tag: Element = 'div',
        themeAppearance,
        themeColor,
        baseZoomUnit,
        baseFontUnit,
        zoomUnitVariable,
        fontUnitVariable,
        themeAppearanceAttr,
        themeColorAttr,
        providerRef,
        onAssetsDownloadSuccess,
        getAssetsPromises,
        children,
        ...rest
    } = props;
    const wrapperElementRef = useRef(null);
    
    const getProviderElement = useCallback(() => {
        if (typeof providerRef === 'function') {
            return providerRef();
        } else if (Element !== React.Fragment) {
            return wrapperElementRef.current;
        } else {
            return document.documentElement;
        }
    }, [providerRef, Element]);
    
    const getEleRef = useCallback(ele => {
        wrapperElementRef.current = ele;
        if (ref && typeof ref === 'function') {
            ref(ele);
        } else if (ref && typeof ref === 'object') {
            ref.current = ele;
        }
     }, [ref]);
 
    useDotProvider({
        themeAppearance: themeAppearance,
        themeColor: themeColor,
        themeAppearanceAttr: themeAppearanceAttr,
        themeColorAttr: themeColorAttr,
 
        baseZoomUnit: baseZoomUnit,
        baseFontUnit: baseFontUnit,
        zoomUnitVariable: zoomUnitVariable,
        fontUnitVariable: fontUnitVariable,
 
        getProviderElement,
 
        onAssetsDownloadSuccess: onAssetsDownloadSuccess,
        getAssetsPromises: getAssetsPromises,
    });
 
    return (
        <Element {...rest} ref={getEleRef}>
            {children}
        </Element>
    );
})
 
DotProvider.propTypes = propTypes;
DotProvider.defaultProps = defaultProps;
 
export default DotProvider;