All files / components/Icon Icon.tsx

75.76% Statements 25/33
14.29% Branches 2/14
33.33% Functions 1/3
75.76% Lines 25/33

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 2172x 2x 2x 2x 2x           2x   2x   2x 2x 2x 2x 2x 2x 2x 2x 2x 2x                                                                                                       2x                     2x                                                                                                                   2x 2x 2x               2x                                                                                                                     2x   2x  
import _ from 'lodash';
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import {
	omitProps,
	StandardProps,
	Overwrite,
} from '../../util/component-types';
 
const cx = lucidClassNames.bind('&-Icon');
 
const { any, string, number, bool, func, oneOf, oneOfType } = PropTypes;
 
export enum Color {
	'neutral-dark' = 'neutral-dark',
	'neutral-light' = 'neutral-light',
	primary = 'primary',
	white = 'white',
	success = 'success',
	warning = 'warning',
	'secondary-one' = 'secondary-one',
	'secondary-two' = 'secondary-two',
	'secondary-three' = 'secondary-three',
}
 
export interface IIconPropsRaw extends StandardProps {
	/** Size variations of the icons. `size` directly effects height and width but the developer should also be conscious of the relationship with `viewBox`. */
	size: number;
 
	/** Size handles width and height, whereas `width` can manually override the width that would be set by size. */
	width?: number | string;
 
	/** Size handles width and height, whereas `height` can manually override the height that would be set by size. */
	height?: number | string;
 
	/** `viewBox` is very important for SVGs. You can think of `viewBox` as the "artboard" for our SVG while `size` is the presented height and width. */
	viewBox: string;
 
	/** Any valid SVG aspect ratio. */
	aspectRatio: string;
 
	/** Adds styling that makes the icon appear clickable. */
	isClickable: boolean;
 
	/** Adds styling that makes the icon appear disabled. Also forces isClickable to be false. */
	isDisabled?: boolean;
 
	/** Called when the user clicks the `Icon`. */
	onClick: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IIconProps;
	}) => void;
 
	/** Called when the user clicks an active, clickable `Icon`. */
	onSelect: ({
		event,
		props,
	}: {
		event: React.MouseEvent;
		props: IIconProps;
	}) => void;
 
	/** Sets the color of the Icon. May not be applicable for icons that are tied to specific colors (e.g. DangerIcon). */
	color: keyof typeof Color;
}
 
export type IIconProps = Overwrite<
	React.HTMLProps<SVGSVGElement>,
	IIconPropsRaw
>;
 
const defaultProps = {
	size: 16,
	aspectRatio: 'xMidYMid meet',
	viewBox: '0 0 16 16',
	isDisabled: false,
	isClickable: false,
	color: Color.primary,
	onClick: _.noop,
	onSelect: _.noop,
};
 
export const Icon = (props: IIconProps): React.ReactElement => {
	const {
		className,
		children,
		color,
		size,
		width,
		height,
		viewBox,
		aspectRatio,
		isClickable,
		isDisabled,
		onClick,
		onSelect,
		...passThroughs
	} = props;
 
	const svgRef = React.createRef<SVGSVGElement>();
 
	function handleClick(event: React.MouseEvent): void {
		onClick({ event, props: props });
 
		if (isClickable && !isDisabled) {
			onSelect({ event, props: props });
			if (svgRef.current) {
				svgRef.current.focus();
			}
		}
	}
 
	return (
		<svg
			width={width ? width : size}
			height={height ? height : size}
			viewBox={viewBox}
			preserveAspectRatio={aspectRatio}
			{...omitProps<IIconProps>(
				passThroughs,
				undefined,
				_.keys(Icon.propTypes)
			)}
			className={cx(
				'&',
				{
					[`&-color-${color}`]: true,
					'&-is-clickable': !isDisabled && isClickable,
					'&-is-disabled': isDisabled,
				},
				className
			)}
			ref={svgRef}
			onClick={handleClick}
		>
			{children}
		</svg>
	);
};
 
Icon.displayName = 'Icon';
Icon.defaultProps = defaultProps;
Icon.peek = {
	description: `
		A basic svg icon. Any props that are not explicitly called out below
		will be passed through to the native \`svg\` component.
	`,
	categories: ['visual design', 'icons'],
};
 
export const propTypes = {
	className: any`
		Classes that are appended to the component defaults. This prop is run
		through the \`classnames\` library.
	`,
 
	size: number`
		Size variations of the icons. \`size\` directly effects height and width
		but the developer should also be conscious of the relationship with
		\`viewBox\`.
	`,
 
	width: oneOfType([number, string])`
		Size handles width and height, whereas \`width\` can manually override the width that would be set by size.
	`,
 
	height: oneOfType([number, string])`
		Size handles width and height, whereas \`height\` can manually override the height that would be set by size.
	`,
 
	viewBox: string`
		\`viewBox\` is very important for SVGs. You can think of \`viewBox\` as
		the "artboard" for our SVG while \`size\` is the presented height and
		width.
	`,
 
	aspectRatio: string`
		Any valid SVG aspect ratio.
	`,
 
	isClickable: bool`
		Adds styling that makes the icon appear clickable.
	`,
 
	isDisabled: bool`
		Adds styling that makes the icon appear disabled.  Also forces
		isClickable to be false.
	`,
 
	onClick: func`
		Called when the user clicks the \`Icon\`. Signature:
		\`({event, props}) => {}\`
	`,
 
	onSelect: func`
		Called when the user clicks an active, clickable \`Icon\`. Signature:
		\`({event, props}) => {}\`
	`,
 
	children: any`
		Any valid React children.
	`,
 
	color: oneOf(_.values(Color))`
		Sets the color of the Icon.  May not be applicable for icons that are tied
		to specific colors (e.g. DangerIcon).
	`,
};
 
Icon.propTypes = propTypes;
 
export default Icon;