Skip to content

Commit

Permalink
feat: use html dialog for the content script modal (#98)
Browse files Browse the repository at this point in the history
- update dependencies
- update CI to use node 17
- use new `tailwindcss` builtin type
- new `Modal` component
  - using `<dialog>` (closes #97)
  - not using `Context` anymore
- implement new `Modal` in content script
  • Loading branch information
e-krebs authored Jun 26, 2022
1 parent 423aa92 commit 2372cea
Show file tree
Hide file tree
Showing 10 changed files with 1,135 additions and 918 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '17'
- uses: actions/cache@v2
with:
path: '**/node_modules'
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
- uses: actions/checkout@v2
- uses: actions/setup-node@v2
with:
node-version: '16'
node-version: '17'
- name: get semantic release info
run: echo new_release_version=${GITHUB_REF:11} >> $GITHUB_ENV
- name: get manifest version
Expand Down
54 changes: 26 additions & 28 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,50 +21,49 @@
"webext-prod": {}
},
"dependencies": {
"@react-aria/checkbox": "^3.3.4",
"@react-aria/textfield": "^3.5.5",
"@react-aria/visually-hidden": "^3.2.8",
"@react-stately/toggle": "^3.2.7",
"@react-aria/checkbox": "^3.4.1",
"@react-aria/textfield": "^3.6.1",
"@react-aria/visually-hidden": "^3.3.1",
"@react-stately/toggle": "^3.3.1",
"classnames": "^2.3.1",
"date-fns": "^2.28.0",
"lodash": "^4.17.21",
"node-vibrant": "^3.1.6",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react-feather": "^2.0.9",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-feather": "^2.0.10",
"react-hotkeys-hook": "^3.4.6",
"react-query": "^3.39.0"
"react-query": "^3.39.1"
},
"devDependencies": {
"@e-krebs/react-library": "^0.0.12",
"@parcel/config-webextension": "^2.5.0",
"@parcel/core": "^2.5.0",
"@parcel/transformer-svg-react": "^2.5.0",
"@parcel/validator-typescript": "^2.5.0",
"@e-krebs/react-library": "^0.0.13",
"@parcel/config-webextension": "^2.6.2",
"@parcel/core": "^2.6.2",
"@parcel/transformer-svg-react": "^2.6.2",
"@parcel/validator-typescript": "^2.6.2",
"@tailwindcss/typography": "^0.5.2",
"@types/chrome": "^0.0.188",
"@types/chrome": "^0.0.190",
"@types/express": "^4.17.13",
"@types/node": "^17.0.35",
"@types/react": "^18.0.9",
"@types/node": "^17.0.45",
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"@types/tailwindcss": "^3.0.10",
"@typescript-eslint/eslint-plugin": "^5.26.0",
"@typescript-eslint/parser": "^5.26.0",
"@typescript-eslint/eslint-plugin": "^5.29.0",
"@typescript-eslint/parser": "^5.29.0",
"bestzip": "^2.2.1",
"eslint": "^8.16.0",
"eslint": "^8.18.0",
"eslint-import-resolver-typescript": "^2.7.1",
"eslint-plugin-import": "^2.26.0",
"eslint-plugin-prettier": "^4.0.0",
"eslint-plugin-react": "^7.30.0",
"eslint-plugin-react-hooks": "^4.5.0",
"parcel": "^2.5.0",
"eslint-plugin-react": "^7.30.1",
"eslint-plugin-react-hooks": "^4.6.0",
"parcel": "^2.6.2",
"postcss": "^8.4.14",
"prettier": "^2.6.2",
"prettier": "^2.7.1",
"prettier-plugin-tailwindcss": "^0.1.11",
"querystring-es3": "^0.2.1",
"rimraf": "^3.0.2",
"tailwindcss": "^3.0.24",
"typescript": "^4.7.2"
"tailwindcss": "^3.1.4",
"typescript": "^4.7.4"
},
"alias": {
"components": "./src/components",
Expand All @@ -79,7 +78,6 @@
"resolutions": {
"json-schema": ">= 0.4.0",
"nth-check": ">= 2.0.1",
"node-forge": ">= 1.3.0",
"postcss": ">= 8.4.12"
"node-forge": ">= 1.3.0"
}
}
87 changes: 87 additions & 0 deletions src/components/Modal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import cx from 'classnames';
import {
HTMLAttributes,
PropsWithChildren,
useCallback,
forwardRef,
useRef,
useState,
useImperativeHandle,
} from 'react';
import { X } from 'react-feather';

interface ModalProps {
title: string;
contentProps: HTMLAttributes<HTMLDivElement>;
onClosed: () => void;
}

export interface ModalRef {
openModal: () => void;
closeModal: () => void;
}

export const Modal = forwardRef<ModalRef, PropsWithChildren<ModalProps>>(
({ title, children, onClosed, contentProps: { className, ...contentProps } }, ref) => {
const modalRef = useRef<HTMLDialogElement>(null);
const [closing, setClosing] = useState(false);

const closeModal = useCallback(() => {
setClosing(true);
setTimeout(() => {
onClosed();
modalRef.current?.close();
}, 150);
}, [modalRef, onClosed]);

const onModalBackdropClick = useCallback(
(event: HTMLElementEventMap['click']) => {
if (!modalRef.current) return;
const rect = modalRef.current.getBoundingClientRect();
const isInDialog =
rect.top <= event.clientY &&
event.clientY <= rect.top + rect.height &&
rect.left <= event.clientX &&
event.clientX <= rect.left + rect.width;
if (!isInDialog && (event.target as Element).tagName.toLowerCase() === 'dialog') {
closeModal();
}
},
[closeModal]
);

const openModal = useCallback(() => {
setClosing(false);
modalRef.current?.removeEventListener('click', onModalBackdropClick);
modalRef.current?.showModal();
modalRef.current?.addEventListener('click', onModalBackdropClick);
}, [onModalBackdropClick]);

useImperativeHandle(ref, () => ({ openModal, closeModal }));

return (
<dialog
ref={modalRef}
className="bg-transparent backdrop:bg-gray-700/80 backdrop:backdrop-blur-sm"
onClick={(e) => e.stopPropagation()}
>
<div
{...contentProps}
className={cx(
'relative grid w-64 gap-4 rounded-lg p-4',
closing ? 'animate-blowDownModal' : 'animate-blowUpModal',
className
)}
>
<X
className="absolute right-0 h-8 w-8 cursor-pointer rounded-full p-1 hover:bg-gray-200 dark:hover:bg-gray-700"
onClick={closeModal}
/>
<h1 className="px-8 pt-3 pb-0 text-center text-xl font-bold capitalize">{title}</h1>
{children}
</div>
</dialog>
);
}
);
Modal.displayName = 'Modal';
47 changes: 0 additions & 47 deletions src/components/Modal/Modal.tsx

This file was deleted.

18 changes: 0 additions & 18 deletions src/components/Modal/ModalContext.ts

This file was deleted.

2 changes: 0 additions & 2 deletions src/components/Modal/index.ts

This file was deleted.

Loading

0 comments on commit 2372cea

Please sign in to comment.