Skip to content

Set longestLineRef once #233

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 25, 2022
Merged
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
51 changes: 27 additions & 24 deletions packages/mdx/src/smooth-code/use-dimensions.tsx
Original file line number Diff line number Diff line change
@@ -56,40 +56,43 @@ function useDimensions(
code.next,
focus.next
)

const lines = (code.prev || code.next!)
.trimEnd()
.split(newlineRe)

const lineCount = lines.length

// avod setting the ref more than once https://github.com/code-hike/codehike/issues/232
let prevLineRefSet = false
const element = (
<code className="ch-code-scroll-parent">
<br />
{lines.map((line, i) => (
<div
ref={
line === prevLongestLine
? prevLineRef
: undefined
}
key={i}
>
{lineNumbers ? (
<span className="ch-code-line-number">
_{lineCount}
</span>
) : undefined}
<div
style={{
display: "inline-block",
// leftPad
marginLeft: 16,
}}
>
<span>{line}</span>
{lines.map((line, i) => {
const ref =
!prevLineRefSet && line === prevLongestLine
? prevLineRef
: undefined
prevLineRefSet = prevLineRefSet || ref != null
return (
<div ref={ref} key={i}>
{lineNumbers ? (
<span className="ch-code-line-number">
_{lineCount}
</span>
) : undefined}
<div
style={{
display: "inline-block",
// leftPad
marginLeft: 16,
}}
>
<span>{line}</span>
</div>
</div>
</div>
))}
)
})}
<br />
</code>
)