All files / src/grid Container.jsx

100% Statements 27/27
84.62% Branches 22/26
100% Functions 12/12
100% Lines 26/26

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                                    228x                   228x                               2436x   2436x 2436x                   2436x 2436x                   2226x 2226x                         36x 18x   18x                     2454x 2454x   2454x 2454x   2454x 18x       2454x                 2436x   228x             2454x 14724x             2454x 14724x                 2454x            
/* eslint-disable react/boolean-prop-naming */
import React, {Component, forwardRef} from 'react';
 
import {autobind} from 'core-decorators';
import PropTypes from 'prop-types';
import styled, {css} from 'styled-components';
 
import {DIMENSIONS} from '../defaultConfig';
import {getConfig, media} from '../utils/lib';
 
/**
 * Container
 *
 * @component
 * @augments {Component<Props, State>}
 * @extends {Component}
 */
class Container extends Component {
    static propTypes = {
        children: PropTypes.node.isRequired,
        as: PropTypes.string,
        className: PropTypes.string,
        fluid: PropTypes.oneOfType([PropTypes.bool, PropTypes.array]),
        innerRef: PropTypes.oneOfType([PropTypes.func, PropTypes.object]),
        /** TestID for cypress testing  */
        testId: PropTypes.string
    }
 
    static defaultProps = {
        as: null,
        className: null,
        fluid: false,
        innerRef: null,
        testId: null
    }
 
    /**
     * Creates an instance of Container.
     *
     * @param {Object} props Component props.
     *
     * @memberof Container
     */
    constructor(props) {
        super(props);
 
        Eif (process.env.NODE_ENV !== 'production') {
            this.state = {debug: false};
        }
    }
 
    /**
     * Sets an event listener to toggle debug mode.
     *
     * @memberof Container
     */
    componentDidMount() {
        Eif (process.env.NODE_ENV !== 'production') {
            window.addEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Unsets an event listener to toggle debug mode.
     *
     * @memberof Container
     */
    componentWillUnmount() {
        Eif (process.env.NODE_ENV !== 'production') {
            window.removeEventListener('keydown', this.toggleDebug);
        }
    }
 
    /**
     * Sets the debug state.
     *
     * @param {KeyboardEvent} e The keyboard event.
     *
     * @memberof Container
     */
    @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 Container
     */
    render() {
        const {as, children, className, fluid, innerRef, testId} = this.props;
        const classNames = [className];
 
        Eif (process.env.NODE_ENV !== 'production') {
            const {debug} = this.state;
 
            if (debug) {
                classNames.push('debug');
            }
        }
 
        return (
            <ContainerElement ref={innerRef} as={as} className={classNames.join(' ')} data-cy={testId} fluid={fluid}>
                {children}
            </ContainerElement>
        );
    }
}
 
// eslint-disable-next-line react/jsx-props-no-spreading
export default forwardRef((props, ref) => <Container {...props} innerRef={ref} />);
 
const ContainerElement = styled.div`
    box-sizing: border-box;
    margin-left: auto;
    margin-right: auto;
    max-width: 100%;
    width: 100%;
 
    ${({theme}) => css`
        ${DIMENSIONS.map(screenSize => getConfig(theme).container[String(screenSize)] && media(theme, screenSize)`
            padding-left: ${getConfig(theme).paddingWidth[String(screenSize)]}px;
            padding-right: ${getConfig(theme).paddingWidth[String(screenSize)]}px;
        `)}
    `}
 
 
    ${({fluid, theme}) => css`
        ${DIMENSIONS.map(screenSize => getConfig(theme).container[String(screenSize)] && media(theme, screenSize)`
            ${(typeof getConfig(theme).container[String(screenSize)] === 'number' && ((Array.isArray(fluid) && !fluid.includes(screenSize)) || fluid === false))
        // eslint-disable-next-line indent
                ? `width: ${getConfig(theme).container[String(screenSize)]}px;`
        // eslint-disable-next-line indent
                : 'width: 100%;'}
        `)}
    `}
 
    ${({theme}) => process.env.NODE_ENV !== 'production' && css`
        &.debug {
            background-color: ${getConfig(theme).debug.container.background};
            outline: ${getConfig(theme).debug.container.outline} solid 1px;
        }
    `}
`;