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