Skip to content

Commit 22ed7cb

Browse files
committed
Fix dimensions when code starts hidden
1 parent 52738ce commit 22ed7cb

File tree

4 files changed

+41
-15
lines changed

4 files changed

+41
-15
lines changed

Diff for: packages/mdx/src/highlighter/index.tsx

+9-4
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { highlight as light } from "@code-hike/lighter"
33

44
const newlineRe = /\r\n|\r|\n/
55

6+
const warnings = new Set()
7+
68
export async function highlight({
79
code,
810
lang,
@@ -35,10 +37,13 @@ export async function highlight({
3537
return { lines, lang }
3638
} catch (e) {
3739
// TODO check error is "missing grammar"
38-
console.warn(
39-
"[Code Hike warning]",
40-
`${lang} is not a valid language, no syntax highlighting will be applied.`
41-
)
40+
if (!warnings.has(lang)) {
41+
console.warn(
42+
"[Code Hike warning]",
43+
`${lang} is not a valid language, no syntax highlighting will be applied.`
44+
)
45+
warnings.add(lang)
46+
}
4247
return highlight({ code, lang: "text", theme })
4348
}
4449
}

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

+5-11
Original file line numberDiff line numberDiff line change
@@ -109,10 +109,7 @@ function BeforeDimensions({
109109
debug?: boolean
110110
}) {
111111
return (
112-
<Wrapper
113-
htmlProps={htmlProps}
114-
style={{ opacity: debug ? 0.9 : 0, overflow: "auto" }}
115-
>
112+
<Wrapper htmlProps={htmlProps} measured={false}>
116113
{element}
117114
</Wrapper>
118115
)
@@ -137,7 +134,7 @@ function AfterDimensions({
137134
htmlProps: HTMLProps
138135
}) {
139136
return (
140-
<Wrapper htmlProps={htmlProps} style={{ opacity: 1 }}>
137+
<Wrapper htmlProps={htmlProps} measured={true}>
141138
<SmoothLines
142139
codeStep={stepInfo}
143140
progress={progress}
@@ -159,12 +156,12 @@ function AfterDimensions({
159156

160157
function Wrapper({
161158
htmlProps,
162-
style,
163159
children,
160+
measured,
164161
}: {
165162
htmlProps?: HTMLProps
166-
style: React.CSSProperties
167163
children: React.ReactNode
164+
measured: boolean
168165
}) {
169166
return (
170167
// not using <pre> because https://github.com/code-hike/codehike/issues/120
@@ -173,10 +170,7 @@ function Wrapper({
173170
className={`ch-code-wrapper ${
174171
htmlProps?.className || ""
175172
}`}
176-
style={{
177-
...style,
178-
...htmlProps?.style,
179-
}}
173+
data-ch-measured={measured}
180174
children={children}
181175
/>
182176
)

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

+6
Original file line numberDiff line numberDiff line change
@@ -51,3 +51,9 @@
5151
// to avoid resets using "border-box" that break the scrollbar https://github.com/code-hike/codehike/issues/240
5252
box-sizing: content-box;
5353
}
54+
.ch-code-wrapper[data-ch-measured="false"] {
55+
overflow: auto;
56+
}
57+
.ch-code-wrapper[data-ch-measured="false"] > * {
58+
opacity: 0;
59+
}

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

+21
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,13 @@ function useDimensions(
4545
const [dimensions, setDimensions] =
4646
React.useState<Dimensions>(null)
4747

48+
// in case the element starts hidden https://github.com/code-hike/codehike/issues/372
49+
const [visibility, markAsVisible] = React.useState(0)
50+
4851
const windowWidth = useWindowWidth()
4952
const prevLineRef = React.useRef<HTMLDivElement>(null!)
5053

54+
// the element to render before dimensions are calculated
5155
const { prevLongestLine, nextLongestLine, element } =
5256
React.useMemo(() => {
5357
const prevLongestLine = getLongestLine(
@@ -135,6 +139,7 @@ function useDimensions(
135139
prevLongestLine,
136140
nextLongestLine,
137141
minColumns,
142+
visibility,
138143
]
139144

140145
useLayoutEffect(() => {
@@ -143,6 +148,21 @@ function useDimensions(
143148
const contentElement = pll?.parentElement!
144149
const codeElement = contentElement.parentElement!
145150

151+
const { width } = codeElement.getBoundingClientRect()
152+
if (!width && visibility === 0) {
153+
const resizeObserver = new ResizeObserver(
154+
([entry]) => {
155+
const { width } = entry.contentRect
156+
if (width) {
157+
resizeObserver.disconnect()
158+
markAsVisible(1)
159+
}
160+
}
161+
)
162+
resizeObserver.observe(codeElement)
163+
return () => resizeObserver.disconnect()
164+
}
165+
146166
// TODO is it clientWidth or clientRect?
147167
const lineContentDiv = pll?.querySelector(
148168
":scope > div"
@@ -195,6 +215,7 @@ function useDimensions(
195215
}
196216
setDimensions(d)
197217
}
218+
return () => {}
198219
}, allDeps)
199220

200221
if (

0 commit comments

Comments
 (0)