Skip to content

Better scrollbars #216

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 4, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 77 additions & 8 deletions packages/mdx/dev/content/test.mdx
Original file line number Diff line number Diff line change
@@ -1,10 +1,79 @@
### with width

<CH.Code debug >

```js
foo
// link(1:2) https://codehike.org
const hi = 'hi'
const hi = 'hi'
const hi = 'hi'
// link[2:18] https://codehike.org
hello world
hello
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with width

<CH.Code debug style={{width: 400}}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code style={{width: 400}}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with zoom

<CH.Code debug maxZoom={0.7} minZoom={0.7}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code maxZoom={0.7} minZoom={0.7}>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

### with line numbers

<CH.Code debug lineNumbers>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>

<CH.Code lineNumbers>

```js
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
const = "hello" + "hello" + "hello" + "hello" + "hello" + "hello"+ "hello" + "goodbye"
```

</CH.Code>
1 change: 1 addition & 0 deletions packages/mdx/src/mdx-client/code.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function mergeCodeConfig<T>(
showExpandButton == null
? props.codeConfig?.showExpandButton
: showExpandButton,
debug: props.debug ?? props.codeConfig?.debug,
}
return { ...rest, codeConfig }
}
Expand Down
15 changes: 6 additions & 9 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export type CodeConfig = {
lineNumbers?: boolean
showCopyButton?: boolean
showExpandButton?: boolean
debug?: boolean
}

function useCodeShift({
Expand Down Expand Up @@ -83,17 +84,12 @@ export function CodeTween({
config.lineNumbers || false,
[config.parentHeight]
)
// return (
// <BeforeDimensions
// element={element}
// htmlProps={preProps}
// />
// )

return !dimensions ? (
return !dimensions || config.debug ? (
<BeforeDimensions
element={element}
htmlProps={preProps}
debug={config.debug}
/>
) : (
<AfterDimensions
Expand All @@ -109,15 +105,16 @@ export function CodeTween({
function BeforeDimensions({
element,
htmlProps,
debug,
}: {
element: React.ReactNode
htmlProps?: HTMLProps
debug?: boolean
}) {
return (
<Wrapper
htmlProps={htmlProps}
// avoid scrollbars when measuring
style={{ overflow: "hidden", opacity: 0 }}
style={{ opacity: debug ? 0.9 : 0, overflow: "auto" }}
>
{element}
</Wrapper>
Expand Down
1 change: 1 addition & 0 deletions packages/mdx/src/smooth-code/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
padding: 0;
box-sizing: content-box;
border: none;
overscroll-behavior-y: contain;
}

.ch-code-scroll-parent ::selection {
Expand Down
10 changes: 6 additions & 4 deletions packages/mdx/src/smooth-code/smooth-container.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function SmoothContainer({

const width = Math.max(
focusWidth + leftPad,
dimensions!.containerWidth
dimensions!.contentWidth
)

const startX = leftPad / zoom
Expand All @@ -75,7 +75,7 @@ export function SmoothContainer({
scale={zoom}
height={Math.max(
focusHeight,
dimensions!.containerHeight
dimensions!.contentHeight
)}
width={width}
>
Expand Down Expand Up @@ -214,8 +214,10 @@ function getContentProps({
originalContentHeight: number
horizontalCenter: boolean
}) {
const { containerWidth, containerHeight, lineHeight } =
dimensions!
const { lineHeight } = dimensions!
const containerHeight = dimensions?.contentHeight
const containerWidth = dimensions?.contentWidth

const originalFocusHeight =
(extremes[1] - extremes[0] + 3) * lineHeight

Expand Down
22 changes: 18 additions & 4 deletions packages/mdx/src/smooth-code/use-dimensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import {
type Dimensions = {
containerWidth: number
containerHeight: number
contentWidth: number
contentHeight: number
deps: React.DependencyList
lineWidths: [number, number]
lineWidth: [number, number]
Expand Down Expand Up @@ -77,7 +79,13 @@ function useDimensions(
_{lineCount}
</span>
) : undefined}
<div style={{ display: "inline-block" }}>
<div
style={{
display: "inline-block",
// leftPad
marginLeft: 16,
}}
>
<span>{line}</span>
</div>
</div>
Expand All @@ -99,7 +107,8 @@ function useDimensions(
useLayoutEffect(() => {
if (prevLineRef.current) {
const pll = prevLineRef.current
const codeElement = pll?.parentElement!
const contentElement = pll?.parentElement!
const codeElement = contentElement.parentElement!

// TODO is it clientWidth or clientRect?
const lineContentDiv = pll?.querySelector(
Expand All @@ -126,6 +135,12 @@ function useDimensions(
containerHeight: getHeightWithoutPadding(
codeElement.parentElement!
)!,
contentWidth: getWidthWithoutPadding(
contentElement.parentElement!
),
contentHeight: getHeightWithoutPadding(
contentElement.parentElement!
)!,
lineWidths: [
plw || nlw || DEFAULT_WIDTH,
nlw || plw || DEFAULT_WIDTH,
Expand All @@ -146,9 +161,8 @@ function useDimensions(
deps: allDeps,
}
setDimensions(d)
// console.log({ d })
}
}, [allDeps])
}, allDeps)

if (
!dimensions ||
Expand Down