All files / Typography Typography.tsx

100% Statements 11/11
50% Branches 1/2
100% Functions 1/1
100% Lines 11/11

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              1x   1x                                             1x       1x 22x 22x   22x                   1x 1x 1x       1x                                      
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 cx = lucidClassNames.bind('&-Typography');
 
const { node, string, oneOf } = PropTypes;
 
export enum ElementTypes {
	p = 'p',
	tabular = 'p',
	h1 = 'h1',
	h2 = 'h2',
	h3 = 'h3',
	a = 'a',
	pre = 'pre',
	code = 'code',
	kbd = 'kbd',
	samp = 'samp',
	span = 'span',
}
 
export interface ITypographyProps
	extends StandardProps,
		React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement> {
	/** This prop defines the type of text that will be displayed. It may be an actual HTML element or something with extra semantic meaning. */
	variant: keyof typeof ElementTypes;
}
 
const defaultProps = {
	variant: ElementTypes.p,
};
 
export const Typography = (props: ITypographyProps) => {
	const { children, className, variant, ...passThroughs } = props;
	const Element = ElementTypes[variant ? variant : 'p'];
 
	return React.createElement(
		Element,
		{
			...passThroughs,
			className: cx('&', `&-${variant}`, className),
		},
		children
	);
};
 
Typography.defaultProps = defaultProps;
Typography.displayName = 'Typography';
Typography.peek = {
	description: `A general component for several types of textual \`HTML\` elements.`,
	categories: ['text'],
};
Typography.propTypes = {
	/**
		Can contain elements or nested \`Typography\` components.
	*/
	children: node,
 
	/**
		Appended to the component-specific class names set on the root element.
	*/
	className: string,
 
	/**
		This prop defines the type of text that will be displayed.
		It may be an actual HTML element or something with extra semantic meaning.
	*/
	variant: oneOf(_.keys(ElementTypes)).isRequired,
};
 
export default Typography;