@@ -13,11 +13,10 @@ const relayout = (
13
13
ratio : number ,
14
14
wrapper ?: HTMLElement
15
15
) => {
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
19
18
20
- const update = ( width ) => ( wrapper . style . maxWidth = width + 'px' )
19
+ const update = ( width : number ) => ( wrapper . style . maxWidth = width + 'px' )
21
20
22
21
// Reset wrapper width
23
22
wrapper . style . maxWidth = ''
@@ -27,9 +26,9 @@ const relayout = (
27
26
const h = container . clientHeight
28
27
29
28
// 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
33
32
34
33
if ( w ) {
35
34
while ( l + 1 < r ) {
@@ -49,12 +48,12 @@ const relayout = (
49
48
50
49
const MINIFIED_RELAYOUT_STR = relayout . toString ( )
51
50
52
- type Props = {
51
+ interface BalancerProps extends React . HTMLAttributes < HTMLElement > {
53
52
/**
54
53
* The HTML tag to use for the wrapper element.
55
54
* @default 'span'
56
55
*/
57
- as ?: string
56
+ as ?: React . ElementType
58
57
/**
59
58
* The balance ratio of the wrapper width (0 <= ratio <= 1).
60
59
* 0 means the wrapper width is the same as the container width (no balance, browser default).
@@ -68,18 +67,20 @@ type Props = {
68
67
// As Next.js adds `display: none` to `body` for development, we need to trigger
69
68
// a re-balance right after the style is removed, synchronously.
70
69
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
+ )
72
73
if ( next_dev_style ) {
73
- const callback = ( mutationList ) => {
74
+ const callback : MutationCallback = ( mutationList ) => {
74
75
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 )
83
84
}
84
85
}
85
86
}
@@ -89,15 +90,14 @@ if (!IS_SERVER && process.env.NODE_ENV !== 'production') {
89
90
}
90
91
}
91
92
92
- const Balancer : React . FC < Props > = ( {
93
- as = 'span' ,
93
+ const Balancer : React . FC < BalancerProps > = ( {
94
+ as : Component = 'span' ,
94
95
ratio = 1 ,
95
96
children,
96
97
...props
97
98
} ) => {
98
- const As = as
99
99
const id = React . useId ( )
100
- const wrapperRef = React . useRef ( )
100
+ const wrapperRef = React . useRef < HTMLElement > ( )
101
101
102
102
// Re-balance on content change and on mount/hydration
103
103
useIsomorphicLayoutEffect ( ( ) => {
@@ -113,7 +113,7 @@ const Balancer: React.FC<Props> = ({
113
113
useIsomorphicLayoutEffect ( ( ) => {
114
114
if ( ! wrapperRef . current ) return
115
115
116
- const container = wrapperRef . current . parentElement as HTMLElement
116
+ const container = wrapperRef . current . parentElement
117
117
if ( ! container ) return
118
118
119
119
const resizeObserver = new ResizeObserver ( ( ) => {
@@ -126,7 +126,7 @@ const Balancer: React.FC<Props> = ({
126
126
127
127
return (
128
128
< >
129
- < As
129
+ < Component
130
130
{ ...props }
131
131
data-br = { id }
132
132
data-brr = { ratio }
@@ -136,17 +136,17 @@ const Balancer: React.FC<Props> = ({
136
136
verticalAlign : 'top' ,
137
137
textDecoration : 'inherit' ,
138
138
} }
139
- suppressHydrationWarning = { true }
139
+ suppressHydrationWarning
140
140
>
141
141
{ children }
142
- </ As >
142
+ </ Component >
143
143
< script
144
- suppressHydrationWarning = { true }
144
+ suppressHydrationWarning
145
145
dangerouslySetInnerHTML = { {
146
146
// Calculate the balance initially for SSR
147
147
__html : `self.${ SYMBOL_KEY } =${ MINIFIED_RELAYOUT_STR } ;self.${ SYMBOL_KEY } ("${ id } ",${ ratio } )` ,
148
148
} }
149
- > </ script >
149
+ / >
150
150
</ >
151
151
)
152
152
}
0 commit comments