Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Components: refactor Notice to pass exhaustive-deps #44157

Merged
merged 3 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/components/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
- `NavigationMenu` updated to ignore `react/exhaustive-deps` eslint rule ([#44090](https://github.com/WordPress/gutenberg/pull/44090)).
- `RangeControl`: updated to pass `react/exhaustive-deps` eslint rule ([#44271](https://github.com/WordPress/gutenberg/pull/44271)).
- `UnitControl` updated to pass the `react/exhaustive-deps` eslint rule ([#44161](https://github.com/WordPress/gutenberg/pull/44161)).
- `Notice`: updated to satisfy `react/exhaustive-deps` eslint rule ([#44157](https://github.com/WordPress/gutenberg/pull/44157))

## 21.0.0 (2022-09-13)

Expand Down
37 changes: 17 additions & 20 deletions packages/components/src/notice/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { BlurView } from '@react-native-community/blur';
/**
* WordPress dependencies
*/
import { useEffect, useRef, Platform } from '@wordpress/element';
import { useEffect, useRef, useCallback, Platform } from '@wordpress/element';
import { usePreferredColorSchemeStyle } from '@wordpress/compose';

/**
Expand All @@ -30,40 +30,37 @@ const Notice = ( { onNoticeHidden, content, id, status } ) => {
const timer = useRef( null );

useEffect( () => {
startAnimation();

return () => {
clearTimeout( timer?.current );
};
}, [] );

function onHide() {
// start animation
Animated.timing( animationValue, {
toValue: 0,
duration: 150,
toValue: 1,
duration: 300,
useNativeDriver: true,
easing: Easing.out( Easing.quad ),
} ).start( ( { finished } ) => {
if ( finished && onNoticeHidden ) {
onNoticeHidden( id );
timer.current = setTimeout( () => {
onHide();
}, HIDE_TIMER );
}
} );
}

function startAnimation() {
return () => {
clearTimeout( timer?.current );
};
}, [ animationValue, onHide, onNoticeHidden ] );

const onHide = useCallback( () => {
Animated.timing( animationValue, {
toValue: 1,
duration: 300,
toValue: 0,
duration: 150,
useNativeDriver: true,
easing: Easing.out( Easing.quad ),
} ).start( ( { finished } ) => {
if ( finished && onNoticeHidden ) {
timer.current = setTimeout( () => {
onHide();
}, HIDE_TIMER );
onNoticeHidden( id );
}
} );
}
}, [ animationValue, onNoticeHidden, id ] );

const noticeSolidStyles = usePreferredColorSchemeStyle(
styles.noticeSolid,
Expand Down
10 changes: 7 additions & 3 deletions packages/components/src/notice/list.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { store as noticesStore } from '@wordpress/notices';
*/
import Notice from './';
import styles from './style.scss';
import { useCallback } from '@wordpress/element';

function NoticeList() {
const { notices } = useSelect( ( select ) => {
Expand All @@ -25,9 +26,12 @@ function NoticeList() {

const { removeNotice } = useDispatch( noticesStore );

function onRemoveNotice( id ) {
removeNotice( id );
}
const onRemoveNotice = useCallback(
( id ) => {
removeNotice( id );
},
[ removeNotice ]
);

if ( ! notices.length ) {
return null;
Expand Down