/* * Copyright (c) Facebook, Inc. and its affiliates. */ import * as React from 'react'; import cn from 'classnames'; import tailwindConfig from '../../../tailwind.config'; import CodeBlock from './CodeBlock'; interface APIAnatomyProps { children: React.ReactNode; } interface AnatomyContent { steps: React.ReactElement[]; code: React.ReactNode; } const twColors = tailwindConfig.theme.extend.colors; const colors = [ { hex: twColors['blue-40'], border: 'border-blue-40', background: 'bg-blue-40', }, { hex: twColors['yellow-40'], border: 'border-yellow-40', background: 'bg-yellow-40', }, { hex: twColors['green-50'], border: 'border-green-50', background: 'bg-green-50', }, { hex: twColors['purple-40'], border: 'border-purple-40', background: 'bg-purple-40', }, ]; export function APIAnatomy({children}: APIAnatomyProps) { const [activeStep, setActiveStep] = React.useState(null); const ref = React.useRef(); const {steps, code} = React.Children.toArray(children).reduce( (acc: AnatomyContent, child) => { if (!React.isValidElement(child)) return acc; switch (child.props.mdxType) { case 'AnatomyStep': acc.steps.push( React.cloneElement(child, { ...child.props, activeStep, handleStepChange: setActiveStep, stepNumber: acc.steps.length + 1, }) ); break; case 'pre': acc.code = ( ); break; } return acc; }, {steps: [], code: null} ); return (
{code}
{steps}
); } interface AnatomyStepProps { children: React.ReactNode; title: string; stepNumber: number; activeStep?: number; handleStepChange: (stepNumber: number) => void; } export function AnatomyStep({ title, stepNumber, children, activeStep, handleStepChange, }: AnatomyStepProps) { const color = colors[stepNumber - 1]; return (
{stepNumber}
{title}
{children}
); }