Skip to content

Fix: Drawer #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Oct 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions src/components/CCIP/Drawer/Drawer.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@
height: 100vh;
z-index: 9999;
background-color: #1a2332e5;
backdrop-filter: blur(var(--space-8x));
backdrop-filter: blur(var(--space-8x)) opacity(0);
transition: backdrop-filter 0.5s ease-in-out;
}

.drawer__open.drawer {
backdrop-filter: blur(var(--space-8x)) opacity(1);
}

.drawer__close {
display: none;
}

.drawer__container {
Expand All @@ -15,8 +24,17 @@
right: 0;
width: 100%;
height: 100vh;
overflow-y: auto;
background-color: white;
transform: translateX(100%);
transition: transform 0.5s ease-in-out;
}

.drawer__content {
overflow-y: auto;
}

.drawer__open .drawer__container {
transform: translateX(0);
}

.drawer__closeMobile {
Expand All @@ -38,7 +56,7 @@
.drawer__close {
position: fixed;
top: var(--space-4x);
right: calc(75% + var(--space-4x));
right: calc(100% + var(--space-4x));
display: flex;
flex-direction: column;
align-items: center;
Expand All @@ -53,6 +71,12 @@
display: flex;
justify-content: center;
align-items: center;
cursor: pointer;
transition: background-color 0.3s ease-in-out;
}

.drawer__close:hover button {
background-color: var(--blue-700);
}

.drawer__close label {
Expand Down
47 changes: 34 additions & 13 deletions src/components/CCIP/Drawer/Drawer.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { useStore } from "@nanostores/react"
import "./Drawer.css"
import { drawerContentStore } from "./drawerStore"
import { useRef, useEffect } from "react"
import { useRef, useEffect, useState } from "react"
import { clsx } from "~/lib"

function Drawer() {
const drawerRef = useRef<HTMLDivElement>(null)
const drawerContentRef = useRef<HTMLDivElement>(null)
const $drawerContent = useStore(drawerContentStore)
const [isOpened, setIsOpened] = useState(false)

// exit when press esc
useEffect(() => {
const handleKeyDown = (event) => {
if (event.key === "Escape") drawerContentStore.set(null)
if (event.key === "Escape") handleClose()
}

document.addEventListener("keydown", handleKeyDown)
Expand All @@ -20,6 +22,10 @@ function Drawer() {
} else {
document.body.style.overflow = "auto"
}

if ($drawerContent) {
setIsOpened(true)
}
return () => {
document.removeEventListener("keydown", handleKeyDown)
document.body.style.overflow = "auto"
Expand All @@ -30,23 +36,38 @@ function Drawer() {

const handleClickOutside = (event) => {
if (drawerRef.current && drawerContentRef.current && !drawerContentRef.current.contains(event.target as Node)) {
drawerContentStore.set(null)
handleClose()
}
}

const handleClose = () => {
setIsOpened(false)

// wait for animation to finish
setTimeout(() => {
drawerContentStore.set(null)
}, 500)
}

return (
<div className="drawer" ref={drawerRef} onClick={handleClickOutside}>
<div className="drawer__close">
<button id="drawer-exit" onClick={() => drawerContentStore.set(null)}>
<svg width="17" height="16" viewBox="0 0 17 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.1667 1.33331L1.83337 14.6666M1.83337 1.33331L15.1667 14.6666" stroke="white" />
</svg>
</button>
<label htmlFor="drawer-exit">Esc</label>
</div>
<div
className={clsx("drawer", {
drawer__open: isOpened,
})}
ref={drawerRef}
onClick={handleClickOutside}
>
<div className="drawer__container" ref={drawerContentRef}>
<div className="drawer__close">
<button id="drawer-exit" onClick={handleClose}>
<svg width="17" height="16" viewBox="0 0 17 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.1667 1.33331L1.83337 14.6666M1.83337 1.33331L15.1667 14.6666" stroke="white" />
</svg>
</button>
<label htmlFor="drawer-exit">Esc</label>
</div>
<div className="drawer__content">
<button onClick={() => drawerContentStore.set(null)} className="drawer__closeMobile">
<button onClick={handleClose} className="drawer__closeMobile">
<svg width="17" height="16" viewBox="0 0 17 16" fill="none" xmlns="http://www.w3.org/2000/svg">
<path d="M15.1667 1.33331L1.83337 14.6666M1.83337 1.33331L15.1667 14.6666" stroke="#000000" />
</svg>
Expand Down