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 image loads on resize window #26543

Merged
merged 7 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import React from 'react';
import PropTypes from 'prop-types';
import {View, PixelRatio} from 'react-native';
import useWindowDimensions from '../../../hooks/useWindowDimensions';
import styles from '../../../styles/styles';

const propTypes = {
/** Cell Container styles */
style: PropTypes.oneOfType([PropTypes.arrayOf(PropTypes.object), PropTypes.object]),
};

const defaultProps = {
style: [],
};

function AttachmentCarouselCellRenderer(props) {
const {windowWidth, isSmallScreenWidth} = useWindowDimensions();
const modalStyles = styles.centeredModalStyles(isSmallScreenWidth, true);
const style = [props.style, styles.h100, {width: PixelRatio.roundToNearestPixel(windowWidth - (modalStyles.marginHorizontal + modalStyles.borderWidth) * 2)}];

return (
<View
// eslint-disable-next-line react/jsx-props-no-spreading
{...props}
style={style}
/>
);
}

AttachmentCarouselCellRenderer.propTypes = propTypes;
AttachmentCarouselCellRenderer.defaultProps = defaultProps;
AttachmentCarouselCellRenderer.displayName = 'AttachmentCarouselCellRenderer';

export default React.memo(AttachmentCarouselCellRenderer);
29 changes: 2 additions & 27 deletions src/components/Attachments/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ import ONYXKEYS from '../../../ONYXKEYS';
import withLocalize from '../../withLocalize';
import compose from '../../../libs/compose';
import useCarouselArrows from './useCarouselArrows';
import useWindowDimensions from '../../../hooks/useWindowDimensions';
import CarouselItem from './CarouselItem';
import Navigation from '../../../libs/Navigation/Navigation';
import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
import variables from '../../../styles/variables';
import AttachmentCarouselCellRenderer from './AttachmentCarouselCellRenderer';

const canUseTouchScreen = DeviceCapabilities.canUseTouchScreen();
const viewabilityConfig = {
Expand All @@ -30,8 +30,6 @@ const viewabilityConfig = {
function AttachmentCarousel({report, reportActions, source, onNavigate, setDownloadButtonVisibility, translate}) {
const scrollRef = useRef(null);

const {windowWidth, isSmallScreenWidth} = useWindowDimensions();

const [containerWidth, setContainerWidth] = useState(0);
const [page, setPage] = useState(0);
const [attachments, setAttachments] = useState([]);
Expand Down Expand Up @@ -117,29 +115,6 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
[containerWidth],
);

/**
* Defines how a container for a single attachment should be rendered
* @param {Object} cellRendererProps
* @returns {JSX.Element}
*/
const renderCell = useCallback(
(cellProps) => {
// Use window width instead of layout width to address the issue in https://github.com/Expensify/App/issues/17760
// considering horizontal margin and border width in centered modal
const modalStyles = styles.centeredModalStyles(isSmallScreenWidth, true);
const style = [cellProps.style, styles.h100, {width: PixelRatio.roundToNearestPixel(windowWidth - (modalStyles.marginHorizontal + modalStyles.borderWidth) * 2)}];

return (
<View
// eslint-disable-next-line react/jsx-props-no-spreading
{...cellProps}
style={style}
/>
);
},
[isSmallScreenWidth, windowWidth],
);

/**
* Defines how a single attachment should be rendered
* @param {Object} item
Expand Down Expand Up @@ -211,7 +186,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
windowSize={5}
maxToRenderPerBatch={3}
data={attachments}
CellRendererComponent={renderCell}
CellRendererComponent={AttachmentCarouselCellRenderer}
renderItem={renderItem}
getItemLayout={getItemLayout}
keyExtractor={(item) => item.source}
Expand Down
6 changes: 4 additions & 2 deletions src/components/withWindowDimensions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import React, {forwardRef, createContext, useState, useEffect} from 'react';
import PropTypes from 'prop-types';
import lodashDebounce from 'lodash/debounce';
import {Dimensions} from 'react-native';
import {SafeAreaInsetsContext} from 'react-native-safe-area-context';
import getComponentDisplayName from '../../libs/getComponentDisplayName';
Expand Down Expand Up @@ -44,14 +45,15 @@ function WindowDimensionsProvider(props) {
useEffect(() => {
const onDimensionChange = (newDimensions) => {
const {window} = newDimensions;

setWindowDimension({
windowHeight: window.height,
windowWidth: window.width,
});
};

const dimensionsEventListener = Dimensions.addEventListener('change', onDimensionChange);
const onDimensionChangeDebounce = lodashDebounce(onDimensionChange, 300);
Copy link
Contributor

Choose a reason for hiding this comment

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

Coming from #28698.
We still want the position of the emoji modal to change instantly based on window size when toggling show/hide, so we switched it to useWindowDimensions without debounce. : )


const dimensionsEventListener = Dimensions.addEventListener('change', onDimensionChangeDebounce);

return () => {
if (!dimensionsEventListener) {
Expand Down
Loading