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

Function CodePlatter

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

Source from the content-addressed store, hash-verified

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

Callers

nothing calls this directly

Calls 9

getParentElementFunction · 0.90
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