Skip to content

Commit f77bec5

Browse files
committed
Refactor last group padding logic into a custom hook
- toggling the item [Fragment] make stutter as the calculated bottom height changes - on group, move the logic responsible for height calculation to a hook
1 parent f394e16 commit f77bec5

3 files changed

Lines changed: 35 additions & 32 deletions

File tree

‎packages/ui/src/components/editor/settings/Group/Group.tsx‎

Lines changed: 4 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
1-
import { useEffect, useRef, useState } from 'react'
21
import styles from './Group.module.scss'
3-
import {
4-
GROUP_TITLE_HEIGHT,
5-
SECTION_HEADER_HEIGHT
6-
} from '../../../../constants/sizes'
2+
import { use_last_group_padding } from './use-last-group-padding'
3+
import { GROUP_TITLE_HEIGHT } from '../../../../constants/sizes'
74

85
type Props = {
96
children: React.ReactNode
@@ -12,35 +9,10 @@ type Props = {
129
}
1310

1411
export const Group: React.FC<Props> = (props) => {
15-
const ref = useRef<HTMLDivElement>(null)
16-
const [height, setHeight] = useState<number>(0)
17-
18-
useEffect(() => {
19-
if (!props.is_last || !ref.current) return
20-
21-
const observer = new ResizeObserver((entries) => {
22-
const entry = entries[0]
23-
if (entry) {
24-
setHeight(entry.contentRect.height)
25-
}
26-
})
27-
28-
observer.observe(ref.current)
29-
return () => observer.disconnect()
30-
}, [props.is_last])
12+
const ref = use_last_group_padding(props.is_last)
3113

3214
return (
33-
<div
34-
ref={ref}
35-
className={props.title ? styles.wrapper : undefined}
36-
style={
37-
props.is_last && height > 0
38-
? {
39-
paddingBottom: `calc(100vh - ${height}px - ${SECTION_HEADER_HEIGHT}px)`
40-
}
41-
: undefined
42-
}
43-
>
15+
<div ref={ref} className={props.title ? styles.wrapper : undefined}>
4416
{props.title && (
4517
<div className={styles.title} style={{ height: GROUP_TITLE_HEIGHT }}>
4618
{props.title}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { useEffect, useRef } from 'react'
2+
import { SECTION_HEADER_HEIGHT } from '../../../../constants/sizes'
3+
4+
export const use_last_group_padding = (is_last?: boolean) => {
5+
const ref = useRef<HTMLDivElement>(null)
6+
7+
useEffect(() => {
8+
if (!is_last || !ref.current) {
9+
if (ref.current) {
10+
ref.current.style.paddingBottom = ''
11+
}
12+
return
13+
}
14+
15+
const observer = new ResizeObserver((entries) => {
16+
const entry = entries[0]
17+
if (entry && ref.current) {
18+
ref.current.style.paddingBottom = `calc(100vh - ${entry.contentRect.height}px - ${SECTION_HEADER_HEIGHT}px)`
19+
}
20+
})
21+
22+
observer.observe(ref.current)
23+
return () => {
24+
observer.disconnect()
25+
if (ref.current) ref.current.style.paddingBottom = ''
26+
}
27+
}, [is_last])
28+
29+
return ref
30+
}

‎packages/ui/src/components/editor/settings/Layout/Layout.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ export const Layout = forwardRef<HTMLDivElement, Props>((props, ref) => {
2525
</div>
2626
)
2727
})
28+
2829
Layout.displayName = 'Layout'

0 commit comments

Comments
 (0)