Skip to content

Commit 36900cc

Browse files
author
Luke Farrell
committed
Added an updateCollageSize func to dynamically adjust size of collage
1 parent b321723 commit 36900cc

4 files changed

Lines changed: 37 additions & 13 deletions

File tree

README.md

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ The number in the first bracket will be the configuration you want to access. E.
8787
- If you want to capture the collage as a single image. Take a look at [react-native-view-shot](https://github.com/gre/react-native-view-shot).
8888
- The number of images has to be equal to the sum of the matrix. e.g. Matrix is [ 1, 2, 1 ] ( 1 + 2 + 1 = 4), there has to be 4 images.
8989
- The collage scaling will not work when in a [Modal](https://facebook.github.io/react-native/docs/modal) component. [Multiple touches are not registered](https://github.com/facebook/react-native/issues/8094).
90+
- Do NOT update height or width props dynamically to change the size of the collage (as image sizes won't be re-calculated correctly, this is due to a race condition in measuring the layout of the collage). Use `ref.current.updateCollageSize({ width, height })` instead to adjust size dynamically.
9091

9192
## Replacing Images
9293

@@ -134,8 +135,26 @@ collageRef.current.replaceImage("https://picsum.photos/200", m, i);
134135
| imageSwapStyleReset | object | Yes | style | The reverse of imageSwapStyle to reset style after swap. Vital for direct manipulation. |
135136
| separatorStyle | object | Yes | style | Style applied to image container. Use border width to create margin between images. |
136137
| containerStyle | object | Yes | style | Style applied to the container of the collage. Collage border can be applied here. |
137-
| imageContainerStyle | object | Yes | style | Style applied to each image container. |
138-
| imageFocussedStyle | object | Yes | style | Style applied to the focused image container. |
138+
| imageContainerStyle | object | Yes | style | Style applied to each image container. |
139+
| imageFocussedStyle | object | Yes | style | Style applied to the focused image container. |
140+
141+
## API Reference
142+
143+
### `updateCollageSize({ width, height })`
144+
145+
Updates the collage width or height (NOTE: width and height props should always be static)
146+
147+
- `size: Object` - new size for collage to be calculated. Currently supported options are:
148+
- `width : number` new width of the collage.
149+
- `height : number` new height of the collage.
150+
151+
### `replaceImage(source, m, i)`
152+
153+
Replaces an image at the matrix and index of the collage
154+
155+
- `source: string | number` - A local file asset or uri
156+
- `m : number` the matrix of the collage (you can think of this as the row or column).
157+
- `i : number` index inside the matrix (you can think of this as index inside the row or column)
139158

140159
## Showcase
141160

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "react-native-images-collage",
3-
"version": "3.3.2",
3+
"version": "3.3.3",
44
"description": "Customizable image grid component for React Native",
55
"keywords": [
66
"react-native",

src/CollageImage.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,6 @@ class CollageImage extends React.Component {
307307
panningRightPadding,
308308
panningTopPadding,
309309
panningBottomPadding,
310-
parentWidth,
311-
parentHeight,
312310
} = this.props;
313311
const { width, height } = this.state;
314312

@@ -323,12 +321,7 @@ class CollageImage extends React.Component {
323321
this.bottomEdgeMax = this.bottomEdge + panningBottomPadding;
324322

325323
// Auto resize collage images when matrix, direction, or collage size is updated
326-
if (
327-
matrix !== prevProps.matrix ||
328-
direction !== prevProps.direction ||
329-
parentHeight !== prevProps.parentHeight ||
330-
parentWidth !== prevProps.parentWidth
331-
) {
324+
if (matrix !== prevProps.matrix || direction !== prevProps.direction) {
332325
if (this.snapAnimation != null) {
333326
// INTERRUPT ANIMATION
334327
this.snapAnimation.stop();

src/DynamicCollage.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,6 @@ class DynamicCollage extends React.Component {
7979
imageContainerStyle={this.props.imageContainerStyle}
8080
onImageFocus={(event) => this.setImageFocusId(event, m, i)}
8181
imageFocussedStyle={this.props.imageFocussedStyle}
82-
parentWidth={width}
83-
parentHeight={height}
8482
/>
8583
);
8684
});
@@ -319,6 +317,20 @@ class DynamicCollage extends React.Component {
319317
return { ...boundries, relativeContainerWidth, relativeContainerHeight };
320318
}
321319

320+
/**
321+
* Updates the collage width and height
322+
*
323+
* @param {Object} size - new size for collage
324+
* @param {number} size.width - width of collage
325+
* @param {number} size.height - height of collage
326+
*/
327+
updateCollageSize({ width, height }) {
328+
this.setState({
329+
collageHeight: height ? height : this.state.collageHeight,
330+
collageWidth: width ? width : this.state.collageWidth,
331+
});
332+
}
333+
322334
/**
323335
* Function used to replace an image in the collage
324336
*

0 commit comments

Comments
 (0)