Skip to content

Commit 6726c4c

Browse files
fix(code-block): mobile horizontal scroll and line spacing [INTORG-767]
1 parent b1791d9 commit 6726c4c

4 files changed

Lines changed: 54 additions & 21 deletions

File tree

cms/src/components/blocks/code-block.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
"code": {
1111
"type": "text",
1212
"required": true,
13+
"description": "4-space indentation is normalized to 2 spaces.",
1314
"pluginOptions": {
1415
"i18n": {
1516
"localized": true

src/components/blocks/CodeBlock.astro

Lines changed: 51 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,26 +19,22 @@ const highlighted = await codeToHtml(code, {
1919
---
2020

2121
<figure
22-
class="my-0 rounded-lg overflow-hidden border border-neutral-25 text-step--1"
22+
class="my-0 w-full min-w-0 rounded-lg border border-neutral-25 text-step--1"
2323
data-component="code-block"
2424
>
25-
{/* Title bar */}
2625
<div
27-
class="flex items-center gap-space-xs px-space-s py-space-2xs bg-[#f6f8fa] border-b border-neutral-25"
26+
class="flex items-center gap-space-xs px-space-s py-space-2xs bg-[#f6f8fa] border-b border-neutral-25 rounded-t-lg"
2827
>
29-
{/* Decorative window dots */}
3028
<div class="flex items-center gap-[6px] shrink-0" aria-hidden="true">
3129
<span class="block h-3 w-3 rounded-full bg-[#ff5f57]"></span>
3230
<span class="block h-3 w-3 rounded-full bg-[#febc2e]"></span>
3331
<span class="block h-3 w-3 rounded-full bg-[#28c840]"></span>
3432
</div>
3533

36-
{/* Filename / title */}
37-
<span class="flex-1 text-neutral-900 text-step--2 truncate">
34+
<span class="flex-1 min-w-0 truncate text-neutral-900 text-step--2">
3835
{title ?? language}
3936
</span>
4037

41-
{/* Copy button */}
4238
<button
4339
type="button"
4440
class="copy-btn shrink-0 inline-flex items-center gap-[5px] cursor-pointer
@@ -75,19 +71,61 @@ const highlighted = await codeToHtml(code, {
7571
</button>
7672
</div>
7773

78-
{/* Highlighted code */}
7974
<div
80-
class="overflow-x-auto
81-
[&_pre]:m-0 [&_pre]:p-space-s [&_pre]:leading-relaxed [&_pre]:bg-white!
82-
[&_code]:font-mono [&_code]:text-step--1
83-
[&_.line]:min-h-[1.5em]"
75+
class="code-block-scroll rounded-b-lg"
76+
data-lenis-prevent-touch
8477
set:html={highlighted}
8578
/>
8679
</figure>
8780

81+
<style>
82+
/* Shiki: block .line + newline nodes between spans; contain:inline-size for mobile scroll. */
83+
[data-component='code-block'] {
84+
contain: inline-size;
85+
}
86+
87+
[data-component='code-block'] .code-block-scroll {
88+
display: block;
89+
width: 100%;
90+
min-width: 0;
91+
overflow-x: auto;
92+
overflow-y: hidden;
93+
overscroll-behavior-x: contain;
94+
-webkit-overflow-scrolling: touch;
95+
touch-action: pan-x pan-y;
96+
line-height: 1.5;
97+
}
98+
99+
[data-component='code-block'] .code-block-scroll :global(pre) {
100+
display: block;
101+
width: max-content;
102+
min-width: 100%;
103+
margin: 0;
104+
padding: var(--spacing-space-s);
105+
background-color: #fff !important;
106+
white-space: normal;
107+
}
108+
109+
[data-component='code-block'] .code-block-scroll :global(code),
110+
[data-component='code-block'] .code-block-scroll :global(.line) {
111+
display: block;
112+
width: max-content;
113+
min-width: 100%;
114+
font-family: ui-monospace, monospace;
115+
font-size: var(--text-step--1);
116+
line-height: inherit;
117+
}
118+
119+
[data-component='code-block'] .code-block-scroll :global(.line) {
120+
white-space: pre;
121+
}
122+
</style>
123+
88124
<script>
89125
document.addEventListener('click', (e) => {
90-
const btn = (e.target as Element)?.closest<HTMLButtonElement>('.copy-btn')
126+
const btn = (e.target as Element)?.closest<HTMLButtonElement>(
127+
'[data-component="code-block"] .copy-btn'
128+
)
91129
if (!btn) return
92130

93131
const code = btn.dataset.code ?? ''

src/components/blocks/DynamicZone.astro

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,5 @@ const resolvedBlocks = blocks
6565
---
6666

6767
<div class="flex flex-col gap-space-xl">
68-
{
69-
resolvedBlocks.map(({ Component, props }) => (
70-
<div class="w-full">
71-
<Component {...props} />
72-
</div>
73-
))
74-
}
68+
{resolvedBlocks.map(({ Component, props }) => <Component {...props} />)}
7569
</div>

src/layouts/BlogPostLayout.astro

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ const breadcrumbs = [
7070
data-prose-blog
7171
itemscope
7272
itemtype="http://schema.org/Article"
73-
class="mb-5xl tablet:mb-4xl desktop:mb-5xl"
73+
class="mb-5xl min-w-0 tablet:mb-4xl desktop:mb-5xl"
7474
>
7575
<BlogPostHeader frontmatter={frontmatter} />
7676
<slot />

0 commit comments

Comments
 (0)