payaza-storefront-layouts 1.0.9 → 1.0.11
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"LayoutPreview.d.ts","sourceRoot":"","sources":["../../src/preview/LayoutPreview.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAe,eAAe,EAAE,MAAM,mBAAmB,CAAC;
|
|
1
|
+
{"version":3,"file":"LayoutPreview.d.ts","sourceRoot":"","sources":["../../src/preview/LayoutPreview.tsx"],"names":[],"mappings":"AAGA,OAAO,EAAe,eAAe,EAAE,MAAM,mBAAmB,CAAC;AAWjE,UAAU,kBAAkB;IAC1B,MAAM,EAAE,eAAe,GAAG,MAAM,CAAC;IACjC,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,YAAY,CAAC,EAAE,MAAM,CAAC;IACtB,aAAa,CAAC,EAAE,CAAC,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACzC;AAiMD,wBAAgB,aAAa,CAAC,KAAK,EAAE,kBAAkB,2CAEtD"}
|
|
@@ -5,17 +5,6 @@ import { getPreviewDataByLayout } from '../lib/preview-data';
|
|
|
5
5
|
import { ToastProvider, StoreProvider, LoadingProvider, AuthProvider, } from '../index';
|
|
6
6
|
import { ShadowDOMWrapper } from './ShadowDOMWrapper';
|
|
7
7
|
import { PreviewRouter } from './PreviewRouter';
|
|
8
|
-
// Try to import Next.js router hooks - will throw if not in Next.js context
|
|
9
|
-
let useRouter;
|
|
10
|
-
let usePathname;
|
|
11
|
-
try {
|
|
12
|
-
const nextNavigation = require('next/navigation');
|
|
13
|
-
useRouter = nextNavigation.useRouter;
|
|
14
|
-
usePathname = nextNavigation.usePathname;
|
|
15
|
-
}
|
|
16
|
-
catch (e) {
|
|
17
|
-
// Not in Next.js context, will use fallback
|
|
18
|
-
}
|
|
19
8
|
/**
|
|
20
9
|
* LayoutPreview Component
|
|
21
10
|
*
|
|
@@ -28,9 +17,6 @@ catch (e) {
|
|
|
28
17
|
function LayoutPreviewWithRouter({ layout, className, initialRoute, onRouteChange }) {
|
|
29
18
|
const [currentRoute, setCurrentRoute] = useState(initialRoute || '/');
|
|
30
19
|
const [isClient, setIsClient] = useState(false);
|
|
31
|
-
// Use Next.js router hooks if available (must be called unconditionally if defined)
|
|
32
|
-
const router = useRouter ? useRouter() : undefined;
|
|
33
|
-
const pathname = usePathname ? usePathname() : undefined;
|
|
34
20
|
// Get preview data for the specified layout
|
|
35
21
|
const previewData = getPreviewDataByLayout(layout);
|
|
36
22
|
// Helper function to normalize and decode route from URL
|
|
@@ -116,35 +102,21 @@ function LayoutPreviewWithRouter({ layout, className, initialRoute, onRouteChang
|
|
|
116
102
|
const handleLinkClick = useCallback((route) => {
|
|
117
103
|
// Normalize route (ensure it starts with /)
|
|
118
104
|
const normalizedRoute = route.startsWith('/') ? route : `/${route}`;
|
|
119
|
-
// Update URL with query param
|
|
105
|
+
// Update URL with query param using pushState
|
|
106
|
+
// PreviewClient will sync with searchParams and trigger re-render
|
|
120
107
|
if (typeof window !== 'undefined') {
|
|
121
|
-
// Use Next.js router if available for proper page re-renders
|
|
122
|
-
if (router && pathname) {
|
|
123
|
-
try {
|
|
124
|
-
// Build the new URL with route query param
|
|
125
|
-
const newUrl = new URL(window.location.href);
|
|
126
|
-
newUrl.searchParams.set('route', normalizedRoute);
|
|
127
|
-
// Use Next.js router to navigate (triggers page re-render)
|
|
128
|
-
// This ensures the Next.js page component re-renders with new searchParams
|
|
129
|
-
router.push(newUrl.pathname + newUrl.search);
|
|
130
|
-
// Update state immediately for instant UI update
|
|
131
|
-
setCurrentRoute(normalizedRoute);
|
|
132
|
-
onRouteChange?.(normalizedRoute);
|
|
133
|
-
return;
|
|
134
|
-
}
|
|
135
|
-
catch (e) {
|
|
136
|
-
console.warn('[LayoutPreview] Failed to use Next.js router, falling back to pushState:', e);
|
|
137
|
-
}
|
|
138
|
-
}
|
|
139
|
-
// Fallback to pushState if Next.js router not available
|
|
140
108
|
const newUrl = new URL(window.location.href);
|
|
141
109
|
// Encode the route for URL (handles /contact -> %2Fcontact)
|
|
142
110
|
newUrl.searchParams.set('route', normalizedRoute);
|
|
143
111
|
window.history.pushState({ route: normalizedRoute }, '', newUrl.toString());
|
|
112
|
+
// Update state immediately for instant UI update
|
|
144
113
|
setCurrentRoute(normalizedRoute);
|
|
145
114
|
onRouteChange?.(normalizedRoute);
|
|
115
|
+
// Trigger a custom event to notify PreviewClient of the change
|
|
116
|
+
// This ensures Next.js searchParams updates
|
|
117
|
+
window.dispatchEvent(new PopStateEvent('popstate'));
|
|
146
118
|
}
|
|
147
|
-
}, [onRouteChange
|
|
119
|
+
}, [onRouteChange]);
|
|
148
120
|
if (!previewData) {
|
|
149
121
|
return (_jsx("div", { className: `flex items-center justify-center p-8 ${className || ''}`, children: _jsxs("div", { className: "text-center", children: [_jsx("p", { className: "text-lg font-semibold text-gray-600", children: "Layout not found" }), _jsxs("p", { className: "text-sm text-gray-500 mt-2", children: ["Layout type: ", layout] })] }) }));
|
|
150
122
|
}
|