@wix/origami-site 1.0.60 → 1.0.62

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@wix/origami-site",
3
- "version": "1.0.60",
3
+ "version": "1.0.62",
4
4
  "description": "Runs an exported Wix origami site: file-based routing, Wix dynamic pages, popups and build checks. Use with @wix/origami-site-astro or @wix/origami-site-express.",
5
5
  "type": "module",
6
6
  "files": [
@@ -46,9 +46,9 @@
46
46
  }
47
47
  },
48
48
  "dependencies": {
49
- "@wix/origami-types": "1.4.5",
49
+ "@wix/origami-types": "1.4.7",
50
50
  "@wix/builder-error-pages": "^1.1.6",
51
- "@wix/origami-transforms": "0.1.63",
51
+ "@wix/origami-transforms": "0.1.65",
52
52
  "@babel/parser": "^7.25.0",
53
53
  "@wix/origami-services": "1.2.9",
54
54
  "@wix/services-manager-react": "^1.0.8",
@@ -93,5 +93,5 @@
93
93
  "optional": true
94
94
  }
95
95
  },
96
- "falconPackageHash": "5c23cbace836e43a7402de15f938c680ba71e694d889d1b44ad218d8"
96
+ "falconPackageHash": "10e42b9b4dbdf72be1cbedeb0dd269ce063f0ce2f5ead374a23d5eb9"
97
97
  }
@@ -0,0 +1,39 @@
1
+ import { createElement, useLayoutEffect, useRef, useSyncExternalStore } from 'react'
2
+ import { closePopup, currentPopup, popup, subscribe } from '../popups/controller.js'
3
+ import './popups.css'
4
+
5
+ /** Renders the open popup in a modal `<dialog>`, and nothing while none is open. It sits inside #main_MF, so the
6
+ * popup shares the page's services and container-query root. The popup's PopupContainer draws the overlay. */
7
+ export function PopupHost() {
8
+ const open = useSyncExternalStore(subscribe, currentPopup)
9
+ const loaded = open && popup(open.seoUri)
10
+ if (!open || !loaded) return null
11
+ // Keyed by popup, so a popup that replaces another gets a fresh dialog and fresh state.
12
+ return createElement(PopupDialog, { key: open.seoUri, title: loaded.title }, createElement(loaded.Popup))
13
+ }
14
+
15
+ /** @param {{ title: string, children: import('react').ReactNode }} props */
16
+ function PopupDialog({ title, children }) {
17
+ const ref = useRef(/** @type {HTMLDialogElement | null} */ (null))
18
+ useLayoutEffect(() => {
19
+ const dialog = ref.current
20
+ if (!dialog) return
21
+ // The popup's content can also close the dialog itself (`form method="dialog"`, `dialog.close()`): the controller
22
+ // follows. The browser fires close in a later task, so a close() of our own reaches a listener only if the dialog
23
+ // is still mounted: StrictMode's remount, which has reopened it by then. A replaced dialog's listener is gone.
24
+ const onClose = () => { if (!dialog.open) closePopup() }
25
+ dialog.addEventListener('close', onClose)
26
+ dialog.showModal()
27
+ return () => {
28
+ dialog.removeEventListener('close', onClose)
29
+ if (dialog.open) dialog.close()
30
+ }
31
+ }, [])
32
+ // ESC fires cancel: the controller closes the popup, not the browser.
33
+ /** @param {Event} event */
34
+ const onCancel = event => {
35
+ event.preventDefault()
36
+ closePopup()
37
+ }
38
+ return createElement('dialog', { ref, className: 'origami-popup', 'aria-label': title, onCancel }, children)
39
+ }
@@ -4,7 +4,9 @@ import type { ComponentType } from 'react'
4
4
  export type PageModule = { default: ComponentType }
5
5
  export type MountPageOptions = {
6
6
  pages: Record<string, () => Promise<PageModule>>
7
- popups: Record<string, () => Promise<unknown>>
7
+ /** By seoUri. Every popup loads before the page renders, so its services register in time. */
8
+ popups: Record<string, { title: string; load: () => Promise<PageModule> }>
9
+ /** Popup page id → seoUri, for the lightbox services' `open({ pageId })`. */
8
10
  seoUriByPageId: Record<string, string>
9
11
  beforeMount: () => Promise<unknown>
10
12
  /** The site's meta site id, from `VITE_WIX_METASITE_ID`. Services get no site identity without it. */
@@ -3,6 +3,8 @@ import { createRoot } from 'react-dom/client'
3
3
  import { registeredServicesConfigs, sealServicesConfigs } from '@wix/origami-services/internal'
4
4
  import { servicesMap } from '@wix/origami-services'
5
5
  import { WixServices } from '@wix/services-manager-react'
6
+ import { lightboxConfig, openPopup, popup, setPopups, startPopups } from '../popups/controller.js'
7
+ import { PopupHost } from './PopupHost.js'
6
8
 
7
9
  let hasReadPageData = false
8
10
  /** @type {import('../server/index.js').PageData | undefined} */
@@ -35,7 +37,7 @@ export function readPageData() {
35
37
  * @param {import('./index.js').MountPageOptions} options
36
38
  * @returns {Promise<() => void>}
37
39
  */
38
- export async function mountPage(element, { pages, beforeMount, siteId }) {
40
+ export async function mountPage(element, { pages, popups = {}, seoUriByPageId = {}, beforeMount, siteId }) {
39
41
  if (!element) throw new Error('Cannot mount page: root element is missing')
40
42
  const data = readPageData()
41
43
  const pageKey = data?.page
@@ -51,17 +53,60 @@ export async function mountPage(element, { pages, beforeMount, siteId }) {
51
53
  } catch (error) {
52
54
  console.error(error)
53
55
  }
54
- // Loading the page registers its globals and own DM configs before services are created.
56
+ // Loading the popups and then the page registers their globals, services and own DM configs before services are
57
+ // created, so a popup's services work on first open. The page registers last, so its own configs win.
58
+ const hasPopups = Object.keys(popups).length > 0
59
+ setPopups(await loadPopups(popups), seoUriByPageId)
55
60
  const { default: Page } = await loadPage()
61
+ // Without popups, the lightbox service keeps its mock and nothing listens for ?popup=.
62
+ const requestConfigs = { ...data?.services, ...(hasPopups ? { '@wix/site-service-lightbox': lightboxConfig } : {}) }
56
63
  const map = servicesMap({
57
- servicesConfigs: registeredServicesConfigs(), requestConfigs: data?.services ?? {}, url: location.href, siteId,
64
+ servicesConfigs: registeredServicesConfigs(), requestConfigs, url: location.href, siteId,
58
65
  userAgent: navigator.userAgent, reducedMotion: globalThis.matchMedia?.('(prefers-reduced-motion: reduce)').matches,
59
66
  })
60
67
  sealServicesConfigs()
61
68
  const root = createRoot(element)
62
69
  // #main_MF is the container-query root: editor-components' `--one-unit: 1cqw` resolves against it.
63
- const main = createElement('div', { id: 'main_MF', style: { containerType: 'inline-size' } }, createElement(Page))
64
- // Stage 3: PopupHost belongs inside WixServices so popups share page services.
70
+ // PopupHost sits inside it too, so popups share the page's services and container-query root.
71
+ const main = createElement('div', { id: 'main_MF', style: { containerType: 'inline-size' } },
72
+ createElement(Page), hasPopups ? createElement(PopupHost) : null)
65
73
  root.render(createElement(StrictMode, null, createElement(WixServices, { servicesMap: map, children: main })))
66
- return () => root.unmount()
74
+ if (!hasPopups) return () => root.unmount()
75
+ startPopups()
76
+ document.addEventListener('click', onPopupLinkClick)
77
+ return () => {
78
+ document.removeEventListener('click', onPopupLinkClick)
79
+ root.unmount()
80
+ }
81
+ }
82
+
83
+ /** A plain click on a link to this page with `?popup=` opens the popup over it instead of loading the page again.
84
+ * The URL doesn't change. @param {MouseEvent} event */
85
+ function onPopupLinkClick(event) {
86
+ if (event.defaultPrevented || event.button !== 0 || event.metaKey || event.ctrlKey || event.shiftKey || event.altKey) return
87
+ const link = event.target instanceof Element ? event.target.closest('a[href]') : null
88
+ if (!(link instanceof HTMLAnchorElement) || (link.target && link.target !== '_self')) return
89
+ const url = new URL(link.href)
90
+ const seoUri = url.searchParams.get('popup')
91
+ if (!seoUri || !popup(seoUri) || url.origin !== location.origin || url.pathname !== location.pathname) return
92
+ event.preventDefault()
93
+ void openPopup(seoUri)
94
+ }
95
+
96
+ /** A popup that fails to load is left out, so the page still renders. Popups load one after another: each registers its
97
+ * configs when its module evaluates and later configs win, so loading them together would let the network pick the order.
98
+ * @param {import('./index.js').MountPageOptions['popups']} popups
99
+ * @returns {Promise<Record<string, import('../popups/controller.js').LoadedPopup>>}
100
+ */
101
+ async function loadPopups(popups) {
102
+ /** @type {Record<string, import('../popups/controller.js').LoadedPopup>} */
103
+ const loaded = {}
104
+ for (const [seoUri, { title, load }] of Object.entries(popups)) {
105
+ try {
106
+ loaded[seoUri] = { title, Popup: (await load()).default }
107
+ } catch (error) {
108
+ console.error(`Cannot load popup ${seoUri}:`, error)
109
+ }
110
+ }
111
+ return loaded
67
112
  }
@@ -0,0 +1,25 @@
1
+ /* The popup layer: fixed, full viewport, one grid cell, as Thunderbolt's ResponsivePopupPage lightbox layer.
2
+ PopupContainer centers itself in the cell and draws the overlay, so the dialog and its backdrop draw nothing. */
3
+ dialog.origami-popup {
4
+ position: fixed;
5
+ inset: 0;
6
+ width: 100%;
7
+ height: 100%;
8
+ max-width: none;
9
+ max-height: none;
10
+ margin: 0;
11
+ padding: 0;
12
+ border: 0;
13
+ background: transparent;
14
+ color: inherit;
15
+ overflow: auto;
16
+ /* Exported sites have no Wix ads bar; PopupContainer's overlay offsets by it. */
17
+ --wix-ads-height: 0px;
18
+ }
19
+ dialog.origami-popup[open] {
20
+ display: grid;
21
+ grid-template: 1fr / 1fr;
22
+ }
23
+ dialog.origami-popup::backdrop {
24
+ background: transparent;
25
+ }
@@ -12,8 +12,10 @@ function moduleSource(table) {
12
12
  return ` [${JSON.stringify(page.key)}]: () => import(${JSON.stringify(entry)})`
13
13
  })
14
14
  const popups = table.popups.map(popup =>
15
- ` [${JSON.stringify(popup.seoUri)}]: () => import(${JSON.stringify(`/src/${popup.itemPath}/_generated/index.jsx`)})`)
16
- return `export const pages = {\n${entries.join(',\n')}\n};\nexport const popups = {\n${popups.join(',\n')}\n};\nexport const seoUriByPageId = {};\n`
15
+ ` [${JSON.stringify(popup.seoUri)}]: { title: ${JSON.stringify(popup.title)}, load: () => import(${JSON.stringify(`/src/${popup.itemPath}/_generated/index.jsx`)}) }`)
16
+ // lightbox.open({ pageId }) names a popup by its Wix page id, as a Wix Forms form stores it.
17
+ const seoUriByPageId = Object.fromEntries(table.popups.flatMap(popup => (popup.pageId ? [[popup.pageId, popup.seoUri]] : [])))
18
+ return `export const pages = {\n${entries.join(',\n')}\n};\nexport const popups = {\n${popups.join(',\n')}\n};\nexport const seoUriByPageId = ${JSON.stringify(seoUriByPageId)};\n`
17
19
  }
18
20
 
19
21
  /** @param {import('./index.js').OrigamiVitePluginsOptions['siteTree']} siteTree
@@ -0,0 +1,69 @@
1
+ // The one place that knows which popup is open. The lightbox service, `?popup=`, links and user code all open and close
2
+ // popups here, and PopupHost renders what it says. A module singleton, since most callers sit outside React.
3
+
4
+ /** @typedef {{ title: string, Popup: import('react').ComponentType }} LoadedPopup */
5
+ /** @typedef {{ seoUri: string, payload: unknown }} OpenPopup */
6
+
7
+ /** @type {Record<string, LoadedPopup>} */
8
+ let popups = {}
9
+ /** @type {Record<string, string>} */
10
+ let seoUriByPageId = {}
11
+ /** @type {OpenPopup | null} */
12
+ let current = null
13
+ /** @type {Set<() => void>} */
14
+ const listeners = new Set()
15
+
16
+ /** @param {Record<string, LoadedPopup>} loaded @param {Record<string, string>} byPageId */
17
+ export function setPopups(loaded, byPageId) {
18
+ popups = loaded
19
+ seoUriByPageId = byPageId
20
+ }
21
+
22
+ /** @param {string} seoUri */
23
+ export const popup = seoUri => (Object.hasOwn(popups, seoUri) ? popups[seoUri] : undefined)
24
+
25
+ /** One popup at a time: opening another replaces it, and opening the open one only replaces its payload.
26
+ * @param {string} seoUri
27
+ * @param {{ payload?: unknown }} [options]
28
+ */
29
+ export async function openPopup(seoUri, { payload } = {}) {
30
+ if (!popup(seoUri)) throw new Error(`No popup named "${seoUri}"`)
31
+ current = { seoUri, payload }
32
+ notify()
33
+ }
34
+
35
+ export function closePopup() {
36
+ if (!current) return
37
+ current = null
38
+ notify()
39
+ }
40
+
41
+ export const currentPopup = () => current
42
+
43
+ /** @param {() => void} listener */
44
+ export function subscribe(listener) {
45
+ listeners.add(listener)
46
+ return () => { listeners.delete(listener) }
47
+ }
48
+
49
+ function notify() {
50
+ for (const listener of listeners) listener()
51
+ }
52
+
53
+ /** The `@wix/site-service-lightbox` config: it forwards to this controller, by slug or by the deprecated page id. */
54
+ export const lightboxConfig = {
55
+ /** @param {{ slug?: string, pageId?: string, payload?: unknown }} options */
56
+ open: ({ slug, pageId, payload }) => {
57
+ if (slug !== undefined || pageId === undefined) return openPopup(String(slug), { payload })
58
+ if (!Object.hasOwn(seoUriByPageId, pageId)) return Promise.reject(new Error(`No popup with page id "${pageId}"`))
59
+ return openPopup(seoUriByPageId[pageId], { payload })
60
+ },
61
+ close: closePopup,
62
+ getPayload: () => current?.payload,
63
+ }
64
+
65
+ /** Opens the popup a `?popup=<seoUri>` URL names, right away and every time. Opening never changes the URL. */
66
+ export function startPopups() {
67
+ const seoUri = new URL(location.href).searchParams.get('popup')
68
+ if (seoUri) openPopup(seoUri).catch(error => console.warn(`Cannot open ?popup=${seoUri}:`, error))
69
+ }
@@ -1,3 +1,6 @@
1
- // PLAN-QUESTION: Popup option/result types are unspecified here; leave options opaque until milestone 3.2.
2
- export function openPopup(seoUri: string, options?: Record<string, unknown>): void
1
+ /** Opens the popup in `src/popups/<seoUri>/` over the current page, replacing any open popup, without changing the
2
+ * URL. Components inside it read `payload` with the lightbox service's `getPayload()`. Rejects when no popup has
3
+ * that name. */
4
+ export function openPopup(seoUri: string, options?: { payload?: unknown }): Promise<void>
5
+ /** Closes the open popup, if any. */
3
6
  export function closePopup(): void
@@ -1 +1 @@
1
- export {}
1
+ export { openPopup, closePopup } from './controller.js'
@@ -25,7 +25,8 @@ const readMetadata = structure => {
25
25
  // Walk Babel's AST without a runtime dependency on @babel/types.
26
26
  const visit = node => {
27
27
  if (!node || typeof node !== 'object') return
28
- if (node.type === 'JSXOpeningElement' && node.name.type === 'JSXIdentifier' && node.name.name === 'Page') {
28
+ // A page's root is <Page>, a popup's <PopupRoot>.
29
+ if (node.type === 'JSXOpeningElement' && node.name.type === 'JSXIdentifier' && ['Page', 'PopupRoot'].includes(node.name.name)) {
29
30
  for (const attribute of node.attributes) {
30
31
  if (attribute.type !== 'JSXAttribute') continue
31
32
  if (attribute.name.name === 'id' && attribute.value?.type === 'StringLiteral' && metadata.pageId === undefined)
@@ -206,6 +207,7 @@ function readPopups(src) {
206
207
  if (nested.length) throw new SiteTreeError('POPUP_NOT_TOP_LEVEL', nested.map(itemPath => `src/${itemPath}`), 'A popup must sit directly in src/popups.')
207
208
  return items.map(itemPath => {
208
209
  const seoUri = basename(itemPath)
209
- return { key: itemPath, seoUri, itemPath, title: pageMetadata(resolve(src, itemPath)).title ?? seoUri }
210
+ const { title, pageId } = pageMetadata(resolve(src, itemPath))
211
+ return { key: itemPath, seoUri, itemPath, title: title ?? seoUri, ...(pageId ? { pageId } : {}) }
210
212
  })
211
213
  }
package/src/styles.d.ts CHANGED
@@ -1,2 +1,4 @@
1
1
  // The copied 404 entry imports the package's stylesheet as a bundler side effect.
2
2
  declare module '@wix/builder-error-pages/style.css'
3
+ // PopupHost imports its own stylesheet the same way.
4
+ declare module '*.css'