Skip to content

Commit

Permalink
Patch v0.0.10
Browse files Browse the repository at this point in the history
  • Loading branch information
xerdnu committed Oct 24, 2023
1 parent 7313d18 commit a9a30ca
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 16 deletions.
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
## [0.0.10] (2023-10-24)

#### Improvements

- Added the prop `isBackground` to act as a container background similar to the native `ImageBackground` component.

#### Bug Fixes

- Fixed a bug that caused the error image not to display.

#### Changes

- Updated documentation.

## [0.0.9] (2023-10-19)

#### Improvements
Expand Down Expand Up @@ -77,6 +91,7 @@

- Initial release.

[0.0.10]: https://github.com/xerdnu/react-native-blasted-image/compare/v0.0.9...v0.0.10
[0.0.9]: https://github.com/xerdnu/react-native-blasted-image/compare/v0.0.8...v0.0.9
[0.0.8]: https://github.com/xerdnu/react-native-blasted-image/compare/v0.0.7...v0.0.8
[0.0.7]: https://github.com/xerdnu/react-native-blasted-image/compare/v0.0.6...v0.0.7
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ import BlastedImage from 'react-native-blasted-image';
| `width` | `Number` | (Optional) Specifies the width of the image. `Overrides width in style` | 100 |
| `height` | `Number` | (Optional) Specifies the height of the image. `Overrides height in style` | 100 |
| `resizeMode` | `String` | (Optional) Resize the image with one of the options: `cover` `contain` `center` `stretch` | cover |
| `isBackground` | `Boolean` | (Optional) Makes the image act as a container background similar to the native `ImageBackground` component | false |
| `style` | `Object` | (Optional) Styles to be applied to the image, e.g., `{borderRadius:20}`.<br>See [View Style Props](https://reactnative.dev/docs/view-style-props) for all available styles. | - |

## Methods
Expand Down
65 changes: 51 additions & 14 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ const NativeBlastedImage = NativeModules.BlastedImage
return NativeBlastedImage.loadImage(imageUrl, headers, skipMemoryCache)
.catch((error) => {
console.error("Error loading image:", error);
throw error;
});
};

const BlastedImageView = requireNativeComponent('BlastedImageView');

const BlastedImage = ({ source, width, height, style, resizeMode }) => {
const BlastedImage = ({ source, width, height, style, resizeMode, isBackground, children }) => {
const [error, setError] = useState(false);

if (!source || (!source.uri && typeof source !== 'number')) {
Expand Down Expand Up @@ -59,7 +60,7 @@ const BlastedImage = ({ source, width, height, style, resizeMode }) => {
// Flatten styles if provided as an array, otherwise use style as-is
const flattenedStyle = Array.isArray(style) ? Object.assign({}, ...style) : style;

const defaultStyle = { overflow: 'hidden', backgroundColor: style?.borderColor || 'transparent' }; // Use border color as background
const defaultStyle = { overflow: 'hidden', position: 'relative', backgroundColor: style?.borderColor || 'transparent' }; // Use border color as background

const {
width: styleWidth, // Get width from style
Expand Down Expand Up @@ -100,26 +101,62 @@ const BlastedImage = ({ source, width, height, style, resizeMode }) => {
height,
};

const childrenStyle = {
position: 'absolute',
top: 0,
left: 0,
justifyContent:'center',
alignItems:'center',
width: adjustedWidth,
height: adjustedHeight,
};

return (
<View style={viewStyle}>
{error ? (
<Image source={require('./assets/image-error.png')} style={{width:adjustedHeight,height:adjustedHeight}} resizeMode={resizeMode} />
) : (typeof source === 'number') ? (
<Image source={source} style={{ width: adjustedWidth, height: adjustedHeight }} resizeMode={resizeMode} />
<View style={!isBackground ? viewStyle : null}>
{isBackground ? (
<View style={viewStyle}>
{renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode)}
</View>
) : (
<BlastedImageView
sourceUri={source.uri}
width={adjustedWidth}
height={adjustedHeight}
resizeMode={resizeMode}
/>
renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode)
)}
{isBackground && <View style={childrenStyle}>{children}</View>}
</View>
);
};

function renderImageContent(error, source, adjustedHeight, adjustedWidth, resizeMode) {
if (error) {
return (
<Image
source={require('./assets/image-error.png')}
style={{ width: adjustedHeight, height: adjustedHeight }}
resizeMode={resizeMode}
/>
);
} else if (typeof source === 'number') {
return (
<Image
source={source}
style={{ width: adjustedWidth, height: adjustedHeight }}
resizeMode={resizeMode}
/>
);
} else {
return (
<BlastedImageView
sourceUri={source.uri}
width={adjustedWidth}
height={adjustedHeight}
resizeMode={resizeMode}
/>
);
}
}

BlastedImage.defaultProps = {
resizeMode: "cover"
resizeMode: "cover",
isBackground: false
};

// clear memory cache
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-blasted-image",
"version": "0.0.9",
"version": "0.0.10",
"description": "A simple yet powerful image component for React Native, powered by Glide and SDWebImage",
"main": "index.js",
"scripts": {
Expand All @@ -10,7 +10,9 @@
"react-native",
"cached",
"image",
"image-cache"
"image-cache",
"fastimage",
"fast-image"
],
"author": "xerdnu",
"license": "MIT, BSD, Apache-2.0",
Expand Down

0 comments on commit a9a30ca

Please sign in to comment.