UNPKG

lucid-ui

Version:

A UI component library from AppNexus.

106 lines 3.86 kB
import _ from 'lodash'; import React from 'react'; import PropTypes from 'react-peek/prop-types'; import { lucidClassNames } from '../../util/style-helpers'; import { omitProps, } from '../../util/component-types'; const cx = lucidClassNames.bind('&-Icon'); const { any, string, number, bool, func, oneOf, oneOfType } = PropTypes; export var Color; (function (Color) { Color["neutral-dark"] = "neutral-dark"; Color["neutral-light"] = "neutral-light"; Color["primary"] = "primary"; Color["white"] = "white"; Color["success"] = "success"; Color["warning"] = "warning"; Color["secondary-one"] = "secondary-one"; Color["secondary-two"] = "secondary-two"; Color["secondary-three"] = "secondary-three"; })(Color || (Color = {})); 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) => { const { className, children, color, size, width, height, viewBox, aspectRatio, isClickable, isDisabled, onClick, onSelect, ...passThroughs } = props; const svgRef = React.createRef(); function handleClick(event) { onClick && onClick({ event, props: props }); if (isClickable && !isDisabled) { onSelect && onSelect({ event, props: props }); if (svgRef.current) { svgRef.current.focus(); } } } return (React.createElement("svg", Object.assign({ width: width ? width : size, height: height ? height : size, viewBox: viewBox, preserveAspectRatio: aspectRatio }, omitProps(passThroughs, undefined, _.keys(Icon.propTypes)), { className: cx('&', { [`&-color-${color}`]: true, '&-is-clickable': !isDisabled && isClickable, '&-is-disabled': isDisabled, }, className), ref: svgRef, onClick: handleClick }), children)); }; 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; //# sourceMappingURL=Icon.js.map