Skip to content

Fix dimensions when code starts hidden #373

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
Jun 9, 2023
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
13 changes: 9 additions & 4 deletions packages/mdx/src/highlighter/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import { highlight as light } from "@code-hike/lighter"

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

const warnings = new Set()

export async function highlight({
code,
lang,
Expand Down Expand Up @@ -35,10 +37,13 @@ export async function highlight({
return { lines, lang }
} catch (e) {
// TODO check error is "missing grammar"
console.warn(
"[Code Hike warning]",
`${lang} is not a valid language, no syntax highlighting will be applied.`
)
if (!warnings.has(lang)) {
console.warn(
"[Code Hike warning]",
`${lang} is not a valid language, no syntax highlighting will be applied.`
)
warnings.add(lang)
}
return highlight({ code, lang: "text", theme })
}
}
16 changes: 5 additions & 11 deletions packages/mdx/src/smooth-code/code-tween.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ function BeforeDimensions({
debug?: boolean
}) {
return (
<Wrapper
htmlProps={htmlProps}
style={{ opacity: debug ? 0.9 : 0, overflow: "auto" }}
>
<Wrapper htmlProps={htmlProps} measured={false}>
{element}
</Wrapper>
)
Expand All @@ -137,7 +134,7 @@ function AfterDimensions({
htmlProps: HTMLProps
}) {
return (
<Wrapper htmlProps={htmlProps} style={{ opacity: 1 }}>
<Wrapper htmlProps={htmlProps} measured={true}>
<SmoothLines
codeStep={stepInfo}
progress={progress}
Expand All @@ -159,12 +156,12 @@ function AfterDimensions({

function Wrapper({
htmlProps,
style,
children,
measured,
}: {
htmlProps?: HTMLProps
style: React.CSSProperties
children: React.ReactNode
measured: boolean
}) {
return (
// not using <pre> because https://github.com/code-hike/codehike/issues/120
Expand All @@ -173,10 +170,7 @@ function Wrapper({
className={`ch-code-wrapper ${
htmlProps?.className || ""
}`}
style={{
...style,
...htmlProps?.style,
}}
data-ch-measured={measured}
children={children}
/>
)
Expand Down
6 changes: 6 additions & 0 deletions packages/mdx/src/smooth-code/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -51,3 +51,9 @@
// to avoid resets using "border-box" that break the scrollbar https://github.com/code-hike/codehike/issues/240
box-sizing: content-box;
}
.ch-code-wrapper[data-ch-measured="false"] {
overflow: auto;
}
.ch-code-wrapper[data-ch-measured="false"] > * {
opacity: 0;
}
21 changes: 21 additions & 0 deletions packages/mdx/src/smooth-code/use-dimensions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,13 @@ function useDimensions(
const [dimensions, setDimensions] =
React.useState<Dimensions>(null)

// in case the element starts hidden https://github.com/code-hike/codehike/issues/372
const [visibility, markAsVisible] = React.useState(0)

const windowWidth = useWindowWidth()
const prevLineRef = React.useRef<HTMLDivElement>(null!)

// the element to render before dimensions are calculated
const { prevLongestLine, nextLongestLine, element } =
React.useMemo(() => {
const prevLongestLine = getLongestLine(
Expand Down Expand Up @@ -135,6 +139,7 @@ function useDimensions(
prevLongestLine,
nextLongestLine,
minColumns,
visibility,
]

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

const { width } = codeElement.getBoundingClientRect()
if (!width && visibility === 0) {
const resizeObserver = new ResizeObserver(
([entry]) => {
const { width } = entry.contentRect
if (width) {
resizeObserver.disconnect()
markAsVisible(1)
}
}
)
resizeObserver.observe(codeElement)
return () => resizeObserver.disconnect()
}

// TODO is it clientWidth or clientRect?
const lineContentDiv = pll?.querySelector(
":scope > div"
Expand Down Expand Up @@ -195,6 +215,7 @@ function useDimensions(
}
setDimensions(d)
}
return () => {}
}, allDeps)

if (
Expand Down