UNPKG

@cimpress/react-components

Version:
134 lines (132 loc) 4.41 kB
var __rest = (this && this.__rest) || function (s, e) { var t = {}; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0) t[p] = s[p]; if (s != null && typeof Object.getOwnPropertySymbols === "function") for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) { if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i])) t[p[i]] = s[p[i]]; } return t; }; import React, { useEffect, useState } from 'react'; import { cx, css } from '@emotion/css'; import cvar from './theme/cvar'; const cardTabNavCss = css ` // override for docusaurus css li + li { margin: 0; } border-radius: inherit; list-style: none; padding: 0; display: flex; justify-content: space-between; `; const navLinkCss = css ` color: ${cvar('color-tabcard-text-selected')}; &:hover { background: ${cvar('color-hover')}; } `; const navItemActiveCss = css ` background: ${cvar('color-background')}; color: ${cvar('color-tabcard-text-selected')}; > a { ${navLinkCss} } `; const navItemCss = css ` background-color: ${cvar('color-nav-item-background')}; border-right: 1px solid ${cvar('color-background')}; display: inline-block; flex-basis: 0; flex-grow: 1; text-align: center; cursor: pointer; > a { color: ${cvar('color-nav-text')}; position: relative; display: flex; flex-direction: column; flex: 1 1 auto; padding: ${cvar('spacing-8')} ${cvar('spacing-16')}; &:hover { text-decoration: none; } } &.active { ${navItemActiveCss} } &:hover { background-color: ${cvar('color-hover')}; color: ${cvar('color-text-interactive')}; } &:first-of-type { border-top-left-radius: inherit; } &:last-of-type { border-top-right-radius: inherit; border-right: none; } `; const cardFooterCss = css ` padding: ${cvar('spacing-8')} ${cvar('spacing-16')}; background-color: ${cvar('color-background-light')}; border-top: 1px solid ${cvar('color-border-light')}; text-align: left; cursor: default; `; const cardBlockCss = css ` position: relative; display: flex; flex-direction: column; background-color: ${cvar('color-background')}; flex: 1 1 auto; padding: ${cvar('spacing-16')}; text-align: left; cursor: default; `; const tabCardCss = css ` border: 1px solid ${cvar('color-border-default')}; border-radius: ${cvar('spacing-4')}; width: 100%; `; /** * A Component implementing a card with internal tabs in the header */ export const TabCard = (_a) => { var { selectedIndex, onSelect = () => { }, tabs = [] } = _a, rest = __rest(_a, ["selectedIndex", "onSelect", "tabs"]); const initialSelectedIndex = selectedIndex != null && selectedIndex >= 0 ? selectedIndex : 0; const [localSelectedIndex, setLocalSelectedIndex] = useState(initialSelectedIndex); const onTabSelect = (e, selectedKey) => { e.preventDefault(); setLocalSelectedIndex(selectedKey); if (onSelect) { onSelect(e, selectedKey); } }; useEffect(() => { if (selectedIndex != null && selectedIndex >= 0) { setLocalSelectedIndex(selectedIndex); } }, [setLocalSelectedIndex, selectedIndex]); const renderTab = (tabInfo, tabIndex) => { var _a; return (React.createElement("li", { key: tabIndex, className: cx(navItemCss, { active: tabIndex === localSelectedIndex }) }, React.createElement("a", { href: (_a = tabInfo.href) !== null && _a !== void 0 ? _a : undefined, className: navLinkCss, onClick: e => onTabSelect(e, tabIndex) }, React.createElement("strong", null, tabInfo.name)))); }; const renderTabNav = () => React.createElement("ul", { className: cardTabNavCss }, tabs.map(renderTab)); const renderFooter = () => { if (tabs[localSelectedIndex].footer) { return React.createElement("div", { className: cardFooterCss }, tabs[localSelectedIndex].footer); } return null; }; return (React.createElement("div", Object.assign({ className: cx('crc-tab-card', tabCardCss) }, rest), React.createElement("div", null, renderTabNav()), React.createElement("div", { className: cardBlockCss }, tabs[localSelectedIndex].block), renderFooter())); }; //# sourceMappingURL=TabCard.js.map