Skip to content

Commit

Permalink
Used cached images for background image
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Apr 2, 2021
1 parent 474e66c commit 232d50f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 15 deletions.
31 changes: 20 additions & 11 deletions src/StyleWrapper/StyleWrapperView.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import React from 'react';
import cx from 'classnames';
import config from '@plone/volto/registry';
import { withScreenHeight } from '@eeacms/volto-block-style/hocs';
import {
withCachedImages,
withScreenHeight,
} from '@eeacms/volto-block-style/hocs';

export function getInlineStyles(data, props = {}) {
return {
Expand Down Expand Up @@ -83,16 +86,18 @@ const StyleWrapperView = (props) => {
children
) : (
<div {...attrs} ref={props.setRef}>
{backgroundImage ? (
<div
className="bg"
style={{
backgroundImage: `url(${backgroundImage}/@@images/image`,
}}
{Object.keys(props.images || {}).map((bgImage) => (
<img
key={`styled-bg-image-${bgImage}`}
alt=""
src={props.images[bgImage]?.src}
className={cx('bg', {
hidden:
backgroundImage !== bgImage || !props.images[bgImage]?.src,
})}
/>
) : (
''
)}
))}

{ViewComponentWrapper ? <ViewComponentWrapper {...props} /> : children}
</div>
)
Expand All @@ -103,4 +108,8 @@ const StyleWrapperView = (props) => {
);
};

export default withScreenHeight(StyleWrapperView);
export default withScreenHeight(
withCachedImages(StyleWrapperView, {
getImage: (props) => props.styleData.backgroundImage || null,
}),
);
3 changes: 2 additions & 1 deletion src/hocs/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import withCachedImages from './withCachedImages';
import withScreenHeight from './withScreenHeight';

export { withScreenHeight };
export { withCachedImages, withScreenHeight };
32 changes: 32 additions & 0 deletions src/hocs/withCachedImages.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import React from 'react';
import { isEmpty } from 'lodash';

export default function withCachedImages(WrappedComponent, config = {}) {
return (props) => {
const mounted = React.useRef(false);
const [images, setImages] = React.useState({});
const image = isEmpty(config.getImage?.(props))
? null
: config.getImage(props);

React.useEffect(() => {
if (!mounted.current) mounted.current = true;
if (image && !images[image]) {
const newImage = new Image();
newImage.src = `${image}/@@images/image`;
setImages({ ...images, [image]: null });
newImage.onload = () => {
if (mounted.current) {
setImages({ ...images, [image]: newImage });
}
};
}
return () => {
mounted.current = false;
};
/* eslint-disable-next-line */
}, [image]);

return <WrappedComponent {...props} images={images} />;
};
}
15 changes: 12 additions & 3 deletions src/styles.less
Original file line number Diff line number Diff line change
Expand Up @@ -83,16 +83,25 @@
}
}

.bg {
img.bg {
/* Set up positioning */
position: absolute;
top: 0;
left: 0;
/* Set up proportionate scaling */
width: 100%;
height: 100%;
background-position: center;
background-size: cover;
object-fit: cover;
z-index: -1;
pointer-events: none;

opacity: 1;
transition: opacity .5s;

&.hidden {
opacity: 0;
transition: opacity .5s;
}
}
}

Expand Down

0 comments on commit 232d50f

Please sign in to comment.