Skip to content

Commit 236d543

Browse files
committed
add animation on open/close
1 parent 3fd4d22 commit 236d543

File tree

4 files changed

+45
-219
lines changed

4 files changed

+45
-219
lines changed

src/bindings/Webapi.res

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Document = {
66
module ClassList = {
77
type t
88
@send external toggle: (t, string) => unit = "toggle"
9+
@send external remove: (t, string) => unit = "remove"
910
}
1011

1112
module Element = {
@@ -14,6 +15,7 @@ module Element = {
1415
@set external setClassName: (Dom.element, string) => unit = "className"
1516
@get external classList: Dom.element => ClassList.t = "classList"
1617
@send external getBoundingClientRect: Dom.element => {..} = "getBoundingClientRect"
18+
@send external addEventListener: (Dom.element, string, unit => unit) => unit = "addEventListener"
1719

1820
module Style = {
1921
@scope("style") @set external width: (Dom.element, string) => unit = "width"

src/components/DocSearch.res

Lines changed: 0 additions & 213 deletions
This file was deleted.

src/components/Search.res

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,23 @@ let transformItems = (items: DocSearch.transformItems) => {
103103
let make = () => {
104104
let (state, setState) = React.useState(_ => Inactive)
105105

106-
React.useEffect1(() => {
106+
let handleCloseModal = () => {
107+
let () = switch ReactDOM.querySelector(".DocSearch-Modal") {
108+
| Some(modal) =>
109+
switch ReactDOM.querySelector("body") {
110+
| Some(body) =>
111+
open Webapi
112+
body->Element.classList->ClassList.remove("DocSearch--active")
113+
modal->Element.addEventListener("transitionend", () => {
114+
setState(_ => Inactive)
115+
})
116+
| None => setState(_ => Inactive)
117+
}
118+
| None => ()
119+
}
120+
}
121+
122+
React.useEffect(() => {
107123
let isEditableTag = el =>
108124
switch el->tagName {
109125
| "TEXTAREA" | "SELECT" | "INPUT" => true
@@ -123,18 +139,22 @@ let make = () => {
123139
switch e.key {
124140
| "/" => focusSearch(e)
125141
| "k" if e.ctrlKey || e.metaKey => focusSearch(e)
126-
| "Escape" => setState(_ => Inactive)
142+
| "Escape" => handleCloseModal()
143+
// setState(_ => Inactive)
127144
| _ => ()
128145
}
129146
}
130147
addKeyboardEventListener("keydown", handleGlobalKeyDown)
131148
Some(() => removeKeyboardEventListener("keydown", handleGlobalKeyDown))
132149
}, [setState])
133150

134-
let onClick = _ => setState(_ => Active)
151+
let onClick = _ => {
152+
setState(_ => Active)
153+
}
135154

136-
let onClose = React.useCallback1(() => {
137-
setState(_ => Inactive)
155+
let onClose = React.useCallback(() => {
156+
// setState(_ => Inactive)
157+
handleCloseModal()
138158
}, [setState])
139159

140160
<>

styles/_docsearch.css

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,18 @@
4242
z-index: 200;
4343
padding: 10vh;
4444
background-color: rgba(0, 0, 0, 0.2);
45-
backdrop-filter: blur(3px);
45+
backdrop-filter: blur(0px);
4646
cursor: inherit !important;
47+
transition: all .05s ease;
4748
@apply fixed top-0 left-0 flex flex-col;
4849
}
4950

51+
.DocSearch--active {
52+
.DocSearch-Container {
53+
backdrop-filter: blur(3px);
54+
}
55+
}
56+
5057
.DocSearch-Container a {
5158
text-decoration: none;
5259
}
@@ -58,6 +65,16 @@
5865

5966
.DocSearch-Modal {
6067
@apply relative min-h-0 mx-auto bg-white w-full flex flex-col shadow-lg max-w-screen-md rounded-lg;
68+
transform: scale(0.95);
69+
opacity: 0;
70+
transition: all .2s ease;
71+
}
72+
73+
.DocSearch--active {
74+
.DocSearch-Modal {
75+
transform: scale(1);
76+
opacity: 1;
77+
}
6178
}
6279

6380
/* Modal Searchbox */

0 commit comments

Comments
 (0)