MCPcopy Create free account
hub / github.com/pinterest/gestalt / ExampleCode

Function ExampleCode

docs/docs-components/ExampleCode.tsx:30–154  ·  view source on GitHub ↗
({
  code,
  hideCodePreview = false,
  readOnly,
  name,
  developmentEditor,
}: Props)

Source from the content-addressed store, hash-verified

28};
29
30export 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

Callers

nothing calls this directly

Calls 2

handleCodeSandboxFunction · 0.70
copyCodeFunction · 0.70

Tested by

no test coverage detected