({
code,
hideCodePreview = false,
readOnly,
name,
developmentEditor,
}: Props)
| 28 | }; |
| 29 | |
| 30 | export default function ExampleCode({ |
| 31 | code, |
| 32 | hideCodePreview = false, |
| 33 | readOnly, |
| 34 | name, |
| 35 | developmentEditor, |
| 36 | }: Props) { |
| 37 | const { devExampleMode } = useAppContext(); |
| 38 | |
| 39 | const [expanded, setExpanded] = useState(developmentEditor); |
| 40 | const [showExpandButton, setShowExpandButton] = useState(hideCodePreview); |
| 41 | const [maxHeight, setMaxHeight] = useState('500px'); |
| 42 | const codeExampleRef = useRef<null | HTMLDivElement>(null); |
| 43 | const codeBoxMinHeight = hideCodePreview ? undefined : '152px'; |
| 44 | let containerBoxMaxHeight; |
| 45 | |
| 46 | // If an example is expanded, maxHeight should be the full code's height |
| 47 | // If !expanded, code blocks that hide the preview should have no min height |
| 48 | // All others have small min height |
| 49 | if (expanded) { |
| 50 | containerBoxMaxHeight = maxHeight; |
| 51 | } else if (hideCodePreview) { |
| 52 | containerBoxMaxHeight = '0'; |
| 53 | } else { |
| 54 | containerBoxMaxHeight = CODE_EXAMPLE_HEIGHT; |
| 55 | } |
| 56 | |
| 57 | useEffect(() => { |
| 58 | const height = codeExampleRef?.current?.clientHeight ?? 0; |
| 59 | |
| 60 | // Save the height so we know how far to animate to |
| 61 | setMaxHeight(`${height}px`); |
| 62 | |
| 63 | if (height > CODE_EXAMPLE_HEIGHT && devExampleMode === 'default') { |
| 64 | setExpanded(false); |
| 65 | setShowExpandButton(true); |
| 66 | } |
| 67 | }, [code, hideCodePreview, devExampleMode]); |
| 68 | |
| 69 | return ( |
| 70 | <Box marginTop={2}> |
| 71 | <Flex |
| 72 | direction="column" |
| 73 | gap={{ |
| 74 | row: 0, |
| 75 | column: 2, |
| 76 | }} |
| 77 | > |
| 78 | <Flex |
| 79 | alignItems="center" |
| 80 | gap={{ |
| 81 | row: 2, |
| 82 | column: 0, |
| 83 | }} |
| 84 | justifyContent="between" |
| 85 | > |
| 86 | <Flex justifyContent="start"> |
| 87 | <OpenSandboxButton |
nothing calls this directly
no test coverage detected