Skip to content
This repository has been archived by the owner on Jun 8, 2022. It is now read-only.

Commit

Permalink
Update version, handle no 'window' var + add next js example
Browse files Browse the repository at this point in the history
  • Loading branch information
Cody Nguyen committed Sep 20, 2021
1 parent 5acb217 commit 87b455d
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "simi-pagebuilder-react",
"version": "1.2.14",
"version": "1.2.17",
"description": "SimiCart React PageBuilder Client",
"author": "SimiCart",
"license": "MIT",
Expand Down
10 changes: 6 additions & 4 deletions src/Content/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -165,10 +165,12 @@ const PbContent = (props) => {
)
history.push(item.dataParsed.openUrl);
else {
window.open(
item.dataParsed.openUrl,
openUrlInNewTab ? '_blank' : '_self',
);
if (typeof window !== 'undefined') {
window.open(
item.dataParsed.openUrl,
openUrlInNewTab ? '_blank' : '_self',
);
}
}
};
}
Expand Down
5 changes: 4 additions & 1 deletion src/Helper/Data.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ export const randomString = (charCount = 20) => {
'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < charCount; i++)
text += possible.charAt(Math.floor(Math.random() * possible.length));
return btoa(text + Date.now());

return typeof window !== 'undefined'
? window.btoa(text + Date.now())
: text + Date.now();
};

export const listToTree = (list) => {
Expand Down
49 changes: 30 additions & 19 deletions src/hooks/useWindowSize.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import { useLayoutEffect, useState } from 'react';

export const useWindowSize = () => {
const [size, setSize] = useState({ width: 0, height: 0 });
const [size, setSize] = useState({
width: typeof window !== 'undefined' ? 0 : 1280,
height: typeof window !== 'undefined' ? 0 : 1280,
});
useLayoutEffect(() => {
function updateSize() {
const newWidth = Math.max(
document.documentElement.clientWidth || 0,
window.innerWidth || 0,
);
const newHeight = window.innerHeight;
if (
newWidth !== window.smpbWindowWidth ||
Math.abs(newHeight - window.smpbWindowHeight) > 160
) {
window.smpbWindowWidth = newWidth;
window.smpbWindowHeight = newHeight;
setSize({
width: newWidth,
height: newHeight,
});
if (typeof window !== 'undefined') {
const newWidth = Math.max(
document.documentElement.clientWidth || 0,
window.innerWidth || 0,
);
const newHeight = window.innerHeight;
if (
newWidth !== window.smpbWindowWidth ||
Math.abs(newHeight - window.smpbWindowHeight) > 160
) {
window.smpbWindowWidth = newWidth;
window.smpbWindowHeight = newHeight;
setSize({
width: newWidth,
height: newHeight,
});
}
}
}
window.addEventListener('resize', updateSize);
updateSize();
return () => window.removeEventListener('resize', updateSize);
if (typeof window !== 'undefined') {
window.addEventListener('resize', updateSize);
updateSize();
}
return () => {
if (typeof window !== 'undefined') {
window.removeEventListener('resize', updateSize);
}
};
}, []);
return size;
};
9 changes: 7 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ export const usePbFinder = (props) => {

const findPage = (pathName) => {
setPathFoFind(pathName);
if (window.smPbPagesByToken) {
if (typeof window !== 'undefined' && window.smPbPagesByToken) {
setPbData(window.smPbPagesByToken);
} else {
if (!loading) {
Expand All @@ -335,7 +335,12 @@ export const usePbFinder = (props) => {
endPoint,
(result) => {
setLoading(false);
if (result && result.data && result.data.spb_page)
if (
result &&
result.data &&
result.data.spb_page &&
typeof window !== 'undefined'
)
window.smPbPagesByToken = result;
setPbData(result);
},
Expand Down

0 comments on commit 87b455d

Please sign in to comment.