Skip to content

Commit 4b939f0

Browse files
committed
Change code left pad
1 parent d74c69d commit 4b939f0

File tree

7 files changed

+89
-44
lines changed

7 files changed

+89
-44
lines changed

packages/mdx/src/client/annotations.tsx

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ function Box({
3131
tc.scope?.includes("string")
3232
)?.settings?.foreground || "yellow"
3333
return (
34-
<span style={{ outline: `2px solid ${border}` }}>
34+
<span
35+
className="ch-code-box-annotation"
36+
style={{ outline: `2px solid ${border}` }}
37+
>
3538
{children}
3639
</span>
3740
)
@@ -63,8 +66,18 @@ function Background({
6366
background: bg,
6467
// cursor: "pointer",
6568
}}
66-
// onClick={_ => alert("clicked")}
69+
className="ch-code-bg-annotation"
6770
>
71+
<span
72+
className="ch-code-bg-annotation-border"
73+
style={{
74+
background: "#00a2d3",
75+
width: "3px",
76+
height: "100%",
77+
position: "absolute",
78+
left: 0,
79+
}}
80+
/>
6881
{children}
6982
</div>
7083
)

packages/playground/next.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const { remarkCodeHike } = require("@code-hike/mdx")
22
const theme = require("shiki/themes/monokai.json")
33
const withBundleAnalyzer = require("@next/bundle-analyzer")(
44
{
5-
// enabled: process.env.ANALYZE === "true",
6-
enabled: true,
5+
enabled: process.env.ANALYZE === "true",
6+
// enabled: true,
77
}
88
)
99
module.exports = withBundleAnalyzer({

packages/smooth-code/src/annotations.tsx

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,18 @@ function defaultMultilineComponent(
152152
cursor: "pointer",
153153
}}
154154
onClick={_ => alert("clicked")}
155+
className="ch-code-bg-annotation"
155156
>
157+
<span
158+
className="ch-code-bg-annotation-border"
159+
style={{
160+
background: "#00a2d3",
161+
width: "3px",
162+
height: "100%",
163+
position: "absolute",
164+
left: 0,
165+
}}
166+
/>
156167
{children}
157168
</div>
158169
)

packages/smooth-code/src/code-tween.tsx

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -168,19 +168,8 @@ function Wrapper({
168168
return (
169169
<pre
170170
{...htmlProps}
171-
style={{
172-
overflow: "auto",
173-
margin: 0,
174-
175-
// hack https://code.iamkate.com/html-and-css/fixing-browsers-broken-monospace-font-handling/
176-
// fontSize: "1em",
177-
// fontFamily: "monospace,monospace",
178-
// lineHeight: `${LINE_HEIGHT}em`,
179-
...style,
180-
...htmlProps?.style,
181-
}}
182-
>
183-
{children}
184-
</pre>
171+
style={{ margin: 0, ...style, ...htmlProps?.style }}
172+
children={children}
173+
/>
185174
)
186175
}

packages/smooth-code/src/smooth-container.tsx

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,10 @@ export function SmoothContainer({
1818
maxZoom?: number
1919
center?: boolean
2020
codeStep: CodeShift
21-
children: (focusWidth: number) => React.ReactNode
21+
children: (
22+
focusWidth: number,
23+
startX: number
24+
) => React.ReactNode
2225
progress: number
2326
}) {
2427
const { prev, next } = getTweenContentProps({
@@ -49,11 +52,15 @@ export function SmoothContainer({
4952
progress
5053
)
5154

55+
const leftPad = 16
56+
5257
const width = Math.max(
53-
focusWidth + 16, // 16 is the left padding
58+
focusWidth + leftPad,
5459
dimensions!.containerWidth
5560
)
5661

62+
const startX = leftPad / zoom
63+
5764
return (
5865
<Container
5966
width={dimensions!.containerWidth}
@@ -69,7 +76,7 @@ export function SmoothContainer({
6976
)}
7077
width={width}
7178
>
72-
{children(focusWidth)}
79+
{children(focusWidth, startX)}
7380
</Content>
7481
</Container>
7582
)
@@ -91,11 +98,11 @@ function Container({
9198
height,
9299
position: "relative",
93100
display: "block",
94-
// overflowX: "auto",
101+
overflow: "auto",
95102
}}
96-
>
97-
{children}
98-
</code>
103+
className="ch-code-scroll-parent"
104+
children={children}
105+
/>
99106
)
100107
}
101108

@@ -125,19 +132,21 @@ function Content({
125132
height: height,
126133
overflow: "hidden",
127134
}}
135+
className="ch-code-scroll-content"
128136
>
129137
<div
130138
style={{
131139
position: "absolute",
132140
top: 0,
133141
left: 0,
134142
transform: `translateX(${dx}px) translateY(${dy}px) scale(${scale})`,
135-
transformOrigin: "16px top",
136-
width: "100%",
143+
transformOrigin: "left top",
144+
width: width / scale,
145+
height: (height - 2 * dy) / scale,
146+
// outline: "1px solid yellow",
137147
}}
138-
>
139-
{children}
140-
</div>
148+
children={children}
149+
/>
141150
</div>
142151
)
143152
}

packages/smooth-code/src/smooth-lines.tsx

Lines changed: 35 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,13 @@ type SmoothLinesProps = {
2020
export function SmoothLines(props: SmoothLinesProps) {
2121
return (
2222
<SmoothContainer {...props}>
23-
{focusWidth => (
23+
{(focusWidth, startX) => (
2424
<Lines
2525
codeStep={props.codeStep}
2626
focusWidth={focusWidth}
2727
lineHeight={props.dimensions!.lineHeight}
2828
progress={props.progress}
29+
startX={startX}
2930
/>
3031
)}
3132
</SmoothContainer>
@@ -37,11 +38,13 @@ function Lines({
3738
progress,
3839
focusWidth,
3940
lineHeight,
41+
startX,
4042
}: {
4143
codeStep: CodeShift
4244
focusWidth: number
4345
lineHeight: number
4446
progress: number
47+
startX: number
4548
}) {
4649
const groups =
4750
progress < 0.5
@@ -58,6 +61,7 @@ function Lines({
5861
t={progress}
5962
focusWidth={focusWidth}
6063
lineHeight={lineHeight}
64+
startX={startX}
6165
key={i}
6266
/>
6367
)
@@ -94,6 +98,7 @@ function Lines({
9498
focusWidth={focusWidth}
9599
lineHeight={lineHeight}
96100
startY={startY}
101+
startX={startX}
97102
/>
98103
</Component>
99104
)
@@ -109,12 +114,14 @@ function LineGroup({
109114
focusWidth,
110115
lineHeight,
111116
t,
117+
startX,
112118
startY = 0,
113119
}: {
114120
lines: CodeLine[]
115121
focusWidth: number
116122
lineHeight: number
117123
t: number
124+
startX: number
118125
startY?: number
119126
}) {
120127
return (
@@ -126,15 +133,33 @@ function LineGroup({
126133
const opacity = getOpacity(focused, t, dx)
127134

128135
return (
129-
<LineContainer
130-
key={key}
131-
dx={16 + dx * focusWidth} // 16 is the left padding, it should be configurable
132-
dy={(dy - startY) * lineHeight}
133-
width={focusWidth}
134-
opacity={opacity}
135-
>
136-
<LineContent line={line} progress={t} dx={dx} />
137-
</LineContainer>
136+
<>
137+
{/* <span
138+
style={{
139+
top: 0,
140+
left: 0,
141+
transform: `translate(${
142+
dx * focusWidth
143+
}px, ${(dy - startY) * lineHeight}px)`,
144+
position: "absolute",
145+
}}
146+
>
147+
13
148+
</span> */}
149+
<LineContainer
150+
key={key}
151+
dx={startX + dx * focusWidth}
152+
dy={(dy - startY) * lineHeight}
153+
width={focusWidth}
154+
opacity={opacity}
155+
>
156+
<LineContent
157+
line={line}
158+
progress={t}
159+
dx={dx}
160+
/>
161+
</LineContainer>
162+
</>
138163
)
139164
})}
140165
</>

packages/storybook/src/code-tween.story.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ console.log(4)
105105
const nextCode = `
106106
console.log(1)
107107
console.log(3)
108-
const x = (y) => y++ * foobarbaz
108+
const x = (y) => y++ * foobarbaz - 123456789
109109
`.trim()
110110

111111
const prevAnnotations = [
@@ -139,9 +139,7 @@ const x = (y) => y++ * foobarbaz
139139
return (
140140
<WithProgress>
141141
{progress => (
142-
<div
143-
style={{ height: 300, outline: "1px solid red" }}
144-
>
142+
<div style={{ height: 300 }}>
145143
{tween ? (
146144
<CodeTween
147145
progress={progress}

0 commit comments

Comments
 (0)