All files / NotchedTag NotchedTag.tsx

100% Statements 19/19
100% Branches 4/4
100% Functions 1/1
100% Lines 19/19

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            2x   2x                                   2x 2x   2x                     2x         2x                                   2x         2x   18x   18x 18x     18x                   18x                 18x                                                                   2x 2x 2x                           2x                                                    
import _ from 'lodash';
import React from 'react';
import PropTypes from 'prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { StandardProps } from '../../util/component-types';
 
const { node, string, oneOf, object } = PropTypes;
 
const cx = lucidClassNames.bind('&-NotchedTag');
 
export enum TagStyle {
	'style-one' = 'style-one',
	'style-two' = 'style-two',
	'style-three' = 'style-three',
}
 
export enum Type {
	filled = 'filled',
	stroke = 'stroke',
}
 
export enum Size {
	large = 'large',
	small = 'small',
}
 
const NOTCH_HEIGHT_LARGE = 5;
const NOTCH_HEIGHT_SMALL = 4;
 
const SIZE_DIMENSION_MAP = {
	[Size.large]: {
		height: '24px',
		width: '40px',
	},
	[Size.small]: {
		height: '18px',
		width: '30px',
	},
};
 
const SIZE_NOTCH_MAP: { [key in Size]: number } = {
	[Size.large]: NOTCH_HEIGHT_LARGE,
	[Size.small]: NOTCH_HEIGHT_SMALL,
};
 
const STROKE_SIZE = '2px';
 
export interface INotchedTagProps
	extends StandardProps,
		React.DetailedHTMLProps<
			React.HTMLAttributes<HTMLDivElement>,
			HTMLDivElement
		> {
	/** Style variations. */
	type?: keyof typeof Type;
 
	/** Size variations. */
	size: keyof typeof Size;
 
	/** Tag style variations. */
	tagStyle: keyof typeof TagStyle;
}
 
const defaultProps = {
	size: Size.large,
	tagStyle: TagStyle['style-one'],
};
 
export const NotchedTag = (props: INotchedTagProps): React.ReactElement => {
	const { children, className, type, style, size, tagStyle, ...passThroughs } =
		props;
 
	const notchHeight = SIZE_NOTCH_MAP[size];
	const notchWidth = notchHeight * Math.sqrt(3); //we want to maintain a 60 degree slice (30,60,90 triangle)
 
	//clips off a corner of the element to create the notched effect
	const slicePolygon = `
		polygon(
			${notchHeight}px 0,
			100% 0,
			100% 100%,
			0 100%,
			0 ${notchWidth}px
		)`;
 
	//used for creating an inset element to create a stroke effect (instead of fill)
	const sliceInnerPolygon = `
		polygon(
			${notchHeight - 1}px 0,
			100% 0,
			100% 100%,
			0 100%,
			0 ${notchWidth - 1}px
		)`;
 
	return (
		<div
			className={cx(
				'&',
				`&-${tagStyle}`,
				`&-${size}`,
				type === Type.filled ? '&-no-border' : '',
				className
			)}
			{...passThroughs}
			style={{
				...style,
				...SIZE_DIMENSION_MAP[size],
				clipPath: slicePolygon,
			}}
		>
			<div
				className={cx(
					'&-container',
					type === Type.filled ? '&-container-filled' : ''
				)}
				style={{
					top: STROKE_SIZE,
					right: STROKE_SIZE,
					left: STROKE_SIZE,
					bottom: STROKE_SIZE,
					clipPath: sliceInnerPolygon,
				}}
			>
				<div className={cx('&-container-centered')}>{children}</div>
			</div>
		</div>
	);
};
NotchedTag.defaultProps = defaultProps;
NotchedTag.displayName = 'NotchedTag';
NotchedTag.propTypes = {
	/**
		Any valid React children.
	*/
	children: node,
	className: string,
	/**
		Style variations.
	*/
	type: oneOf(_.values(Type)),
	size: oneOf(_.values(Size)),
	tagStyle: oneOf(_.values(TagStyle)),
	style: object,
};
NotchedTag.peek = {
	description: `A banner that displays a prominent message.`,
	notes: {
		overview: `
			Notched tag helps users visually identify a high-priority object and its location in the object hierarchy.
		`,
		intendedUse: `
			Use \`NotchedTag\` in tables, page headers, and in the global search to help users way-find while monitoring their objects. View \`DetailsPageHeader\` in ANX-React to see an example of Notched tag in context.
								
			**Styling notes**
			
			- Use filled in Notched tags, \`type:"filled"\`, for the currently-viewed or highest-priority object. 
			- Use empty Notched tags, \`type:"stroke"\`, for the other objects associated with the viewed/priority object.
			- Use \`size:"small"\` in tables.
			- In page headers, use \`size:"large"\` \`type:"filled"\` for the highest-priority object, and \`size:"small"\` \`type:"stroke"\` for secondary objects.
		`,
		technicalRecommendations: `
			- \`tagStyle:style-one"\` is for Advertiser objects.
			- \`tagStyle:style-two"\` is for Insertion Order objects.
			- \`tagStyle:style-three"\` is for Line Item objects.
		`,
	},
	categories: ['visual design'],
};
 
export default NotchedTag;