Skip to content

Commit a0f207c

Browse files
committed
improve glass effect performance
1 parent b722d7f commit a0f207c

File tree

2 files changed

+42
-56
lines changed

2 files changed

+42
-56
lines changed

src/components/BoardLine.tsx

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,9 @@
1-
import { filterNullable } from "array-utils-ts"
21
import clsx from "clsx"
32
import { useAtom, useAtomValue } from "jotai"
4-
import { range } from "lodash-es"
53
import { FC, useCallback, useEffect, useRef } from "react"
64
import { ActiveTab, ActualDate, TlSelected } from "../store"
75
import { useOnClickOutside } from "../utils/useOnClickOutside"
86

9-
const updateSelection = (els: [HTMLElement, HTMLElement] | null) => {
10-
document.querySelectorAll("[data-tl-idx]").forEach((x) => x.classList.remove("tl-active"))
11-
12-
const abc = filterNullable(els?.map((x) => x.getAttribute("data-tl-idx")) ?? [])
13-
.map((x) => parseInt(x, 10))
14-
.filter((x) => !isNaN(x))
15-
.sort((a, b) => a - b)
16-
17-
if (abc.length !== 2) return
18-
19-
for (const x of range(24).filter((x) => x < abc[0] || abc[1] < x)) {
20-
document.querySelectorAll(`[data-tl-idx="${x}"]`).forEach((x) => x.classList.add("tl-active"))
21-
}
22-
}
23-
247
// prettier-ignore
258
const getIdx = (idx: string) => document.querySelector(`[data-tl-home=true] [data-tl-idx="${idx}"]`) as HTMLElement
269
const getNow = () => document.querySelector("[data-tl-home=true] [data-tl-now=true]") as HTMLElement
@@ -35,70 +18,76 @@ export const BoardLine: FC<BoardLineProps> = ({ rtRef, tlRef }) => {
3518
const date = useAtomValue(ActualDate)
3619

3720
const [selected, setSelected] = useAtom(TlSelected)
38-
const cRef = useRef<HTMLDivElement>(null)
39-
// const lRef = useRef<HTMLDivElement>(null)
40-
// const rRef = useRef<HTMLDivElement>(null)
21+
const cRef = useRef<HTMLDivElement>(null) // center line
22+
const lRef = useRef<HTMLDivElement>(null) // left glass
23+
const rRef = useRef<HTMLDivElement>(null) // right glass
4124

4225
const selectedRef = useRef(selected)
4326
selectedRef.current = selected
4427

4528
// root ref here to work click on event buttons
4629
useOnClickOutside(rtRef, () => setSelected(null))
4730

48-
const setLine = useCallback((x: number, width: number) => {
49-
if (!tlRef.current || !cRef.current) return
31+
const resetLine = useCallback(() => {
32+
if (!cRef.current || !lRef.current || !rRef.current) return
33+
34+
cRef.current.style.opacity = "0"
35+
lRef.current.style.opacity = "0"
36+
rRef.current.style.opacity = "0"
37+
}, [])
38+
39+
const setLine = useCallback((x: number, width: number, glass = false) => {
40+
if (!tlRef.current || !cRef.current || !lRef.current || !rRef.current) return
5041

51-
const el = tlRef.current
52-
const rt = el.getBoundingClientRect()
53-
const xx = rt.left + el.scrollLeft
42+
const hh = tlRef.current.getBoundingClientRect().height
43+
const pl = tlRef.current.getBoundingClientRect().left - tlRef.current.scrollLeft
44+
const cx = x - pl
5445

46+
cRef.current.style.top = `${hh / 2}px`
47+
cRef.current.style.height = `${hh - 16}px`
5548
cRef.current.style.opacity = "1"
56-
cRef.current.style.top = `${rt.height / 2}px`
57-
cRef.current.style.height = `${rt.height - 16}px`
58-
cRef.current.style.left = `${x - xx}px`
49+
cRef.current.style.left = `${cx}px`
5950
cRef.current.style.width = `${width}px`
6051

61-
// const [head, tail] = [getIdx("0"), getIdx("23")]
62-
// if (!lRef.current || !rRef.current || !head || !tail) return
63-
64-
// const x1 = head.getBoundingClientRect().left - xx
65-
// lRef.current.style.top = `${rt.height / 2}px`
66-
// lRef.current.style.height = `${rt.height - 16}px`
67-
// lRef.current.style.opacity = glass ? "1" : "0"
68-
// lRef.current.style.left = `${x1}px`
69-
// lRef.current.style.width = `${x - xx - x1}px`
70-
71-
// const x2 = x - xx + width
72-
// rRef.current.style.top = `${rt.height / 2}px`
73-
// rRef.current.style.height = `${rt.height - 16}px`
74-
// rRef.current.style.opacity = glass ? "1" : "0"
75-
// rRef.current.style.left = `${x2}px`
76-
// rRef.current.style.width = `${tail.getBoundingClientRect().right - x2 - 58.5}px`
52+
const [head, tail] = [getIdx("0"), getIdx("23")]
53+
if (!head || !tail) return
54+
55+
const lx = head.getBoundingClientRect().left - pl
56+
const rx = tail.getBoundingClientRect().right - pl
57+
58+
lRef.current.style.top = `${hh / 2}px`
59+
lRef.current.style.height = `${hh - 16}px`
60+
lRef.current.style.opacity = glass ? "1" : "0"
61+
lRef.current.style.left = `${lx}px`
62+
lRef.current.style.width = `${cx - lx}px`
63+
64+
rRef.current.style.top = `${hh / 2}px`
65+
rRef.current.style.height = `${hh - 16}px`
66+
rRef.current.style.opacity = glass ? "1" : "0"
67+
rRef.current.style.left = `${cx + width}px`
68+
rRef.current.style.width = `${rx - cx - width}px`
7769
}, [])
7870

7971
const calc = useCallback(() => {
8072
if (!tlRef.current) return
8173

8274
const selected = selectedRef.current
8375
if (!selected) {
84-
updateSelection(null)
85-
8676
const el = getNow()
87-
if (!el && cRef.current) cRef.current.style.opacity = "0"
77+
if (!el && cRef.current) resetLine()
8878
else {
8979
const rc = el.getBoundingClientRect()
9080
setLine(rc.left, rc.width)
9181
}
9282
} else {
9383
const ae = getIdx(selected[0])
9484
const be = getIdx(selected[1])
95-
updateSelection([ae, be])
9685

9786
const a = ae.getBoundingClientRect()
9887
const b = be.getBoundingClientRect()
9988
const l = Math.min(b.left, a.left)
10089
const w = b.left < a.left ? a.right - b.left : b.right - a.left
101-
setLine(l, w)
90+
setLine(l, w, true)
10291
}
10392
}, [])
10493

@@ -114,19 +103,20 @@ export const BoardLine: FC<BoardLineProps> = ({ rtRef, tlRef }) => {
114103
setSelected(null)
115104
}, [home])
116105

117-
const cls = "absolute pointer-events-none select-none box-border -translate-y-1/2"
106+
const cls = "absolute z-[10] box-border -translate-y-1/2 pointer-events-none select-none"
107+
const sideCls = clsx(cls, "z-[14] bg-blue-100/50")
118108

119109
return (
120110
<>
121-
{/* <div ref={lRef} className={clsx(cls, "z-[14] h-full w-full bg-red-100/50")} /> */}
111+
<div ref={lRef} className={sideCls} />
122112
<div
123113
ref={cRef}
124114
className={clsx(
125115
cls,
126116
"z-[15] w-[32px] rounded-md border-2 border-red-500/50 dark:border-red-500/80",
127117
)}
128118
/>
129-
{/* <div ref={rRef} className={clsx(cls, "z-[14] h-full w-full bg-green-100/50")} /> */}
119+
<div ref={rRef} className={sideCls} />
130120
</>
131121
)
132122
}

src/index.css

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,6 @@
5656
#root {
5757
@apply mx-auto max-w-[1040px] overflow-hidden px-2.5 pb-8;
5858
}
59-
60-
.tl-active {
61-
@apply bg-blue-100/50;
62-
}
6359
}
6460

6561
@layer utilities {

0 commit comments

Comments
 (0)