Skip to content

Commit

Permalink
feat(use-floating): separate reference and floating
Browse files Browse the repository at this point in the history
Separate reference and floating stating so that when their setter is
called but they don't change the components do not re-render.
  • Loading branch information
megheaiulian committed Feb 14, 2025
1 parent d9ed3d1 commit ab243b9
Showing 1 changed file with 6 additions and 8 deletions.
14 changes: 6 additions & 8 deletions src/use-floating.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useCallback, useEffect, useMemo, useState } from '@pionjs/pion';
import { useEffect, useMemo, useState } from '@pionjs/pion';
import {
autoUpdate,
computePosition,
Expand Down Expand Up @@ -30,8 +30,9 @@ export const useFloating = ({
placement = 'bottom-start',
strategy,
middleware = defaultMiddleware,
}: UseFloating) => {
const [[reference, floating], setRefs] = useState<[Element?, Element?]>([]);
}: UseFloating | undefined = {}) => {
const [reference, setReference] = useState<Element>();
const [floating, setFloating] = useState<Element>();
const [position, setPosition] = useState<ComputePositionReturn>();

useEffect(() => {
Expand All @@ -49,11 +50,8 @@ export const useFloating = ({
}, [reference, floating, placement, strategy, middleware]);

return {
setReference: useCallback(
(el?: Element) => setRefs(([, _]) => [el, _]),
[],
),
setFloating: useCallback((el?: Element) => setRefs(([_]) => [_, el]), []),
setReference,
setFloating,
styles: useMemo(
() =>
position ? { left: `${position.x}px`, top: `${position.y}px` } : {},
Expand Down

0 comments on commit ab243b9

Please # to comment.