Skip to content

Commit

Permalink
refactor: wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
Layouwen committed Dec 29, 2024
1 parent 47d1086 commit 09f568c
Showing 1 changed file with 18 additions and 14 deletions.
32 changes: 18 additions & 14 deletions src/components/popover/wrapper.tsx
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import type { ReactElement, ReactNode } from 'react'
import React, { forwardRef, useImperativeHandle, useRef } from 'react'

interface WrapperProps {
children?: ReactNode
}
import React, { ReactElement, useImperativeHandle, useRef } from 'react'

export interface WrapperRef {
element: Element | null
element: Element
}

export const Wrapper = forwardRef<WrapperRef, WrapperProps>(
export const Wrapper = React.forwardRef<WrapperRef, { children: ReactElement }>(
({ children }, ref) => {
const elementRef = useRef<HTMLElement>(null)
const childRef = useRef<any>(null)

useImperativeHandle(ref, () => ({
element: elementRef.current,
element: childRef.current,
}))

const child = React.Children.only(children) as ReactElement<any, any>
return React.cloneElement(child, {
ref: elementRef,
})
return (
<>
<span
style={{ display: 'none' }}
ref={el => {
if (el) {
childRef.current = el.nextElementSibling
}
}}
/>
{React.Children.only(children)}
</>
)
}
)

0 comments on commit 09f568c

Please sign in to comment.