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

Support "100%" or flex: 1 width #9

Open
papmodern opened this issue Nov 6, 2017 · 4 comments
Open

Support "100%" or flex: 1 width #9

papmodern opened this issue Nov 6, 2017 · 4 comments

Comments

@papmodern
Copy link

No description provided.

@omarqe
Copy link

omarqe commented Aug 14, 2018

Is there any update on this?

@coffenbacher
Copy link

coffenbacher commented Jun 8, 2019

Did anyone figure out a workaround on this?

Edit, here's what I ended up doing, maybe not the slickest thing ever but it works for me:

const example = () => {
  const [wrapperWidth, setWrapperWidth] = useState(0)
  return (
    <View
      style={{ display: 'flex', flexDirection: 'row' }}
      onLayout={event => setWrapperWidth(event.nativeEvent.layout.width)}
    >
      <AutoHeightImage
        width={wrapperWidth}
        source={{ uri: 'https://i.imgur.com/example.png' }}
      />
    </View>
  )
}

@marcorm
Copy link

marcorm commented Jun 23, 2019

@coffenbacher good workaround man... it's still a little strange that react-native don't do it for free... I know they say that they don't add this functionality by default when talking of images taken from the web ({{uri:...}} but this decision should be left to the programmer...
Sorry for the outburst :)

@Sir-hennihau
Copy link

Sir-hennihau commented Aug 15, 2020

I enhanced @coffenbacher 's solution a little, so you can also use it also as a ImageBackground.

import React, { useState, ReactNode } from "react";
import { ViewProps, View, LayoutChangeEvent, StyleSheet } from "react-native";
import AutoHeightImage, {
    AutoHeightImageProps,
} from "react-native-auto-height-image";

interface WrappedAutoHeightImageProps
    extends Omit<AutoHeightImageProps, "width"> {
    children?: ReactNode;
    containerProps?: ViewProps;
}

/**
 * Uses wrapper to set dynamic width of a AutoHeightImage
 * @see https://github.com/vivaxy/react-native-auto-height-image/issues/9
 */
export const WrappedAutoHeightImage = ({
    children,
    containerProps,
    ...rest
}: WrappedAutoHeightImageProps) => {
    const [wrapperWidth, setWrapperWidth] = useState(0);
    const [imageHeight, setImageHeight] = useState(0);

    const onWrapperLayout = (event: LayoutChangeEvent) =>
        setWrapperWidth(event.nativeEvent.layout.width);

    const onImageLayout = (event: LayoutChangeEvent) =>
        setImageHeight(event.nativeEvent.layout.height);

    return (
        <View
            onLayout={onWrapperLayout}
            {...containerProps}
            style={[
                {
                    alignItems: "center",
                    display: "flex",
                    flexDirection: "row",
                    height: imageHeight,
                    justifyContent: "center",
                    width: "100%",
                },
                containerProps?.style,
            ]}

        >
            <AutoHeightImage
                onLayout={onImageLayout}
                onError={(error) => console.log("error", error)}
                width={wrapperWidth}
                style={[StyleSheet.absoluteFill]}
                {...rest}
            />

            {imageHeight ? children : undefined}
        </View>
    );
};

Usage:

  <WrappedAutoHeightImage
                        source={{
                            uri:
                                "https://cdn.pixabay.com/photo/2018/09/24/03/05/cat-3699032_960_720.jpg",
                        }}
                    >
                        <Text>Cats are small lions, prove me wrong</Text>
   </WrappedAutoHeightImage>

You can still use this component just as an image, if you don't provide children.

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

6 participants