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

AspectRatio on web doesn't responsive when resize browser #4699

Open
1 of 5 tasks
TVGSOFT opened this issue Mar 8, 2022 · 3 comments
Open
1 of 5 tasks

AspectRatio on web doesn't responsive when resize browser #4699

TVGSOFT opened this issue Mar 8, 2022 · 3 comments

Comments

@TVGSOFT
Copy link

TVGSOFT commented Mar 8, 2022

Description

AspectRatio on web doesn't responsive when resize browser

CodeSandbox/Snack link

https://snack.expo.dev/@giaptran/274416

Steps to reproduce

The width and height calculations were wrong.

const AspectView = forwardRef((props: any, ref?: any) => {
  const [layout, setLayout] = React.useState();
  const inputStyle = [StyleSheet.flatten(props.style) || {}];
  if (layout) {
    // @ts-ignore
    let { width = 0, height = 0 } = layout;
    if (width === 0) {
      inputStyle.push({ width: height * props.aspectRatio, height });
    } else {
      inputStyle.push({ width, height: width / props.aspectRatio });
    }
  }

  return (
    <Box
      {...props}
      ref={ref}
      style={inputStyle}
      onLayout={({ nativeEvent: { layout: inLayout } }: any) =>
        setLayout(inLayout)
      }
    />
  );
});

Expected calculation should like below:

const AspectView = forwardRef((props: any, ref?: any) => {
  const [inputStyle, setInputStyle] = React.useState<any>();

  const handleLayoutChange = (event: LayoutChangeEvent): void => {
    const { width = 0, height = 0 } = event.nativeEvent.layout;

    if (width === 0 && height !== 0) {
      setInputStyle({ width: height * props.aspectRatio, height: "100%" });
    } else if (width !== 0 && height === 0) {
      setInputStyle({ width: "100%", height: width / props.aspectRatio });
    }
  };

  return (
    <Box
      {...props}
      ref={ref}
      style={[props.style, inputStyle]}
      onLayout={handleLayoutChange}
    />
  );
});

NativeBase Version

3.3.7

Platform

  • Android
  • CRA
  • Expo
  • iOS
  • Next

Other Platform

No response

Additional Information

No response

@surajahmed
Copy link
Collaborator

@TVGSOFT Thanks for reporting! We'll look into this.

@stale
Copy link

stale bot commented May 25, 2022

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label May 25, 2022
@Viraj-10 Viraj-10 removed the stale label Sep 19, 2022
@sahilvasuja
Copy link

Hey. I see your code here is reference with the help of this link you can modify your code and set as image as responsive.

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

No branches or pull requests

4 participants