All files / src/grid Spacer.jsx

84.09% Statements 37/44
72.5% Branches 29/40
70.59% Functions 12/17
86.05% Lines 37/43

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 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208                                    42x                   42x                               312x   312x         312x           312x     312x                 312x   312x 312x   312x 312x                   270x 270x   270x 270x                                         635x 635x 635x 635x   635x 36x                                                       348x 348x 348x   348x 348x   348x         348x                                   42x     348x       348x       348x       348x       348x       348x       348x            
import React, {Component, createRef} from 'react';
 
import {autobind} from 'core-decorators';
import PropTypes from 'prop-types';
import styled, {css} from 'styled-components';
 
import {getConfig} from '../utils/lib';
import {calcFlex, calcHeight, calcInline, calcMaxHeight, calcMaxWidth, calcWidth} from '../utils/spacerHelpers';
 
/**
 * Spacer
 *
 * @augments {Component<Props, State>}
 *
 * @class Spacer
 * @extends {Component}
 */
class Spacer extends Component {
    static propTypes = {
        inline: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
        maxX: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
        maxY: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
        /** TestID for cypress testing  */
        testId: PropTypes.string,
        x: PropTypes.oneOfType([PropTypes.number, PropTypes.object]),
        y: PropTypes.oneOfType([PropTypes.number, PropTypes.object])
    }
 
    static defaultProps = {
        inline: false,
        maxX: null,
        maxY: null,
        testId: null,
        x: null,
        y: null
    }
 
    /**
     * Creates an instance of Spacer.
     *
     * @param {Object} props Component props.
     * @memberof Spacer
     */
    constructor(props) {
        super(props);
 
        this.state = {
            ...(process.env.NODE_ENV === 'production' ? {} : {debug: false}),
            direction: 'X'
        };
 
        Iif (typeof window === 'undefined') {
            this.observer = {
                disconnect() {},
                observe() {}
            };
        } else {
            this.observer = new MutationObserver(this.observeMutation);
        }
 
        this.spacing = createRef();
    }
 
    /**
     * Reads the parent flex direction value.
     *
     * @memberof Spacer
     */
    componentDidMount() {
        this.findFlexDirection();
 
        this.observer.observe(this.spacing.current.parentElement, {attributes: true});
        window.addEventListener('resize', this.findFlexDirection);
 
        Eif (process.env.NODE_ENV !== 'production') {
            window.addEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Removes listeners.
     *
     * @memberof Spacer
     */
    componentWillUnmount() {
        this.observer.disconnect();
        window.removeEventListener('resize', this.findFlexDirection);
 
        Eif (process.env.NODE_ENV !== 'production') {
            window.removeEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Observes any mutations made.
     *
     * @memberof Spacer
     */
    @autobind
    observeMutation() {
        this.findFlexDirection();
    }
 
    /**
     * Reads the parent flex direction value.
     *
     * @memberof Spacer
     */
    @autobind
    findFlexDirection() {
        Eif (window.getComputedStyle(this.spacing.current.parentElement)) {
            const {direction: oldDirection} = this.state;
            const {flexDirection} = window.getComputedStyle(this.spacing.current.parentElement);
            const direction = ['column', 'column-reverse'].includes(flexDirection) ? 'Y' : 'X';
 
            if (oldDirection !== direction) {
                this.setState({direction});
            }
        }
    }
 
    /**
     * Sets the debug state.
     *
     * @param {KeyboardEvent} e The keyboard event.
     *
     * @memberof Row
     */
    @autobind
    toggleDebug(e) {
        if (e.ctrlKey && e.code === 'KeyD') {
            e.preventDefault();
            // eslint-disable-next-line no-invalid-this
            this.setState(oldState => ({debug: !oldState.debug}));
        }
    }
 
    /**
     * Renders the Component.
     *
     * @returns {JSX} Component.
     * @memberof Spacer
     */
    render() {
        const {direction} = this.state;
        const {inline, maxX, maxY, testId, x, y} = this.props;
        let className = null;
 
        Eif (process.env.NODE_ENV !== 'production') {
            const {debug} = this.state;
 
            Iif (debug) {
                className = 'debug';
            }
        }
 
        return (
            <SpacerElement
                ref={this.spacing}
                className={className}
                data-cy={testId}
                direction={direction}
                inline={inline}
                maxX={maxX}
                maxY={maxY}
                x={x}
                y={y}
            />
        );
    }
}
 
export default Spacer;
 
const SpacerElement = styled.span`
    flex: 1 1 0px;
 
    ${({inline, theme}) => css`
        ${calcInline(inline, theme)}
    `}
 
    ${({direction, theme, x, y}) => css`
        ${calcFlex(direction, theme, x, y)}
    `}
 
    ${({theme, y}) => ((typeof y === 'number' || typeof y === 'object') && y !== null) && css`
        ${calcHeight(theme, y)}
    `}
 
    ${({maxY, theme}) => ((typeof maxY === 'number' || typeof maxY === 'object') && maxY !== null) && css`
        ${calcMaxHeight(maxY, theme)}
    `}
 
    ${({maxX, theme}) => ((typeof maxX === 'number' || typeof maxX === 'object') && maxX !== null) && css`
        ${calcMaxWidth(maxX, theme)}
    `}
 
    ${({theme, x}) => ((typeof x === 'number' || typeof x === 'object') && x !== null) && css`
        ${calcWidth(theme, x)}
    `}
 
    ${({theme}) => process.env.NODE_ENV !== 'production' && css`
        &.debug {
            background-color: ${getConfig(theme).debug.spacer.background};
            outline: ${getConfig(theme).debug.spacer.outline} solid 1px;
        }
    `}
`;