Skip to content

Commit

Permalink
fix dependency react footgun
Browse files Browse the repository at this point in the history
  • Loading branch information
elevatebart committed Nov 30, 2023
1 parent 1a294a4 commit 1fba21c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 15 deletions.
2 changes: 1 addition & 1 deletion components/DocMenu/react/DocMenuReact.cy.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ describe('<DocMenu/>', () => {
<DocMenu
items={[
{
label: 'Baz',
label: 'Baaaaaaz',
items: [{ label: 'Bar', items: [{ href: '/foo', label: 'Foo' }] }],
},
]}
Expand Down
50 changes: 39 additions & 11 deletions components/DocMenu/react/_DocGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,16 @@ export const DocGroup: React.FC<DocGroupProps> = ({
)

React.useEffect(() => {
setHeight?.(open ? itemsHeights.reduce((a, b) => a + b, 1) : 1)
}, [itemsHeights, open, setHeight])
// if this group contains other groups, ignore this effect
// it will be triggered by a leaf and trickle up
if (group.items.some((item) => 'items' in item)) {
return
}

const newHeight = open ? itemsHeights.reduce((a, b) => a + b, 1) : 1

setHeight?.(newHeight)
}, [itemsHeights, open, setHeight, group.items, group.label])

const [activeTop, setActiveTop] = React.useState(0)

Expand All @@ -44,13 +52,16 @@ export const DocGroup: React.FC<DocGroupProps> = ({
setOpen(open)
}

function onSetHeight(index: number, height: number) {
setItemsHeights((prev) => {
const newHeights = [...prev]
newHeights[index] = height
return newHeights
})
}
const onSetHeightCallback = React.useCallback(
(index: number, height: number) => {
setItemsHeights((prev) => {
const newHeights = [...prev]
newHeights[index] = height
return newHeights
})
},
[setItemsHeights],
)

const Head = collapsible ? 'button' : group.href ? 'a' : 'div'

Expand Down Expand Up @@ -99,11 +110,12 @@ export const DocGroup: React.FC<DocGroupProps> = ({
{group.items.map((item, index) =>
'items' in item ? (
<li key={index} className="relative list-none p-0">
<DocGroup
<DocGroupWithIndex
group={item}
depth={depth + 1}
setHeight={(height) => onSetHeight(index, height)}
setHeight={onSetHeightCallback}
collapsible={collapsible}
index={index}
/>
</li>
) : (
Expand All @@ -120,3 +132,19 @@ export const DocGroup: React.FC<DocGroupProps> = ({
</>
)
}

const DocGroupWithIndex: React.FC<
Omit<DocGroupProps, 'setHeight'> & {
index: number
setHeight: (index: number, height: number) => void
}
> = ({ setHeight, index, ...props }) => {
const onSetHeight = React.useCallback(
(height: number) => {
setHeight(index, height)
},
[index, setHeight],
)

return <DocGroup {...props} setHeight={onSetHeight} />
}
4 changes: 1 addition & 3 deletions components/DocMenu/react/_DocLink.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ export const DocLink: React.FC<DocLinkProps> = ({
if (item.active) {
onActive?.(activeLIRef?.current?.offsetTop || 0)
}
// supposed to happen only on mount
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [])
}, [onActive, item.active])

return (
<li
Expand Down

0 comments on commit 1fba21c

Please sign in to comment.