generated from eea/volto-addon-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added ScreenSize AppExtras with screen store
This is needed for saving the width & height of the screen on resize
- Loading branch information
Showing
12 changed files
with
147 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
/* eslint-disable no-extend-native */ | ||
import React from 'react'; | ||
import { connect } from 'react-redux'; | ||
import { compose } from 'redux'; | ||
import { setScreen } from '@eeacms/volto-block-style/actions'; | ||
|
||
const pixelToNumber = (pixel) => { | ||
return parseInt(pixel.replace('px', '')); | ||
}; | ||
|
||
Number.prototype.toPixel = function toPixel() { | ||
return `${this}px`; | ||
}; | ||
|
||
const ScreenSize = (props) => { | ||
const updateScreen = () => { | ||
const screenHeight = | ||
window.innerHeight || | ||
document.documentElement.clientHeight || | ||
document.body.clientHeight || | ||
0; | ||
const screenWidth = | ||
window.innerWidth || | ||
document.documentElement.clientWidth || | ||
document.body.clientWidth || | ||
0; | ||
const headerWrapper = document.querySelector( | ||
'.header-wrapper:not(.mobile)', | ||
); | ||
const contentArea = document.querySelector('.ui.segment.content-area'); | ||
const toolbar = document.querySelector('#toolbar .toolbar.expanded'); | ||
const firstHeading = document.querySelector('.documentFirstHeading'); | ||
const headerWrapperStyle = headerWrapper | ||
? window.getComputedStyle(headerWrapper) | ||
: {}; | ||
const contentAreaStyle = | ||
contentArea && !firstHeading | ||
? window.getComputedStyle(contentArea) | ||
: { marginTop: '0px', paddingTop: '0px' }; | ||
|
||
const offsetHeight = | ||
(headerWrapperStyle.display !== 'none' | ||
? headerWrapper?.offsetHeight || 0 | ||
: 0) + | ||
(pixelToNumber(contentAreaStyle.marginTop) + | ||
pixelToNumber(contentAreaStyle.paddingTop) || 0) + | ||
((toolbar?.offsetHeight || 0) < screenHeight | ||
? toolbar?.offsetHeight || 0 | ||
: 0); | ||
const newScreen = { | ||
screenHeight, | ||
screenWidth, | ||
offsetHeight, | ||
}; | ||
|
||
props.setScreen({ ...props.screen, ...newScreen }); | ||
}; | ||
|
||
React.useEffect(() => { | ||
if (__CLIENT__) { | ||
updateScreen(); | ||
window.addEventListener('resize', updateScreen); | ||
} | ||
return () => { | ||
if (__CLIENT__) { | ||
window.removeEventListener('resize', updateScreen); | ||
} | ||
}; | ||
/* eslint-disable-next-line */ | ||
}, []); | ||
|
||
return ''; | ||
}; | ||
|
||
export default compose( | ||
connect( | ||
(state, props) => ({ | ||
screen: state.screen, | ||
}), | ||
{ setScreen }, | ||
), | ||
)(ScreenSize); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import ScreenSize from './ScreenSize'; | ||
|
||
export default ScreenSize; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import ScreenSize from './ScreenSize'; | ||
|
||
export default (config) => { | ||
config.settings.appExtras = [ | ||
...(config.settings.appExtras || []), | ||
{ | ||
match: '/**', | ||
component: ScreenSize, | ||
}, | ||
]; | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './screen'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export const setScreen = (screen = {}) => { | ||
return { | ||
type: 'SET_SCREEN', | ||
screen, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
import withCachedImages from './withCachedImages'; | ||
import withScreenHeight from './withScreenHeight'; | ||
|
||
export { withCachedImages, withScreenHeight }; | ||
export { withCachedImages }; |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import screen from './screen'; | ||
|
||
export default (config) => { | ||
config.addonReducers = { ...(config.addonReducers || {}), screen }; | ||
|
||
return config; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
const initialState = {}; | ||
|
||
export default function screen(state = initialState, action = {}) { | ||
switch (action.type) { | ||
case 'SET_SCREEN': | ||
return { | ||
...state, | ||
...action.screen, | ||
}; | ||
|
||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters