lucid-ui
Version:
A UI component library from AppNexus.
111 lines (96 loc) • 5.2 kB
JavaScript
import _keys from "lodash/keys";
import _noop from "lodash/noop";
function _extends() { _extends = Object.assign || function (target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]; for (var key in source) { if (Object.prototype.hasOwnProperty.call(source, key)) { target[key] = source[key]; } } } return target; }; return _extends.apply(this, arguments); }
function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter); }
function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
function _objectWithoutProperties(source, excluded) { if (source == null) return {}; var target = _objectWithoutPropertiesLoose(source, excluded); var key, i; if (Object.getOwnPropertySymbols) { var sourceSymbolKeys = Object.getOwnPropertySymbols(source); for (i = 0; i < sourceSymbolKeys.length; i++) { key = sourceSymbolKeys[i]; if (excluded.indexOf(key) >= 0) continue; if (!Object.prototype.propertyIsEnumerable.call(source, key)) continue; target[key] = source[key]; } } return target; }
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
import React from 'react';
import PropTypes from 'react-peek/prop-types';
import { lucidClassNames } from '../../util/style-helpers';
import { omitProps } from '../../util/component-types';
var cx = lucidClassNames.bind('&-Button');
var arrayOf = PropTypes.arrayOf,
bool = PropTypes.bool,
func = PropTypes.func,
node = PropTypes.node,
oneOf = PropTypes.oneOf,
oneOfType = PropTypes.oneOfType,
string = PropTypes.string;
var defaultProps = {
isDisabled: false,
isActive: false,
onClick: _noop,
type: 'button',
hasOnlyIcon: false
};
/** Test Button description */
export var Button = function Button(props) {
var isDisabled = props.isDisabled,
isActive = props.isActive,
onClick = props.onClick,
hasOnlyIcon = props.hasOnlyIcon,
kind = props.kind,
size = props.size,
className = props.className,
children = props.children,
type = props.type,
passThroughs = _objectWithoutProperties(props, ["isDisabled", "isActive", "onClick", "hasOnlyIcon", "kind", "size", "className", "children", "type"]);
var buttonRef = /*#__PURE__*/React.createRef();
function handleClick(event) {
if (!isDisabled) {
// required to correctly apply the focus state in Safari and Firefox
// (still valid 2019-07-22)
if (buttonRef.current) {
buttonRef.current.focus();
}
onClick({
event: event,
props: props
});
}
}
return /*#__PURE__*/React.createElement("button", _extends({}, omitProps(passThroughs, undefined, [].concat(_toConsumableArray(_keys(Button.propTypes)), ['callbackId'])), {
ref: buttonRef,
className: cx('&', {
'&-is-disabled': isDisabled,
'&-is-active': isActive,
'&-primary': kind === 'primary',
'&-link': kind === 'link',
'&-invisible': kind === 'invisible',
'&-danger': kind === 'danger',
'&-short': size === 'short',
'&-small': size === 'small',
'&-large': size === 'large',
'&-has-only-icon': hasOnlyIcon
}, className),
onClick: handleClick,
disabled: isDisabled,
type: type
}), /*#__PURE__*/React.createElement("span", {
className: cx('&-content')
}, children));
};
Button.defaultProps = defaultProps;
Button.displayName = 'Button';
Button.peek = {
description: "\n\t\tA basic button. Any props that are not explicitly called out below will\n\t\tbe passed through to the native `button` component.\n\t",
categories: ['controls', 'buttons']
};
Button.propName = 'Button';
Button.propTypes = {
isDisabled: bool,
isActive: bool,
className: string,
hasOnlyIcon: bool,
children: oneOfType([node, arrayOf(node)]),
kind: oneOf(['primary', 'link', 'danger', 'invisible']),
size: oneOf(['short', 'small', 'large']),
onClick: func,
type: string
};
export default Button;