Skip to content

Commit

Permalink
don't run screen update on scroll event + add withScreenSize hocs
Browse files Browse the repository at this point in the history
  • Loading branch information
razvanMiu committed Mar 22, 2023
1 parent d399e34 commit ed17a5c
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
10 changes: 6 additions & 4 deletions src/ScreenSize/ScreenSize.jsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
/* eslint-disable no-extend-native */
import React from 'react';
import { compose } from 'redux';
import cs from 'classnames';
import config from '@plone/volto/registry';
import { BodyClass } from '@plone/volto/helpers';
import { updateScreen } from '../actions';
import { detectTouchScreen } from '../utils';
import { withScreenSize } from '../hocs';

if (!Number.prototype.toPixel) {
Number.prototype.toPixel = function toPixel() {
Expand Down Expand Up @@ -34,6 +36,7 @@ class ScreenSize extends React.Component {
const screen = {
height: window.screen.availHeight,
width: window.screen.availWidth,
browserToolbarHeight: window.outerHeight - window.innerHeight,
};

const page = {
Expand Down Expand Up @@ -77,16 +80,15 @@ class ScreenSize extends React.Component {
if (__SERVER__) return;
this.updateScreen({
hasTouchScreen: detectTouchScreen(),
browserToolbarHeight: window.outerHeight - window.innerHeight,
});
window.addEventListener('resize', debounce(this.updateScreen));
window.addEventListener('scroll', debounce(this.updateScreen));
// window.addEventListener('scroll', debounce(this.updateScreen));
}

componentWillUnmount() {
if (__SERVER__) return;
window.removeEventListener('resize', debounce(this.updateScreen));
window.removeEventListener('scroll', debounce(this.updateScreen));
// window.removeEventListener('scroll', debounce(this.updateScreen));
}

componentDidUpdate(prevProps) {
Expand All @@ -105,4 +107,4 @@ class ScreenSize extends React.Component {
}
}

export default ScreenSize;
export default compose(withScreenSize)(ScreenSize);
2 changes: 1 addition & 1 deletion src/ScreenSize/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export default (config) => {
config.settings.appExtras = [
...(config.settings.appExtras || []),
{
match: '/**',
match: '',
component: ScreenSize,
},
];
Expand Down
3 changes: 3 additions & 0 deletions src/hocs/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import withScreenSize from './withScreenSize';

export { withScreenSize };
10 changes: 10 additions & 0 deletions src/hocs/withScreenSize.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import React from 'react';
import { useSelector } from 'react-redux';

export default function withScreenSize(WrappedComponent) {
return (props) => {
const screen = useSelector((state) => state.screen);

return <WrappedComponent {...props} screen={screen} />;
};
}

0 comments on commit ed17a5c

Please sign in to comment.