Skip to content

Commit 908896a

Browse files
committed
add bottom margin to Code with nomargin prop to opt out
1 parent b0d2233 commit 908896a

File tree

4 files changed

+28
-13
lines changed

4 files changed

+28
-13
lines changed

.changeset/calm-buses-shout.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@fuzdev/fuz_code': minor
3+
---
4+
5+
add bottom margin to `Code` with `nomargin` prop to opt out

src/lib/Code.svelte

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
grammar,
1313
inline = false,
1414
wrap = false,
15+
nomargin = false,
1516
syntax_styler = syntax_styler_global,
1617
children,
1718
...rest
@@ -74,6 +75,13 @@
7475
* @default false
7576
*/
7677
wrap?: boolean;
78+
/**
79+
* Whether to disable the default margin-bottom on block code.
80+
* Block code has `margin-bottom: var(--space_lg)` by default when not `:last-child`.
81+
*
82+
* @default false
83+
*/
84+
nomargin?: boolean;
7785
/**
7886
* Custom SyntaxStyler instance to use for highlighting.
7987
* Allows using a different styler with custom grammars or configuration.
@@ -121,7 +129,7 @@
121129

122130
<!-- eslint-disable svelte/no-at-html-tags -->
123131

124-
<code {...rest} class:inline class:wrap data-lang={lang}
132+
<code {...rest} class:inline class:wrap class:nomargin data-lang={lang}
125133
>{#if highlighting_disabled}{content}{:else if children}{@render children(
126134
html_content,
127135
)}{:else}{@html html_content}{/if}</code
@@ -143,4 +151,8 @@
143151
/* unset what we set above, otherwise rely on Fuz CSS base styles */
144152
white-space: pre-wrap;
145153
}
154+
155+
code:not(.inline):not(.nomargin):not(:last-child) {
156+
margin-bottom: var(--space_lg);
157+
}
146158
</style>

src/routes/CodeTome.svelte

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -76,9 +76,7 @@ import '@fuzdev/fuz_code/theme_variables.css'; // also this if not using Fuz CSS
7676
<a href="https://github.com/pngwn/prism-svelte"><code>prism-svelte</code></a>
7777
by <a href="https://github.com/pngwn">@pngwn</a>:
7878
</p>
79-
<div class="mb_lg">
80-
<Code content={'<Code content="<scr..." />'} />
81-
</div>
79+
<Code content={'<Code content="<scr..." />'} />
8280
<p>styled:</p>
8381
<div>
8482
<Code
@@ -99,9 +97,7 @@ import '@fuzdev/fuz_code/theme_variables.css'; // also this if not using Fuz CSS
9997
<p>
10098
<TomeLink name="Code" /> supports TypeScript with <code>lang="ts"</code>:
10199
</p>
102-
<div class="mb_lg">
103-
<Code content={`<Code lang="ts" content="export type A<T> = ('b' | 3) & T;" />`} />
104-
</div>
100+
<Code content={`<Code lang="ts" content="export type A<T> = ('b' | 3) & T;" />`} />
105101
<div>
106102
<Code lang="ts" content={`export type A<T> = ('b' | 3) & T;`} />
107103
</div>
@@ -112,9 +108,7 @@ import '@fuzdev/fuz_code/theme_variables.css'; // also this if not using Fuz CSS
112108
<TomeLink name="Code" /> supports Markdown with <code>lang="md"</code>, and fenced blocks for
113109
all languages:
114110
</p>
115-
<div class="mb_lg">
116-
<Code content={`<Code lang="md" content="# hello \`world\` ..." />`} />
117-
</div>
111+
<Code content={`<Code lang="md" content="# hello \`world\` ..." />`} />
118112
<div>
119113
<Code lang="md" content={`# hello \`world\`\n\n\`\`\`ts\n\tconst a = 1;\n\`\`\``} />
120114
</div>
@@ -124,9 +118,7 @@ import '@fuzdev/fuz_code/theme_variables.css'; // also this if not using Fuz CSS
124118
<p>
125119
Passing <code>lang={'{'}null}</code> disables syntax styling:
126120
</p>
127-
<div class="mb_lg">
128-
<Code content={'<Code lang={null} content="<aside>all is gray</aside>" />'} />
129-
</div>
121+
<Code content={'<Code lang={null} content="<aside>all is gray</aside>" />'} />
130122
<Code lang={null} content={`<aside>all is gray</aside>`} />
131123
</section>
132124
<section>

src/routes/library.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,12 @@
195195
"optional": true,
196196
"description": "Whether to wrap long lines in block code.\nSets `white-space: pre-wrap` instead of `white-space: pre`.\n\n**Behavior:**\n- Wraps at whitespace (spaces, newlines)\n- Long tokens without spaces (URLs, hashes) will still scroll horizontally\n- Default `false` provides traditional code block behavior\n\nOnly affects block code (ignored for inline mode)."
197197
},
198+
{
199+
"name": "nomargin",
200+
"type": "boolean",
201+
"optional": true,
202+
"description": "Whether to disable the default margin-bottom on block code.\nBlock code has `margin-bottom: var(--space_lg)` by default when not `:last-child`."
203+
},
198204
{
199205
"name": "syntax_styler",
200206
"type": "SyntaxStyler",

0 commit comments

Comments
 (0)