Skip to content

Commit 01cc93b

Browse files
authored
feat: add UI to close about license modals
* Explicit close for about and license modals * Close about/license models with click outside the modal.
1 parent 1351d5f commit 01cc93b

File tree

3 files changed

+60
-19
lines changed

3 files changed

+60
-19
lines changed

src/AboutModal.tsx

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import React from "react";
12
import { useModalRef } from "./misc/useModalRef";
23

34
import cannonKeys from "./assets/cannonkeys.png";
@@ -173,19 +174,27 @@ const sponsors = [
173174
];
174175

175176
export const AboutModal = ({ open, onClose }: AboutModalProps) => {
176-
const ref = useModalRef(open);
177+
const ref = useModalRef(open, true);
177178

178179
return (
179180
<dialog
180181
ref={ref}
181-
onClose={onClose}
182182
className="p-5 rounded-lg border-text-base border min-w-min w-[70vw]"
183+
onClose={onClose}
183184
>
184-
<p className="py-1">
185-
ZMK Studio is made possible thanks to the generous donation of time from
186-
our contributors, as well as the financial sponsorship from the
187-
following vendors:
188-
</p>
185+
<div className="flex justify-between items-start">
186+
<p className="py-1 mr-2">
187+
ZMK Studio is made possible thanks to the generous donation of time
188+
from our contributors, as well as the financial sponsorship from the
189+
following vendors:
190+
</p>
191+
<button
192+
className="p-1.5 rounded-md bg-gray-100 text-black hover:bg-gray-300"
193+
onClick={onClose}
194+
>
195+
Close
196+
</button>
197+
</div>
189198
<div className="grid gap-2 auto-rows-auto grid-cols-[auto_minmax(min-content,1fr)] justify-items-center items-center">
190199
{sponsors.map((s) => {
191200
const heightVariants = {
@@ -195,7 +204,7 @@ export const AboutModal = ({ open, onClose }: AboutModalProps) => {
195204
};
196205

197206
return (
198-
<>
207+
<React.Fragment key={s.level}>
199208
<label>{s.level}</label>
200209
<div
201210
className={`grid grid-rows-1 gap-x-1 auto-cols-fr grid-flow-col justify-items-center items-center ${
@@ -210,7 +219,7 @@ export const AboutModal = ({ open, onClose }: AboutModalProps) => {
210219
};
211220

212221
return (
213-
<a href={v.url} target="_blank">
222+
<a key={v.name} href={v.url} target="_blank">
214223
<picture aria-label={v.name}>
215224
{v.darkModeImg && (
216225
<source
@@ -225,7 +234,7 @@ export const AboutModal = ({ open, onClose }: AboutModalProps) => {
225234
);
226235
})}
227236
</div>
228-
</>
237+
</React.Fragment>
229238
);
230239
})}
231240
</div>

src/misc/LicenseNoticeModal.tsx

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,28 @@ export const LicenseNoticeModal = ({
1111
open,
1212
onClose,
1313
}: LicenseNoticeModalProps) => {
14-
const ref = useModalRef(open);
14+
const ref = useModalRef(open, true);
15+
1516
return (
1617
<dialog
1718
ref={ref}
18-
onClose={onClose}
1919
className="p-5 rounded-lg border-text-base border min-w-min w-[60vw]"
20+
onClose={onClose}
2021
>
2122
<div>
22-
<p>
23-
ZMK Studio is released under the open source Apache 2.0 license. A
24-
copy of the NOTICE file from the ZMK Studio repository is included
25-
here:
26-
</p>
23+
<div className="flex justify-between items-start">
24+
<p className="mr-2">
25+
ZMK Studio is released under the open source Apache 2.0 license. A
26+
copy of the NOTICE file from the ZMK Studio repository is included
27+
here:
28+
</p>
29+
<button
30+
className="p-1.5 rounded-md bg-gray-100 text-black hover:bg-gray-300"
31+
onClick={onClose}
32+
>
33+
Close
34+
</button>
35+
</div>
2736
<pre className="m-4 font-mono text-xs">{NOTICE}</pre>
2837
</div>
2938
</dialog>

src/misc/useModalRef.ts

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { MutableRefObject, useEffect, useRef } from "react";
22

33
export function useModalRef(
4-
open: boolean
4+
open: boolean,
5+
closeOnOutsideClick?: boolean,
56
): MutableRefObject<HTMLDialogElement | null> {
67
const ref = useRef<HTMLDialogElement | null>(null);
78

@@ -10,10 +11,32 @@ export function useModalRef(
1011
if (ref.current && !ref.current?.open) {
1112
ref.current?.showModal();
1213
}
14+
if (closeOnOutsideClick) {
15+
const handleClickOutside = (e: MouseEvent) => {
16+
const target = e.target as HTMLDialogElement | null;
17+
if (!target) return;
18+
19+
const { top, left, width, height } = target.getBoundingClientRect();
20+
const clickedInDialog =
21+
top <= e.clientY &&
22+
e.clientY <= top + height &&
23+
left <= e.clientX &&
24+
e.clientX <= left + width;
25+
26+
if (!clickedInDialog) {
27+
target.close();
28+
}
29+
};
30+
31+
document.addEventListener("mousedown", handleClickOutside);
32+
return () => {
33+
document.removeEventListener("mousedown", handleClickOutside);
34+
};
35+
}
1336
} else {
1437
ref.current?.close();
1538
}
16-
}, [open]);
39+
}, [open, closeOnOutsideClick]);
1740

1841
return ref;
1942
}

0 commit comments

Comments
 (0)