All files / spells withTweener.js

0% Statements 0/13
0% Branches 0/14
0% Functions 0/5
0% Lines 0/13

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                                                                                                                       
/*
 * Copyright (c) 2022-2023 Braun Nathanael
 *
 * This project is dual licensed under one of the following licenses:
 * - Creative Commons Attribution-NoDerivatives 4.0 International License.
 * - GNU AFFERO GENERAL PUBLIC LICENSE Version 3
 *
 * You should have received a copy of theses licenses along with this work.
 * If not, see <http://creativecommons.org/licenses/by-nd/4.0/> or <http://www.gnu.org/licenses/agpl-3.0.txt>.
 */
 
import React from "react";
import is    from "is";
 
import { isReactComponent } from '../utils/react';
import TweenerContext       from "../comps/TweenerContext";
 
 
const SimpleObjectProto = ( {} ).constructor;
 
 
/**
 * asTweener decorator
 * @param argz
 * @returns {*}
 */
export default function withTweener( ...argz ) {
    
    let BaseComponent = ( !argz[ 0 ] || isReactComponent(argz[ 0 ]) ) && argz.shift(),
        opts          = ( !argz[ 0 ] || argz[ 0 ] instanceof SimpleObjectProto ) && argz.shift() || {};
    
    if ( !( BaseComponent && ( BaseComponent.prototype instanceof React.Component || BaseComponent === React.Component ) ) ) {
        return function ( BaseComponent ) {
            return withTweener(BaseComponent, opts)
        }
    }
    
    class TweenerToProps extends React.Component {
        static displayName = ( BaseComponent.displayName || BaseComponent.name );
        
        render() {
            return <TweenerContext.Consumer>
                {
                    tweener => {
                        return <BaseComponent { ...this.props } tweener={ tweener }
                                              ref={ this.props.forwardedRef }/>;
                    }
                }
            </TweenerContext.Consumer>;
        }
    }
    
    
    let withRef         = React.forwardRef(( props, ref ) => {
        return <TweenerToProps { ...props } forwardedRef={ ref }/>;
    });
    withRef.displayName = TweenerToProps.displayName;
    return withRef;
}