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 Pressable not inferring dimensions from it's children #3020

Merged
merged 3 commits into from
Aug 1, 2024

Conversation

latekvo
Copy link
Contributor

@latekvo latekvo commented Jul 30, 2024

Description

When Pressable does not have it's dimensions robustly set, it should infer them from it's children, to fit-content.
Unfortunately that's not what happens.

This PR fixes this issue.

Test plan

  • run the provided code in place of the EmptyExample
  • see how prior to this fix, the text is either not displayed or it's squished to a single letter per line, and after it, both Pressables are identical

Results

before after
Screenshot 2024-07-31 at 15 12 34 Screenshot 2024-07-31 at 15 10 44

Code

import React from 'react';
import { StyleSheet, Text, View, Pressable as RNPressable } from 'react-native';
import {
  Pressable as GHPressable,
  PressableProps,
} from 'react-native-gesture-handler';

function Pressables(props: PressableProps) {
  const onPressInGH = () => console.log('GH press');
  const onPressInRN = () => console.log('RN press');

  return (
    <View style={styles.container}>
      <GHPressable
        {...(props as any)}
        onPressIn={onPressInGH}
        style={[styles.pressable, props.style]}>
        {props.children ?? <Text>Gesture Handler!</Text>}
      </GHPressable>
      <RNPressable
        {...(props as any)}
        onPressIn={onPressInRN}
        style={[styles.pressable, props.style]}>
        {props.children ?? <Text>React Native!</Text>}
      </RNPressable>
    </View>
  );
}

export default function EmptyExample() {
  return (
    <View style={styles.multirow}>
      <Text style={styles.header}>Padding</Text>
      <Pressables style={{ padding: 16 }} />

      <Text style={styles.header}>GH nested pressable</Text>
      <Pressables style={{ flex: 1, backgroundColor: 'plum' }}>
        <GHPressable
          style={{
            backgroundColor: 'orange',
          }}>
          <Text>Gesture Handler</Text>
        </GHPressable>
      </Pressables>

      <Text style={styles.header}>RN nested pressable</Text>
      <Pressables style={{ flex: 1, backgroundColor: 'plum' }}>
        <RNPressable
          style={{
            backgroundColor: 'orange',
          }}>
          <Text>React Native</Text>
        </RNPressable>
      </Pressables>

      <Text style={styles.header}>2 nested pressables</Text>
      <Pressables
        style={{ flex: 1, backgroundColor: 'plum', flexDirection: 'row' }}>
        <GHPressable
          style={{
            backgroundColor: 'pink',
          }}>
          <Text style={{ padding: 8 }}>GH</Text>
        </GHPressable>
        <RNPressable
          style={{
            backgroundColor: 'orange',
          }}>
          <Text style={{ padding: 8 }}>RN</Text>
        </RNPressable>
      </Pressables>

      <Text style={styles.header}>Nested box model styling</Text>
      <Pressables>
        <View style={{ backgroundColor: 'orange', padding: 8, margin: 8 }}>
          <View style={{ backgroundColor: 'plum', margin: 8 }}>
            <Text>Hello World!</Text>
          </View>
        </View>
      </Pressables>

      <Text style={styles.header}>Flex view in a fixed size Pressable</Text>
      <Pressables style={{ width: 100, height: 100 }}>
        <View style={styles.textWrapper}>
          <Text>Pressable!</Text>
        </View>
      </Pressables>

      <Text style={styles.header}>Flex view in a formless size Pressable</Text>
      <Pressables>
        <View style={styles.textWrapper}>
          <Text>Pressable!</Text>
        </View>
      </Pressables>
    </View>
  );
}

const styles = StyleSheet.create({
  header: {
    fontSize: 16,
    fontWeight: 'bold',
    textAlign: 'center',
  },
  multirow: {
    flex: 1,
    flexDirection: 'column',
    overflow: 'scroll',
  },
  container: {
    flexDirection: 'row',
    justifyContent: 'space-around',
    gap: 30,
    margin: 30,
    marginTop: 5,
  },
  pressable: {
    backgroundColor: 'tomato',
    borderWidth: 1,
    maxWidth: '30%',
  },
  textWrapper: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
});

@latekvo latekvo marked this pull request as ready for review July 30, 2024 15:56
@latekvo latekvo requested a review from j-piasecki July 30, 2024 15:56
@latekvo latekvo marked this pull request as draft July 30, 2024 16:06
@latekvo
Copy link
Contributor Author

latekvo commented Jul 30, 2024

Other available solutions include:

  • StyleSheet.absoluteFill, { position: 'relative' }
  • StyleSheet.absoluteFillObject, { position: 'relative' }
  • { width: 'auto', height: 'auto' }
  • { width: 'auto' }

I think StyleSheet.absoluteFill, { position: 'relative' } is the most promising one but I'll have to look into it.

update:
Could not find a difference between any of the listed styles.
Additionally tested:

  • StyleSheet.absoluteFill, { position: 'relative', width: 'auto' }
  • StyleSheet.absoluteFill, { position: 'relative', width: '100%' }

and most other possible combinations of these styles.

@latekvo latekvo marked this pull request as ready for review July 31, 2024 13:13
@latekvo
Copy link
Contributor Author

latekvo commented Jul 31, 2024

As of f3f590a I updated the description to include nested flexbox example.

Technically { height: '100%' } would suffice instead of the current { width: '100%', height: '100%' } based solely on the tests that I did, but I am not fully confident I understand the underlaying mechanics of why that is, which is why I would prefer to leave it as { width: '100%', height: '100%' }.

Please let me know WDYT @j-piasecki

@latekvo latekvo requested a review from m-bert July 31, 2024 13:22
@j-piasecki
Copy link
Member

j-piasecki commented Jul 31, 2024

Whether width or height alone would suffice is likely dependant on the layout direction - with flexDirection: 'column' height will be enough and with flexDirection: 'row' width will be enough.

Using both is probably best.

Copy link
Contributor

@m-bert m-bert left a comment

Choose a reason for hiding this comment

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

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants