-
Notifications
You must be signed in to change notification settings - Fork 184
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Popper): add prop
onReferenceHiddenChanged
(#8126)
* feat(Popper): add prop getFloatingElementHiddenStyles * fix: fix after merge conficts * feat(Popper, Popover, Tooltip): add prop `onReferenceHiddenChanged` and remove prop `getFloatingElementHiddenStyles` * fix: return old style calculation * fix(Popover, Popper, Tooltip): rename `onReferenceHiddenChanged` to `onReferenceHiddenChange`. Add prop JSDoc * fix: fix jsdoc Co-authored-by: Inomdzhon Mirdzhamolov <[email protected]> --------- Co-authored-by: Inomdzhon Mirdzhamolov <[email protected]>
- Loading branch information
1 parent
0e4317a
commit e07de46
Showing
14 changed files
with
133 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
39 changes: 39 additions & 0 deletions
39
packages/vkui/src/lib/floating/useReferenceHiddenChangeCallback.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { renderHook } from '@testing-library/react'; | ||
import { useReferenceHiddenChangeCallback } from './useReferenceHiddenChangeCallback'; | ||
|
||
type HookArguments = Parameters<typeof useReferenceHiddenChangeCallback>; | ||
type RenderHookProps = { | ||
hideMiddleware: HookArguments[0]; | ||
onReferenceHiddenChange: HookArguments[1]; | ||
}; | ||
|
||
describe('usePlacementChangeCallback', () => { | ||
it('calls callback on initial render when initial placement differ from resolvedPlacement', () => { | ||
const onReferenceHiddenChangeStub = jest.fn(); | ||
|
||
const defaultProps: RenderHookProps = { | ||
hideMiddleware: { | ||
referenceHidden: false, | ||
}, | ||
onReferenceHiddenChange: onReferenceHiddenChangeStub, | ||
}; | ||
|
||
const { rerender } = renderHook<void, RenderHookProps>( | ||
({ hideMiddleware, onReferenceHiddenChange }) => | ||
useReferenceHiddenChangeCallback(hideMiddleware, onReferenceHiddenChange), | ||
{ | ||
initialProps: defaultProps, | ||
}, | ||
); | ||
|
||
expect(onReferenceHiddenChangeStub).not.toHaveBeenCalled(); | ||
|
||
rerender({ ...defaultProps, hideMiddleware: { referenceHidden: true } }); | ||
|
||
expect(onReferenceHiddenChangeStub).toHaveBeenNthCalledWith(1, true); | ||
|
||
rerender({ ...defaultProps, hideMiddleware: { referenceHidden: false } }); | ||
|
||
expect(onReferenceHiddenChangeStub).toHaveBeenNthCalledWith(2, false); | ||
}); | ||
}); |
26 changes: 26 additions & 0 deletions
26
packages/vkui/src/lib/floating/useReferenceHiddenChangeCallback.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import * as React from 'react'; | ||
import { useIsomorphicLayoutEffect } from '../useIsomorphicLayoutEffect'; | ||
import { type UseFloatingData } from './types/common'; | ||
import { type FloatingComponentProps } from './types/component'; | ||
|
||
export function useReferenceHiddenChangeCallback( | ||
hideMiddleware: UseFloatingData['middlewareData']['hide'], | ||
onReferenceHiddenChange: FloatingComponentProps['onReferenceHiddenChange'], | ||
) { | ||
const prevHiddenRef = React.useRef<boolean | undefined>(hideMiddleware?.referenceHidden); | ||
React.useEffect(() => { | ||
prevHiddenRef.current = hideMiddleware?.referenceHidden; | ||
}); | ||
|
||
useIsomorphicLayoutEffect( | ||
function checkHiddenChanged() { | ||
if (!onReferenceHiddenChange) { | ||
return; | ||
} | ||
if (hideMiddleware?.referenceHidden !== prevHiddenRef.current) { | ||
onReferenceHiddenChange(hideMiddleware?.referenceHidden || false); | ||
} | ||
}, | ||
[hideMiddleware?.referenceHidden, onReferenceHiddenChange], | ||
); | ||
} |