PluginProbe
Extendify / 2.2.2
Extendify v2.2.2
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 2.2.2, at src/Library/components/Modal.jsx

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