-
-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[docs] Code block scroll improvements (#1187)
- Loading branch information
Showing
8 changed files
with
188 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import * as React from 'react'; | ||
import * as BaseDemo from 'docs/src/blocks/Demo'; | ||
import { Collapsible } from '@base-ui-components/react/collapsible'; | ||
import * as ScrollArea from '../ScrollArea'; | ||
|
||
interface DemoCodeBlockProps { | ||
collapsibleOpen: boolean; | ||
/** How many lines should the code block have to get collapsed instead of rendering fully */ | ||
collapsibleLinesThreshold?: number; | ||
/** When compact, we don't show a preview of the collapse code */ | ||
compact: boolean; | ||
} | ||
|
||
function Root(props: React.ComponentProps<typeof ScrollArea.Root>) { | ||
return ( | ||
<ScrollArea.Root | ||
{...props} | ||
className="DemoCodeBlockRoot" | ||
tabIndex={-1} | ||
onKeyDown={(event: React.KeyboardEvent) => { | ||
if ( | ||
event.key === 'a' && | ||
(event.metaKey || event.ctrlKey) && | ||
!event.shiftKey && | ||
!event.altKey | ||
) { | ||
event.preventDefault(); | ||
window.getSelection()?.selectAllChildren(event.currentTarget); | ||
} | ||
}} | ||
/> | ||
); | ||
} | ||
|
||
export function DemoCodeBlock({ | ||
compact, | ||
collapsibleOpen, | ||
collapsibleLinesThreshold = 12, | ||
}: DemoCodeBlockProps) { | ||
const demoContext = React.useContext(BaseDemo.DemoContext); | ||
|
||
if (!demoContext) { | ||
throw new Error('Demo.Playground must be used within a Demo.Root'); | ||
} | ||
|
||
const { selectedFile } = demoContext; | ||
const lineBreaks = selectedFile.content.match(/\n/g) ?? []; | ||
|
||
if (lineBreaks.length < collapsibleLinesThreshold) { | ||
return ( | ||
<Root> | ||
<ScrollArea.Viewport> | ||
<BaseDemo.SourceBrowser className="DemoSourceBrowser" /> | ||
</ScrollArea.Viewport> | ||
<ScrollArea.Corner /> | ||
<ScrollArea.Scrollbar orientation="vertical" /> | ||
<ScrollArea.Scrollbar orientation="horizontal" /> | ||
</Root> | ||
); | ||
} | ||
|
||
return ( | ||
<React.Fragment> | ||
<Root | ||
render={ | ||
<Collapsible.Panel | ||
keepMounted={compact ? undefined : true} | ||
hidden={compact ? undefined : false} | ||
/> | ||
} | ||
> | ||
<ScrollArea.Viewport | ||
aria-hidden={!collapsibleOpen} | ||
data-closed={collapsibleOpen ? undefined : ''} | ||
className="DemoCodeBlockViewport" | ||
{...(!collapsibleOpen && { tabIndex: undefined, style: { overflow: undefined } })} | ||
> | ||
<BaseDemo.SourceBrowser className="DemoSourceBrowser" /> | ||
</ScrollArea.Viewport> | ||
|
||
{collapsibleOpen && ( | ||
<React.Fragment> | ||
<ScrollArea.Corner /> | ||
<ScrollArea.Scrollbar orientation="vertical" /> | ||
<ScrollArea.Scrollbar orientation="horizontal" /> | ||
</React.Fragment> | ||
)} | ||
</Root> | ||
|
||
<Collapsible.Trigger className="DemoCollapseButton"> | ||
{collapsibleOpen ? 'Hide' : 'Show'} code | ||
</Collapsible.Trigger> | ||
</React.Fragment> | ||
); | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.