Skip to content

Commit 6d3345b

Browse files
Docs: fix missing "Copy to clipboard" tooltips when <Example> was used, not just <Code> (#41582)
Co-authored-by: Julien Déramond <[email protected]>
1 parent 2acf59d commit 6d3345b

File tree

2 files changed

+26
-19
lines changed

2 files changed

+26
-19
lines changed

site/src/components/shortcodes/Code.astro

Lines changed: 24 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,20 @@ interface Props {
3333
* This takes precedence over the `code` prop.
3434
*/
3535
filePath?: string
36+
/**
37+
* Defines if the `<Code>` component is nested inside an `<Example>` component or not.
38+
* @default false
39+
*/
40+
nestedInExample?: boolean
3641
}
3742
38-
const { class: className, code, containerClass, fileMatch, filePath, lang } = Astro.props
43+
const { class: className, code, containerClass, fileMatch, filePath, lang, nestedInExample = false } = Astro.props
3944
4045
let codeToDisplay = filePath
4146
? fs.readFileSync(path.join(process.cwd(), filePath), 'utf8')
4247
: Array.isArray(code)
43-
? code.join('\n')
44-
: code
48+
? code.join('\n')
49+
: code
4550
4651
if (filePath && fileMatch && codeToDisplay) {
4752
const match = codeToDisplay.match(new RegExp(fileMatch))
@@ -130,19 +135,23 @@ if (filePath && fileMatch && codeToDisplay) {
130135
})
131136
</script>
132137

133-
<div class:list={['bd-code-snippet', containerClass]}>
138+
<div class:list={[{ 'bd-code-snippet': !nestedInExample }, containerClass]}>
134139
{
135-
Astro.slots.has('pre') ? (
136-
<slot name="pre" />
137-
) : (
138-
<div class="bd-clipboard">
139-
<button type="button" class="btn-clipboard">
140-
<svg class="bi" role="img" aria-label="Copy">
141-
<use xlink:href="#clipboard" />
142-
</svg>
143-
</button>
144-
</div>
145-
)
140+
nestedInExample
141+
? (<></>)
142+
: Astro.slots.has('pre')
143+
? (
144+
<slot name="pre" />
145+
)
146+
: (
147+
<div class="bd-clipboard">
148+
<button type="button" class="btn-clipboard">
149+
<svg class="bi" role="img" aria-label="Copy">
150+
<use xlink:href="#clipboard" />
151+
</svg>
152+
</button>
153+
</div>
154+
)
146155
}
147156
<div class="highlight">
148157
{

site/src/components/shortcodes/Example.astro

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
import { replacePlaceholdersInHtml } from '@libs/placeholder'
3-
import { Prism } from '@astrojs/prism'
3+
import Code from '@components/shortcodes/Code.astro'
44
55
interface Props {
66
/**
@@ -96,9 +96,7 @@ const simplifiedMarkup = markup
9696
</div>
9797
</div>
9898
)}
99-
<div class="highlight">
100-
<Prism code={simplifiedMarkup} lang={lang} />
101-
</div>
99+
<Code code={simplifiedMarkup} lang={lang} nestedInExample={true} />
102100
</>
103101
)
104102
}

0 commit comments

Comments
 (0)