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

[TS migration] Migrate 'OpacityView.js' component to TypeScript #30074

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
69 changes: 0 additions & 69 deletions src/components/OpacityView.js

This file was deleted.

55 changes: 55 additions & 0 deletions src/components/OpacityView.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import {StyleProp, ViewStyle} from 'react-native';
import Animated, {AnimatedStyle, useAnimatedStyle, useSharedValue, withTiming} from 'react-native-reanimated';
import shouldRenderOffscreen from '@libs/shouldRenderOffscreen';
import variables from '@styles/variables';

type OpacityViewProps = {
/** Should we dim the view */
shouldDim: boolean;

/** Content to render */
children: React.ReactNode;

/**
* Array of style objects
* @default []
*/
style?: StyleProp<AnimatedStyle<ViewStyle>>;

/**
* The value to use for the opacity when the view is dimmed
* @default variables.hoverDimValue
*/
dimmingValue?: number;

/** Whether the view needs to be rendered offscreen (for Android only) */
needsOffscreenAlphaCompositing?: boolean;
};

function OpacityView({shouldDim, children, style = [], dimmingValue = variables.hoverDimValue, needsOffscreenAlphaCompositing = false}: OpacityViewProps) {
const opacity = useSharedValue(1);
const opacityStyle = useAnimatedStyle(() => ({
opacity: opacity.value,
}));

React.useEffect(() => {
if (shouldDim) {
opacity.value = withTiming(dimmingValue, {duration: 50});
} else {
opacity.value = withTiming(1, {duration: 50});
}
}, [shouldDim, dimmingValue, opacity]);

return (
<Animated.View
style={[opacityStyle, style]}
needsOffscreenAlphaCompositing={shouldRenderOffscreen ? needsOffscreenAlphaCompositing : undefined}
>
{children}
</Animated.View>
);
}

OpacityView.displayName = 'OpacityView';
export default OpacityView;
Loading