All files / Banner Banner.tsx

93.33% Statements 14/15
100% Branches 6/6
50% Functions 1/2
93.33% Lines 14/15

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                  176x   176x                                                                           176x                   176x                       37x   37x                   37x   37x 2x     37x                                                                                       176x 176x 176x                                                           176x                                                                                  
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { CSSTransition } from 'react-transition-group';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps, StandardProps } from '../../util/component-types';
import CloseIcon from '../Icon/CloseIcon/CloseIcon';
import { IIconProps } from '../Icon/Icon';
 
const cx = lucidClassNames.bind('&-Banner');
 
const { bool, element, func, node, oneOf, string } = PropTypes;
 
export interface IBannerProps
	extends StandardProps,
		React.DetailedHTMLProps<
			React.HTMLAttributes<HTMLDivElement>,
			HTMLDivElement
		> {
	/** Pass in a icon component for custom icons within `Banner`. */
	icon: React.ReactElement | null;
 
	/** Set this to `true` if you want to have a `x` close icon. */
	isCloseable: boolean;
 
	/** If set to `false` the banner will not be filled in.
	 * @default = true
	 */
	isFilled: boolean;
 
	/** If set to `true` the banner have smaller padding on the inside. */
	isSmall: boolean;
 
	/** Style variations of the `Banner`. */
	kind: 'primary' | 'success' | 'warning' | 'danger' | 'info' | 'default';
 
	/** Called when the user closes the `Banner`. */
	onClose: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IIconProps;
	}) => void;
 
	/** Controls the visibility of the `Banner`. */
	isClosed: boolean;
}
 
const defaultProps = {
	icon: null,
	isCloseable: true,
	isFilled: true,
	isSmall: false,
	kind: 'default' as const,
	onClose: _.noop,
	isClosed: false,
};
 
export const Banner = (props: IBannerProps): React.ReactElement => {
	const {
		icon,
		kind,
		className,
		children,
		isCloseable,
		isClosed,
		isFilled,
		isSmall,
		onClose,
		...passThroughs
	} = props;
 
	const handleClose = ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IIconProps;
	}): void => {
		onClose({ event, props });
	};
 
	let displayedIcon: any = null;
 
	if (icon) {
		displayedIcon = icon;
	}
 
	return (
		<CSSTransition
			in={!isClosed}
			classNames={cx('&')}
			timeout={300}
			unmountOnExit
		>
			<section
				{...omitProps(passThroughs, undefined, _.keys(Banner.propTypes))}
				className={cx(
					'&',
					{
						'&-has-icon': displayedIcon,
						'&-has-close': isCloseable,
						'&-primary': kind === 'primary',
						'&-success': kind === 'success',
						'&-warning': kind === 'warning',
						'&-danger': kind === 'danger',
						'&-info': kind === 'info',
						'&-small': isSmall,
						'&-filled': isFilled,
					},
					className
				)}
			>
				{displayedIcon ? (
					<span className={cx('&-icon')}>{displayedIcon}</span>
				) : null}
 
				<span className={cx('&-content')}>{children}</span>
 
				{isCloseable ? (
					<CloseIcon
						isClickable
						size={8}
						className={cx('&-close')}
						onClick={handleClose}
					/>
				) : null}
			</section>
		</CSSTransition>
	);
};
 
Banner.defaultProps = defaultProps;
Banner.displayName = 'Banner';
Banner.peek = {
	description: `
		A banner that displays a prominent message.
	`,
	notes: {
		overview: `
			A banner that displays a prominent message.
		`,
		intendedUse: `
			Communicates information, success, a warning, or an error. 
								
			**Styling notes**
			
			- Banners usually display at the top of a page.
			- Use the solid filled banner for single-line content.
			- Use the outlined banner for multi-line content.
			- Color use:
				- Use \`kind:"info"\` (blue) for information, like instructions for a feature.
				- Use \`kind:"success"\` (green) for success messages, like completing a task successfully.
				- Use \`kind:"warning"\` (yellow) for warnings, like a line item that is under-delivering.
				- Use \`kind:"danger"\` (orange) for danger messages, like an error message for missing required content.
				- Use grey banners for new feature announcements.
		`,
		technicalRecommendations: `
			Short single-line content can be passed in as a simple string. Multi-line messages should be passed wrapped in a \`<p>\` tag.
			You can apply styling as needed within a banner, for example using \`strong\`, \`a href\`, or \`em\`.
		`,
	},
	categories: ['communication'],
};
Banner.propTypes = {
	icon: element`
		Pass in a icon component for custom icons within \`Banner\`.
	`,
 
	isCloseable: bool`
		Set this to \`true\` if you want to have a \`x\` close icon.
	`,
 
	isFilled: bool`
		Defaults to true.
		If set to \`false\` the banner will not be filled in.
	`,
 
	isSmall: bool`
		If set to \`true\` the banner have smaller padding on the inside.
	`,
 
	className: string`
		Class names that are appended to the defaults.
	`,
 
	children: node`
		Any valid React children.
	`,
 
	kind: oneOf(['primary', 'success', 'warning', 'danger', 'info', 'default'])`
		Style variations of the \`Banner\`.
	`,
 
	onClose: func`
		Called when the user closes the \`Banner\`.  Signature:
		\`({ event, props }) => {}\`
	`,
 
	isClosed: bool`
		Controls the visibility of the \`Banner\`.
	`,
};
 
export default Banner;