UNPKG

@primer/components

Version:
75 lines (54 loc) 4.78 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = void 0; var _classnames = _interopRequireDefault(require("classnames")); var _react = _interopRequireWildcard(require("react")); var _styledComponents = _interopRequireWildcard(require("styled-components")); var _constants = require("../constants"); var _sx = _interopRequireDefault(require("../sx")); var _SelectMenuContext = require("./SelectMenuContext"); 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 _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } 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 tabStyles = (0, _styledComponents.css)(["flex:1;padding:", " ", ";font-size:", ";font-weight:500;color:", ";text-align:center;background-color:transparent;border:0;box-shadow:inset 0 -1px 0 ", ";@media (min-width:", "){flex:none;padding:", " ", ";border:", " solid transparent;border-bottom-width:0;border-top-left-radius:", ";border-top-right-radius:", ";}&[aria-selected='true']{z-index:1;color:", ";background-color:", ";box-shadow:0 0 0 1px ", ";@media (min-width:", "){border-color:", ";box-shadow:none;}}&:focus{background-color:", ";}"], (0, _constants.get)('space.2'), (0, _constants.get)('space.3'), (0, _constants.get)('fontSizes.0'), (0, _constants.get)('colors.fg.muted'), (0, _constants.get)('colors.border.muted'), (0, _constants.get)('breakpoints.0'), (0, _constants.get)('space.1'), (0, _constants.get)('space.3'), (0, _constants.get)('borderWidths.1'), (0, _constants.get)('radii.2'), (0, _constants.get)('radii.2'), (0, _constants.get)('colors.text-primary'), (0, _constants.get)('colors.canvas.overlay'), (0, _constants.get)('colors.border.muted'), (0, _constants.get)('breakpoints.0'), (0, _constants.get)('colors.border.muted'), (0, _constants.get)('colors.neutral.subtle')); const StyledTab = _styledComponents.default.button.withConfig({ displayName: "SelectMenuTab__StyledTab", componentId: "ga32sz-0" })(["", " ", " ", ";"], tabStyles, _constants.COMMON, _sx.default); const SelectMenuTab = ({ tabName = '', index, className, onClick, ...rest }) => { const menuContext = (0, _react.useContext)(_SelectMenuContext.MenuContext); const handleClick = e => { // if consumer has attached an onClick event, call it onClick && onClick(e); if (!e.defaultPrevented) { var _menuContext$setSelec; (_menuContext$setSelec = menuContext.setSelectedTab) === null || _menuContext$setSelec === void 0 ? void 0 : _menuContext$setSelec.call(menuContext, tabName); } }; // if no tab is selected when the component renders, show the first tab (0, _react.useEffect)(() => { if (!menuContext.selectedTab && index === 0) { var _menuContext$setSelec2; (_menuContext$setSelec2 = menuContext.setSelectedTab) === null || _menuContext$setSelec2 === void 0 ? void 0 : _menuContext$setSelec2.call(menuContext, tabName); } }, [index, menuContext, tabName]); const isSelected = menuContext.selectedTab === tabName; return /*#__PURE__*/_react.default.createElement(StyledTab, _extends({ role: "tab", className: (0, _classnames.default)('SelectMenuTab', className), "aria-selected": isSelected, onClick: handleClick }, rest), tabName); }; SelectMenuTab.displayName = "SelectMenuTab"; SelectMenuTab.displayName = 'SelectMenu.Tab'; var _default = SelectMenuTab; exports.default = _default;