mirror of
https://github.com/carbon-design-system/carbon-components-svelte.git
synced 2025-09-15 02:11:05 +00:00
docs(code-snippet): add examples for dynnamic, hidden code snippets
This commit is contained in:
parent
0f6d42eb42
commit
5e13f04e3c
3 changed files with 67 additions and 0 deletions
|
@ -51,6 +51,20 @@ let comment = `
|
|||
|
||||
<CodeSnippet wrapText type="multi" code="{comment}" />
|
||||
|
||||
### Dynamic multi-line code
|
||||
|
||||
For dynamically updated code, you must use the `code` prop instead of the default slot.
|
||||
|
||||
<FileSource src="/framed/CodeSnippet/DynamicCodeSnippet" />
|
||||
|
||||
### Hidden multi-line code
|
||||
|
||||
There may be cases where your code snippet is visually hidden. The logic to render the "Show more" button relies on the element's computed height. For visually hidden content, the button will not appear because the height is `0`.
|
||||
|
||||
The recommended workaround is to re-render the component. See the example below.
|
||||
|
||||
<FileSource src="/framed/CodeSnippet/HiddenCodeSnippet" />
|
||||
|
||||
### Skeleton
|
||||
|
||||
The default skeleton type is `"single"`.
|
||||
|
|
15
docs/src/pages/framed/CodeSnippet/DynamicCodeSnippet.svelte
Normal file
15
docs/src/pages/framed/CodeSnippet/DynamicCodeSnippet.svelte
Normal file
|
@ -0,0 +1,15 @@
|
|||
<script>
|
||||
import { ToggleSmall, CodeSnippet } from "carbon-components-svelte";
|
||||
|
||||
let toggled = false;
|
||||
|
||||
$: length = toggled ? 20 : 10;
|
||||
$: code = Array.from({ length }, (_, i) => i + 1).join("\n");
|
||||
</script>
|
||||
|
||||
<ToggleSmall
|
||||
style="margin-bottom: var(--cds-spacing-05)"
|
||||
labelText="Trigger snippet overflow"
|
||||
bind:toggled
|
||||
/>
|
||||
<CodeSnippet type="multi" code="{code}" />
|
38
docs/src/pages/framed/CodeSnippet/HiddenCodeSnippet.svelte
Normal file
38
docs/src/pages/framed/CodeSnippet/HiddenCodeSnippet.svelte
Normal file
|
@ -0,0 +1,38 @@
|
|||
<script>
|
||||
import { CodeSnippet, Tabs, Tab, TabContent } from "carbon-components-svelte";
|
||||
|
||||
let selected = 0;
|
||||
|
||||
const code = Array.from({ length: 20 }, (_, i) => i + 1).join("\n");
|
||||
</script>
|
||||
|
||||
<style>
|
||||
p {
|
||||
margin-bottom: var(--cds-spacing-05);
|
||||
}
|
||||
</style>
|
||||
|
||||
<Tabs bind:selected>
|
||||
<Tab label="Tab 1" />
|
||||
<Tab label="Tab 2" />
|
||||
<div slot="content">
|
||||
<TabContent>
|
||||
<p>Tab 2 contains a multi-line code snippet.</p>
|
||||
<p>
|
||||
Inactive tab content is visually hidden but still rendered in the DOM.
|
||||
As a result, the "Show more" button will not appear because the computed
|
||||
height of the code element is 0.
|
||||
</p>
|
||||
<p>
|
||||
To work around this, you can force the code snippet to be re-rendered
|
||||
when the tab content is active.
|
||||
</p>
|
||||
</TabContent>
|
||||
<TabContent>
|
||||
<p>The "Show more" button should appear.</p>
|
||||
{#if selected === 1}
|
||||
<CodeSnippet type="multi" code="{code}" />
|
||||
{/if}
|
||||
</TabContent>
|
||||
</div>
|
||||
</Tabs>
|
Loading…
Add table
Add a link
Reference in a new issue