MCPcopy Create free account
hub / github.com/adobe/react-spectrum / CodePlatter

Function CodePlatter

packages/dev/s2-docs/src/CodePlatter.tsx:103–268  ·  view source on GitHub ↗
({children, type, showCoachMark}: CodePlatterProps)

Source from the content-addressed store, hash-verified

101}
102
103export function CodePlatter({children, type, showCoachMark}: CodePlatterProps) {
104 let codeRef = useRef<HTMLDivElement | null>(null);
105 let [showShadcn, setShowShadcn] = useState(false);
106 // let [showCodeSandbox, setShowCodeSandbox] = useState(false);
107 let getText = () => getTextContent(codeRef.current!.querySelector('pre')!);
108 let {library} = useContext(CodePlatterContext);
109 if (!type) {
110 if (library === 'react-aria') {
111 // oxlint-disable-next-line react/react-compiler
112 type = 'vanilla';
113 } else if (library === 'react-spectrum') {
114 type = 's2';
115 }
116 }
117
118 let {files, deps = {}, urls = {}, entry} = useContext(FileProviderContext) ?? {};
119 let shadcn = useContext(ShadcnContext);
120 let shareUrl = useContext(ShareContext);
121
122 return (
123 <div className={platterStyle}>
124 <Toolbar showCoachMark={showCoachMark}>
125 <ActionButtonGroup orientation="vertical" isQuiet density="regular" size="S">
126 <CopyButton ariaLabel="Copy code" tooltip="Copy code" getText={getText} />
127 {(shareUrl || files || shadcn) && (
128 <MenuTrigger align="end">
129 <TooltipTrigger placement="end">
130 <ActionButton aria-label="Open in…">
131 <OpenIn />
132 </ActionButton>
133 <Tooltip>Open in…</Tooltip>
134 </TooltipTrigger>
135 <Menu hideLinkOutIcon>
136 {shareUrl && (
137 <MenuItem
138 onAction={() => {
139 // Find previous heading element to get hash.
140 let url = new URL(shareUrl, location.href);
141 let node: Element | null = codeRef.current;
142
143 // Search for the nearest heading by walking up the tree and checking previous siblings
144 while (node && node.tagName !== 'ARTICLE') {
145 // Check previous siblings
146 let sibling = node.previousElementSibling;
147 while (sibling) {
148 if (sibling instanceof HTMLHeadingElement) {
149 node = sibling;
150 break;
151 }
152 // Also check inside the sibling for headings
153 let headingInSibling = sibling.querySelector('h1, h2, h3, h4, h5, h6');
154 if (headingInSibling instanceof HTMLHeadingElement) {
155 node = headingInSibling;
156 break;
157 }
158 sibling = sibling.previousElementSibling;
159 }
160

Callers

nothing calls this directly

Calls 8

getCodeSandboxFilesFunction · 0.90
zipFunction · 0.90
createStackBlitzFunction · 0.90
createElementMethod · 0.80
appendChildMethod · 0.80
getExampleFilesFunction · 0.70
toStringMethod · 0.65
removeMethod · 0.65

Tested by

no test coverage detected