-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26654 from rayane-djouah/Migrate-Icon/index.js-to…
…-function-component Migrate Icon/index.js and FloatingActionButton to function component
- Loading branch information
Showing
8 changed files
with
199 additions
and
232 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,49 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, {useEffect} from 'react'; | ||
import Animated, {Easing, interpolateColor, useAnimatedProps, useSharedValue, withTiming} from 'react-native-reanimated'; | ||
import Svg, {Path} from 'react-native-svg'; | ||
import useTheme from '@hooks/useTheme'; | ||
|
||
const AnimatedPath = Animated.createAnimatedComponent(Path); | ||
|
||
const propTypes = { | ||
/* Current state (active or not active) of the component */ | ||
isActive: PropTypes.bool.isRequired, | ||
}; | ||
|
||
function FabPlusIcon({isActive}) { | ||
const theme = useTheme(); | ||
const animatedValue = useSharedValue(isActive ? 1 : 0); | ||
|
||
useEffect(() => { | ||
animatedValue.value = withTiming(isActive ? 1 : 0, { | ||
duration: 340, | ||
easing: Easing.inOut(Easing.ease), | ||
}); | ||
}, [isActive, animatedValue]); | ||
|
||
const animatedProps = useAnimatedProps(() => { | ||
const fill = interpolateColor(animatedValue.value, [0, 1], [theme.textLight, theme.textDark]); | ||
|
||
return { | ||
fill, | ||
}; | ||
}); | ||
|
||
return ( | ||
<Svg | ||
width={20} | ||
height={20} | ||
> | ||
<AnimatedPath | ||
d="M12,3c0-1.1-0.9-2-2-2C8.9,1,8,1.9,8,3v5H3c-1.1,0-2,0.9-2,2c0,1.1,0.9,2,2,2h5v5c0,1.1,0.9,2,2,2c1.1,0,2-0.9,2-2v-5h5c1.1,0,2-0.9,2-2c0-1.1-0.9-2-2-2h-5V3z" | ||
animatedProps={animatedProps} | ||
/> | ||
</Svg> | ||
); | ||
} | ||
|
||
FabPlusIcon.propTypes = propTypes; | ||
FabPlusIcon.displayName = 'FabPlusIcon'; | ||
|
||
export default FabPlusIcon; |
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,85 @@ | ||
import PropTypes from 'prop-types'; | ||
import React, {useEffect, useRef} from 'react'; | ||
import {View} from 'react-native'; | ||
import Animated, {Easing, interpolateColor, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated'; | ||
import PressableWithFeedback from '@components/Pressable/PressableWithFeedback'; | ||
import Tooltip from '@components/Tooltip/PopoverAnchorTooltip'; | ||
import useLocalize from '@hooks/useLocalize'; | ||
import useTheme from '@hooks/useTheme'; | ||
import useThemeStyles from '@hooks/useThemeStyles'; | ||
import FabPlusIcon from './FabPlusIcon'; | ||
|
||
const AnimatedPressable = Animated.createAnimatedComponent(PressableWithFeedback); | ||
AnimatedPressable.displayName = 'AnimatedPressable'; | ||
|
||
const propTypes = { | ||
/* Callback to fire on request to toggle the FloatingActionButton */ | ||
onPress: PropTypes.func.isRequired, | ||
|
||
/* Current state (active or not active) of the component */ | ||
isActive: PropTypes.bool.isRequired, | ||
|
||
/* An accessibility label for the button */ | ||
accessibilityLabel: PropTypes.string.isRequired, | ||
|
||
/* An accessibility role for the button */ | ||
role: PropTypes.string.isRequired, | ||
}; | ||
|
||
const FloatingActionButton = React.forwardRef(({onPress, isActive, accessibilityLabel, role}, ref) => { | ||
const theme = useTheme(); | ||
const styles = useThemeStyles(); | ||
const {translate} = useLocalize(); | ||
const fabPressable = useRef(null); | ||
const animatedValue = useSharedValue(isActive ? 1 : 0); | ||
const buttonRef = ref; | ||
|
||
useEffect(() => { | ||
animatedValue.value = withTiming(isActive ? 1 : 0, { | ||
duration: 340, | ||
easing: Easing.inOut(Easing.ease), | ||
}); | ||
}, [isActive, animatedValue]); | ||
|
||
const animatedStyle = useAnimatedStyle(() => { | ||
const backgroundColor = interpolateColor(animatedValue.value, [0, 1], [theme.success, theme.buttonDefaultBG]); | ||
|
||
return { | ||
transform: [{rotate: `${animatedValue.value * 135}deg`}], | ||
backgroundColor, | ||
borderRadius: styles.floatingActionButton.borderRadius, | ||
}; | ||
}); | ||
|
||
return ( | ||
<Tooltip text={translate('common.new')}> | ||
<View style={styles.floatingActionButtonContainer}> | ||
<AnimatedPressable | ||
ref={(el) => { | ||
fabPressable.current = el; | ||
if (buttonRef) { | ||
buttonRef.current = el; | ||
} | ||
}} | ||
accessibilityLabel={accessibilityLabel} | ||
role={role} | ||
pressDimmingValue={1} | ||
onPress={(e) => { | ||
// Drop focus to avoid blue focus ring. | ||
fabPressable.current.blur(); | ||
onPress(e); | ||
}} | ||
onLongPress={() => {}} | ||
style={[styles.floatingActionButton, animatedStyle]} | ||
> | ||
<FabPlusIcon isActive={isActive} /> | ||
</AnimatedPressable> | ||
</View> | ||
</Tooltip> | ||
); | ||
}); | ||
|
||
FloatingActionButton.propTypes = propTypes; | ||
FloatingActionButton.displayName = 'FloatingActionButton'; | ||
|
||
export default FloatingActionButton; |
Oops, something went wrong.