Skip to content

Commit 09cd0c1

Browse files
committed
conditional window calls for server side rendering
1 parent 066f143 commit 09cd0c1

File tree

2 files changed

+30
-18
lines changed

2 files changed

+30
-18
lines changed

src/components/Calendar.jsx

+7-18
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import {
3131
extractUrls,
3232
replaceUrlsWithAnchorTags,
3333
} from '../utils/url-to-link';
34+
import { getAspectRatio, getInitialView, isBrowser, isMinWidth } from '../utils/view-size';
3435

3536
function Calendar() {
3637
const calendarRef = createRef();
@@ -40,28 +41,18 @@ function Calendar() {
4041
const [clickedEvent, setClickedEvent] = useState([]);
4142
const [showEventDetails, setShowEventDetails] = useState(false);
4243
const [eventDetails, setEventDetails] = useState(false);
43-
const [aspectRatio, setAspectRatio] = useState(
44-
window.outerWidth > window.innerHeight
45-
? 1.35
46-
: window.innerWidth / window.innerHeight
47-
);
48-
const [initialView, setInitialView] = useState(
49-
window.outerWidth > 600 ? 'dayGridMonth' : 'timeGridWeek'
50-
);
44+
const [aspectRatio, setAspectRatio] = useState(getAspectRatio());
45+
const [initialView, setInitialView] = useState(getInitialView());
5146

5247
useEscKey(() => setShowEventDetails(false));
5348

5449
const [popupPosition, setPopupPosition] = useState({});
5550

5651
const windowResize = () => {
57-
setAspectRatio(
58-
window.outerWidth > window.innerHeight
59-
? 1.35
60-
: window.innerWidth / window.innerHeight
61-
);
62-
setInitialView(window.outerWidth > 600 ? 'dayGridMonth' : 'timeGridWeek');
52+
setAspectRatio(getAspectRatio());
53+
setInitialView(isMinWidth() ? 'dayGridMonth' : 'timeGridWeek');
6354
setShowEventDetails(false);
64-
window.outerWidth < 600 && setPopupPosition({ left: 0, top: 0 });
55+
!isMinWidth() && setPopupPosition({ left: 0, top: 0 });
6556
};
6657

6758
const createPopupPosition = (event) => {
@@ -85,7 +76,7 @@ function Calendar() {
8576

8677
const handleEventClick = useCallback(
8778
(clickInfo) => {
88-
window.outerWidth > 600 && createPopupPosition(clickInfo.jsEvent);
79+
isMinWidth() && createPopupPosition(clickInfo.jsEvent);
8980
setEventDetails(clickInfo.event);
9081
setShowEventDetails(true);
9182
if (clickedEvent.length) {
@@ -165,8 +156,6 @@ function Calendar() {
165156
<Icon path={mdiClose} size={1} />
166157
</button>
167158
</div>
168-
169-
{/* <div>{seriesICS}</div> */}
170159
<h2 className="event-title">{eventDetails.title}</h2>
171160
<div className="event-time">
172161
<div className="icon">

src/utils/view-size.js

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
export function isBrowser() {
2+
return typeof window !== 'undefined';
3+
}
4+
5+
export function isMinWidth() {
6+
return isBrowser() ? window.outerWidth > 600 : true;
7+
}
8+
9+
export function getAspectRatio() {
10+
return isBrowser()
11+
? window.outerWidth > window.innerHeight
12+
? 1.35
13+
: window.innerWidth / window.innerHeight
14+
: 1.35;
15+
}
16+
17+
export function getInitialView() {
18+
return isBrowser()
19+
? window.outerWidth > 600
20+
? 'dayGridMonth'
21+
: 'timeGridWeek'
22+
: 'dayGridMonth';
23+
}

0 commit comments

Comments
 (0)