All files / src/Alert Alert.tsx

100% Statements 31/31
85.71% Branches 24/28
100% Functions 7/7
100% Lines 30/30

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 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233                                                          1x   1x                               42x 42x   42x             42x               42x 42x   42x     42x                                                           42x                                 1x   41x 41x                                   7x 7x   7x             7x                                     40x 40x   40x             40x                         3x 3x   3x             3x                             40x 40x   40x             40x            
import * as React from 'react';
import { Box as ReakitBox } from 'reakit';
 
import { Omit } from '../types';
import { useClassName, createComponent, createElement, createHook, useUniqueId } from '../utils';
import { Box, BoxProps } from '../Box';
import { Button, ButtonProps } from '../Button';
import { Icon, IconProps } from '../Icon';
import { Text } from '../Text';
 
import * as styles from './styles';
 
export type LocalAlertProps = {
  accent?: true | 'top' | 'bottom';
  hasIcon?: boolean;
  isFilled?: boolean;
  isInline?: boolean;
  onClickClose?: ButtonProps['onClick'];
  showCloseButton?: boolean;
  standalone?: boolean;
  title?: string;
  type?: string;
  closeButtonProps?: Omit<ButtonProps, 'children'>;
  closeButtonIconProps?: Omit<IconProps, 'icon'>;
  iconProps?: Omit<IconProps, 'icon'>;
};
export type AlertProps = BoxProps & LocalAlertProps;
 
export type AlertContextOptions = AlertProps & { descriptionId?: string; titleId?: string; themeKey?: string };
export const AlertContext = React.createContext<AlertContextOptions>({});
 
const useProps = createHook<AlertProps>(
  (props, { themeKey, themeKeyOverride }) => {
    const {
      closeButtonProps,
      closeButtonIconProps,
      hasIcon,
      iconProps,
      isFilled,
      isInline,
      onClickClose,
      overrides,
      showCloseButton,
      standalone,
      title,
      type,
      ...restProps
    } = props;
    const boxProps = Box.useProps(restProps);
 
    const className = useClassName({
      style: styles.Alert,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      prevClassName: boxProps.className
    });
    const alertCloseButtonClassName = useClassName({
      style: styles.AlertCloseButton,
      styleProps: props,
      themeKey,
      themeKeyOverride,
      themeKeySuffix: 'CloseButton'
    });
 
    const titleId = useUniqueId('alertTitle');
    const descriptionId = useUniqueId('alertDescription');
 
    const context = React.useMemo(() => ({ descriptionId, titleId, ...props }), [descriptionId, props, titleId]);
 
    const children = (
      <AlertContext.Provider value={context}>
        <Box display="flex" alignItems="center" justifyContent="space-between">
          <Box display="flex" alignItems="center">
            {standalone ? (
              props.children
            ) : (
              <React.Fragment>
                {hasIcon && <AlertIcon overrides={overrides} />}
                <AlertContent overrides={overrides}>
                  {title && <AlertTitle overrides={overrides}>{title}</AlertTitle>}
                  <AlertDescription overrides={overrides}>{props.children}</AlertDescription>
                </AlertContent>
              </React.Fragment>
            )}
          </Box>
          {showCloseButton && (
            <Box display="flex">
              <Button.Close
                className={alertCloseButtonClassName}
                onClick={onClickClose}
                palette={props.isFilled ? `${type}Inverted` : type}
                iconProps={closeButtonIconProps}
                {...closeButtonProps}
              />
            </Box>
          )}
        </Box>
      </AlertContext.Provider>
    );
 
    return {
      ...boxProps,
      'aria-describedby': props.children ? descriptionId : undefined,
      'aria-labelledby': props.title ? titleId : undefined,
      className,
      children,
      role: 'alert'
    };
  },
  {
    defaultProps: {
      type: 'info'
    },
    themeKey: 'Alert'
  }
);
 
export const Alert = createComponent<AlertProps>(
  props => {
    const alertProps = useProps(props);
    return createElement({ children: props.children, component: ReakitBox, use: props.use, htmlProps: alertProps });
  },
  {
    attach: {
      useProps
    },
    themeKey: 'Alert'
  }
);
 
/////////////////////////////////////
 
export type LocalAlertIconProps = {
  iconProps?: Omit<IconProps, 'icon'>;
};
export type AlertIconProps = BoxProps & LocalAlertIconProps;
 
export function AlertIcon(props: AlertIconProps) {
  const { iconProps, ...restProps } = props;
  const context = React.useContext(AlertContext);
 
  const alertIconWrapperClassName = useClassName({
    style: styles.AlertIconWrapper,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Alert',
    themeKeySuffix: 'IconWrapper'
  });
 
  return (
    <Box className={alertIconWrapperClassName} {...restProps}>
      <Icon
        aria-hidden
        color={context.isFilled ? `${context.type}Inverted` : context.type}
        fontSize={props.children || !context.isInline ? '400' : '200'}
        icon={context.type}
        {...iconProps}
      />
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalAlertContentProps = {};
export type AlertContentProps = BoxProps & LocalAlertContentProps;
 
export function AlertContent(props: AlertContentProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(AlertContext);
 
  const alertContentClassName = useClassName({
    style: styles.AlertContent,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Alert',
    themeKeySuffix: 'Content'
  });
 
  return (
    <Box className={alertContentClassName} {...restProps}>
      {children}
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalAlertTitleProps = {};
export type AlertTitleProps = BoxProps & LocalAlertTitleProps;
 
export function AlertTitle(props: AlertTitleProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(AlertContext);
 
  const alertTitleClassName = useClassName({
    style: styles.AlertTitle,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Alert',
    themeKeySuffix: 'Title'
  });
 
  return (
    <Box className={alertTitleClassName} {...restProps}>
      <Text fontWeight="semibold" id={context.titleId}>
        {children}
      </Text>
    </Box>
  );
}
 
/////////////////////////////////////
 
export type LocalAlertDescriptionProps = {};
export type AlertDescriptionProps = BoxProps & LocalAlertDescriptionProps;
 
export function AlertDescription(props: AlertDescriptionProps) {
  const { children, ...restProps } = props;
  const context = React.useContext(AlertContext);
 
  const alertDescriptionClassName = useClassName({
    style: styles.AlertDescription,
    styleProps: { ...context, ...props },
    themeKey: context.themeKey || 'Alert',
    themeKeySuffix: 'Description'
  });
 
  return (
    <Box className={alertDescriptionClassName} id={context.descriptionId} {...restProps}>
      {children}
    </Box>
  );
}