Skip to content

Commit befb5d5

Browse files
committed
chore: cleanup
1 parent 69f5f65 commit befb5d5

1 file changed

Lines changed: 20 additions & 34 deletions

File tree

packages/ui/src/components/code.tsx

Lines changed: 20 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export function Code<T>(props: CodeProps<T>) {
129129
let findOverlay!: HTMLDivElement
130130
let findOverlayFrame: number | undefined
131131
let findOverlayScroll: HTMLElement[] = []
132-
let findPositionFrame: number | undefined
132+
let findScroll: HTMLElement | undefined
133133
let observer: MutationObserver | undefined
134134
let renderToken = 0
135135
let selectionFrame: number | undefined
@@ -303,29 +303,13 @@ export function Code<T>(props: CodeProps<T>) {
303303

304304
const positionFindBar = () => {
305305
if (!findBar || !wrapper) return
306-
const scrollParent = getScrollParent(wrapper)
307-
if (!scrollParent) {
308-
findBar.style.position = "absolute"
309-
findBar.style.top = "8px"
310-
findBar.style.right = "8px"
311-
findBar.style.left = ""
312-
return
313-
}
314-
const scrollTop = scrollParent.scrollTop
306+
const scrollTop = findScroll ? findScroll.scrollTop : window.scrollY
315307
findBar.style.position = "absolute"
316308
findBar.style.top = `${scrollTop + 8}px`
317309
findBar.style.right = "8px"
318310
findBar.style.left = ""
319311
}
320312

321-
const scheduleFindPosition = () => {
322-
if (findPositionFrame !== undefined) return
323-
findPositionFrame = requestAnimationFrame(() => {
324-
findPositionFrame = undefined
325-
positionFindBar()
326-
})
327-
}
328-
329313
const scanFind = (root: ShadowRoot, query: string) => {
330314
const needle = query.toLowerCase()
331315
const out: Range[] = []
@@ -440,13 +424,19 @@ export function Code<T>(props: CodeProps<T>) {
440424
syncOverlayScroll()
441425
scheduleOverlay()
442426
}
443-
if (opts?.scroll && active) scrollToRange(active)
427+
if (opts?.scroll && active) {
428+
scrollToRange(active)
429+
positionFindBar()
430+
}
444431
return
445432
}
446433

447434
clearHighlightFind()
448435
syncOverlayScroll()
449-
if (opts?.scroll && active) scrollToRange(active)
436+
if (opts?.scroll && active) {
437+
scrollToRange(active)
438+
positionFindBar()
439+
}
450440
scheduleOverlay()
451441
}
452442

@@ -474,12 +464,14 @@ export function Code<T>(props: CodeProps<T>) {
474464
return
475465
}
476466
scrollToRange(active)
467+
positionFindBar()
477468
return
478469
}
479470

480471
clearHighlightFind()
481472
syncOverlayScroll()
482473
scrollToRange(active)
474+
positionFindBar()
483475
scheduleOverlay()
484476
}
485477

@@ -492,13 +484,14 @@ export function Code<T>(props: CodeProps<T>) {
492484
findCurrent = host
493485
findTarget = host
494486

487+
findScroll = getScrollParent(wrapper) ?? undefined
495488
if (!findOpen()) setFindOpen(true)
496489
requestAnimationFrame(() => {
490+
applyFind({ scroll: true })
497491
positionFindBar()
498492
findInput?.focus()
499493
findInput?.select()
500494
})
501-
applyFind({ scroll: true })
502495
},
503496
close: closeFind,
504497
}
@@ -521,20 +514,18 @@ export function Code<T>(props: CodeProps<T>) {
521514

522515
createEffect(() => {
523516
if (!findOpen()) return
524-
const scrollParent = getScrollParent(wrapper)
525-
const target = scrollParent ?? window
517+
findScroll = getScrollParent(wrapper) ?? undefined
518+
const target = findScroll ?? window
526519

527-
const handler = () => scheduleFindPosition()
520+
const handler = () => positionFindBar()
528521
target.addEventListener("scroll", handler, { passive: true })
529522
window.addEventListener("resize", handler, { passive: true })
523+
handler()
530524

531525
onCleanup(() => {
532526
target.removeEventListener("scroll", handler)
533527
window.removeEventListener("resize", handler)
534-
if (findPositionFrame !== undefined) {
535-
cancelAnimationFrame(findPositionFrame)
536-
findPositionFrame = undefined
537-
}
528+
findScroll = undefined
538529
})
539530
})
540531

@@ -918,11 +909,6 @@ export function Code<T>(props: CodeProps<T>) {
918909
dragFrame = undefined
919910
}
920911

921-
if (findPositionFrame !== undefined) {
922-
cancelAnimationFrame(findPositionFrame)
923-
findPositionFrame = undefined
924-
}
925-
926912
dragStart = undefined
927913
dragEnd = undefined
928914
dragMoved = false
@@ -977,7 +963,7 @@ export function Code<T>(props: CodeProps<T>) {
977963
stepFind(e.shiftKey ? -1 : 1)
978964
}}
979965
/>
980-
<div class="shrink-0 text-12-regular text-text-weak tabular-nums">
966+
<div class="shrink-0 text-12-regular text-text-weak tabular-nums text-right" style={{ width: "10ch" }}>
981967
{findCount() ? `${findIndex() + 1}/${findCount()}` : "0/0"}
982968
</div>
983969
<div class="flex items-center">

0 commit comments

Comments
 (0)