PluginProbe
Extendify / 0.9.3
Extendify v0.9.3
3.2.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 All 127 releases
extendify / src / Onboarding / components / PagePreview.js

PagePreview.js in Extendify 0.9.3, at src/Onboarding/components/PagePreview.js

135 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { useMemo } from '@wordpress/element'
2 import { __, sprintf } from '@wordpress/i18n'
3 import classNames from 'classnames'
4 import { getTemplate } from '@onboarding/api/DataApi'
5 import { StylePreview } from '@onboarding/components/StyledPreview'
6 import { useFetch } from '@onboarding/hooks/useFetch'
7 import { findTheCode } from '@onboarding/lib/util'
8 import { useUserSelectionStore } from '@onboarding/state/UserSelections'
9 import { Checkmark } from '@onboarding/svg'
10
11 export const fetcher = (data) => getTemplate(data)
12 export const PagePreview = ({
13 page,
14 blockHeight,
15 required = false,
16 displayOnly = false,
17 title = '',
18 }) => {
19 const { siteType, style, toggle, has } = useUserSelectionStore()
20 const isHome = page?.slug === 'home'
21 const { data: pageData } = useFetch(
22 {
23 siteType: siteType.slug,
24 layoutType: page.slug,
25 baseLayout: isHome ? style?.homeBaseLayout : null,
26 kit: page.slug !== 'home' ? style?.kit : null,
27 },
28 fetcher,
29 )
30 if (displayOnly) {
31 return (
32 <div
33 className="text-base p-2 bg-transparent overflow-hidden rounded-lg border border-gray-100"
34 style={{ height: blockHeight }}>
35 {title && (
36 <div className="p-3 pb-0 bg-white text-left">{title}</div>
37 )}
38 <StylePreviewWrapper
39 key={style?.recordId}
40 page={page}
41 measure={false}
42 blockHeight={blockHeight}
43 style={{
44 ...style,
45 code: findTheCode({ template: pageData }),
46 }}
47 />
48 </div>
49 )
50 }
51
52 return (
53 <div
54 role="button"
55 tabIndex={0}
56 aria-label={__('Press to select', 'extendify')}
57 disabled={required}
58 className="text-base p-0 bg-transparent overflow-hidden rounded-lg border border-gray-100 button-focus"
59 onClick={() => required || toggle('pages', page)}
60 title={
61 required && title
62 ? sprintf(__('%s page is required', 'extendify'), title)
63 : sprintf(__('Toggle %s page', 'extendify'), title)
64 }
65 onKeyDown={(e) => {
66 if (['Enter', 'Space', ' '].includes(e.key)) {
67 if (!required) toggle('pages', page)
68 }
69 }}>
70 <div className="border-gray-100 border-b-0 min-w-sm z-30 relative bg-white pt-3 px-3 pb-1.5 flex justify-between items-center">
71 {title && (
72 <div
73 className={classNames('flex items-center', {
74 'text-gray-700': !has('pages', page),
75 })}>
76 <span className="text-left">{title}</span>
77 {required && (
78 <span className="w-4 h-4 text-base leading-none pl-2 mr-6 dashicons dashicons-lock"></span>
79 )}
80 </div>
81 )}
82 {has('pages', page) ? (
83 <div
84 className={classNames('w-5 h-5 rounded-sm', {
85 'bg-gray-700': required,
86 'bg-partner-primary-bg': !required,
87 })}>
88 <Checkmark className="text-white w-5" />
89 </div>
90 ) : (
91 <div
92 className={classNames('border w-5 h-5 rounded-sm', {
93 'border-gray-700': required,
94 'border-partner-primary-bg': !required,
95 })}></div>
96 )}
97 </div>
98 <div className="p-2 relative" style={{ height: blockHeight - 44 }}>
99 <StylePreviewWrapper
100 key={style?.recordId}
101 page={page}
102 blockHeight={blockHeight}
103 style={{
104 ...style,
105 code: findTheCode({ template: pageData }),
106 }}
107 />
108 </div>
109 </div>
110 )
111 }
112
113 const StylePreviewWrapper = ({
114 page,
115 style,
116 measure = true,
117 blockHeight = false,
118 }) => {
119 const context = useMemo(
120 () => ({
121 type: 'page',
122 detail: page.slug,
123 measure,
124 }),
125 [page, measure],
126 )
127 return (
128 <StylePreview
129 style={style}
130 context={context}
131 blockHeight={blockHeight}
132 />
133 )
134 }
135