@redocly/theme
Version:
Shared UI components lib
132 lines (128 loc) • 5.91 kB
JavaScript
;
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;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.StepWrapper = void 0;
exports.CodeStep = CodeStep;
const react_1 = __importStar(require("react"));
const styled_components_1 = __importDefault(require("styled-components"));
const contexts_1 = require("../../../core/contexts");
function CodeStep({ id, heading, stepKey, when, unless, children, }) {
const compRef = (0, react_1.useRef)(null);
const { areConditionsMet } = (0, react_1.useContext)(contexts_1.CodeWalkthroughControlsStateContext);
const { activeStep, setActiveStep, register, unregister, lockObserver, filtersElementRef } = (0, react_1.useContext)(contexts_1.CodeWalkthroughStepsContext);
const isActive = activeStep === id;
const [scrollMarginTop, setScrollMarginTop] = (0, react_1.useState)(0);
const handleActivateStep = () => {
if (lockObserver) {
lockObserver.current = true;
if (compRef.current) {
compRef.current.scrollIntoView({ behavior: 'smooth', block: 'start' });
}
setActiveStep(id);
setTimeout(() => {
lockObserver.current = false;
}, 1000);
}
};
(0, react_1.useEffect)(() => {
// If the step is active during first render, scroll to it
// This is to ensure that the step is visible when the page is loaded
if (isActive) {
// Ensure scrollMarginTop calculated before step being scrolled.
setTimeout(handleActivateStep, 5);
}
// Ignore dependency array because we only need to run this once
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
(0, react_1.useEffect)(() => {
var _a, _b;
const currentCompRef = compRef.current;
if (currentCompRef) {
currentCompRef
.querySelectorAll('a, button, input, textarea, select, [tabindex]')
.forEach((el) => {
el.setAttribute('tabindex', '-1');
});
register(currentCompRef);
}
const filtersElementHeight = ((_a = filtersElementRef === null || filtersElementRef === void 0 ? void 0 : filtersElementRef.current) === null || _a === void 0 ? void 0 : _a.clientHeight) || 0;
const navbarHeight = ((_b = document.querySelector('nav')) === null || _b === void 0 ? void 0 : _b.clientHeight) || 0;
setScrollMarginTop(filtersElementHeight + navbarHeight + 10);
return () => {
if (currentCompRef) {
unregister(currentCompRef);
}
};
}, [activeStep, register, unregister, filtersElementRef]);
if (!areConditionsMet({ when, unless })) {
if (isActive) {
setActiveStep(null);
}
return null;
}
return (react_1.default.createElement(exports.StepWrapper, { "data-component-name": "Markdoc/CodeWalkthrough/CodeStep", ref: compRef, isActive: isActive, scrollMarginTop: scrollMarginTop, "data-step-key": stepKey, "data-step-active": isActive, onClick: handleActivateStep, onFocus: handleActivateStep, tabIndex: 0, className: "code-step-wrapper" },
react_1.default.createElement(StepContent, { isActive: isActive },
heading ? react_1.default.createElement(StepHeading, null, heading) : null,
children)));
}
const StepContent = styled_components_1.default.div `
margin: var(--spacing-xs) 0px var(--spacing-xs) calc(var(--spacing-unit) * 3.5);
padding: var(--spacing-md) var(--spacing-lg);
background: ${({ isActive }) => (isActive ? 'var(--layer-color)' : 'none')};
border-radius: var(--border-radius);
&:hover {
background-color: ${({ isActive }) => isActive ? 'var(--code-step-bg-active-hover)' : 'var(--code-step-bg-hover)'};
}
`;
const StepHeading = styled_components_1.default.p `
font-weight: var(--font-weight-semibold);
`;
exports.StepWrapper = styled_components_1.default.div `
position: relative;
scroll-margin-top: ${({ scrollMarginTop }) => scrollMarginTop}px;
&::before {
content: '';
position: absolute;
width: 6px;
height: 100%;
background-color: ${({ isActive }) => isActive ? 'var(--code-step-vertical-line-bg-active)' : 'none'};
border-radius: var(--border-radius-lg);
}
&:hover::before {
background-color: ${({ isActive }) => isActive
? 'var(--code-step-vertical-line-bg-active)'
: 'var(--code-step-vertical-line-bg-hover)'};
}
&:hover::before {
width: ${({ isActive }) => (isActive ? '8px' : '6px')};
}
&:hover ${StepContent} {
background-color: ${({ isActive }) => isActive ? 'var(--code-step-bg-active-hover)' : 'var(--code-step-bg-hover)'};
}
`;
//# sourceMappingURL=CodeStep.js.map