UNPKG

@redocly/theme

Version:

Shared UI components lib

123 lines (118 loc) 4.86 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CodeBlockTabs = CodeBlockTabs; const react_1 = __importStar(require("react")); const styled_components_1 = __importStar(require("styled-components")); const hooks_1 = require("../../core/hooks"); const Button_1 = require("../../components/Button/Button"); const DocumentBlankIcon_1 = require("../../icons/DocumentBlankIcon/DocumentBlankIcon"); const ChevronLeftIcon_1 = require("../../icons/ChevronLeftIcon/ChevronLeftIcon"); const ChevronRightIcon_1 = require("../../icons/ChevronRightIcon/ChevronRightIcon"); function CodeBlockTabs({ tabs }) { const containerRef = (0, react_1.useRef)(null); const tabRefs = (0, react_1.useRef)([]); const { showControls, handlePrevTab, handleNextTab } = (0, hooks_1.useCodeBlockTabsControls)({ tabs, containerRef, tabRefs, }); (0, react_1.useEffect)(() => { const activeTab = tabRefs.current.find((tab) => (tab === null || tab === void 0 ? void 0 : tab.dataset.name) === tabs.activeTabName); if (activeTab) { activeTab.scrollIntoView({ block: 'nearest', inline: 'center' }); } }, [tabs.activeTabName]); return (react_1.default.createElement(CodeBlockTabsWrapper, { ref: containerRef, "data-component-name": "CodeBlock/CodeBlockTabs" }, react_1.default.createElement(ShadowWrapper, null, react_1.default.createElement(Tabs, null, tabs.files.map(({ name }, i) => (react_1.default.createElement(Tab, { ref: (el) => { tabRefs.current[i] = el; }, "data-name": name, active: name === tabs.activeTabName, key: name, onClick: () => tabs.handleTabSwitch(name) }, react_1.default.createElement(DocumentBlankIcon_1.DocumentBlankIcon, null), name))))), showControls && (react_1.default.createElement(Controls, null, react_1.default.createElement(ControlButton, { size: "small", onClick: handlePrevTab, "data-testid": "prev-button" }, react_1.default.createElement(ChevronLeftIcon_1.ChevronLeftIcon, null)), react_1.default.createElement(ControlButton, { size: "small", onClick: handleNextTab, "data-testid": "next-button" }, react_1.default.createElement(ChevronRightIcon_1.ChevronRightIcon, null)))))); } const CodeBlockTabsWrapper = styled_components_1.default.div ` display: flex; overflow: hidden; padding: var(--spacing-xxs) 0; `; const Controls = styled_components_1.default.div ` display: flex; gap: calc(var(--spacing-xxs) / 2); `; const ControlButton = (0, styled_components_1.default)(Button_1.Button) ` padding: 0 calc(var(--spacing-xxs) / 2); & + & { margin-left: 0; } `; const ShadowWrapper = styled_components_1.default.div ` position: relative; overflow: hidden; :after { position: absolute; content: ''; width: 16px; height: 100%; right: 0; top: 0; background: var(--bg-raised-gradient); } `; const Tabs = styled_components_1.default.div ` display: flex; overflow-x: auto; padding-right: var(--spacing-base); &::-webkit-scrollbar { display: none; } `; const Tab = styled_components_1.default.button ` display: inline-flex; align-items: center; padding: 0 var(--spacing-sm); background-color: transparent; height: 24px; border-radius: var(--border-radius); cursor: pointer; gap: var(--spacing-xs); color: var(--text-color-secondary); ${({ active }) => active ? (0, styled_components_1.css) ` color: var(--text-color-primary); background-color: var(--tab-bg-color-filled); ` : (0, styled_components_1.css) ` &:hover { color: var(--text-color-primary); } `} `; //# sourceMappingURL=CodeBlockTabs.js.map