@pack/react 0.0.1 → 0.0.3

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/dist/index.d.ts CHANGED
@@ -1,6 +1,8 @@
1
- export * from "./types";
2
- export { registerSection } from "./register-section";
3
- export { registerStorefrontSettingsSchema } from "./register-storefront-settings-schema";
4
- export { RenderSections } from "./render-sections";
5
- export { PreviewProvider, PreviewContext, usePreviewContext } from "./preview";
1
+ import { RenderSections } from "./render-sections";
2
+ import { PreviewProvider } from "./preview/preview-provider";
3
+ import { usePreviewContext } from "./preview/preview-content";
4
+ import { useSiteSettings } from "./use-site-settings";
5
+ import { registerSection } from "./register-section";
6
+ import { registerStorefrontSettingsSchema } from "./register-storefront-settings-schema";
7
+ export { registerSection, registerStorefrontSettingsSchema, usePreviewContext, useSiteSettings, PreviewProvider, RenderSections, };
6
8
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,SAAS,CAAC;AACxB,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gCAAgC,EAAE,MAAM,uCAAuC,CAAC;AACzF,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,eAAe,EAAE,MAAM,4BAA4B,CAAC;AAC7D,OAAO,EAAE,iBAAiB,EAAE,MAAM,2BAA2B,CAAC;AAC9D,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AACtD,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AACrD,OAAO,EAAE,gCAAgC,EAAE,MAAM,uCAAuC,CAAC;AAEzF,OAAO,EACL,eAAe,EACf,gCAAgC,EAChC,iBAAiB,EACjB,eAAe,EACf,eAAe,EACf,cAAc,GACf,CAAC"}
package/dist/index.js CHANGED
@@ -1,5 +1,7 @@
1
- export * from "./types";
2
- export { registerSection } from "./register-section";
3
- export { registerStorefrontSettingsSchema } from "./register-storefront-settings-schema";
4
- export { RenderSections } from "./render-sections";
5
- export { PreviewProvider, PreviewContext, usePreviewContext } from "./preview";
1
+ import { RenderSections } from "./render-sections";
2
+ import { PreviewProvider } from "./preview/preview-provider";
3
+ import { usePreviewContext } from "./preview/preview-content";
4
+ import { useSiteSettings } from "./use-site-settings";
5
+ import { registerSection } from "./register-section";
6
+ import { registerStorefrontSettingsSchema } from "./register-storefront-settings-schema";
7
+ export { registerSection, registerStorefrontSettingsSchema, usePreviewContext, useSiteSettings, PreviewProvider, RenderSections, };
@@ -0,0 +1,11 @@
1
+ /// <reference types="react" />
2
+ type PreviewContextValue = {
3
+ isPreview: boolean;
4
+ siteSettings: any;
5
+ previewStorefrontSettings?: any;
6
+ setPreviewStorefrontSettings: (siteSettings: any) => void;
7
+ };
8
+ export declare const PreviewContext: import("react").Context<PreviewContextValue>;
9
+ export declare const usePreviewContext: () => PreviewContextValue;
10
+ export {};
11
+ //# sourceMappingURL=preview-content.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-content.d.ts","sourceRoot":"","sources":["../../src/preview/preview-content.tsx"],"names":[],"mappings":";AAEA,KAAK,mBAAmB,GAAG;IACzB,SAAS,EAAE,OAAO,CAAC;IACnB,YAAY,EAAE,GAAG,CAAC;IAClB,yBAAyB,CAAC,EAAE,GAAG,CAAC;IAChC,4BAA4B,EAAE,CAAC,YAAY,EAAE,GAAG,KAAK,IAAI,CAAC;CAC3D,CAAC;AAEF,eAAO,MAAM,cAAc,8CAIzB,CAAC;AAEH,eAAO,MAAM,iBAAiB,2BAAmC,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { createContext, useContext } from 'react';
2
+ export const PreviewContext = createContext({
3
+ isPreview: false,
4
+ setPreviewStorefrontSettings: () => { },
5
+ siteSettings: undefined,
6
+ });
7
+ export const usePreviewContext = () => useContext(PreviewContext);
@@ -0,0 +1,9 @@
1
+ import React, { ReactNode } from 'react';
2
+ interface PreviewContentProps {
3
+ children: ReactNode;
4
+ siteSettings: any;
5
+ isPreviewModeEnabled?: boolean;
6
+ }
7
+ export declare function PreviewProvider({ children, isPreviewModeEnabled, siteSettings, }: PreviewContentProps): React.JSX.Element;
8
+ export {};
9
+ //# sourceMappingURL=preview-provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"preview-provider.d.ts","sourceRoot":"","sources":["../../src/preview/preview-provider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,EAAC,SAAS,EAAW,MAAM,OAAO,CAAC;AAGjD,UAAU,mBAAmB;IAC3B,QAAQ,EAAE,SAAS,CAAC;IACpB,YAAY,EAAE,GAAG,CAAC;IAClB,oBAAoB,CAAC,EAAE,OAAO,CAAC;CAChC;AAED,wBAAgB,eAAe,CAAC,EAC9B,QAAQ,EACR,oBAAoB,EACpB,YAAY,GACb,EAAE,mBAAmB,qBAgBrB"}
@@ -0,0 +1,11 @@
1
+ import React, { useState } from 'react';
2
+ import { PreviewContext } from './preview-content';
3
+ export function PreviewProvider({ children, isPreviewModeEnabled, siteSettings, }) {
4
+ const [previewStorefrontSettings, setPreviewStorefrontSettings] = useState();
5
+ return (React.createElement(PreviewContext.Provider, { value: {
6
+ isPreview: !!isPreviewModeEnabled,
7
+ previewStorefrontSettings,
8
+ setPreviewStorefrontSettings,
9
+ siteSettings,
10
+ } }, children));
11
+ }
@@ -1,3 +1,3 @@
1
- import React from "react";
1
+ import React from 'react';
2
2
  export declare function RenderSections({ content }: any): React.JSX.Element | null;
3
3
  //# sourceMappingURL=render-sections.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"render-sections.d.ts","sourceRoot":"","sources":["../src/render-sections.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAqC1B,wBAAgB,cAAc,CAAC,EAAE,OAAO,EAAE,EAAE,GAAG,4BAqB9C"}
1
+ {"version":3,"file":"render-sections.d.ts","sourceRoot":"","sources":["../src/render-sections.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAkB,MAAM,OAAO,CAAC;AAmCvC,wBAAgB,cAAc,CAAC,EAAC,OAAO,EAAC,EAAE,GAAG,4BA4B5C"}
@@ -1,31 +1,31 @@
1
- import React from "react";
2
- import { generateUUID } from "./utils/uuid";
3
- import { sectionMap } from "./register-section";
4
- import { storefrontSettingsSchema } from "./register-storefront-settings-schema";
5
- import { useCustomizerShell } from "./useCustomizerShell";
6
- import { usePreviewContext } from "./preview";
7
- function Sections({ sections }) {
1
+ import React, { useEffect } from 'react';
2
+ import { v4 as uuidv4 } from 'uuid';
3
+ import { useCustomizerShell } from './use-customizer-shell';
4
+ import { usePreviewContext } from './preview/preview-content';
5
+ import { sectionMap } from './register-section';
6
+ import { storefrontSettingsSchema } from './register-storefront-settings-schema';
7
+ function Sections({ content, sections }) {
8
8
  const renderedSections = sections
9
9
  .map((section) => {
10
+ // TODO: Return a consistent data structure from the API and the customizer
10
11
  // Normalize section data
11
12
  const data = section.data || section;
12
- const sectionName = data._template;
13
- const Section = sectionMap.get(sectionName);
14
- if (!Section) {
15
- console.warn(`No registered section for name: ${sectionName}`);
13
+ const schemaKey = data._template;
14
+ const Component = sectionMap.get(schemaKey);
15
+ if (!Component)
16
16
  return null;
17
- }
18
- return (React.createElement("section", { key: generateUUID(), "data-comp": sectionName, "data-comp-id": section?.id, hidden: data?.sectionVisibility === "hidden" },
19
- React.createElement(Section, { "comp-name": sectionName, cms: data })));
17
+ return (React.createElement("section", { key: uuidv4(), "data-comp": schemaKey, "data-comp-id": section?.id, hidden: data?.sectionVisibility === 'hidden' },
18
+ React.createElement(Component, { "comp-name": schemaKey, cms: data })));
20
19
  })
21
20
  .filter(Boolean);
22
21
  return React.createElement(React.Fragment, null, renderedSections);
23
22
  }
24
23
  export function RenderSections({ content }) {
25
- const preview = usePreviewContext();
26
- const { content: liveContent } = useCustomizerShell({
27
- isPreview: preview,
28
- sectionComponents: sectionMap,
24
+ const { isPreview, setPreviewStorefrontSettings } = usePreviewContext();
25
+ const { content: liveContent, storefrontSettings } = useCustomizerShell({
26
+ environment: 'production',
27
+ isPreview,
28
+ sectionMap,
29
29
  data: {
30
30
  content,
31
31
  template: content.template?.type,
@@ -36,8 +36,12 @@ export function RenderSections({ content }) {
36
36
  },
37
37
  storefrontSettingsSchema,
38
38
  });
39
+ useEffect(() => {
40
+ storefrontSettings &&
41
+ setPreviewStorefrontSettings({ settings: storefrontSettings });
42
+ }, [setPreviewStorefrontSettings, storefrontSettings]);
39
43
  const sections = liveContent?.sections?.nodes || liveContent?.sections;
40
44
  if (!sections)
41
45
  return null;
42
- return React.createElement(Sections, { sections: sections });
46
+ return React.createElement(Sections, { content: liveContent, sections: sections });
43
47
  }
@@ -0,0 +1,5 @@
1
+ export declare const useCustomizerShell: ({ environment, isPreview, sectionComponents, data, storefrontSettingsSchema, }: any) => {
2
+ content: any;
3
+ storefrontSettings: any;
4
+ };
5
+ //# sourceMappingURL=use-customizer-shell.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-customizer-shell.d.ts","sourceRoot":"","sources":["../src/use-customizer-shell.tsx"],"names":[],"mappings":"AAIA,eAAO,MAAM,kBAAkB,mFAM5B,GAAG;;;CAyGL,CAAC"}
@@ -1,30 +1,27 @@
1
- import { useCallback, useEffect, useState } from "react";
2
- import { connectToParent } from "penpal";
3
- export const useCustomizerShell = ({ isPreview, sectionComponents, data = {}, storefrontSettingsSchema, }) => {
4
- let location;
5
- if (typeof window !== "undefined") {
6
- location = window.location;
7
- }
1
+ import { useCallback, useEffect, useState } from 'react';
2
+ import { useLocation, useNavigate } from '@remix-run/react';
3
+ import { connectToParent } from 'penpal';
4
+ export const useCustomizerShell = ({ environment = 'production', isPreview, sectionComponents, data = {}, storefrontSettingsSchema, }) => {
5
+ const navigate = useNavigate();
6
+ const location = useLocation();
8
7
  const [content, setContent] = useState(data.content);
9
8
  const [storefrontSettings, setStorefrontSettings] = useState(null);
10
9
  const [parentConnection, setParentConnection] = useState(null);
11
10
  const refreshSections = useCallback(() => {
12
11
  if (!sectionComponents || !parentConnection)
13
12
  return [];
14
- const sectionSchemas = [];
15
- sectionComponents.forEach((component) => {
16
- if (component.Schema) {
17
- const evaluatedSchema = typeof component.Schema === "function"
18
- ? component.Schema(data)
19
- : component.Schema;
20
- sectionSchemas.push(evaluatedSchema);
21
- }
22
- });
13
+ const sectionSchemas = sectionComponents
14
+ .map((section) => {
15
+ return typeof section.Schema === 'function'
16
+ ? section.Schema(data)
17
+ : section.Schema;
18
+ })
19
+ .filter(Boolean);
23
20
  try {
24
21
  parentConnection.setSectionsSchemas(JSON.stringify(sectionSchemas));
25
22
  }
26
23
  catch (error) {
27
- parentConnection.displayError("Something went wrong parsing sections");
24
+ parentConnection.displayError('Something went wrong parsing sections');
28
25
  }
29
26
  return;
30
27
  }, [data, parentConnection, sectionComponents]);
@@ -42,12 +39,12 @@ export const useCustomizerShell = ({ isPreview, sectionComponents, data = {}, st
42
39
  const connection = connectToParent({
43
40
  methods: {
44
41
  routeToPage(path) {
45
- location?.assign(path);
42
+ navigate(path);
46
43
  },
47
44
  scrollToSection(sectionId) {
48
45
  const sectionEl = document.querySelector(`[data-comp-id="${sectionId}"]`);
49
46
  if (sectionEl)
50
- sectionEl.scrollIntoView({ behavior: "smooth" });
47
+ sectionEl.scrollIntoView({ behavior: 'smooth' });
51
48
  },
52
49
  setPageData(content) {
53
50
  setContent(content);
@@ -59,8 +56,10 @@ export const useCustomizerShell = ({ isPreview, sectionComponents, data = {}, st
59
56
  });
60
57
  connection.promise.then((parent) => {
61
58
  const { template, templateType, content } = data;
59
+ parent.sendStorefrontSettings();
62
60
  parent.setCurrentRoute({
63
- currentPath: location?.pathname || "",
61
+ environment,
62
+ currentPath: location.pathname,
64
63
  template,
65
64
  templateType,
66
65
  handle: content.handle,
@@ -69,6 +68,9 @@ export const useCustomizerShell = ({ isPreview, sectionComponents, data = {}, st
69
68
  });
70
69
  setParentConnection(parent);
71
70
  });
71
+ return () => {
72
+ connection.destroy();
73
+ };
72
74
  }, []);
73
75
  useEffect(() => {
74
76
  if (!isPreview)
@@ -83,10 +85,7 @@ export const useCustomizerShell = ({ isPreview, sectionComponents, data = {}, st
83
85
  sectionComponents,
84
86
  ]);
85
87
  if (!isPreview) {
86
- return {
87
- content: data.content,
88
- storefrontSettings: data.settings?.settings,
89
- };
88
+ return { content: data.content, storefrontSettings: data.settings?.settings };
90
89
  }
91
90
  return { content, storefrontSettings };
92
91
  };
@@ -0,0 +1,2 @@
1
+ export declare const useSiteSettings: () => Record<string, any>;
2
+ //# sourceMappingURL=use-site-settings.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"use-site-settings.d.ts","sourceRoot":"","sources":["../src/use-site-settings.ts"],"names":[],"mappings":"AAEA,eAAO,MAAM,eAAe,QAAO,OAAO,MAAM,EAAE,GAAG,CAOpD,CAAC"}
@@ -0,0 +1,7 @@
1
+ import { usePreviewContext } from './preview/preview-content';
2
+ export const useSiteSettings = () => {
3
+ const { isPreview, previewStorefrontSettings, siteSettings } = usePreviewContext();
4
+ return isPreview
5
+ ? previewStorefrontSettings
6
+ : siteSettings?.data?.siteSettings;
7
+ };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@pack/react",
3
3
  "description": "React",
4
- "version": "0.0.1",
4
+ "version": "0.0.3",
5
5
  "exports": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
7
7
  "engines": {
@@ -21,13 +21,16 @@
21
21
  "files": [
22
22
  "dist"
23
23
  ],
24
- "dependencies": {
25
- "penpal": "^6.2.2"
26
- },
24
+ "dependencies": {},
27
25
  "devDependencies": {
26
+ "@remix-run/react": "^1.19.3",
27
+ "@shopify/remix-oxygen": "^1.1.3",
28
28
  "@types/react": "^18.2.20",
29
+ "@types/uuid": "^9.0.2",
30
+ "penpal": "^6.2.2",
29
31
  "react": "^18.2.0",
30
- "react-dom": "^18.2.0"
32
+ "react-dom": "^18.2.0",
33
+ "uuid": "^9.0.0"
31
34
  },
32
35
  "peerDependencies": {
33
36
  "react": "^18.0.0",
@@ -1,4 +0,0 @@
1
- /// <reference types="react" />
2
- export declare const PreviewContext: import("react").Context<boolean>;
3
- export declare const usePreviewContext: () => boolean;
4
- //# sourceMappingURL=PreviewContent.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PreviewContent.d.ts","sourceRoot":"","sources":["../../src/preview/PreviewContent.tsx"],"names":[],"mappings":";AAEA,eAAO,MAAM,cAAc,kCAAgC,CAAC;AAE5D,eAAO,MAAM,iBAAiB,eAAmC,CAAC"}
@@ -1,3 +0,0 @@
1
- import { createContext, useContext } from 'react';
2
- export const PreviewContext = createContext(false);
3
- export const usePreviewContext = () => useContext(PreviewContext);
@@ -1,8 +0,0 @@
1
- import React from "react";
2
- interface PreviewContentProps {
3
- children: React.ReactNode;
4
- preview?: boolean;
5
- }
6
- export declare function PreviewProvider({ preview, children }: PreviewContentProps): React.JSX.Element;
7
- export {};
8
- //# sourceMappingURL=PreviewProvider.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"PreviewProvider.d.ts","sourceRoot":"","sources":["../../src/preview/PreviewProvider.tsx"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAG1B,UAAU,mBAAmB;IAC3B,QAAQ,EAAE,KAAK,CAAC,SAAS,CAAC;IAC1B,OAAO,CAAC,EAAE,OAAO,CAAC;CACnB;AAED,wBAAgB,eAAe,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,mBAAmB,qBAMzE"}
@@ -1,5 +0,0 @@
1
- import React from "react";
2
- import { PreviewContext } from "./PreviewContent";
3
- export function PreviewProvider({ preview, children }) {
4
- return (React.createElement(PreviewContext.Provider, { value: !!preview }, children));
5
- }
@@ -1,3 +0,0 @@
1
- export { PreviewProvider } from "./PreviewProvider";
2
- export { PreviewContext, usePreviewContext } from "./PreviewContent";
3
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/preview/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAC;AACpD,OAAO,EAAE,cAAc,EAAE,iBAAiB,EAAE,MAAM,kBAAkB,CAAC"}
@@ -1,2 +0,0 @@
1
- export { PreviewProvider } from "./PreviewProvider";
2
- export { PreviewContext, usePreviewContext } from "./PreviewContent";
@@ -1,13 +0,0 @@
1
- import { SectionMap } from "./types";
2
- interface useCustomizerShellProps {
3
- isPreview?: boolean;
4
- sectionComponents?: SectionMap;
5
- data?: any;
6
- storefrontSettingsSchema?: any;
7
- }
8
- export declare const useCustomizerShell: ({ isPreview, sectionComponents, data, storefrontSettingsSchema, }: useCustomizerShellProps) => {
9
- content: any;
10
- storefrontSettings: any;
11
- };
12
- export {};
13
- //# sourceMappingURL=useCustomizerShell.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"useCustomizerShell.d.ts","sourceRoot":"","sources":["../src/useCustomizerShell.tsx"],"names":[],"mappings":"AAEA,OAAO,EAAU,UAAU,EAAE,MAAM,SAAS,CAAC;AAE7C,UAAU,uBAAuB;IAC/B,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,iBAAiB,CAAC,EAAE,UAAU,CAAC;IAC/B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,wBAAwB,CAAC,EAAE,GAAG,CAAC;CAChC;AAED,eAAO,MAAM,kBAAkB,sEAK5B,uBAAuB;;;CA4GzB,CAAC"}
@@ -1,2 +0,0 @@
1
- export declare function generateUUID(): string;
2
- //# sourceMappingURL=uuid.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"uuid.d.ts","sourceRoot":"","sources":["../../src/utils/uuid.ts"],"names":[],"mappings":"AAGA,wBAAgB,YAAY,WAM3B"}
@@ -1,11 +0,0 @@
1
- /*
2
- * Generate a UUID using crypto and fallback to Math.random if crypto is not available.
3
- */
4
- export function generateUUID() {
5
- if (typeof crypto !== "undefined" && !!crypto.randomUUID) {
6
- return crypto.randomUUID();
7
- }
8
- else {
9
- return Math.random().toString(16).substring(2);
10
- }
11
- }