Skip to content

Commit 8030656

Browse files
authored
Merge pull request #216 from code-hike/better-scrollbars
Better scrollbars
2 parents 57f1715 + fcee42a commit 8030656

File tree

6 files changed

+109
-25
lines changed

6 files changed

+109
-25
lines changed

Diff for: packages/mdx/dev/content/test.mdx

+77-8
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,79 @@
1+
### with width
2+
3+
<CH.Code debug >
4+
15
```js
2-
foo
3-
// link(1:2) https://codehike.org
4-
const hi = 'hi'
5-
const hi = 'hi'
6-
const hi = 'hi'
7-
// link[2:18] https://codehike.org
8-
hello world
9-
hello
6+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
7+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
108
```
9+
10+
</CH.Code>
11+
12+
<CH.Code>
13+
14+
```js
15+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
16+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
17+
```
18+
19+
</CH.Code>
20+
21+
### with width
22+
23+
<CH.Code debug style={{width: 400}}>
24+
25+
```js
26+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
27+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
28+
```
29+
30+
</CH.Code>
31+
32+
<CH.Code style={{width: 400}}>
33+
34+
```js
35+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
36+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
37+
```
38+
39+
</CH.Code>
40+
41+
### with zoom
42+
43+
<CH.Code debug maxZoom={0.7} minZoom={0.7}>
44+
45+
```js
46+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
47+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
48+
```
49+
50+
</CH.Code>
51+
52+
<CH.Code maxZoom={0.7} minZoom={0.7}>
53+
54+
```js
55+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
56+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
57+
```
58+
59+
</CH.Code>
60+
61+
### with line numbers
62+
63+
<CH.Code debug lineNumbers>
64+
65+
```js
66+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
67+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
68+
```
69+
70+
</CH.Code>
71+
72+
<CH.Code lineNumbers>
73+
74+
```js
75+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
76+
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
77+
```
78+
79+
</CH.Code>

Diff for: packages/mdx/src/mdx-client/code.tsx

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ export function mergeCodeConfig<T>(
5555
showExpandButton == null
5656
? props.codeConfig?.showExpandButton
5757
: showExpandButton,
58+
debug: props.debug ?? props.codeConfig?.debug,
5859
}
5960
return { ...rest, codeConfig }
6061
}

Diff for: packages/mdx/src/smooth-code/code-tween.tsx

+6-9
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ export type CodeConfig = {
4646
lineNumbers?: boolean
4747
showCopyButton?: boolean
4848
showExpandButton?: boolean
49+
debug?: boolean
4950
}
5051

5152
function useCodeShift({
@@ -83,17 +84,12 @@ export function CodeTween({
8384
config.lineNumbers || false,
8485
[config.parentHeight]
8586
)
86-
// return (
87-
// <BeforeDimensions
88-
// element={element}
89-
// htmlProps={preProps}
90-
// />
91-
// )
9287

93-
return !dimensions ? (
88+
return !dimensions || config.debug ? (
9489
<BeforeDimensions
9590
element={element}
9691
htmlProps={preProps}
92+
debug={config.debug}
9793
/>
9894
) : (
9995
<AfterDimensions
@@ -109,15 +105,16 @@ export function CodeTween({
109105
function BeforeDimensions({
110106
element,
111107
htmlProps,
108+
debug,
112109
}: {
113110
element: React.ReactNode
114111
htmlProps?: HTMLProps
112+
debug?: boolean
115113
}) {
116114
return (
117115
<Wrapper
118116
htmlProps={htmlProps}
119-
// avoid scrollbars when measuring
120-
style={{ overflow: "hidden", opacity: 0 }}
117+
style={{ opacity: debug ? 0.9 : 0, overflow: "auto" }}
121118
>
122119
{element}
123120
</Wrapper>

Diff for: packages/mdx/src/smooth-code/index.scss

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
padding: 0;
2020
box-sizing: content-box;
2121
border: none;
22+
overscroll-behavior-y: contain;
2223
}
2324

2425
.ch-code-scroll-parent ::selection {

Diff for: packages/mdx/src/smooth-code/smooth-container.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ export function SmoothContainer({
5959

6060
const width = Math.max(
6161
focusWidth + leftPad,
62-
dimensions!.containerWidth
62+
dimensions!.contentWidth
6363
)
6464

6565
const startX = leftPad / zoom
@@ -75,7 +75,7 @@ export function SmoothContainer({
7575
scale={zoom}
7676
height={Math.max(
7777
focusHeight,
78-
dimensions!.containerHeight
78+
dimensions!.contentHeight
7979
)}
8080
width={width}
8181
>
@@ -214,8 +214,10 @@ function getContentProps({
214214
originalContentHeight: number
215215
horizontalCenter: boolean
216216
}) {
217-
const { containerWidth, containerHeight, lineHeight } =
218-
dimensions!
217+
const { lineHeight } = dimensions!
218+
const containerHeight = dimensions?.contentHeight
219+
const containerWidth = dimensions?.contentWidth
220+
219221
const originalFocusHeight =
220222
(extremes[1] - extremes[0] + 3) * lineHeight
221223

Diff for: packages/mdx/src/smooth-code/use-dimensions.tsx

+18-4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ import {
99
type Dimensions = {
1010
containerWidth: number
1111
containerHeight: number
12+
contentWidth: number
13+
contentHeight: number
1214
deps: React.DependencyList
1315
lineWidths: [number, number]
1416
lineWidth: [number, number]
@@ -77,7 +79,13 @@ function useDimensions(
7779
_{lineCount}
7880
</span>
7981
) : undefined}
80-
<div style={{ display: "inline-block" }}>
82+
<div
83+
style={{
84+
display: "inline-block",
85+
// leftPad
86+
marginLeft: 16,
87+
}}
88+
>
8189
<span>{line}</span>
8290
</div>
8391
</div>
@@ -99,7 +107,8 @@ function useDimensions(
99107
useLayoutEffect(() => {
100108
if (prevLineRef.current) {
101109
const pll = prevLineRef.current
102-
const codeElement = pll?.parentElement!
110+
const contentElement = pll?.parentElement!
111+
const codeElement = contentElement.parentElement!
103112

104113
// TODO is it clientWidth or clientRect?
105114
const lineContentDiv = pll?.querySelector(
@@ -126,6 +135,12 @@ function useDimensions(
126135
containerHeight: getHeightWithoutPadding(
127136
codeElement.parentElement!
128137
)!,
138+
contentWidth: getWidthWithoutPadding(
139+
contentElement.parentElement!
140+
),
141+
contentHeight: getHeightWithoutPadding(
142+
contentElement.parentElement!
143+
)!,
129144
lineWidths: [
130145
plw || nlw || DEFAULT_WIDTH,
131146
nlw || plw || DEFAULT_WIDTH,
@@ -146,9 +161,8 @@ function useDimensions(
146161
deps: allDeps,
147162
}
148163
setDimensions(d)
149-
// console.log({ d })
150164
}
151-
}, [allDeps])
165+
}, allDeps)
152166

153167
if (
154168
!dimensions ||

0 commit comments

Comments
 (0)