Skip to content

Split layout props in GenericTouchable instead of using containerStyle #1617

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
17 changes: 11 additions & 6 deletions src/components/touchables/GenericTouchable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ import { Component } from 'react';
import {
Animated,
Platform,
StyleProp,
ViewStyle,
TouchableWithoutFeedbackProps,
StyleSheet,
} from 'react-native';

import { State } from '../../State';
Expand All @@ -17,6 +16,7 @@ import {
} from '../../handlers/gestureHandlerCommon';
import { NativeViewGestureHandlerPayload } from '../../handlers/NativeViewGestureHandler';
import { TouchableNativeFeedbackExtraProps } from './TouchableNativeFeedback.android';
import splitStyleProp from './splitStyleProp';

/**
* Each touchable is a states' machine which preforms transitions.
Expand Down Expand Up @@ -46,8 +46,6 @@ export interface GenericTouchableProps extends TouchableWithoutFeedbackProps {
nativeID?: string;
shouldActivateOnStart?: boolean;
disallowInterruption?: boolean;

containerStyle?: StyleProp<ViewStyle>;
}

interface InternalProps {
Expand Down Expand Up @@ -261,10 +259,11 @@ export default class GenericTouchable extends Component<
onLayout: this.props.onLayout,
hitSlop: this.props.hitSlop,
};
const { outer, inner } = splitStyleProp(this.props.style);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The style prop will be split on every render (and also causes bridge traffic since the object changes). Not sure if this has any noticeable performance implications but that's something to keep in mind since some screens may contain a lot of touchables.


return (
<BaseButton
style={this.props.containerStyle}
style={outer}
onHandlerStateChange={
// TODO: not sure if it can be undefined instead of null
this.props.disabled ? undefined : this.onHandlerStateChange
Expand All @@ -275,10 +274,16 @@ export default class GenericTouchable extends Component<
disallowInterruption={this.props.disallowInterruption}
testID={this.props.testID}
{...this.props.extraButtonProps}>
<Animated.View {...coreProps} style={this.props.style}>
<Animated.View {...coreProps} style={[styles.innerView, inner]}>
{this.props.children}
</Animated.View>
</BaseButton>
);
}
}

const styles = StyleSheet.create({
innerView: {
flexGrow: 1,
},
});
61 changes: 61 additions & 0 deletions src/components/touchables/splitStyleProp.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { FlexStyle, StyleProp, StyleSheet } from 'react-native';

const OUTER_PROPS: { [key in keyof FlexStyle]?: true } = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if these are really all the style props that need to be applied to the outer view or if there are more (e.g. backfaceVisibility, elevation, transform and all of ShadowStyleIOS).

It would probably be best to add complex layout examples to test this, as you mentioned.

alignSelf: true,
bottom: true,
display: true,
end: true,
flex: true,
flexBasis: true,
flexGrow: true,
flexShrink: true,
height: true,
left: true,
margin: true,
marginBottom: true,
marginEnd: true,
marginHorizontal: true,
marginLeft: true,
marginRight: true,
marginStart: true,
marginTop: true,
marginVertical: true,
maxHeight: true,
maxWidth: true,
minHeight: true,
minWidth: true,
position: true,
right: true,
start: true,
top: true,
width: true,
zIndex: true,
};

/**
* Split a style prop between an "outer" and "inner" views in a way that makes it behave
* as if it was a single view.
*
* @example
* const { outer, inner } = splitStyleProp(style);
* return (
* <View style={outer}>
* <View style={[{ flexGrow: 1 }, inner]} />
* </View>
* );
*/
export default function splitStyleProp<T extends FlexStyle>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be splitStyleProp<T extends ViewStyle> for better clarity.

style?: StyleProp<T>
): { outer: T; inner: T } {
const resolvedStyle = StyleSheet.flatten(style);
const inner: Record<string, unknown> = {};
const outer: Record<string, unknown> = {};
Object.entries(resolvedStyle).forEach(([key, value]) => {
if ((OUTER_PROPS as { [key: string]: true | undefined })[key] === true) {
outer[key] = value;
} else {
inner[key] = value;
}
});
return { outer: outer as T, inner: inner as T };
}