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

Fix inner styling of Pressable #2982

Merged
merged 30 commits into from
Jul 9, 2024
Merged
Show file tree
Hide file tree
Changes from 19 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
734c429
add flex styling example
latekvo Jul 4, 2024
923b95c
add initial inner style extraction
latekvo Jul 4, 2024
aa493dd
split styleProp into inner and outer styles
latekvo Jul 4, 2024
7aa9566
update example
latekvo Jul 4, 2024
95d06cd
add custom type utility
latekvo Jul 4, 2024
b83faa9
fix babel import/export errors
latekvo Jul 4, 2024
c848804
[unsafe] initial working version
latekvo Jul 4, 2024
4e26276
fixed insufficient outer styles deletion, fixed clarity, fixed most t…
latekvo Jul 4, 2024
a1f0f2c
move style manipulating functions to utils
latekvo Jul 4, 2024
9d4446a
change types to remove more errors
latekvo Jul 4, 2024
4c75fa7
flatten styles, remove web issues
latekvo Jul 5, 2024
6af3cd1
remove pressable import error, fix types
latekvo Jul 5, 2024
9f471cc
fix typing issues on extractStyles
latekvo Jul 5, 2024
36f1a24
imporve variable names
latekvo Jul 5, 2024
4b799d1
remove temporary styling example
latekvo Jul 5, 2024
01348b4
remove unnecessary comment
latekvo Jul 5, 2024
4a7a00e
add source clarification for inner style keys
latekvo Jul 5, 2024
12a69f8
remove outer style from the inner style keys
latekvo Jul 5, 2024
f00a18a
convert for-of loops into function streams
latekvo Jul 5, 2024
c1933eb
Merge branch 'main' into @latekvo/fix-pressable-flex-styling
latekvo Jul 8, 2024
83efbed
apply style splitting change proposals
latekvo Jul 8, 2024
5595026
combine style splitting into a single function
latekvo Jul 8, 2024
259f0d3
fix dependency array lints
latekvo Jul 8, 2024
e348d8f
combine both inner and outer key separation into a single pass
latekvo Jul 8, 2024
9aa0c0f
remove unnecessary exports
latekvo Jul 8, 2024
f612444
Merge remote-tracking branch 'origin/main' into @latekvo/fix-pressabl…
latekvo Jul 8, 2024
b3278cc
remove unncessary Gesture flag
latekvo Jul 8, 2024
75d4109
change style splitting from streams to for loop
latekvo Jul 8, 2024
4846fd7
remove unnecessary type casts
latekvo Jul 9, 2024
60b77b9
remove unnecessary null check
latekvo Jul 9, 2024
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
12 changes: 10 additions & 2 deletions src/components/Pressable/Pressable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ import {
isTouchWithinInset,
adaptTouchEvent,
addInsets,
innerStyleKeys,
excludeStyles,
extractStyles,
} from './utils';
import { PressabilityDebugView } from '../../handlers/PressabilityDebugView';
import { GestureTouchEvent } from '../../handlers/gestureHandlerCommon';
Expand Down Expand Up @@ -254,8 +257,13 @@ export default function Pressable(props: PressableProps) {
? props.children({ pressed: pressedState })
: props.children;

const flattenedStyles = StyleSheet.flatten((styleProp ?? {}) as ViewStyle);
latekvo marked this conversation as resolved.
Show resolved Hide resolved

const innerStyles = extractStyles(flattenedStyles, innerStyleKeys);
const outerStyles = excludeStyles(flattenedStyles, innerStyleKeys);
latekvo marked this conversation as resolved.
Show resolved Hide resolved

return (
<View style={styleProp}>
<View style={outerStyles}>
<GestureDetector gesture={gesture}>
<NativeButton
ref={pressableRef}
Expand All @@ -267,7 +275,7 @@ export default function Pressable(props: PressableProps) {
props.android_ripple?.color ?? defaultRippleColor
)}
rippleRadius={props.android_ripple?.radius ?? undefined}
style={[StyleSheet.absoluteFill, pointerStyle]}>
style={[StyleSheet.absoluteFill, pointerStyle, innerStyles]}>
{childrenProp}
{__DEV__ ? (
<PressabilityDebugView color="red" hitSlop={normalizedHitSlop} />
Expand Down
62 changes: 61 additions & 1 deletion src/components/Pressable/utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Insets } from 'react-native';
import { Insets, ViewStyle } from 'react-native';
import { LongPressGestureHandlerEventPayload } from '../../handlers/GestureHandlerEventPayload';
import {
TouchData,
Expand Down Expand Up @@ -119,12 +119,72 @@ const adaptTouchEvent = (event: GestureTouchEvent): PressableEvent => {
};
};

type StylePropKeys = (keyof ViewStyle)[];

// Source:
// - From ViewStyle extracted FlexStyle sub-interface which contains all of the box-model manipulating props.
// - From FlexStyle handpicked those styles, which act on the inner part of the box-model.
const innerStyleKeys = [
'alignContent',
m-bert marked this conversation as resolved.
Show resolved Hide resolved
latekvo marked this conversation as resolved.
Show resolved Hide resolved
'alignItems',
'flexBasis',
'flexDirection',
'flexWrap',
'rowGap',
'gap',
'columnGap',
'justifyContent',
'overflow',
'padding',
'paddingBottom',
'paddingEnd',
'paddingHorizontal',
'paddingLeft',
'paddingRight',
'paddingStart',
'paddingTop',
'paddingVertical',
'start',
'end',
'direction', // iOS only
] as StylePropKeys;

const extractStyles = (from: ViewStyle, keys: StylePropKeys): ViewStyle => {
if (!from) {
return {} as ViewStyle;
}

return Object.fromEntries(
keys
.map((key) => [key, from[key]])
.filter(([_key, value]) => value !== undefined)
) as ViewStyle;
latekvo marked this conversation as resolved.
Show resolved Hide resolved
};

const excludeStyles = (from: ViewStyle, keys: StylePropKeys): ViewStyle => {
if (!from) {
return {} as ViewStyle;
}

const exclusiveData = { ...from };

keys
.filter((key) => exclusiveData[key] !== undefined)
.forEach((key) => (exclusiveData[key] = undefined));
latekvo marked this conversation as resolved.
Show resolved Hide resolved

return exclusiveData;
};

export {
innerStyleKeys,
StylePropKeys,
numberAsInset,
addInsets,
touchToPressEvent,
changeToTouchData,
isTouchWithinInset,
adaptStateChangeEvent,
adaptTouchEvent,
extractStyles,
excludeStyles,
};
Loading