diff --git a/packages/components/src/notice/index.native.js b/packages/components/src/notice/index.native.js index 9b5b8faa0214f..72910b17620c5 100644 --- a/packages/components/src/notice/index.native.js +++ b/packages/components/src/notice/index.native.js @@ -7,15 +7,14 @@ import { Text, TouchableWithoutFeedback, View, - Dimensions, - Platform, + useWindowDimensions, } from 'react-native'; import { BlurView } from '@react-native-community/blur'; /** * WordPress dependencies */ -import { useEffect, useRef, useState } from '@wordpress/element'; +import { useEffect, useRef, Platform } from '@wordpress/element'; import { usePreferredColorSchemeStyle } from '@wordpress/compose'; /** @@ -23,57 +22,48 @@ import { usePreferredColorSchemeStyle } from '@wordpress/compose'; */ import styles from './style.scss'; -const Notice = ( { onNoticeHidden, content, id, status } ) => { - const [ width, setWidth ] = useState( Dimensions.get( 'window' ).width ); - const [ visible, setVisible ] = useState( true ); +const HIDE_TIMER = 3000; +const Notice = ( { onNoticeHidden, content, id, status } ) => { + const { width } = useWindowDimensions(); const animationValue = useRef( new Animated.Value( 0 ) ).current; const timer = useRef( null ); - const isIOS = Platform.OS === 'ios'; - - const onDimensionsChange = () => { - setWidth( Dimensions.get( 'window' ).width ); - }; - - useEffect( () => { - const dimensionsChangeSubscription = Dimensions.addEventListener( - 'change', - onDimensionsChange - ); - return () => { - dimensionsChangeSubscription.remove(); - }; - }, [] ); useEffect( () => { startAnimation(); + return () => { clearTimeout( timer?.current ); }; - }, [ visible, id ] ); + }, [] ); - const onHide = () => { - setVisible( false ); - }; + function onHide() { + Animated.timing( animationValue, { + toValue: 0, + duration: 150, + useNativeDriver: true, + easing: Easing.out( Easing.quad ), + } ).start( ( { finished } ) => { + if ( finished && onNoticeHidden ) { + onNoticeHidden( id ); + } + } ); + } - const startAnimation = () => { + function startAnimation() { Animated.timing( animationValue, { - toValue: visible ? 1 : 0, - duration: visible ? 300 : 150, + toValue: 1, + duration: 300, useNativeDriver: true, easing: Easing.out( Easing.quad ), - } ).start( () => { - if ( visible && onNoticeHidden ) { + } ).start( ( { finished } ) => { + if ( finished && onNoticeHidden ) { timer.current = setTimeout( () => { onHide(); - }, 3000 ); - } - - if ( ! visible && onNoticeHidden ) { - onNoticeHidden( id ); + }, HIDE_TIMER ); } } ); - }; + } const noticeSolidStyles = usePreferredColorSchemeStyle( styles.noticeSolid, @@ -95,25 +85,25 @@ const Notice = ( { onNoticeHidden, content, id, status } ) => { status === 'error' && errorTextStyles, ]; + const containerStyles = [ + styles.notice, + ! Platform.isIOS && noticeSolidStyles, + { + width, + transform: [ + { + translateY: animationValue.interpolate( { + inputRange: [ 0, 1 ], + outputRange: [ -24, 0 ], + } ), + }, + ], + }, + ]; + return ( <> - + @@ -121,7 +111,7 @@ const Notice = ( { onNoticeHidden, content, id, status } ) => { - { isIOS && ( + { Platform.isIOS && ( { + const { getNotices } = select( noticesStore ); + return { + notices: getNotices(), + }; + }, [] ); - removeNotice( id ) { - const { removeNotice } = this.props; + const { removeNotice } = useDispatch( noticesStore ); + + function onRemoveNotice( id ) { removeNotice( id ); } - render() { - const { notices, shouldStack } = this.props; - - if ( ! notices.length ) { - return null; - } + if ( ! notices.length ) { + return null; + } - return ( - - { shouldStack ? ( - notices - .reverse() - .map( ( notice ) => ( - - ) ) - ) : ( + return ( + + { notices.map( ( notice ) => { + return ( - ) } - - ); - } + ); + } ) } + + ); } -export default compose( [ - withSelect( ( select ) => { - const { getNotices } = select( 'core/notices' ); - - return { - notices: getNotices(), - }; - } ), - withDispatch( ( dispatch ) => { - const { removeNotice } = dispatch( 'core/notices' ); - - return { - removeNotice, - }; - } ), -] )( NoticeList ); +export default NoticeList; diff --git a/packages/components/src/notice/style.native.scss b/packages/components/src/notice/style.native.scss index 3b0d79b899630..4ee29cf58ef25 100644 --- a/packages/components/src/notice/style.native.scss +++ b/packages/components/src/notice/style.native.scss @@ -33,6 +33,7 @@ .notice { justify-content: center; + position: absolute; } .noticeContent {