Skip to content

Commit 244610d

Browse files
types improvement
1 parent 05b2f49 commit 244610d

File tree

4 files changed

+71
-34
lines changed

4 files changed

+71
-34
lines changed

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
"license": "MIT",
2222
"devDependencies": {
2323
"@types/node": "^18.11.13",
24+
"@types/react": "^18.0.26",
2425
"react": "^18.2.0",
2526
"tsup": "^6.4.0",
2627
"typescript": "^4.8.4"

pnpm-lock.yaml

+40-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/index.tsx

+29-29
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ const relayout = (
1313
ratio: number,
1414
wrapper?: HTMLElement
1515
) => {
16-
wrapper =
17-
wrapper || (document.querySelector(`[data-br="${id}"]`) as HTMLElement)
18-
const container = wrapper.parentElement as HTMLElement
16+
wrapper = wrapper || document.querySelector<HTMLElement>(`[data-br="${id}"]`)
17+
const container = wrapper.parentElement
1918

20-
const update = (width) => (wrapper.style.maxWidth = width + 'px')
19+
const update = (width: number) => (wrapper.style.maxWidth = width + 'px')
2120

2221
// Reset wrapper width
2322
wrapper.style.maxWidth = ''
@@ -27,9 +26,9 @@ const relayout = (
2726
const h = container.clientHeight
2827

2928
// Synchronously do binary search and calculate the layout
30-
let l = w / 2
31-
let r = w
32-
let m
29+
let l: number = w / 2
30+
let r: number = w
31+
let m: number
3332

3433
if (w) {
3534
while (l + 1 < r) {
@@ -49,12 +48,12 @@ const relayout = (
4948

5049
const MINIFIED_RELAYOUT_STR = relayout.toString()
5150

52-
type Props = {
51+
interface BalancerProps extends React.HTMLAttributes<HTMLElement> {
5352
/**
5453
* The HTML tag to use for the wrapper element.
5554
* @default 'span'
5655
*/
57-
as?: string
56+
as?: React.ElementType
5857
/**
5958
* The balance ratio of the wrapper width (0 <= ratio <= 1).
6059
* 0 means the wrapper width is the same as the container width (no balance, browser default).
@@ -68,18 +67,20 @@ type Props = {
6867
// As Next.js adds `display: none` to `body` for development, we need to trigger
6968
// a re-balance right after the style is removed, synchronously.
7069
if (!IS_SERVER && process.env.NODE_ENV !== 'production') {
71-
const next_dev_style = document.querySelector('[data-next-hide-fouc]')
70+
const next_dev_style = document.querySelector<HTMLElement>(
71+
'[data-next-hide-fouc]'
72+
)
7273
if (next_dev_style) {
73-
const callback = (mutationList) => {
74+
const callback: MutationCallback = (mutationList) => {
7475
for (const mutation of mutationList) {
75-
for (const node of mutation.removedNodes) {
76-
if (node === next_dev_style) {
77-
observer.disconnect()
78-
const el = document.querySelectorAll('[data-br]')
79-
// @ts-ignore
80-
for (const e of el) {
81-
self[SYMBOL_KEY](0, +e.dataset.brr, e)
82-
}
76+
for (const node of Array.from(mutation.removedNodes)) {
77+
if (node !== next_dev_style) continue
78+
79+
observer.disconnect()
80+
const elements = document.querySelectorAll<HTMLElement>('[data-br]')
81+
82+
for (const element of Array.from(elements)) {
83+
self[SYMBOL_KEY](0, +element.dataset.brr, element)
8384
}
8485
}
8586
}
@@ -89,15 +90,14 @@ if (!IS_SERVER && process.env.NODE_ENV !== 'production') {
8990
}
9091
}
9192

92-
const Balancer: React.FC<Props> = ({
93-
as = 'span',
93+
const Balancer: React.FC<BalancerProps> = ({
94+
as: Component = 'span',
9495
ratio = 1,
9596
children,
9697
...props
9798
}) => {
98-
const As = as
9999
const id = React.useId()
100-
const wrapperRef = React.useRef()
100+
const wrapperRef = React.useRef<HTMLElement>()
101101

102102
// Re-balance on content change and on mount/hydration
103103
useIsomorphicLayoutEffect(() => {
@@ -113,7 +113,7 @@ const Balancer: React.FC<Props> = ({
113113
useIsomorphicLayoutEffect(() => {
114114
if (!wrapperRef.current) return
115115

116-
const container = wrapperRef.current.parentElement as HTMLElement
116+
const container = wrapperRef.current.parentElement
117117
if (!container) return
118118

119119
const resizeObserver = new ResizeObserver(() => {
@@ -126,7 +126,7 @@ const Balancer: React.FC<Props> = ({
126126

127127
return (
128128
<>
129-
<As
129+
<Component
130130
{...props}
131131
data-br={id}
132132
data-brr={ratio}
@@ -136,17 +136,17 @@ const Balancer: React.FC<Props> = ({
136136
verticalAlign: 'top',
137137
textDecoration: 'inherit',
138138
}}
139-
suppressHydrationWarning={true}
139+
suppressHydrationWarning
140140
>
141141
{children}
142-
</As>
142+
</Component>
143143
<script
144-
suppressHydrationWarning={true}
144+
suppressHydrationWarning
145145
dangerouslySetInnerHTML={{
146146
// Calculate the balance initially for SSR
147147
__html: `self.${SYMBOL_KEY}=${MINIFIED_RELAYOUT_STR};self.${SYMBOL_KEY}("${id}",${ratio})`,
148148
}}
149-
></script>
149+
/>
150150
</>
151151
)
152152
}

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
"compilerOptions": {
33
"jsx": "react",
44
"moduleResolution": "node",
5+
"esModuleInterop": true,
56
"lib": ["es2015", "dom"]
67
}
78
}

0 commit comments

Comments
 (0)