PluginProbe
Extendify / 1.16.1
Extendify v1.16.1
3.2.0 3.1.6 3.1.5 3.1.4 3.1.3 3.1.2 3.1.1 3.1.0 3.0.6 3.0.5 3.0.4 trunk 0.1.0 0.10.0 0.10.1 0.10.2 0.11.0 0.11.1 0.2.0 0.3.0 0.3.1 0.4.0 0.5.0 0.6.0 0.7.0 All 126 releases
extendify / src / Library / components / Modal.jsx

Modal.jsx in Extendify 1.16.1, at src/Library/components/Modal.jsx

129 lines 4.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { dispatch } from '@wordpress/data';
2 import { useLayoutEffect, useEffect, useRef } from '@wordpress/element';
3 import { __ } from '@wordpress/i18n';
4 import { Dialog } from '@headlessui/react';
5 import { useActivityStore } from '@shared/state/activity';
6 import { motion } from 'framer-motion';
7 import { updateOption } from '@library/api/WPApi';
8 import { useGlobalsStore } from '@library/state/global';
9 import { useSiteSettingsStore } from '@library/state/site';
10 import { useUserStore } from '@library/state/user';
11 import { insertBlocks } from '@library/util/insert';
12 import { ModalContent } from './ModalContent';
13 import { Sidebar } from './sidebar/Sidebar';
14 import { Topbar } from './topbar/Topbar';
15
16 const isNewPage = window?.location?.pathname?.includes('post-new.php');
17
18 export const Modal = () => {
19 const { incrementActivity } = useActivityStore();
20 const { open, setOpen } = useGlobalsStore();
21 const { updateUserOption, openOnNewPage } = useUserStore();
22 const { category, siteType, incrementImports } = useSiteSettingsStore();
23 const { createNotice } = dispatch('core/notices');
24 const once = useRef(false);
25
26 const onClose = () => {
27 setOpen(false);
28 };
29 const insertPattern = async (blocks) => {
30 await insertBlocks(blocks);
31 incrementImports();
32 onClose();
33 createNotice('info', __('Pattern added', 'extendify-local'), {
34 isDismissible: true,
35 type: 'snackbar',
36 });
37 // update the general options to reflect the new pattern
38 await updateOption('extendify_check_for_image_imports', true);
39 };
40
41 useLayoutEffect(() => {
42 if (open || once.current) return;
43 once.current = true;
44 if (openOnNewPage && isNewPage) {
45 // Minimize HC if its open
46 window.dispatchEvent(new CustomEvent('extendify-hc:minimize'));
47 incrementActivity('library-auto-open');
48 setOpen(true);
49 return;
50 }
51 const search = new URLSearchParams(window.location.search);
52 if (search.has('ext-open')) {
53 setOpen(true);
54 incrementActivity('library-search-param-auto-open');
55 }
56 }, [openOnNewPage, setOpen, incrementActivity, open]);
57
58 useEffect(() => {
59 const search = new URLSearchParams(window.location.search);
60
61 if (search.has('ext-close')) {
62 setOpen(false);
63 search.delete('ext-close');
64 window.history.replaceState(
65 {},
66 '',
67 window.location.pathname + '?' + search.toString(),
68 );
69 }
70 }, [setOpen, incrementActivity]);
71
72 useEffect(() => {
73 const handleOpen = () => setOpen(true);
74 const handleClose = () => setOpen(false);
75 window.addEventListener('extendify::open-library', handleOpen);
76 window.addEventListener('extendify::close-library', handleClose);
77 return () => {
78 window.removeEventListener('extendify::open-library', handleOpen);
79 window.removeEventListener('extendify::close-library', handleClose);
80 };
81 }, [setOpen, open]);
82
83 if (!open) return null;
84
85 return (
86 <Dialog
87 className="extendify-library extendify-library-modal"
88 open={open}
89 static
90 onClose={() => undefined}>
91 <div className="absolute mx-auto h-full w-full md:p-8">
92 <div
93 className="fixed inset-0 bg-black/30"
94 style={{ backdropFilter: 'blur(2px)' }}
95 aria-hidden="true"
96 />
97 <motion.div
98 key="library-modal"
99 initial={{ y: 30, opacity: 0 }}
100 animate={{ y: 0, opacity: 1 }}
101 exit={{ y: 0, opacity: 0 }}
102 transition={{ duration: 0.3 }}
103 className="relative mx-auto h-full w-full max-w-screen-3xl bg-white shadow-2xl sm:flex sm:overflow-hidden">
104 <Dialog.Title className="sr-only">
105 {__('Design Patterns', 'extendify-local')}
106 </Dialog.Title>
107 <Sidebar />
108 <div className="relative flex w-full flex-col bg-[#FAFAFA]">
109 <Topbar
110 openOnNewPage={openOnNewPage}
111 updateUserOption={updateUserOption}
112 onClose={onClose}
113 />
114 <div
115 id="extendify-library-patterns-list"
116 className="flex-grow overflow-y-auto">
117 <ModalContent
118 insertPattern={insertPattern}
119 category={category}
120 siteType={siteType}
121 />
122 </div>
123 </div>
124 </motion.div>
125 </div>
126 </Dialog>
127 );
128 };
129