All files / Toasts ToastElement.js

26.08% Statements 6/23
0% Branches 0/61
0% Functions 0/6
27.27% Lines 6/22

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                    20x           20x                                                             20x             20x                                                                               20x                                                                                                                                                                                                     20x    
import React, {
    useEffect,
    useRef,
    useState
} from 'react';
 
import { CloseIcon } from './icons';
import { NOOP } from './utils';
import { appearances } from './appearances';
 
export const gutter = 8;
 
 
// NOTE: invoke animation when NOT `autoDismiss` with opacity of 0 to avoid a
// paint bug in FireFox.
// https://bugzilla.mozilla.org/show_bug.cgi?id=625289
const Countdown = ({ autoDismissTimeout, opacity, isRunning, ...props }) => (
    <div
        className="toast-countdown"
        style={{
            // animation: `${shrinkKeyframes} ${autoDismissTimeout}ms linear`,
            animationPlayState: isRunning ? 'running' : 'paused',
            backgroundColor: 'rgba(0,0,0,0.1)',
            bottom: 0,
            height: 0,
            left: 0,
            opacity,
            position: 'absolute',
            width: '100%',
        }}
        {...props}
    />
);
 
 
function getTranslate(placement) {
    const pos = placement.split('-');
    const relevantPlacement = pos[1] === 'center' ? pos[0] : pos[1];
    const translateMap = {
        right: 'translate3d(120%, 0, 0)',
        left: 'translate3d(-120%, 0, 0)',
        bottom: 'translate3d(0, 120%, 0)',
        top: 'translate3d(0, -120%, 0)',
    };
 
    return translateMap[relevantPlacement];
}
const toastStates = (placement) => ({
    entering: { transform: getTranslate(placement) },
    entered: { transform: 'translate3d(0,0,0)' },
    exiting: { transform: 'scale(0.66)', opacity: 0 },
    exited: { transform: 'scale(0.66)', opacity: 0 },
});
 
const ToastElement = ({ appearance, placement, transitionDuration, transitionState, ...props }) => {
    const [height, setHeight] = useState('auto');
    const elementRef = useRef(null);
 
    useEffect(
        () => {
            if (transitionState === 'entered') {
                const el = elementRef.current;
                setHeight(el.offsetHeight + gutter);
            }
            if (transitionState === 'exiting') {
                setHeight(0);
            }
        },
        [transitionState]
    );
 
    return (
        <div
            ref={elementRef}
            style={{
                height,
                transition: `height ${transitionDuration - 100}ms 100ms`,
            }}
        >
            <div
                className={`${(appearances[appearance]?.background) ? appearances[appearance].background : 'bg-white dark:bg-gray-600'}
                    ${(appearances[appearance]?.border) ? appearances[appearance].border : 'border-gray-200 dark:border-gray-600'}
                    border overflow-hidden shadow-lg rounded-lg mx-auto mb-2 flex flex-row w-96`}
                style={{
                    transition: `transform ${transitionDuration}ms cubic-bezier(0.2, 0, 0, 1), opacity ${transitionDuration}ms`,
                    ...toastStates(placement)[transitionState],
                }}
                {...props}
            />
        </div>
    );
};
 
 
export const DefaultToast = ({
    appearance = 'misc',
    autoDismiss,
    autoDismissTimeout,
    title,
    content,
    isRunning,
    onDismiss,
    placement,
    transitionDuration,
    transitionState,
    onMouseEnter,
    onMouseLeave,
    action,
    actionLabel,
    secondaryAction,
    secondaryActionLabel,
    theme,
    color,
    ...otherProps
}) => {
    const style = appearances[appearance];
 
    return (
        <ToastElement
            appearance={appearance}
            placement={placement}
            transitionState={transitionState}
            transitionDuration={transitionDuration}
            onMouseEnter={onMouseEnter}
            onMouseLeave={onMouseLeave}
            {...otherProps}
        >
            {
                (appearance !== 'plain')
                    ? (
                        <div className={`flex ${(content !== '') ? 'pt-2' : 'pt-4'} pl-3 ${style.iconColor}`}>
                            <Countdown
                                opacity={autoDismiss ? 1 : 0}
                                autoDismissTimeout={autoDismissTimeout}
                                isRunning={isRunning}
                            />
                            <style.icon />
                        </div>
                    ) : null
            }
            <div className={`flex-grow ${(content !== '') ? 'p-2' : 'p-4 pl-2'} ${(appearance === 'plain') ? 'pl-4' : ''}`}>
                <div className={`font-medium ${(style?.title) ? style.title : 'dark:text-white'}`}>
                    {title}
                </div>
                {
                    (content !== '')
                        ? (
                            <div className={`text-sm tracking-tight ${(style?.content) ? style.content : 'text-gray-600 dark:text-gray-300'}`}>
                                {content}
                            </div>
                        ) : null
                }
                {
                    (action && secondaryAction)
                        ? (
                            <div className="flex flex-row gap-x-6 pt-2">
                                <button role="button" className={`text-${(color) ? color : theme}-600 dark:text-${(color) ? color : theme}-500 hover:text-${(color) ? color : theme}-500 dark:hover:text-${(color) ? color : theme}-600 font-bold`} onClick={action}>
                                    {actionLabel}
                                </button>
                                <button role="button" className="text-gray-500 dark:text-white hover:font-medium" onClick={(secondaryAction === 'dismiss') ? onDismiss : secondaryAction}>
                                    {secondaryActionLabel}
                                </button>
                            </div>
                        ) : null
                }
            </div>
            {
                (action && !secondaryAction)
                    ? (
                        <div className={`flex items-center text-${(color) ? color : theme}-600 dark:text-${(color) ? color : theme}-500 hover:text-${(color) ? color : theme}-500 dark:hover:text-${(color) ? color : theme}-600 px-4`}>
                            <button role="button" className="font-bold" onClick={action}>{actionLabel}</button>
                        </div>
                    ) : null
            }
            {
                (onDismiss && secondaryAction !== 'dismiss')
                    ? (
                        <div
                            role="button"
                            className={`text-gray-800 dark:text-gray-200 cursor-pointer flex-shrink-0 pr-3 opacity-50 hover:opacity-100 transition-opacity ${(content !== '') ? 'pt-2' : 'pt-4'}`}
                            onClick={onDismiss}
                        >
                            <CloseIcon />
                            <span className="sr-only">
                                Close
                            </span>
                        </div>
                    ) : null
            }
        </ToastElement >
    )
};
 
DefaultToast.defaultProps = {
    onDismiss: NOOP,
};