-
Notifications
You must be signed in to change notification settings - Fork 4.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[RNMobile] Auto-scroll upon block insertion (#57273)
* Add `useScrollToSection` hook * Add `useScrollToElement` hook * Update logic to retrieve `KeyboardAwareFlatList` ref using `forwardRef` * Replace `useScrollToTextInput` with `useScrollToSection` * Expose `scrollToSection` and `scrollToElement` in Android implementation of `KeyboardAwareFlatList ` * Add `useScrollUponInsertion` hook * Add `useScroll` hook to abstract common logic in `KeyboardAwareFlatList` * Calculate `nativeScrollRef` based on platform in `useScroll` hook * Update unit test to adapt `useScrollToSection` hook * Remove `nativeScrollRef` in favor of calculating the ref in `KeyboardAwareFlatList` platform-specific components * Add unit test for `useScroll` hook * Ensure layout event of `BlockListItemCell` is triggered in tests * Avoid triggering auto-scroll for the same client ID * Update `react-native-editor` changelog
- Loading branch information
Showing
15 changed files
with
462 additions
and
168 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
52 changes: 52 additions & 0 deletions
52
packages/block-editor/src/components/block-list/use-scroll-upon-insertion.native.js
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,52 @@ | ||
/** | ||
* WordPress dependencies | ||
*/ | ||
import { useEffect } from '@wordpress/element'; | ||
import { useSelect } from '@wordpress/data'; | ||
|
||
/** | ||
* Internal dependencies | ||
*/ | ||
import { useBlockListContext } from './block-list-context'; | ||
import { store as blockEditorStore } from '../../store'; | ||
|
||
const useScrollUponInsertion = ( { | ||
clientId, | ||
isSelected, | ||
isLayoutCalculated, | ||
elementRef, | ||
} ) => { | ||
const { scrollRef } = useBlockListContext(); | ||
const wasBlockJustInserted = useSelect( | ||
( select ) => | ||
!! select( blockEditorStore ).wasBlockJustInserted( | ||
clientId, | ||
'inserter_menu' | ||
), | ||
[ clientId ] | ||
); | ||
useEffect( () => { | ||
const lastScrollTo = scrollRef?.lastScrollTo.current; | ||
const alreadyScrolledTo = lastScrollTo?.clientId === clientId; | ||
if ( | ||
alreadyScrolledTo || | ||
! isSelected || | ||
! scrollRef || | ||
! wasBlockJustInserted || | ||
! isLayoutCalculated | ||
) { | ||
return; | ||
} | ||
scrollRef.scrollToElement( elementRef ); | ||
lastScrollTo.clientId = clientId; | ||
}, [ | ||
isSelected, | ||
scrollRef, | ||
wasBlockJustInserted, | ||
elementRef, | ||
isLayoutCalculated, | ||
clientId, | ||
] ); | ||
}; | ||
|
||
export default useScrollUponInsertion; |
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
Oops, something went wrong.