Skip to content

Commit

Permalink
refactor: portalRootの監視は暗黙的でわかりにくいので、mount済みかどうかを返す関数を追加する (#4624)
Browse files Browse the repository at this point in the history
  • Loading branch information
misako0927 authored May 21, 2024
1 parent be43b37 commit 5d2cbd9
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 6 deletions.
3 changes: 1 addition & 2 deletions packages/smarthr-ui/src/components/ComboBox/useListBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ export const useListBox = <T,>({
)

const { spacing } = useTheme()
const { portalRoot, createPortal } = usePortal()
const { createPortal } = usePortal()
const listBoxId = useId()
const { items: partialOptions, renderIntersection } = usePartialRendering({
items: options,
Expand Down Expand Up @@ -343,7 +343,6 @@ export const useListBox = <T,>({
handleAdd,
handleSelect,
handleHoverOption,
portalRoot,
],
)

Expand Down
4 changes: 2 additions & 2 deletions packages/smarthr-ui/src/components/DatePicker/Portal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const portal = tv({
})

export const Portal = forwardRef<HTMLDivElement, Props>(({ inputRect, ...props }, ref) => {
const { portalRoot, createPortal } = usePortal()
const { isPortalRootMounted, createPortal } = usePortal()

const [position, setPosition] = useState({
top: 0,
Expand All @@ -36,7 +36,7 @@ export const Portal = forwardRef<HTMLDivElement, Props>(({ inputRect, ...props }
return
}
setPosition(getPortalPosition(inputRect, containerRef.current.offsetHeight))
}, [inputRect, portalRoot])
}, [inputRect, isPortalRootMounted])

const containerStyleProps = useMemo(() => {
const container = portal()
Expand Down
5 changes: 3 additions & 2 deletions packages/smarthr-ui/src/components/Dropdown/Dropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ export const Dropdown: FC<PropsWithChildren> = ({ children }) => {
const [triggerRect, setTriggerRect] = useState<Rect>(initialRect)

const { rootTriggerRef } = useContext(DropdownContext)
const { createPortal, portalRoot, isChildPortal, PortalParentProvider } = usePortal()
const { createPortal, portalRoot, isPortalRootMounted, isChildPortal, PortalParentProvider } =
usePortal()

const triggerElementRef = useRef<HTMLDivElement>(null)
const contentId = useId()
Expand Down Expand Up @@ -79,7 +80,7 @@ export const Dropdown: FC<PropsWithChildren> = ({ children }) => {
if (!active) return null
return createPortal(props.children)
},
[active, createPortal, portalRoot],
[active, createPortal, isPortalRootMounted],
)
// set the displayName explicit for DevTools
DropdownContentRoot.displayName = 'DropdownContentRoot'
Expand Down
3 changes: 3 additions & 0 deletions packages/smarthr-ui/src/hooks/usePortal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,11 @@ export function usePortal() {
[portalRoot],
)

const isPortalRootMounted = useCallback(() => portalRoot !== null, [portalRoot])

return {
portalRoot,
isPortalRootMounted,
isChildPortal,
PortalParentProvider,
createPortal: wrappedCreatePortal,
Expand Down

0 comments on commit 5d2cbd9

Please sign in to comment.