From 6ce26c89412bb54f9be4c0de3d92c3f74d170bc1 Mon Sep 17 00:00:00 2001 From: Max Lynch Date: Mon, 21 Dec 2020 18:11:20 -0600 Subject: [PATCH] Modal --- components/Modal.jsx | 74 ++++++++++++++++++++++++++++++++++++++++++++ components/Nav.jsx | 22 ++++++------- package-lock.json | 16 ++++++++++ package.json | 1 + pages/index.js | 45 +++++++++++++++++++++++---- store/index.js | 8 +++++ 6 files changed, 147 insertions(+), 19 deletions(-) create mode 100644 components/Modal.jsx create mode 100644 store/index.js diff --git a/components/Modal.jsx b/components/Modal.jsx new file mode 100644 index 0000000..56b6952 --- /dev/null +++ b/components/Modal.jsx @@ -0,0 +1,74 @@ +import classNames from 'classnames'; +import { useLayoutEffect, useRef, useState } from 'react'; +import { useDrag } from 'react-use-gesture'; + +const Modal = ({ open, onClose, children }) => { + const ref = useRef(); + const [dragging, setDragging] = useState(false); + const [rect, setRect] = useState(null); + const [y, setY] = useState(100000); + + useLayoutEffect(() => { + const rect = ref.current?.getBoundingClientRect(); + setRect(rect); + setY(-rect.width); + }, []); + + useLayoutEffect(() => { + if (open) { + setY(0); + } else if (rect) { + setY(rect.height); + } + }, [rect, open]); + + const bind = useDrag( + ({ down, movement: [mx, my] }) => { + setY(my < 0 ? 0 : my); + + if (down) { + setDragging(true); + } else { + setDragging(false); + } + + // If the drag ended, snap the menu back + if (!down) { + const mid = rect.height; + if (y > mid / 2) { + // Close + setY(rect.height); + onClose(); + } else { + // Re-open + setY(0); + } + } + }, + { + axis: 'y', + } + ); + + return ( +
+ {children} +
+ ); +}; + +export default Modal; diff --git a/components/Nav.jsx b/components/Nav.jsx index 3aaa7ea..82c8470 100644 --- a/components/Nav.jsx +++ b/components/Nav.jsx @@ -1,6 +1,7 @@ import { useEffect, useState } from 'react'; import { Plugins } from '@capacitor/core'; +import Store from '../store'; const Nav = ({ page, onShowMenu }) => { const [showMenu, setShowMenu] = useState(false); @@ -77,18 +78,6 @@ const Nav = ({ page, onShowMenu }) => {

{page.title}

- {/*} - Workflow - Workflow - */}
@@ -121,7 +110,14 @@ const Nav = ({ page, onShowMenu }) => {
-