UNPKG

@primer/components

Version:
308 lines (285 loc) • 11.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Button = void 0; var _react = _interopRequireWildcard(require("react")); var _Box = _interopRequireDefault(require("../Box")); var _styledComponents = _interopRequireDefault(require("styled-components")); var _sx = _interopRequireWildcard(require("../sx")); var _ThemeProvider = require("../ThemeProvider"); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function (nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; } 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); } const TEXT_ROW_HEIGHT = '20px'; // custom value off the scale const getVariantStyles = (variant = 'default', theme) => { const style = { default: { color: 'btn.text', backgroundColor: 'btn.bg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.shadow}, ${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.insetShadow}`, '&:hover:not([disabled])': { backgroundColor: 'btn.hoverBg' }, // focus must come before :active so that the active box shadow overrides '&:focus:not([disabled])': { boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.focusShadow}` }, '&:active:not([disabled])': { backgroundColor: 'btn.selectedBg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.shadowActive}` }, '&:disabled': { color: 'primer.fg.disabled', backgroundColor: 'btn.disabledBg' } }, primary: { color: 'btn.primary.text', backgroundColor: 'btn.primary.bg', borderColor: 'border.subtle', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.primary.shadow}`, '&:hover:not([disabled])': { color: 'btn.primary.hoverText', backgroundColor: 'btn.primary.hoverBg' }, // focus must come before :active so that the active box shadow overrides '&:focus:not([disabled])': { boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.primary.focusShadow}` }, '&:active:not([disabled])': { backgroundColor: 'btn.primary.selectedBg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.primary.selectedShadow}` }, '&:disabled': { color: 'btn.primary.disabledText', backgroundColor: 'btn.primary.disabledBg' }, '[data-component="ButtonCounter"]': { backgroundColor: 'btn.primary.counterBg', color: 'btn.primary.text' } }, danger: { color: 'btn.danger.text', backgroundColor: 'btn.bg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.shadow}`, '&:hover:not([disabled])': { color: 'btn.danger.hoverText', backgroundColor: 'btn.danger.hoverBg', borderColor: 'btn.danger.hoverBorder', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.danger.hoverShadow}`, '[data-component="ButtonCounter"]': { backgroundColor: 'btn.danger.hoverCounterBg', color: 'btn.danger.hoverText' } }, // focus must come before :active so that the active box shadow overrides '&:focus:not([disabled])': { borderColor: 'btn.danger.focusBorder', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.danger.focusShadow}` }, '&:active:not([disabled])': { color: 'btn.danger.selectedText', backgroundColor: 'btn.danger.selectedBg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.danger.selectedShadow}`, borderColor: 'btn.danger.selectedBorder' }, '&:disabled': { color: 'btn.danger.disabledText', backgroundColor: 'btn.danger.disabledBg', borderColor: 'btn.danger.disabledBorder', '[data-component="ButtonCounter"]': { backgroundColor: 'btn.danger.disabledCounterBg' } }, '[data-component="ButtonCounter"]': { color: 'btn.danger.text', backgroundColor: 'btn.danger.counterBg' } }, invisible: { color: 'accent.fg', backgroundColor: 'transparent', border: '0', boxShadow: 'none', '&:hover:not([disabled])': { backgroundColor: 'btn.hoverBg' }, // focus must come before :active so that the active box shadow overrides '&:focus:not([disabled])': { boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.focusShadow}` }, '&:active:not([disabled])': { backgroundColor: 'btn.selectedBg' }, '&:disabled': { color: 'primer.fg.disabled' } }, outline: { color: 'btn.outline.text', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.shadow}`, '&:hover': { color: 'btn.outline.hoverText', backgroundColor: 'btn.outline.hoverBg', borderColor: 'outline.hoverBorder', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.outline.hoverShadow}`, '[data-component="ButtonCounter"]': { backgroundColor: 'btn.outline.hoverCounterBg', color: 'btn.outline.hoverText' } }, // focus must come before :active so that the active box shadow overrides '&:focus': { borderColor: 'btn.outline.focusBorder', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.outline.focusShadow}` }, '&:active:not([disabled])': { color: 'btn.outline.selectedText', backgroundColor: 'btn.outline.selectedBg', boxShadow: `${theme === null || theme === void 0 ? void 0 : theme.shadows.btn.outline.selectedShadow}`, borderColor: 'btn.outline.selectedBorder' }, '&:disabled': { color: 'btn.outline.disabledText', backgroundColor: 'btn.outline.disabledBg', borderColor: 'btn.border', '[data-component="ButtonCounter"]': { backgroundColor: 'btn.outline.disabledCounterBg' } }, '[data-component="ButtonCounter"]': { backgroundColor: 'btn.outline.counterBg', color: 'btn.outline.text' } } }; return style[variant]; }; const getSizeStyles = (size = 'medium', variant = 'default', iconOnly) => { let paddingY, paddingX, fontSize; switch (size) { case 'small': paddingY = 3; paddingX = 12; fontSize = 0; break; case 'large': paddingY = 9; paddingX = 20; fontSize = 2; break; case 'medium': default: paddingY = 5; paddingX = 16; fontSize = 1; } if (iconOnly) { paddingX = paddingY + 2; } if (variant === 'invisible') { paddingY = paddingY + 1; } return { paddingY: `${paddingY}px`, paddingX: `${paddingX}px`, fontSize, '[data-component="ButtonCounter"]': { fontSize } }; }; const ButtonBase = _styledComponents.default.button.withConfig({ displayName: "button__ButtonBase", componentId: "sc-15k5iqk-0" })(_sx.default); const Button = /*#__PURE__*/(0, _react.forwardRef)(({ children, sx: sxProp = {}, ...props }, forwardedRef) => { const { icon: Icon, leadingIcon: LeadingIcon, trailingIcon: TrailingIcon, variant = 'default', size = 'medium' } = props; const iconOnly = !!Icon; const { theme } = (0, _ThemeProvider.useTheme)(); const styles = { borderRadius: '2', border: '1px solid', borderColor: theme === null || theme === void 0 ? void 0 : theme.colors.btn.border, display: 'grid', gridTemplateAreas: '"leadingIcon text trailingIcon"', fontWeight: 'bold', lineHeight: TEXT_ROW_HEIGHT, whiteSpace: 'nowrap', verticalAlign: 'middle', cursor: 'pointer', appearance: 'none', userSelect: 'none', textDecoration: 'none', textAlign: 'center', '& > :not(:last-child)': { mr: '2' }, '&:focus': { outline: 'none' }, '&:disabled': { cursor: 'default' }, '&:disabled svg': { opacity: '0.6' }, '[data-component="leadingIcon"]': { gridArea: 'leadingIcon' }, '[data-component="text"]': { gridArea: 'text' }, '[data-component="trailingIcon"]': { gridArea: 'trailingIcon' } }; const iconWrapStyles = { display: 'inline-block' }; const sxStyles = _sx.merge.all([styles, getSizeStyles(size, variant, iconOnly), getVariantStyles(variant, theme), sxProp]); return /*#__PURE__*/_react.default.createElement(ButtonBase, _extends({ sx: sxStyles, ref: forwardedRef }, props), LeadingIcon && /*#__PURE__*/_react.default.createElement(_Box.default, { as: "span", "data-component": "leadingIcon", sx: iconWrapStyles, "aria-hidden": !iconOnly }, /*#__PURE__*/_react.default.createElement(LeadingIcon, null)), /*#__PURE__*/_react.default.createElement("span", { "data-component": "text", hidden: Icon ? true : false }, children), Icon && /*#__PURE__*/_react.default.createElement(_Box.default, { "data-component": "icon-only", as: "span", sx: { display: 'inline-block' }, "aria-hidden": !iconOnly }, /*#__PURE__*/_react.default.createElement(Icon, null)), TrailingIcon && /*#__PURE__*/_react.default.createElement(_Box.default, { as: "span", "data-component": "trailingIcon", sx: { ...iconWrapStyles, ml: 2 }, "aria-hidden": !iconOnly }, /*#__PURE__*/_react.default.createElement(TrailingIcon, null))); }); exports.Button = Button; Button.displayName = 'Button'; Object.assign(Button, {});