1
+ /*
2
+ Licensed per https://github.com/privacy-tech-lab/privacy-pioneer/blob/main/LICENSE
3
+ privacy-tech-lab, https://privacytechlab.org/
4
+ */
5
+
6
+ import React from "react" ;
7
+ import styled from "styled-components" ;
8
+ import { motion } from "framer-motion" ;
9
+ import { useHistory , useLocation } from "react-router-dom" ;
10
+
11
+ /**
12
+ * Generally this would be in a style.js file
13
+ * Since it belongs to such a simple component, it's here.
14
+ */
15
+ const SScaffold = styled ( motion . main ) `
16
+ max-width: 1192px;
17
+ width: calc(100% - 128px);
18
+ margin-left: 64px;
19
+ margin-right: 64px;
20
+ margin-top: 16px;
21
+ margin-bottom: 32px;
22
+ display: flex;
23
+ flex-direction: column;
24
+ ` ;
25
+
26
+ /**
27
+ * Implements the basic deisgn visual layout stucture.
28
+ * This is the like the main div for each page (aka each component in view besides AppView).
29
+ * It handles animations from page to page
30
+ * @param {object } props
31
+ */
32
+ const Scaffold = ( props ) => {
33
+ const history = useHistory ( ) ;
34
+ const location = useLocation ( ) ;
35
+
36
+ /**
37
+ * This is an attempt to restore scroll position when navigating between pages using session storage
38
+ * I haven't tried to see if the default scroll restoration works with hash router
39
+ * So you could comment out this fuction and see if navigating between scrolling pages correctly restores
40
+ * I don't think it's working perfectly at the moment
41
+ * @param {object } history
42
+ * @param {object } location
43
+ */
44
+ const configureScrollPosition = ( history , location ) => {
45
+ if (
46
+ history . action === "POP" &&
47
+ location . pathname === history . location . pathname
48
+ ) {
49
+ /**
50
+ * @type {number }
51
+ */
52
+ const pageYOffset =
53
+ parseInt ( window . sessionStorage . getItem ( `pageYOffset-${ location . pathname } ` ) ?? "0" ) ;
54
+ window . sessionStorage . removeItem (
55
+ `pageYOffset-${ history . location . pathname } `
56
+ ) ;
57
+ window . scrollTo ( 0 , pageYOffset ) ;
58
+ } else if (
59
+ history . action === "PUSH" &&
60
+ location . pathname === history . location . pathname
61
+ ) {
62
+ window . scrollTo ( 0 , 0 ) ;
63
+ } else if (
64
+ history . action === "PUSH" &&
65
+ location . pathname !== history . location . pathname
66
+ ) {
67
+ window . sessionStorage . setItem (
68
+ `pageYOffset-${ location . pathname } ` ,
69
+ window . scrollY . toString ( )
70
+ ) ;
71
+ }
72
+ } ;
73
+
74
+ return (
75
+ < SScaffold
76
+ initial = { { opacity : 0 } }
77
+ animate = { { opacity : 1 } }
78
+ exit = { { opacity : 0 } }
79
+ transition = { { duration : 0.25 , type : "tween" , ease : "easeOut" } }
80
+ onAnimationStart = { ( ) => configureScrollPosition ( history , location ) }
81
+ >
82
+ { props . children }
83
+ </ SScaffold >
84
+ ) ;
85
+ } ;
86
+
87
+ export default Scaffold ;
0 commit comments