From 979bd27ff5feb4520033c82eaad788a01f245e29 Mon Sep 17 00:00:00 2001 From: Alexander Wang Date: Mon, 30 Sep 2024 11:48:06 -0600 Subject: [PATCH] add see more button --- docs/tour/grid-diagrams.md | 21 +++++++-------- src/theme/CodeBlock/CodeBlock.scss | 41 +++++++++++++++++++++++++++++- src/theme/CodeBlock/index.js | 19 +++++++++++++- 3 files changed, 67 insertions(+), 14 deletions(-) diff --git a/docs/tour/grid-diagrams.md b/docs/tour/grid-diagrams.md index 3966e8c..845f4fe 100644 --- a/docs/tour/grid-diagrams.md +++ b/docs/tour/grid-diagrams.md @@ -16,9 +16,9 @@ Grid diagrams let you display objects in a structured grid.
-:::info -Source code for this is at the bottom of the page. -::: + + {Grid} + Two keywords do all the magic: - `grid-rows` @@ -120,15 +120,17 @@ Setting `grid-gap` is equivalent to setting both `vertical-gap` and `horizontal- `vertical-gap` and `horizontal-gap` can override `grid-gap`. +### Gap size 0 + `grid-gap: 0` in particular can create some interesting constructions: -### Like this map of Japan +#### Like this map of Japan
> [D2 source](https://github.com/terrastruct/d2/blob/master/docs/examples/japan-grid/japan.d2) -### Or a table of data +#### Or a table of data
@@ -136,6 +138,8 @@ Setting `grid-gap` is equivalent to setting both `vertical-gap` and `horizontal- {Table} +### Gap size 0 + ## Connections Connections for grids themselves work normally as you'd expect. @@ -191,10 +195,3 @@ It'd be nicer if it were centered. This can be achieved by adding 2 invisible el
-## Source code - -This is the script for the image at the top of this page. - - - {Grid} - diff --git a/src/theme/CodeBlock/CodeBlock.scss b/src/theme/CodeBlock/CodeBlock.scss index c0e1c97..d49216f 100644 --- a/src/theme/CodeBlock/CodeBlock.scss +++ b/src/theme/CodeBlock/CodeBlock.scss @@ -45,10 +45,10 @@ right: 1.2rem; background: var(--copy-bg); border: 1px solid var(--copy-border); + border-radius: 4px; display: flex; align-items: center; justify-content: center; - border-radius: 4px; width: 2rem; height: 2rem; cursor: pointer; @@ -108,3 +108,42 @@ .PlaygroundLink { right: 3.6rem; } + +.ExpandToggleWrapper { + position: absolute; + bottom: 10px; + left: 50%; + transform: translateX(-50%); + display: flex; + justify-content: center; + width: 100%; +} + +.ExpandToggle { + background-color: transparent; + border: 1px solid var(--copy-border); + border-radius: 4px; + padding: 5px 10px; + font-size: 14px; + color: var(--blue-800); + cursor: pointer; +} + +.CodeBlock pre::before { + content: ""; + position: absolute; + bottom: 0; + left: 0; + right: 0; + height: 50px; + background: linear-gradient( + to bottom, + rgba(255, 255, 255, 0) 0%, + rgba(255, 255, 255, 1) 100% + ); + pointer-events: none; +} + +.CodeBlock.expanded pre::before { + display: none; +} diff --git a/src/theme/CodeBlock/index.js b/src/theme/CodeBlock/index.js index 5567e9b..f274dde 100644 --- a/src/theme/CodeBlock/index.js +++ b/src/theme/CodeBlock/index.js @@ -50,6 +50,10 @@ export default function D2CodeBlock(props) { scope = `source.${lang.replace(/^language-/, "")}`; } + const [isExpanded, setIsExpanded] = React.useState(!props.expandeable); + const toggleExpand = () => { + setIsExpanded((prev) => !prev); + }; let [tmGrammar, setTMGrammar] = React.useState(getTextMateGrammar(scope)); const [clipboardTooltipText, setClipboardTooltipText] = React.useState("Copy to clipboard"); @@ -73,6 +77,10 @@ export default function D2CodeBlock(props) { let preStyle = { position: "relative", lineHeight: "25px", + // Just a large enough value to fit all + maxHeight: isExpanded ? "6000px" : "200px", + overflow: "hidden", + transition: "max-height 0.5s ease", }; const { colorMode } = docusaurusThemeCommon.useColorMode(); switch (colorMode) { @@ -144,7 +152,9 @@ export default function D2CodeBlock(props) { } return ( -
+
)}
{children}
+ {props.expandeable && ( +
+ +
+ )}
); }