Skip to content

Commit

Permalink
lazyload bg img on footer also
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiggr committed Feb 20, 2023
1 parent a0633c9 commit ae07162
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/helpers/index.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
export useClickOutside from './useClickOutside';
export usePrevious from './usePrevious';
export useOnScreen from './useOnScreen';
27 changes: 27 additions & 0 deletions src/helpers/useOnScreen.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/* eslint-disable react-hooks/exhaustive-deps */
import React from 'react';

function useOnScreen(ref, rootMargin = '0px') {
// State and setter for storing whether element is visible
const [isIntersecting, setIntersecting] = React.useState(false);
React.useEffect(() => {
const observer = new IntersectionObserver(
([entry]) => {
// Update our state when observer callback fires
setIntersecting(entry.isIntersecting);
},
{
rootMargin,
},
);
if (ref.current) {
observer.observe(ref.current);
}
return () => {
observer.unobserve(ref.current);
};
}, []); // Empty array ensures that effect is only run on mount and unmount
return isIntersecting;
}

export { useOnScreen };
9 changes: 8 additions & 1 deletion src/ui/Footer/Footer.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import FooterHeader from '@eeacms/volto-eea-design-system/ui/Footer/FooterHeader
import SubFooter from '@eeacms/volto-eea-design-system/ui/Footer/SubFooter';
import Social from '@eeacms/volto-eea-design-system/ui/Footer/Social';
import Contact from '@eeacms/volto-eea-design-system/ui/Footer/Contact';
import { useOnScreen } from '../../helpers/useOnScreen';

/**
* Component to display the footer.
Expand All @@ -22,9 +23,15 @@ import Contact from '@eeacms/volto-eea-design-system/ui/Footer/Contact';

const Footer = (props) => {
const { children } = props;

const bgImgRef = React.useRef();
const onScreen = useOnScreen(bgImgRef, '-10px');
return (
<footer id={'footer'}>
<div className="footer-wrapper">
<div
ref={bgImgRef}
className={onScreen ? 'footer-wrapper' : 'footer-wrapper-nobg'}
>
<Container>{children}</Container>
</div>
</footer>
Expand Down
5 changes: 4 additions & 1 deletion theme/themes/eea/extras/footer.less
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ footer {
background-position: @wrapperBackgroundPosition;
background-size: @mobileWrapperBackgroundSize;
}
.footer-wrapper-nobg {
width: @wrapperWidth;
padding: @mobileWrapperPadding;
}
}

footer .theme-sites {
Expand Down Expand Up @@ -201,7 +205,6 @@ footer .footer-wrapper .menu {
}
}
}

}

@media only screen and (min-width: @computerBreakpoint) {
Expand Down

0 comments on commit ae07162

Please sign in to comment.