@shopify/hydrogen-react 2022.7.0
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/README.md +218 -0
- package/dist/dev/ExternalVideo.cjs +39 -0
- package/dist/dev/ExternalVideo.cjs.map +1 -0
- package/dist/dev/ExternalVideo.js +39 -0
- package/dist/dev/ExternalVideo.js.map +1 -0
- package/dist/dev/Image.cjs +104 -0
- package/dist/dev/Image.cjs.map +1 -0
- package/dist/dev/Image.js +104 -0
- package/dist/dev/Image.js.map +1 -0
- package/dist/dev/MediaFile.cjs +57 -0
- package/dist/dev/MediaFile.cjs.map +1 -0
- package/dist/dev/MediaFile.js +57 -0
- package/dist/dev/MediaFile.js.map +1 -0
- package/dist/dev/Metafield.cjs +295 -0
- package/dist/dev/Metafield.cjs.map +1 -0
- package/dist/dev/Metafield.js +295 -0
- package/dist/dev/Metafield.js.map +1 -0
- package/dist/dev/ModelViewer.cjs +145 -0
- package/dist/dev/ModelViewer.cjs.map +1 -0
- package/dist/dev/ModelViewer.js +145 -0
- package/dist/dev/ModelViewer.js.map +1 -0
- package/dist/dev/Money.cjs +40 -0
- package/dist/dev/Money.cjs.map +1 -0
- package/dist/dev/Money.js +40 -0
- package/dist/dev/Money.js.map +1 -0
- package/dist/dev/ProductPrice.cjs +61 -0
- package/dist/dev/ProductPrice.cjs.map +1 -0
- package/dist/dev/ProductPrice.js +61 -0
- package/dist/dev/ProductPrice.js.map +1 -0
- package/dist/dev/ProductProvider.cjs +161 -0
- package/dist/dev/ProductProvider.cjs.map +1 -0
- package/dist/dev/ProductProvider.js +161 -0
- package/dist/dev/ProductProvider.js.map +1 -0
- package/dist/dev/ShopPayButton.cjs +64 -0
- package/dist/dev/ShopPayButton.cjs.map +1 -0
- package/dist/dev/ShopPayButton.js +64 -0
- package/dist/dev/ShopPayButton.js.map +1 -0
- package/dist/dev/ShopifyProvider.cjs +46 -0
- package/dist/dev/ShopifyProvider.cjs.map +1 -0
- package/dist/dev/ShopifyProvider.js +46 -0
- package/dist/dev/ShopifyProvider.js.map +1 -0
- package/dist/dev/Video.cjs +44 -0
- package/dist/dev/Video.cjs.map +1 -0
- package/dist/dev/Video.js +44 -0
- package/dist/dev/Video.js.map +1 -0
- package/dist/dev/flatten-connection.cjs +23 -0
- package/dist/dev/flatten-connection.cjs.map +1 -0
- package/dist/dev/flatten-connection.js +23 -0
- package/dist/dev/flatten-connection.js.map +1 -0
- package/dist/dev/image-size.cjs +80 -0
- package/dist/dev/image-size.cjs.map +1 -0
- package/dist/dev/image-size.js +80 -0
- package/dist/dev/image-size.js.map +1 -0
- package/dist/dev/index.cjs +35 -0
- package/dist/dev/index.cjs.map +1 -0
- package/dist/dev/index.js +35 -0
- package/dist/dev/index.js.map +1 -0
- package/dist/dev/load-script.cjs +52 -0
- package/dist/dev/load-script.cjs.map +1 -0
- package/dist/dev/load-script.js +52 -0
- package/dist/dev/load-script.js.map +1 -0
- package/dist/dev/storefront-api-constants.cjs +5 -0
- package/dist/dev/storefront-api-constants.cjs.map +1 -0
- package/dist/dev/storefront-api-constants.js +5 -0
- package/dist/dev/storefront-api-constants.js.map +1 -0
- package/dist/dev/storefront-client.cjs +72 -0
- package/dist/dev/storefront-client.cjs.map +1 -0
- package/dist/dev/storefront-client.js +72 -0
- package/dist/dev/storefront-client.js.map +1 -0
- package/dist/dev/useMoney.cjs +72 -0
- package/dist/dev/useMoney.cjs.map +1 -0
- package/dist/dev/useMoney.js +72 -0
- package/dist/dev/useMoney.js.map +1 -0
- package/dist/prod/ExternalVideo.cjs +39 -0
- package/dist/prod/ExternalVideo.cjs.map +1 -0
- package/dist/prod/ExternalVideo.js +39 -0
- package/dist/prod/ExternalVideo.js.map +1 -0
- package/dist/prod/Image.cjs +99 -0
- package/dist/prod/Image.cjs.map +1 -0
- package/dist/prod/Image.js +99 -0
- package/dist/prod/Image.js.map +1 -0
- package/dist/prod/MediaFile.cjs +59 -0
- package/dist/prod/MediaFile.cjs.map +1 -0
- package/dist/prod/MediaFile.js +59 -0
- package/dist/prod/MediaFile.js.map +1 -0
- package/dist/prod/Metafield.cjs +288 -0
- package/dist/prod/Metafield.cjs.map +1 -0
- package/dist/prod/Metafield.js +288 -0
- package/dist/prod/Metafield.js.map +1 -0
- package/dist/prod/ModelViewer.cjs +143 -0
- package/dist/prod/ModelViewer.cjs.map +1 -0
- package/dist/prod/ModelViewer.js +143 -0
- package/dist/prod/ModelViewer.js.map +1 -0
- package/dist/prod/Money.cjs +40 -0
- package/dist/prod/Money.cjs.map +1 -0
- package/dist/prod/Money.js +40 -0
- package/dist/prod/Money.js.map +1 -0
- package/dist/prod/ProductPrice.cjs +61 -0
- package/dist/prod/ProductPrice.cjs.map +1 -0
- package/dist/prod/ProductPrice.js +61 -0
- package/dist/prod/ProductPrice.js.map +1 -0
- package/dist/prod/ProductProvider.cjs +161 -0
- package/dist/prod/ProductProvider.cjs.map +1 -0
- package/dist/prod/ProductProvider.js +161 -0
- package/dist/prod/ProductProvider.js.map +1 -0
- package/dist/prod/ShopPayButton.cjs +64 -0
- package/dist/prod/ShopPayButton.cjs.map +1 -0
- package/dist/prod/ShopPayButton.js +64 -0
- package/dist/prod/ShopPayButton.js.map +1 -0
- package/dist/prod/ShopifyProvider.cjs +46 -0
- package/dist/prod/ShopifyProvider.cjs.map +1 -0
- package/dist/prod/ShopifyProvider.js +46 -0
- package/dist/prod/ShopifyProvider.js.map +1 -0
- package/dist/prod/Video.cjs +44 -0
- package/dist/prod/Video.cjs.map +1 -0
- package/dist/prod/Video.js +44 -0
- package/dist/prod/Video.js.map +1 -0
- package/dist/prod/flatten-connection.cjs +18 -0
- package/dist/prod/flatten-connection.cjs.map +1 -0
- package/dist/prod/flatten-connection.js +18 -0
- package/dist/prod/flatten-connection.js.map +1 -0
- package/dist/prod/image-size.cjs +80 -0
- package/dist/prod/image-size.cjs.map +1 -0
- package/dist/prod/image-size.js +80 -0
- package/dist/prod/image-size.js.map +1 -0
- package/dist/prod/index.cjs +35 -0
- package/dist/prod/index.cjs.map +1 -0
- package/dist/prod/index.js +35 -0
- package/dist/prod/index.js.map +1 -0
- package/dist/prod/load-script.cjs +52 -0
- package/dist/prod/load-script.cjs.map +1 -0
- package/dist/prod/load-script.js +52 -0
- package/dist/prod/load-script.js.map +1 -0
- package/dist/prod/storefront-api-constants.cjs +5 -0
- package/dist/prod/storefront-api-constants.cjs.map +1 -0
- package/dist/prod/storefront-api-constants.js +5 -0
- package/dist/prod/storefront-api-constants.js.map +1 -0
- package/dist/prod/storefront-client.cjs +57 -0
- package/dist/prod/storefront-client.cjs.map +1 -0
- package/dist/prod/storefront-client.js +57 -0
- package/dist/prod/storefront-client.js.map +1 -0
- package/dist/prod/useMoney.cjs +72 -0
- package/dist/prod/useMoney.cjs.map +1 -0
- package/dist/prod/useMoney.js +72 -0
- package/dist/prod/useMoney.js.map +1 -0
- package/dist/types/ExternalVideo.d.ts +67 -0
- package/dist/types/Image.d.ts +54 -0
- package/dist/types/MediaFile.d.ts +31 -0
- package/dist/types/Metafield.d.ts +58 -0
- package/dist/types/ModelViewer.d.ts +57 -0
- package/dist/types/Money.d.ts +29 -0
- package/dist/types/ProductPrice.d.ts +22 -0
- package/dist/types/ProductProvider.d.ts +74 -0
- package/dist/types/ShopPayButton.d.ts +42 -0
- package/dist/types/ShopifyProvider.d.ts +42 -0
- package/dist/types/Video.d.ts +20 -0
- package/dist/types/flatten-connection.d.ts +17 -0
- package/dist/types/image-size.d.ts +36 -0
- package/dist/types/index.d.cts +15 -0
- package/dist/types/index.d.ts +15 -0
- package/dist/types/load-script.d.ts +11 -0
- package/dist/types/storefront-api-constants.d.ts +1 -0
- package/dist/types/storefront-api-response.types.d.ts +68 -0
- package/dist/types/storefront-api-types.d.ts +6537 -0
- package/dist/types/storefront-client.d.ts +63 -0
- package/dist/types/useMoney.d.ts +55 -0
- package/dist/umd/hydrogen-react.dev.js +1472 -0
- package/dist/umd/hydrogen-react.dev.js.map +1 -0
- package/dist/umd/hydrogen-react.prod.js +3 -0
- package/dist/umd/hydrogen-react.prod.js.map +1 -0
- package/package.json +106 -0
- package/storefront.schema.json +1 -0
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
import { createContext, useMemo, useState, useEffect, useCallback, useContext } from "react";
|
|
2
|
+
import { flattenConnection } from "./flatten-connection.js";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
const ProductOptionsContext = createContext(null);
|
|
5
|
+
function ProductProvider({
|
|
6
|
+
children,
|
|
7
|
+
data: product,
|
|
8
|
+
initialVariantId: explicitVariantId
|
|
9
|
+
}) {
|
|
10
|
+
const variants = useMemo(() => {
|
|
11
|
+
var _a;
|
|
12
|
+
return flattenConnection((_a = product.variants) != null ? _a : {});
|
|
13
|
+
}, [product.variants]);
|
|
14
|
+
if (!isProductVariantArray(variants)) {
|
|
15
|
+
throw new Error(`<ProductProvider/> requires 'product.variants.nodes' or 'product.variants.edges'`);
|
|
16
|
+
}
|
|
17
|
+
const options = useMemo(() => getOptions(variants), [variants]);
|
|
18
|
+
const [selectedVariant, setSelectedVariant] = useState(() => getVariantBasedOnIdProp(explicitVariantId, variants));
|
|
19
|
+
const [selectedOptions, setSelectedOptions] = useState(() => getSelectedOptions(selectedVariant));
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const newSelectedVariant = getVariantBasedOnIdProp(explicitVariantId, variants);
|
|
22
|
+
setSelectedVariant(newSelectedVariant);
|
|
23
|
+
setSelectedOptions(getSelectedOptions(newSelectedVariant));
|
|
24
|
+
}, [explicitVariantId, variants]);
|
|
25
|
+
const setSelectedOption = useCallback((name, value2) => {
|
|
26
|
+
setSelectedOptions((selectedOptions2) => {
|
|
27
|
+
const opts = {
|
|
28
|
+
...selectedOptions2,
|
|
29
|
+
[name]: value2
|
|
30
|
+
};
|
|
31
|
+
setSelectedVariant(getSelectedVariant(variants, opts));
|
|
32
|
+
return opts;
|
|
33
|
+
});
|
|
34
|
+
}, [setSelectedOptions, variants]);
|
|
35
|
+
const isOptionInStock = useCallback((option, value2) => {
|
|
36
|
+
var _a;
|
|
37
|
+
const proposedVariant = getSelectedVariant(variants, {
|
|
38
|
+
...selectedOptions,
|
|
39
|
+
...{
|
|
40
|
+
[option]: value2
|
|
41
|
+
}
|
|
42
|
+
});
|
|
43
|
+
return (_a = proposedVariant == null ? void 0 : proposedVariant.availableForSale) != null ? _a : true;
|
|
44
|
+
}, [selectedOptions, variants]);
|
|
45
|
+
const sellingPlanGroups = useMemo(() => {
|
|
46
|
+
var _a;
|
|
47
|
+
return flattenConnection((_a = product.sellingPlanGroups) != null ? _a : {}).map((sellingPlanGroup) => {
|
|
48
|
+
var _a2;
|
|
49
|
+
return {
|
|
50
|
+
...sellingPlanGroup,
|
|
51
|
+
sellingPlans: flattenConnection((_a2 = sellingPlanGroup == null ? void 0 : sellingPlanGroup.sellingPlans) != null ? _a2 : {})
|
|
52
|
+
};
|
|
53
|
+
});
|
|
54
|
+
}, [product.sellingPlanGroups]);
|
|
55
|
+
const [selectedSellingPlan, setSelectedSellingPlan] = useState(void 0);
|
|
56
|
+
const selectedSellingPlanAllocation = useMemo(() => {
|
|
57
|
+
var _a, _b;
|
|
58
|
+
if (!selectedVariant || !selectedSellingPlan) {
|
|
59
|
+
return;
|
|
60
|
+
}
|
|
61
|
+
if (!((_a = selectedVariant.sellingPlanAllocations) == null ? void 0 : _a.nodes) && !((_b = selectedVariant.sellingPlanAllocations) == null ? void 0 : _b.edges)) {
|
|
62
|
+
throw new Error(`<ProductProvider/>: You must include 'sellingPlanAllocations.nodes' or 'sellingPlanAllocations.edges' in your variants in order to calculate selectedSellingPlanAllocation`);
|
|
63
|
+
}
|
|
64
|
+
return flattenConnection(selectedVariant.sellingPlanAllocations).find((allocation) => {
|
|
65
|
+
var _a2;
|
|
66
|
+
return ((_a2 = allocation == null ? void 0 : allocation.sellingPlan) == null ? void 0 : _a2.id) === selectedSellingPlan.id;
|
|
67
|
+
});
|
|
68
|
+
}, [selectedVariant, selectedSellingPlan]);
|
|
69
|
+
const value = useMemo(() => ({
|
|
70
|
+
variants,
|
|
71
|
+
variantsConnection: product.variants,
|
|
72
|
+
options,
|
|
73
|
+
selectedVariant,
|
|
74
|
+
setSelectedVariant,
|
|
75
|
+
selectedOptions,
|
|
76
|
+
setSelectedOption,
|
|
77
|
+
setSelectedOptions,
|
|
78
|
+
isOptionInStock,
|
|
79
|
+
selectedSellingPlan,
|
|
80
|
+
setSelectedSellingPlan,
|
|
81
|
+
selectedSellingPlanAllocation,
|
|
82
|
+
sellingPlanGroups,
|
|
83
|
+
sellingPlanGroupsConnection: product.sellingPlanGroups
|
|
84
|
+
}), [isOptionInStock, options, product.sellingPlanGroups, product.variants, selectedOptions, selectedSellingPlan, selectedSellingPlanAllocation, selectedVariant, sellingPlanGroups, setSelectedOption, variants]);
|
|
85
|
+
return /* @__PURE__ */ jsx(ProductOptionsContext.Provider, {
|
|
86
|
+
value,
|
|
87
|
+
children
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
function useProduct() {
|
|
91
|
+
const context = useContext(ProductOptionsContext);
|
|
92
|
+
if (!context) {
|
|
93
|
+
throw new Error(`'useProduct' must be a child of <ProductProvider />`);
|
|
94
|
+
}
|
|
95
|
+
return context;
|
|
96
|
+
}
|
|
97
|
+
function getSelectedVariant(variants, choices) {
|
|
98
|
+
var _a, _b;
|
|
99
|
+
if (!variants.length || ((_b = (_a = variants == null ? void 0 : variants[0]) == null ? void 0 : _a.selectedOptions) == null ? void 0 : _b.length) !== Object.keys(choices).length) {
|
|
100
|
+
return;
|
|
101
|
+
}
|
|
102
|
+
return variants == null ? void 0 : variants.find((variant) => {
|
|
103
|
+
return Object.entries(choices).every(([name, value]) => {
|
|
104
|
+
var _a2;
|
|
105
|
+
return (_a2 = variant == null ? void 0 : variant.selectedOptions) == null ? void 0 : _a2.some((option) => (option == null ? void 0 : option.name) === name && (option == null ? void 0 : option.value) === value);
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
function getOptions(variants) {
|
|
110
|
+
const map = variants.reduce((memo, variant) => {
|
|
111
|
+
var _a;
|
|
112
|
+
if (!variant.selectedOptions) {
|
|
113
|
+
throw new Error(`'getOptions' requires 'variant.selectedOptions'`);
|
|
114
|
+
}
|
|
115
|
+
(_a = variant == null ? void 0 : variant.selectedOptions) == null ? void 0 : _a.forEach((opt) => {
|
|
116
|
+
var _a2, _b, _c, _d;
|
|
117
|
+
memo[(_a2 = opt == null ? void 0 : opt.name) != null ? _a2 : ""] = memo[(_b = opt == null ? void 0 : opt.name) != null ? _b : ""] || /* @__PURE__ */ new Set();
|
|
118
|
+
memo[(_c = opt == null ? void 0 : opt.name) != null ? _c : ""].add((_d = opt == null ? void 0 : opt.value) != null ? _d : "");
|
|
119
|
+
});
|
|
120
|
+
return memo;
|
|
121
|
+
}, {});
|
|
122
|
+
return Object.keys(map).map((option) => {
|
|
123
|
+
return {
|
|
124
|
+
name: option,
|
|
125
|
+
values: Array.from(map[option])
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
function getVariantBasedOnIdProp(explicitVariantId, variants) {
|
|
130
|
+
if (explicitVariantId) {
|
|
131
|
+
const foundVariant = variants.find((variant) => (variant == null ? void 0 : variant.id) === explicitVariantId);
|
|
132
|
+
if (!foundVariant) {
|
|
133
|
+
console.warn(`<ProductProvider/> received a 'initialVariantId' prop, but could not actually find a variant with that ID`);
|
|
134
|
+
}
|
|
135
|
+
return foundVariant;
|
|
136
|
+
}
|
|
137
|
+
if (explicitVariantId === null) {
|
|
138
|
+
return null;
|
|
139
|
+
}
|
|
140
|
+
if (explicitVariantId === void 0) {
|
|
141
|
+
return variants.find((variant) => variant == null ? void 0 : variant.availableForSale) || variants[0];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
function getSelectedOptions(selectedVariant) {
|
|
145
|
+
return (selectedVariant == null ? void 0 : selectedVariant.selectedOptions) ? selectedVariant.selectedOptions.reduce((memo, optionSet) => {
|
|
146
|
+
var _a, _b;
|
|
147
|
+
memo[(_a = optionSet == null ? void 0 : optionSet.name) != null ? _a : ""] = (_b = optionSet == null ? void 0 : optionSet.value) != null ? _b : "";
|
|
148
|
+
return memo;
|
|
149
|
+
}, {}) : {};
|
|
150
|
+
}
|
|
151
|
+
function isProductVariantArray(maybeVariantArray) {
|
|
152
|
+
if (!maybeVariantArray || !Array.isArray(maybeVariantArray)) {
|
|
153
|
+
return false;
|
|
154
|
+
}
|
|
155
|
+
return true;
|
|
156
|
+
}
|
|
157
|
+
export {
|
|
158
|
+
ProductProvider,
|
|
159
|
+
useProduct
|
|
160
|
+
};
|
|
161
|
+
//# sourceMappingURL=ProductProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ProductProvider.js","sources":["../../src/ProductProvider.tsx"],"sourcesContent":["import {\n useMemo,\n useState,\n useEffect,\n useCallback,\n createContext,\n useContext,\n} from 'react';\nimport type {\n SelectedOption as SelectedOptionType,\n SellingPlan,\n SellingPlanAllocation,\n Product,\n ProductVariant as ProductVariantType,\n ProductVariantConnection,\n SellingPlan as SellingPlanType,\n SellingPlanAllocation as SellingPlanAllocationType,\n SellingPlanGroup as SellingPlanGroupType,\n SellingPlanGroupConnection,\n} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\nimport {flattenConnection} from './flatten-connection.js';\n\nconst ProductOptionsContext = createContext<ProductHookValue | null>(null);\n\ntype InitialVariantId = ProductVariantType['id'] | null;\n\ninterface ProductProviderProps {\n /** A [Product object](https://shopify.dev/api/storefront/reference/products/product). */\n data: PartialDeep<Product, {recurseIntoArrays: true}>;\n /** A `ReactNode` element. */\n children: React.ReactNode;\n /**\n * The initially selected variant.\n * The following logic applies to `initialVariantId`:\n * 1. If `initialVariantId` is provided, then it's used even if it's out of stock.\n * 2. If `initialVariantId` is provided but is `null`, then no variant is used.\n * 3. If nothing is passed to `initialVariantId` then the first available / in-stock variant is used.\n * 4. If nothing is passed to `initialVariantId` and no variants are in stock, then the first variant is used.\n */\n initialVariantId?: InitialVariantId;\n}\n\n/**\n * `<ProductProvider />` is a context provider that enables use of the `useProduct()` hook.\n *\n * It helps manage selected options and variants for a product.\n */\nexport function ProductProvider({\n children,\n data: product,\n initialVariantId: explicitVariantId,\n}: ProductProviderProps) {\n // The flattened variants\n const variants = useMemo(\n () => flattenConnection(product.variants ?? {}),\n [product.variants]\n );\n\n if (!isProductVariantArray(variants)) {\n throw new Error(\n `<ProductProvider/> requires 'product.variants.nodes' or 'product.variants.edges'`\n );\n }\n\n // All the options available for a product, based on all the variants\n const options = useMemo(() => getOptions(variants), [variants]);\n\n /**\n * Track the selectedVariant within the provider.\n */\n const [selectedVariant, setSelectedVariant] = useState<\n | PartialDeep<ProductVariantType, {recurseIntoArrays: true}>\n | undefined\n | null\n >(() => getVariantBasedOnIdProp(explicitVariantId, variants));\n\n /**\n * Track the selectedOptions within the provider. If a `initialVariantId`\n * is passed, use that to select initial options.\n */\n const [selectedOptions, setSelectedOptions] = useState<SelectedOptions>(() =>\n getSelectedOptions(selectedVariant)\n );\n\n /**\n * When the initialVariantId changes, we need to make sure we\n * update the selected variant and selected options. If not,\n * then the selected variant and options will reference incorrect\n * values.\n */\n useEffect(() => {\n const newSelectedVariant = getVariantBasedOnIdProp(\n explicitVariantId,\n variants\n );\n setSelectedVariant(newSelectedVariant);\n setSelectedOptions(getSelectedOptions(newSelectedVariant));\n }, [explicitVariantId, variants]);\n\n /**\n * Allow the developer to select an option.\n */\n const setSelectedOption = useCallback(\n (name: string, value: string) => {\n setSelectedOptions((selectedOptions) => {\n const opts = {...selectedOptions, [name]: value};\n setSelectedVariant(getSelectedVariant(variants, opts));\n return opts;\n });\n },\n [setSelectedOptions, variants]\n );\n\n const isOptionInStock = useCallback(\n (option: string, value: string) => {\n const proposedVariant = getSelectedVariant(variants, {\n ...selectedOptions,\n ...{[option]: value},\n });\n\n return proposedVariant?.availableForSale ?? true;\n },\n [selectedOptions, variants]\n );\n\n const sellingPlanGroups = useMemo(\n () =>\n flattenConnection(product.sellingPlanGroups ?? {}).map(\n (sellingPlanGroup) => ({\n ...sellingPlanGroup,\n sellingPlans: flattenConnection(sellingPlanGroup?.sellingPlans ?? {}),\n })\n ),\n [product.sellingPlanGroups]\n );\n\n /**\n * Track the selectedSellingPlan within the hook. If `initialSellingPlanId`\n * is passed, use that as an initial value. Look it up from the `selectedVariant`, since\n * that is also a requirement.\n */\n const [selectedSellingPlan, setSelectedSellingPlan] = useState<\n PartialDeep<SellingPlan, {recurseIntoArrays: true}> | undefined\n >(undefined);\n\n const selectedSellingPlanAllocation = useMemo<\n PartialDeep<SellingPlanAllocation, {recurseIntoArrays: true}> | undefined\n >(() => {\n if (!selectedVariant || !selectedSellingPlan) {\n return;\n }\n\n if (\n !selectedVariant.sellingPlanAllocations?.nodes &&\n !selectedVariant.sellingPlanAllocations?.edges\n ) {\n throw new Error(\n `<ProductProvider/>: You must include 'sellingPlanAllocations.nodes' or 'sellingPlanAllocations.edges' in your variants in order to calculate selectedSellingPlanAllocation`\n );\n }\n\n return flattenConnection(selectedVariant.sellingPlanAllocations).find(\n (allocation) => allocation?.sellingPlan?.id === selectedSellingPlan.id\n );\n }, [selectedVariant, selectedSellingPlan]);\n\n const value = useMemo<ProductHookValue>(\n () => ({\n variants,\n variantsConnection: product.variants,\n options,\n selectedVariant,\n setSelectedVariant,\n selectedOptions,\n setSelectedOption,\n setSelectedOptions,\n isOptionInStock,\n selectedSellingPlan,\n setSelectedSellingPlan,\n selectedSellingPlanAllocation,\n sellingPlanGroups,\n sellingPlanGroupsConnection: product.sellingPlanGroups,\n }),\n [\n isOptionInStock,\n options,\n product.sellingPlanGroups,\n product.variants,\n selectedOptions,\n selectedSellingPlan,\n selectedSellingPlanAllocation,\n selectedVariant,\n sellingPlanGroups,\n setSelectedOption,\n variants,\n ]\n );\n\n return (\n <ProductOptionsContext.Provider value={value}>\n {children}\n </ProductOptionsContext.Provider>\n );\n}\n\n/**\n * Provides access to the context value provided by `<ProductProvider />`. Must be a descendent of `<ProductProvider />`.\n */\nexport function useProduct() {\n const context = useContext(ProductOptionsContext);\n\n if (!context) {\n throw new Error(`'useProduct' must be a child of <ProductProvider />`);\n }\n\n return context;\n}\n\nfunction getSelectedVariant(\n variants: PartialDeep<ProductVariantType, {recurseIntoArrays: true}>[],\n choices: SelectedOptions\n): PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | undefined {\n /**\n * Ensure the user has selected all the required options, not just some.\n */\n if (\n !variants.length ||\n variants?.[0]?.selectedOptions?.length !== Object.keys(choices).length\n ) {\n return;\n }\n\n return variants?.find((variant) => {\n return Object.entries(choices).every(([name, value]) => {\n return variant?.selectedOptions?.some(\n (option) => option?.name === name && option?.value === value\n );\n });\n });\n}\n\nfunction getOptions(\n variants: PartialDeep<ProductVariantType, {recurseIntoArrays: true}>[]\n): OptionWithValues[] {\n const map = variants.reduce((memo, variant) => {\n if (!variant.selectedOptions) {\n throw new Error(`'getOptions' requires 'variant.selectedOptions'`);\n }\n variant?.selectedOptions?.forEach((opt) => {\n memo[opt?.name ?? ''] = memo[opt?.name ?? ''] || new Set();\n memo[opt?.name ?? ''].add(opt?.value ?? '');\n });\n\n return memo;\n }, {} as Record<string, Set<string>>);\n\n return Object.keys(map).map((option) => {\n return {\n name: option,\n values: Array.from(map[option]),\n };\n });\n}\n\nfunction getVariantBasedOnIdProp(\n explicitVariantId: InitialVariantId | undefined,\n variants: Array<\n PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | undefined\n >\n) {\n // get the initial variant based on the logic outlined in the comments for 'initialVariantId' above\n // * 1. If `initialVariantId` is provided, then it's used even if it's out of stock.\n if (explicitVariantId) {\n const foundVariant = variants.find(\n (variant) => variant?.id === explicitVariantId\n );\n if (!foundVariant) {\n console.warn(\n `<ProductProvider/> received a 'initialVariantId' prop, but could not actually find a variant with that ID`\n );\n }\n return foundVariant;\n }\n // * 2. If `initialVariantId` is provided but is `null`, then no variant is used.\n if (explicitVariantId === null) {\n return null;\n }\n // * 3. If nothing is passed to `initialVariantId` then the first available / in-stock variant is used.\n // * 4. If nothing is passed to `initialVariantId` and no variants are in stock, then the first variant is used.\n if (explicitVariantId === undefined) {\n return variants.find((variant) => variant?.availableForSale) || variants[0];\n }\n}\n\nfunction getSelectedOptions(\n selectedVariant:\n | PartialDeep<ProductVariantType, {recurseIntoArrays: true}>\n | undefined\n | null\n): SelectedOptions {\n return selectedVariant?.selectedOptions\n ? selectedVariant.selectedOptions.reduce<SelectedOptions>(\n (memo, optionSet) => {\n memo[optionSet?.name ?? ''] = optionSet?.value ?? '';\n return memo;\n },\n {}\n )\n : {};\n}\n\nfunction isProductVariantArray(\n maybeVariantArray:\n | (PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | undefined)[]\n | undefined\n): maybeVariantArray is PartialDeep<\n ProductVariantType,\n {recurseIntoArrays: true}\n>[] {\n if (!maybeVariantArray || !Array.isArray(maybeVariantArray)) {\n return false;\n }\n\n return true;\n}\n\nexport interface OptionWithValues {\n name: SelectedOptionType['name'];\n values: SelectedOptionType['value'][];\n}\n\ntype ProductHookValue = PartialDeep<\n {\n /** An array of the variant `nodes` from the `VariantConnection`. */\n variants: ProductVariantType[];\n variantsConnection?: ProductVariantConnection;\n /** An array of the product's options and values. */\n options: OptionWithValues[];\n /** The selected variant. */\n selectedVariant?: ProductVariantType | null;\n selectedOptions: SelectedOptions;\n /** The selected selling plan. */\n selectedSellingPlan?: SellingPlanType;\n /** The selected selling plan allocation. */\n selectedSellingPlanAllocation?: SellingPlanAllocationType;\n /** The selling plan groups. */\n sellingPlanGroups?: (Omit<SellingPlanGroupType, 'sellingPlans'> & {\n sellingPlans: SellingPlanType[];\n })[];\n sellingPlanGroupsConnection?: SellingPlanGroupConnection;\n },\n {recurseIntoArrays: true}\n> & {\n /** A callback to set the selected variant to the variant passed as an argument. */\n setSelectedVariant: (\n variant: PartialDeep<ProductVariantType, {recurseIntoArrays: true}> | null\n ) => void;\n /** A callback to set the selected option. */\n setSelectedOption: (\n name: SelectedOptionType['name'],\n value: SelectedOptionType['value']\n ) => void;\n /** A callback to set multiple selected options at once. */\n setSelectedOptions: (options: SelectedOptions) => void;\n /** A callback to set the selected selling plan to the one passed as an argument. */\n setSelectedSellingPlan: (\n sellingPlan: PartialDeep<SellingPlanType, {recurseIntoArrays: true}>\n ) => void;\n /** A callback that returns a boolean indicating if the option is in stock. */\n isOptionInStock: (\n name: SelectedOptionType['name'],\n value: SelectedOptionType['value']\n ) => boolean;\n};\n\nexport type SelectedOptions = {\n [key: string]: string;\n};\n"],"names":["ProductOptionsContext","createContext","ProductProvider","children","data","product","initialVariantId","explicitVariantId","variants","useMemo","flattenConnection","isProductVariantArray","Error","options","getOptions","selectedVariant","setSelectedVariant","useState","getVariantBasedOnIdProp","selectedOptions","setSelectedOptions","getSelectedOptions","useEffect","newSelectedVariant","setSelectedOption","useCallback","name","value","opts","getSelectedVariant","isOptionInStock","option","proposedVariant","availableForSale","sellingPlanGroups","map","sellingPlanGroup","sellingPlans","selectedSellingPlan","setSelectedSellingPlan","undefined","selectedSellingPlanAllocation","sellingPlanAllocations","nodes","edges","find","allocation","sellingPlan","id","variantsConnection","sellingPlanGroupsConnection","_jsx","useProduct","context","useContext","choices","length","Object","keys","variant","entries","every","some","reduce","memo","forEach","opt","Set","add","values","Array","from","foundVariant","console","warn","optionSet","maybeVariantArray","isArray"],"mappings":"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"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const ShopifyProvider = require("./ShopifyProvider.cjs");
|
|
4
|
+
const loadScript = require("./load-script.cjs");
|
|
5
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
+
const SHOPJS_URL = "https://cdn.shopify.com/shopifycloud/shop-js/v1.0/client.js";
|
|
7
|
+
function ShopPayButton({
|
|
8
|
+
variantIds,
|
|
9
|
+
className,
|
|
10
|
+
variantIdsAndQuantities,
|
|
11
|
+
width
|
|
12
|
+
}) {
|
|
13
|
+
const {
|
|
14
|
+
storeDomain
|
|
15
|
+
} = ShopifyProvider.useShop();
|
|
16
|
+
const shopPayLoadedStatus = loadScript.useLoadScript(SHOPJS_URL);
|
|
17
|
+
let ids;
|
|
18
|
+
if (variantIds && variantIdsAndQuantities) {
|
|
19
|
+
throw new Error(DoublePropsErrorMessage);
|
|
20
|
+
}
|
|
21
|
+
if (variantIds) {
|
|
22
|
+
ids = variantIds.reduce((prev, curr) => {
|
|
23
|
+
const bareId = getIdFromGid(curr);
|
|
24
|
+
if (bareId) {
|
|
25
|
+
prev.push(bareId);
|
|
26
|
+
}
|
|
27
|
+
return prev;
|
|
28
|
+
}, []);
|
|
29
|
+
} else if (variantIdsAndQuantities) {
|
|
30
|
+
ids = variantIdsAndQuantities.reduce((prev, curr) => {
|
|
31
|
+
var _a;
|
|
32
|
+
const bareId = getIdFromGid(curr == null ? void 0 : curr.id);
|
|
33
|
+
if (bareId) {
|
|
34
|
+
prev.push(`${bareId}:${(_a = curr == null ? void 0 : curr.quantity) != null ? _a : 1}`);
|
|
35
|
+
}
|
|
36
|
+
return prev;
|
|
37
|
+
}, []);
|
|
38
|
+
} else {
|
|
39
|
+
throw new Error(MissingPropsErrorMessage);
|
|
40
|
+
}
|
|
41
|
+
const style = width ? {
|
|
42
|
+
"--shop-pay-button-width": width
|
|
43
|
+
} : void 0;
|
|
44
|
+
return /* @__PURE__ */ jsxRuntime.jsx("div", {
|
|
45
|
+
className,
|
|
46
|
+
style,
|
|
47
|
+
children: shopPayLoadedStatus === "done" && /* @__PURE__ */ jsxRuntime.jsx("shop-pay-button", {
|
|
48
|
+
"store-url": `https://${storeDomain}`,
|
|
49
|
+
variants: ids.join(",")
|
|
50
|
+
})
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function getIdFromGid(id) {
|
|
54
|
+
if (!id)
|
|
55
|
+
return;
|
|
56
|
+
return id.split("/").pop();
|
|
57
|
+
}
|
|
58
|
+
const MissingPropsErrorMessage = `You must pass in either "variantIds" or "variantIdsAndQuantities" to ShopPayButton`;
|
|
59
|
+
const DoublePropsErrorMessage = `You must provide either a variantIds or variantIdsAndQuantities prop, but not both in the ShopPayButton component`;
|
|
60
|
+
exports.DoublePropsErrorMessage = DoublePropsErrorMessage;
|
|
61
|
+
exports.MissingPropsErrorMessage = MissingPropsErrorMessage;
|
|
62
|
+
exports.ShopPayButton = ShopPayButton;
|
|
63
|
+
exports.getIdFromGid = getIdFromGid;
|
|
64
|
+
//# sourceMappingURL=ShopPayButton.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShopPayButton.cjs","sources":["../../src/ShopPayButton.tsx"],"sourcesContent":["import React from 'react';\nimport {useShop} from './ShopifyProvider.js';\nimport {useLoadScript} from './load-script.js';\n\n// By using 'never' in the \"or\" cases below, it makes these props \"exclusive\" and means that you cannot pass both of them; you must pass either one OR the other.\ntype ShopPayButtonProps = {\n /** A string of classes to apply to the `div` that wraps the Shop Pay button. */\n className?: string;\n /** A string that's applied to the [CSS custom property (variable)](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) `--shop-pay-button-width` for the [Buy with Shop Pay component](https://shopify.dev/custom-storefronts/tools/web-components#buy-with-shop-pay-component). */\n width?: string;\n} & (\n | {\n /** An array of IDs of the variants to purchase with Shop Pay. This will only ever have a quantity of 1 for each variant. If you want to use other quantities, then use 'variantIdsAndQuantities'. */\n variantIds: string[];\n /** An array of variant IDs and quantities to purchase with Shop Pay. */\n variantIdsAndQuantities?: never;\n }\n | {\n /** An array of IDs of the variants to purchase with Shop Pay. This will only ever have a quantity of 1 for each variant. If you want to use other quantities, then use 'variantIdsAndQuantities'. */\n variantIds?: never;\n /** An array of variant IDs and quantities to purchase with Shop Pay. */\n variantIdsAndQuantities: Array<{\n id: string;\n quantity: number;\n }>;\n }\n);\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace JSX {\n interface IntrinsicElements {\n 'shop-pay-button': {\n variants: string;\n 'store-url': string;\n };\n }\n }\n}\n\nconst SHOPJS_URL =\n 'https://cdn.shopify.com/shopifycloud/shop-js/v1.0/client.js';\n\n/**\n * The `ShopPayButton` component renders a button that redirects to the Shop Pay checkout.\n * It renders a [`<shop-pay-button>`](https://shopify.dev/custom-storefronts/tools/web-components) custom element, for which it will lazy-load the source code automatically.\n * It relies on the `<ShopProvider>` context provider.\n */\nexport function ShopPayButton({\n variantIds,\n className,\n variantIdsAndQuantities,\n width,\n}: ShopPayButtonProps) {\n const {storeDomain} = useShop();\n const shopPayLoadedStatus = useLoadScript(SHOPJS_URL);\n\n let ids: string[];\n\n if (variantIds && variantIdsAndQuantities) {\n throw new Error(DoublePropsErrorMessage);\n }\n\n if (variantIds) {\n ids = variantIds.reduce<string[]>((prev, curr) => {\n const bareId = getIdFromGid(curr);\n if (bareId) {\n prev.push(bareId);\n }\n return prev;\n }, []);\n } else if (variantIdsAndQuantities) {\n ids = variantIdsAndQuantities.reduce<string[]>((prev, curr) => {\n const bareId = getIdFromGid(curr?.id);\n if (bareId) {\n prev.push(`${bareId}:${curr?.quantity ?? 1}`);\n }\n return prev;\n }, []);\n } else {\n throw new Error(MissingPropsErrorMessage);\n }\n\n const style = width\n ? ({\n '--shop-pay-button-width': width,\n } as React.CSSProperties)\n : undefined;\n\n return (\n <div className={className} style={style}>\n {shopPayLoadedStatus === 'done' && (\n <shop-pay-button\n store-url={`https://${storeDomain}`}\n variants={ids.join(',')}\n />\n )}\n </div>\n );\n}\n\n/**\n * Takes a string in the format of \"gid://shopify/ProductVariant/41007289630776\" and returns a string of the ID part at the end: \"41007289630776\"\n */\nexport function getIdFromGid(id?: string) {\n if (!id) return;\n return id.split('/').pop();\n}\n\nexport const MissingPropsErrorMessage = `You must pass in either \"variantIds\" or \"variantIdsAndQuantities\" to ShopPayButton`;\nexport const DoublePropsErrorMessage = `You must provide either a variantIds or variantIdsAndQuantities prop, but not both in the ShopPayButton component`;\n"],"names":["SHOPJS_URL","ShopPayButton","variantIds","className","variantIdsAndQuantities","width","storeDomain","useShop","shopPayLoadedStatus","useLoadScript","ids","Error","DoublePropsErrorMessage","reduce","prev","curr","bareId","getIdFromGid","push","id","quantity","MissingPropsErrorMessage","style","undefined","_jsx","join","split","pop"],"mappings":";;;;;AAwCA,MAAMA,aACJ;AAOK,SAASC,cAAc;AAAA,EAC5BC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAJ4B,GAKP;AACf,QAAA;AAAA,IAACC;AAAAA,MAAeC,gBAAtB,QAAA;AACMC,QAAAA,sBAAsBC,yBAAcT,UAAD;AAErCU,MAAAA;AAEJ,MAAIR,cAAcE,yBAAyB;AACnC,UAAA,IAAIO,MAAMC,uBAAV;AAAA,EACP;AAED,MAAIV,YAAY;AACdQ,UAAMR,WAAWW,OAAiB,CAACC,MAAMC,SAAS;AAC1CC,YAAAA,SAASC,aAAaF,IAAD;AAC3B,UAAIC,QAAQ;AACVF,aAAKI,KAAKF,MAAV;AAAA,MACD;AACMF,aAAAA;AAAAA,IACR,GAAE,CANG,CAAA;AAAA,aAOGV,yBAAyB;AAClCM,UAAMN,wBAAwBS,OAAiB,CAACC,MAAMC,SAAS;;AACvDC,YAAAA,SAASC,aAAaF,6BAAMI,EAAP;AAC3B,UAAIH,QAAQ;AACVF,aAAKI,KAAM,GAAEF,WAAUD,kCAAMK,aAANL,YAAkB,GAAzC;AAAA,MACD;AACMD,aAAAA;AAAAA,IACR,GAAE,CANG,CAAA;AAAA,EAAA,OAOD;AACC,UAAA,IAAIH,MAAMU,wBAAV;AAAA,EACP;AAED,QAAMC,QAAQjB,QACT;AAAA,IACC,2BAA2BA;AAAAA,EAE7BkB,IAAAA;AAEJ,wCACE,OAAA;AAAA,IAAK;AAAA,IAAsB;AAAA,IAA3B,UACGf,wBAAwB,UACvBgB,2BAAAA,IAAA,mBAAA;AAAA,MACE,aAAY,WAAUlB;AAAAA,MACtB,UAAUI,IAAIe,KAAK,GAAT;AAAA,IAAA,CAFZ;AAAA,EAAA,CAHN;AAUD;AAKM,SAASR,aAAaE,IAAa;AACxC,MAAI,CAACA;AAAI;AACT,SAAOA,GAAGO,MAAM,GAAT,EAAcC,IAArB;AACD;AAEM,MAAMN,2BAA4B;AAClC,MAAMT,0BAA2B;;;;;"}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { useShop } from "./ShopifyProvider.js";
|
|
2
|
+
import { useLoadScript } from "./load-script.js";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
const SHOPJS_URL = "https://cdn.shopify.com/shopifycloud/shop-js/v1.0/client.js";
|
|
5
|
+
function ShopPayButton({
|
|
6
|
+
variantIds,
|
|
7
|
+
className,
|
|
8
|
+
variantIdsAndQuantities,
|
|
9
|
+
width
|
|
10
|
+
}) {
|
|
11
|
+
const {
|
|
12
|
+
storeDomain
|
|
13
|
+
} = useShop();
|
|
14
|
+
const shopPayLoadedStatus = useLoadScript(SHOPJS_URL);
|
|
15
|
+
let ids;
|
|
16
|
+
if (variantIds && variantIdsAndQuantities) {
|
|
17
|
+
throw new Error(DoublePropsErrorMessage);
|
|
18
|
+
}
|
|
19
|
+
if (variantIds) {
|
|
20
|
+
ids = variantIds.reduce((prev, curr) => {
|
|
21
|
+
const bareId = getIdFromGid(curr);
|
|
22
|
+
if (bareId) {
|
|
23
|
+
prev.push(bareId);
|
|
24
|
+
}
|
|
25
|
+
return prev;
|
|
26
|
+
}, []);
|
|
27
|
+
} else if (variantIdsAndQuantities) {
|
|
28
|
+
ids = variantIdsAndQuantities.reduce((prev, curr) => {
|
|
29
|
+
var _a;
|
|
30
|
+
const bareId = getIdFromGid(curr == null ? void 0 : curr.id);
|
|
31
|
+
if (bareId) {
|
|
32
|
+
prev.push(`${bareId}:${(_a = curr == null ? void 0 : curr.quantity) != null ? _a : 1}`);
|
|
33
|
+
}
|
|
34
|
+
return prev;
|
|
35
|
+
}, []);
|
|
36
|
+
} else {
|
|
37
|
+
throw new Error(MissingPropsErrorMessage);
|
|
38
|
+
}
|
|
39
|
+
const style = width ? {
|
|
40
|
+
"--shop-pay-button-width": width
|
|
41
|
+
} : void 0;
|
|
42
|
+
return /* @__PURE__ */ jsx("div", {
|
|
43
|
+
className,
|
|
44
|
+
style,
|
|
45
|
+
children: shopPayLoadedStatus === "done" && /* @__PURE__ */ jsx("shop-pay-button", {
|
|
46
|
+
"store-url": `https://${storeDomain}`,
|
|
47
|
+
variants: ids.join(",")
|
|
48
|
+
})
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
function getIdFromGid(id) {
|
|
52
|
+
if (!id)
|
|
53
|
+
return;
|
|
54
|
+
return id.split("/").pop();
|
|
55
|
+
}
|
|
56
|
+
const MissingPropsErrorMessage = `You must pass in either "variantIds" or "variantIdsAndQuantities" to ShopPayButton`;
|
|
57
|
+
const DoublePropsErrorMessage = `You must provide either a variantIds or variantIdsAndQuantities prop, but not both in the ShopPayButton component`;
|
|
58
|
+
export {
|
|
59
|
+
DoublePropsErrorMessage,
|
|
60
|
+
MissingPropsErrorMessage,
|
|
61
|
+
ShopPayButton,
|
|
62
|
+
getIdFromGid
|
|
63
|
+
};
|
|
64
|
+
//# sourceMappingURL=ShopPayButton.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShopPayButton.js","sources":["../../src/ShopPayButton.tsx"],"sourcesContent":["import React from 'react';\nimport {useShop} from './ShopifyProvider.js';\nimport {useLoadScript} from './load-script.js';\n\n// By using 'never' in the \"or\" cases below, it makes these props \"exclusive\" and means that you cannot pass both of them; you must pass either one OR the other.\ntype ShopPayButtonProps = {\n /** A string of classes to apply to the `div` that wraps the Shop Pay button. */\n className?: string;\n /** A string that's applied to the [CSS custom property (variable)](https://developer.mozilla.org/en-US/docs/Web/CSS/--*) `--shop-pay-button-width` for the [Buy with Shop Pay component](https://shopify.dev/custom-storefronts/tools/web-components#buy-with-shop-pay-component). */\n width?: string;\n} & (\n | {\n /** An array of IDs of the variants to purchase with Shop Pay. This will only ever have a quantity of 1 for each variant. If you want to use other quantities, then use 'variantIdsAndQuantities'. */\n variantIds: string[];\n /** An array of variant IDs and quantities to purchase with Shop Pay. */\n variantIdsAndQuantities?: never;\n }\n | {\n /** An array of IDs of the variants to purchase with Shop Pay. This will only ever have a quantity of 1 for each variant. If you want to use other quantities, then use 'variantIdsAndQuantities'. */\n variantIds?: never;\n /** An array of variant IDs and quantities to purchase with Shop Pay. */\n variantIdsAndQuantities: Array<{\n id: string;\n quantity: number;\n }>;\n }\n);\n\ndeclare global {\n // eslint-disable-next-line @typescript-eslint/no-namespace\n namespace JSX {\n interface IntrinsicElements {\n 'shop-pay-button': {\n variants: string;\n 'store-url': string;\n };\n }\n }\n}\n\nconst SHOPJS_URL =\n 'https://cdn.shopify.com/shopifycloud/shop-js/v1.0/client.js';\n\n/**\n * The `ShopPayButton` component renders a button that redirects to the Shop Pay checkout.\n * It renders a [`<shop-pay-button>`](https://shopify.dev/custom-storefronts/tools/web-components) custom element, for which it will lazy-load the source code automatically.\n * It relies on the `<ShopProvider>` context provider.\n */\nexport function ShopPayButton({\n variantIds,\n className,\n variantIdsAndQuantities,\n width,\n}: ShopPayButtonProps) {\n const {storeDomain} = useShop();\n const shopPayLoadedStatus = useLoadScript(SHOPJS_URL);\n\n let ids: string[];\n\n if (variantIds && variantIdsAndQuantities) {\n throw new Error(DoublePropsErrorMessage);\n }\n\n if (variantIds) {\n ids = variantIds.reduce<string[]>((prev, curr) => {\n const bareId = getIdFromGid(curr);\n if (bareId) {\n prev.push(bareId);\n }\n return prev;\n }, []);\n } else if (variantIdsAndQuantities) {\n ids = variantIdsAndQuantities.reduce<string[]>((prev, curr) => {\n const bareId = getIdFromGid(curr?.id);\n if (bareId) {\n prev.push(`${bareId}:${curr?.quantity ?? 1}`);\n }\n return prev;\n }, []);\n } else {\n throw new Error(MissingPropsErrorMessage);\n }\n\n const style = width\n ? ({\n '--shop-pay-button-width': width,\n } as React.CSSProperties)\n : undefined;\n\n return (\n <div className={className} style={style}>\n {shopPayLoadedStatus === 'done' && (\n <shop-pay-button\n store-url={`https://${storeDomain}`}\n variants={ids.join(',')}\n />\n )}\n </div>\n );\n}\n\n/**\n * Takes a string in the format of \"gid://shopify/ProductVariant/41007289630776\" and returns a string of the ID part at the end: \"41007289630776\"\n */\nexport function getIdFromGid(id?: string) {\n if (!id) return;\n return id.split('/').pop();\n}\n\nexport const MissingPropsErrorMessage = `You must pass in either \"variantIds\" or \"variantIdsAndQuantities\" to ShopPayButton`;\nexport const DoublePropsErrorMessage = `You must provide either a variantIds or variantIdsAndQuantities prop, but not both in the ShopPayButton component`;\n"],"names":["SHOPJS_URL","ShopPayButton","variantIds","className","variantIdsAndQuantities","width","storeDomain","useShop","shopPayLoadedStatus","useLoadScript","ids","Error","DoublePropsErrorMessage","reduce","prev","curr","bareId","getIdFromGid","push","id","quantity","MissingPropsErrorMessage","style","undefined","_jsx","join","split","pop"],"mappings":";;;AAwCA,MAAMA,aACJ;AAOK,SAASC,cAAc;AAAA,EAC5BC;AAAAA,EACAC;AAAAA,EACAC;AAAAA,EACAC;AAJ4B,GAKP;AACf,QAAA;AAAA,IAACC;AAAAA,MAAeC,QAAtB;AACMC,QAAAA,sBAAsBC,cAAcT,UAAD;AAErCU,MAAAA;AAEJ,MAAIR,cAAcE,yBAAyB;AACnC,UAAA,IAAIO,MAAMC,uBAAV;AAAA,EACP;AAED,MAAIV,YAAY;AACdQ,UAAMR,WAAWW,OAAiB,CAACC,MAAMC,SAAS;AAC1CC,YAAAA,SAASC,aAAaF,IAAD;AAC3B,UAAIC,QAAQ;AACVF,aAAKI,KAAKF,MAAV;AAAA,MACD;AACMF,aAAAA;AAAAA,IACR,GAAE,CANG,CAAA;AAAA,aAOGV,yBAAyB;AAClCM,UAAMN,wBAAwBS,OAAiB,CAACC,MAAMC,SAAS;;AACvDC,YAAAA,SAASC,aAAaF,6BAAMI,EAAP;AAC3B,UAAIH,QAAQ;AACVF,aAAKI,KAAM,GAAEF,WAAUD,kCAAMK,aAANL,YAAkB,GAAzC;AAAA,MACD;AACMD,aAAAA;AAAAA,IACR,GAAE,CANG,CAAA;AAAA,EAAA,OAOD;AACC,UAAA,IAAIH,MAAMU,wBAAV;AAAA,EACP;AAED,QAAMC,QAAQjB,QACT;AAAA,IACC,2BAA2BA;AAAAA,EAE7BkB,IAAAA;AAEJ,6BACE,OAAA;AAAA,IAAK;AAAA,IAAsB;AAAA,IAA3B,UACGf,wBAAwB,UACvBgB,oBAAA,mBAAA;AAAA,MACE,aAAY,WAAUlB;AAAAA,MACtB,UAAUI,IAAIe,KAAK,GAAT;AAAA,IAAA,CAFZ;AAAA,EAAA,CAHN;AAUD;AAKM,SAASR,aAAaE,IAAa;AACxC,MAAI,CAACA;AAAI;AACT,SAAOA,GAAGO,MAAM,GAAT,EAAcC,IAArB;AACD;AAEM,MAAMN,2BAA4B;AAClC,MAAMT,0BAA2B;"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const react = require("react");
|
|
4
|
+
const storefrontApiConstants = require("./storefront-api-constants.cjs");
|
|
5
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
+
const ShopifyContext = react.createContext({
|
|
7
|
+
storeDomain: "test.myshopify.com",
|
|
8
|
+
storefrontToken: "abc123",
|
|
9
|
+
storefrontApiVersion: storefrontApiConstants.SFAPI_VERSION,
|
|
10
|
+
country: {
|
|
11
|
+
isoCode: "US"
|
|
12
|
+
},
|
|
13
|
+
language: {
|
|
14
|
+
isoCode: "EN"
|
|
15
|
+
},
|
|
16
|
+
locale: "EN-US"
|
|
17
|
+
});
|
|
18
|
+
function ShopifyProvider({
|
|
19
|
+
children,
|
|
20
|
+
shopifyConfig
|
|
21
|
+
}) {
|
|
22
|
+
if (!shopifyConfig) {
|
|
23
|
+
throw new Error(`The 'shopifyConfig' prop must be passed to '<ShopifyProvider/>'`);
|
|
24
|
+
}
|
|
25
|
+
if (shopifyConfig.storefrontApiVersion !== storefrontApiConstants.SFAPI_VERSION) {
|
|
26
|
+
console.warn(`This version of Hydrogen-UI is built for Shopify's Storefront API version ${storefrontApiConstants.SFAPI_VERSION}, but it looks like you're using version ${shopifyConfig.storefrontApiVersion}. There may be issues or bugs if you use a mismatched version of Hydrogen-UI and the Storefront API.`);
|
|
27
|
+
}
|
|
28
|
+
const finalConfig = react.useMemo(() => ({
|
|
29
|
+
...shopifyConfig,
|
|
30
|
+
storeDomain: shopifyConfig.storeDomain.replace(/^https?:\/\//, "")
|
|
31
|
+
}), [shopifyConfig]);
|
|
32
|
+
return /* @__PURE__ */ jsxRuntime.jsx(ShopifyContext.Provider, {
|
|
33
|
+
value: finalConfig,
|
|
34
|
+
children
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
function useShop() {
|
|
38
|
+
const shopContext = react.useContext(ShopifyContext);
|
|
39
|
+
if (!shopContext) {
|
|
40
|
+
throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`);
|
|
41
|
+
}
|
|
42
|
+
return shopContext;
|
|
43
|
+
}
|
|
44
|
+
exports.ShopifyProvider = ShopifyProvider;
|
|
45
|
+
exports.useShop = useShop;
|
|
46
|
+
//# sourceMappingURL=ShopifyProvider.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShopifyProvider.cjs","sources":["../../src/ShopifyProvider.tsx"],"sourcesContent":["import {createContext, useContext, useMemo, type ReactNode} from 'react';\nimport type {LanguageCode, CountryCode, Shop} from './storefront-api-types.js';\nimport {SFAPI_VERSION} from './storefront-api-constants.js';\n\nconst ShopifyContext = createContext<ShopifyContextValue>({\n storeDomain: 'test.myshopify.com',\n storefrontToken: 'abc123',\n storefrontApiVersion: SFAPI_VERSION,\n country: {\n isoCode: 'US',\n },\n language: {\n isoCode: 'EN',\n },\n locale: 'EN-US',\n});\n\n/**\n * The `<ShopifyProvider/>` component enables use of the `useShop()` hook. The component should wrap your app.\n */\nexport function ShopifyProvider({\n children,\n shopifyConfig,\n}: {\n children: ReactNode;\n shopifyConfig: ShopifyContextValue;\n}) {\n if (!shopifyConfig) {\n throw new Error(\n `The 'shopifyConfig' prop must be passed to '<ShopifyProvider/>'`\n );\n }\n\n if (shopifyConfig.storefrontApiVersion !== SFAPI_VERSION) {\n console.warn(\n `This version of Hydrogen-UI is built for Shopify's Storefront API version ${SFAPI_VERSION}, but it looks like you're using version ${shopifyConfig.storefrontApiVersion}. There may be issues or bugs if you use a mismatched version of Hydrogen-UI and the Storefront API.`\n );\n }\n\n const finalConfig = useMemo<ShopifyContextValue>(\n () => ({\n ...shopifyConfig,\n storeDomain: shopifyConfig.storeDomain.replace(/^https?:\\/\\//, ''),\n }),\n [shopifyConfig]\n );\n\n return (\n <ShopifyContext.Provider value={finalConfig}>\n {children}\n </ShopifyContext.Provider>\n );\n}\n\n/**\n * Provides access to the `shopifyConfig` prop of `<ShopifyProvider/>`. Must be a descendent of `<ShopifyProvider/>`.\n */\nexport function useShop() {\n const shopContext = useContext(ShopifyContext);\n if (!shopContext) {\n throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`);\n }\n return shopContext;\n}\n\n/**\n * Shopify-specific values that are used in various Hydrogen-UI components and hooks.\n */\nexport type ShopifyContextValue = {\n /** The globally-unique identifier for the Shop */\n storefrontId?: string;\n /** The host name of the domain (eg: `{shop}.myshopify.com`). If a URL with a scheme (for example `https://`) is passed in, then the scheme is removed. */\n storeDomain: Shop['primaryDomain']['host'];\n /** The Storefront API public access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details. */\n storefrontToken: string;\n /** The Storefront API version. This should almost always be the same as the version Hydrogen-UI was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details. */\n storefrontApiVersion: string;\n country?: {\n /**\n * The code designating a country, which generally follows ISO 3166-1 alpha-2 guidelines. If a territory doesn't have a country code value in the `CountryCode` enum, it might be considered a subdivision of another country. For example, the territories associated with Spain are represented by the country code `ES`, and the territories associated with the United States of America are represented by the country code `US`.\n */\n isoCode: CountryCode;\n };\n language?: {\n /**\n * `ISO 369` language codes supported by Shopify.\n */\n isoCode: LanguageCode;\n };\n /**\n * The locale string based on `country` and `language`.\n */\n locale?: string;\n};\n"],"names":["ShopifyContext","createContext","storeDomain","storefrontToken","storefrontApiVersion","SFAPI_VERSION","country","isoCode","language","locale","ShopifyProvider","children","shopifyConfig","Error","console","warn","finalConfig","useMemo","replace","_jsx","useShop","shopContext","useContext"],"mappings":";;;;;AAIA,MAAMA,iBAAiBC,MAAAA,cAAmC;AAAA,EACxDC,aAAa;AAAA,EACbC,iBAAiB;AAAA,EACjBC,sBAAsBC,uBAAAA;AAAAA,EACtBC,SAAS;AAAA,IACPC,SAAS;AAAA,EADF;AAAA,EAGTC,UAAU;AAAA,IACRD,SAAS;AAAA,EADD;AAAA,EAGVE,QAAQ;AAVgD,CAAtB;AAgB7B,SAASC,gBAAgB;AAAA,EAC9BC;AAAAA,EACAC;AAF8B,GAM7B;AACD,MAAI,CAACA,eAAe;AACZ,UAAA,IAAIC,MACP,iEADG;AAAA,EAGP;AAEGD,MAAAA,cAAcR,yBAAyBC,sCAAe;AACxDS,YAAQC,KACL,6EAA4EV,uBAAyDO,yDAAAA,cAAcR,0HADtJ;AAAA,EAGD;AAEKY,QAAAA,cAAcC,MAAAA,QAClB,OAAO;AAAA,IACL,GAAGL;AAAAA,IACHV,aAAaU,cAAcV,YAAYgB,QAAQ,gBAAgB,EAAlD;AAAA,EAAA,IAEf,CAACN,aAAD,CALyB;AASzB,SAAAO,2BAAA,IAAC,eAAe,UAAhB;AAAA,IAAyB,OAAOH;AAAAA,IAAhC;AAAA,EAAA,CADF;AAKD;AAKM,SAASI,UAAU;AAClBC,QAAAA,cAAcC,iBAAWtB,cAAD;AAC9B,MAAI,CAACqB,aAAa;AACV,UAAA,IAAIR,MAAO,wDAAX;AAAA,EACP;AACMQ,SAAAA;AACR;;;"}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { createContext, useMemo, useContext } from "react";
|
|
2
|
+
import { SFAPI_VERSION } from "./storefront-api-constants.js";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
const ShopifyContext = createContext({
|
|
5
|
+
storeDomain: "test.myshopify.com",
|
|
6
|
+
storefrontToken: "abc123",
|
|
7
|
+
storefrontApiVersion: SFAPI_VERSION,
|
|
8
|
+
country: {
|
|
9
|
+
isoCode: "US"
|
|
10
|
+
},
|
|
11
|
+
language: {
|
|
12
|
+
isoCode: "EN"
|
|
13
|
+
},
|
|
14
|
+
locale: "EN-US"
|
|
15
|
+
});
|
|
16
|
+
function ShopifyProvider({
|
|
17
|
+
children,
|
|
18
|
+
shopifyConfig
|
|
19
|
+
}) {
|
|
20
|
+
if (!shopifyConfig) {
|
|
21
|
+
throw new Error(`The 'shopifyConfig' prop must be passed to '<ShopifyProvider/>'`);
|
|
22
|
+
}
|
|
23
|
+
if (shopifyConfig.storefrontApiVersion !== SFAPI_VERSION) {
|
|
24
|
+
console.warn(`This version of Hydrogen-UI is built for Shopify's Storefront API version ${SFAPI_VERSION}, but it looks like you're using version ${shopifyConfig.storefrontApiVersion}. There may be issues or bugs if you use a mismatched version of Hydrogen-UI and the Storefront API.`);
|
|
25
|
+
}
|
|
26
|
+
const finalConfig = useMemo(() => ({
|
|
27
|
+
...shopifyConfig,
|
|
28
|
+
storeDomain: shopifyConfig.storeDomain.replace(/^https?:\/\//, "")
|
|
29
|
+
}), [shopifyConfig]);
|
|
30
|
+
return /* @__PURE__ */ jsx(ShopifyContext.Provider, {
|
|
31
|
+
value: finalConfig,
|
|
32
|
+
children
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
function useShop() {
|
|
36
|
+
const shopContext = useContext(ShopifyContext);
|
|
37
|
+
if (!shopContext) {
|
|
38
|
+
throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`);
|
|
39
|
+
}
|
|
40
|
+
return shopContext;
|
|
41
|
+
}
|
|
42
|
+
export {
|
|
43
|
+
ShopifyProvider,
|
|
44
|
+
useShop
|
|
45
|
+
};
|
|
46
|
+
//# sourceMappingURL=ShopifyProvider.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"ShopifyProvider.js","sources":["../../src/ShopifyProvider.tsx"],"sourcesContent":["import {createContext, useContext, useMemo, type ReactNode} from 'react';\nimport type {LanguageCode, CountryCode, Shop} from './storefront-api-types.js';\nimport {SFAPI_VERSION} from './storefront-api-constants.js';\n\nconst ShopifyContext = createContext<ShopifyContextValue>({\n storeDomain: 'test.myshopify.com',\n storefrontToken: 'abc123',\n storefrontApiVersion: SFAPI_VERSION,\n country: {\n isoCode: 'US',\n },\n language: {\n isoCode: 'EN',\n },\n locale: 'EN-US',\n});\n\n/**\n * The `<ShopifyProvider/>` component enables use of the `useShop()` hook. The component should wrap your app.\n */\nexport function ShopifyProvider({\n children,\n shopifyConfig,\n}: {\n children: ReactNode;\n shopifyConfig: ShopifyContextValue;\n}) {\n if (!shopifyConfig) {\n throw new Error(\n `The 'shopifyConfig' prop must be passed to '<ShopifyProvider/>'`\n );\n }\n\n if (shopifyConfig.storefrontApiVersion !== SFAPI_VERSION) {\n console.warn(\n `This version of Hydrogen-UI is built for Shopify's Storefront API version ${SFAPI_VERSION}, but it looks like you're using version ${shopifyConfig.storefrontApiVersion}. There may be issues or bugs if you use a mismatched version of Hydrogen-UI and the Storefront API.`\n );\n }\n\n const finalConfig = useMemo<ShopifyContextValue>(\n () => ({\n ...shopifyConfig,\n storeDomain: shopifyConfig.storeDomain.replace(/^https?:\\/\\//, ''),\n }),\n [shopifyConfig]\n );\n\n return (\n <ShopifyContext.Provider value={finalConfig}>\n {children}\n </ShopifyContext.Provider>\n );\n}\n\n/**\n * Provides access to the `shopifyConfig` prop of `<ShopifyProvider/>`. Must be a descendent of `<ShopifyProvider/>`.\n */\nexport function useShop() {\n const shopContext = useContext(ShopifyContext);\n if (!shopContext) {\n throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`);\n }\n return shopContext;\n}\n\n/**\n * Shopify-specific values that are used in various Hydrogen-UI components and hooks.\n */\nexport type ShopifyContextValue = {\n /** The globally-unique identifier for the Shop */\n storefrontId?: string;\n /** The host name of the domain (eg: `{shop}.myshopify.com`). If a URL with a scheme (for example `https://`) is passed in, then the scheme is removed. */\n storeDomain: Shop['primaryDomain']['host'];\n /** The Storefront API public access token. Refer to the [authentication](https://shopify.dev/api/storefront#authentication) documentation for more details. */\n storefrontToken: string;\n /** The Storefront API version. This should almost always be the same as the version Hydrogen-UI was built for. Learn more about Shopify [API versioning](https://shopify.dev/api/usage/versioning) for more details. */\n storefrontApiVersion: string;\n country?: {\n /**\n * The code designating a country, which generally follows ISO 3166-1 alpha-2 guidelines. If a territory doesn't have a country code value in the `CountryCode` enum, it might be considered a subdivision of another country. For example, the territories associated with Spain are represented by the country code `ES`, and the territories associated with the United States of America are represented by the country code `US`.\n */\n isoCode: CountryCode;\n };\n language?: {\n /**\n * `ISO 369` language codes supported by Shopify.\n */\n isoCode: LanguageCode;\n };\n /**\n * The locale string based on `country` and `language`.\n */\n locale?: string;\n};\n"],"names":["ShopifyContext","createContext","storeDomain","storefrontToken","storefrontApiVersion","SFAPI_VERSION","country","isoCode","language","locale","ShopifyProvider","children","shopifyConfig","Error","console","warn","finalConfig","useMemo","replace","_jsx","useShop","shopContext","useContext"],"mappings":";;;AAIA,MAAMA,iBAAiBC,cAAmC;AAAA,EACxDC,aAAa;AAAA,EACbC,iBAAiB;AAAA,EACjBC,sBAAsBC;AAAAA,EACtBC,SAAS;AAAA,IACPC,SAAS;AAAA,EADF;AAAA,EAGTC,UAAU;AAAA,IACRD,SAAS;AAAA,EADD;AAAA,EAGVE,QAAQ;AAVgD,CAAtB;AAgB7B,SAASC,gBAAgB;AAAA,EAC9BC;AAAAA,EACAC;AAF8B,GAM7B;AACD,MAAI,CAACA,eAAe;AACZ,UAAA,IAAIC,MACP,iEADG;AAAA,EAGP;AAEGD,MAAAA,cAAcR,yBAAyBC,eAAe;AACxDS,YAAQC,KACL,6EAA4EV,yDAAyDO,cAAcR,0HADtJ;AAAA,EAGD;AAEKY,QAAAA,cAAcC,QAClB,OAAO;AAAA,IACL,GAAGL;AAAAA,IACHV,aAAaU,cAAcV,YAAYgB,QAAQ,gBAAgB,EAAlD;AAAA,EAAA,IAEf,CAACN,aAAD,CALyB;AASzB,SAAAO,oBAAC,eAAe,UAAhB;AAAA,IAAyB,OAAOH;AAAAA,IAAhC;AAAA,EAAA,CADF;AAKD;AAKM,SAASI,UAAU;AAClBC,QAAAA,cAAcC,WAAWtB,cAAD;AAC9B,MAAI,CAACqB,aAAa;AACV,UAAA,IAAIR,MAAO,wDAAX;AAAA,EACP;AACMQ,SAAAA;AACR;"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
const imageSize = require("./image-size.cjs");
|
|
4
|
+
const react = require("react");
|
|
5
|
+
const jsxRuntime = require("react/jsx-runtime");
|
|
6
|
+
function Video(props) {
|
|
7
|
+
var _a, _b;
|
|
8
|
+
const {
|
|
9
|
+
data,
|
|
10
|
+
previewImageOptions,
|
|
11
|
+
id = data.id,
|
|
12
|
+
playsInline = true,
|
|
13
|
+
controls = true,
|
|
14
|
+
sourceProps = {},
|
|
15
|
+
...passthroughProps
|
|
16
|
+
} = props;
|
|
17
|
+
const posterUrl = imageSize.shopifyImageLoader({
|
|
18
|
+
src: (_b = (_a = data.previewImage) == null ? void 0 : _a.url) != null ? _b : "",
|
|
19
|
+
...previewImageOptions
|
|
20
|
+
});
|
|
21
|
+
if (!data.sources) {
|
|
22
|
+
throw new Error(`<Video/> requires a 'data.sources' array`);
|
|
23
|
+
}
|
|
24
|
+
return /* @__PURE__ */ jsxRuntime.jsx("video", {
|
|
25
|
+
...passthroughProps,
|
|
26
|
+
id,
|
|
27
|
+
playsInline,
|
|
28
|
+
controls,
|
|
29
|
+
poster: posterUrl,
|
|
30
|
+
children: data.sources.map((source) => {
|
|
31
|
+
if (!((source == null ? void 0 : source.url) && (source == null ? void 0 : source.mimeType))) {
|
|
32
|
+
throw new Error(`<Video/> needs 'source.url' and 'source.mimeType'`);
|
|
33
|
+
}
|
|
34
|
+
return /* @__PURE__ */ react.createElement("source", {
|
|
35
|
+
...sourceProps,
|
|
36
|
+
key: source.url,
|
|
37
|
+
src: source.url,
|
|
38
|
+
type: source.mimeType
|
|
39
|
+
});
|
|
40
|
+
})
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
exports.Video = Video;
|
|
44
|
+
//# sourceMappingURL=Video.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Video.cjs","sources":["../../src/Video.tsx"],"sourcesContent":["import {type HTMLAttributes} from 'react';\nimport {shopifyImageLoader} from './image-size.js';\nimport type {Video as VideoType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\n\nexport interface VideoProps {\n /** An object with fields that correspond to the Storefront API's [Video object](https://shopify.dev/api/storefront/latest/objects/video). */\n data: PartialDeep<VideoType, {recurseIntoArrays: true}>;\n /** An object of image size options for the video's `previewImage`. Uses `shopifyImageLoader` to generate the `poster` URL. */\n previewImageOptions?: Parameters<typeof shopifyImageLoader>[0];\n /** Props that will be passed to the `video` element's `source` children elements. */\n sourceProps?: HTMLAttributes<HTMLSourceElement> & {\n 'data-testid'?: string;\n };\n}\n\n/**\n * The `Video` component renders a `video` for the Storefront API's [Video object](https://shopify.dev/api/storefront/reference/products/video).\n */\nexport function Video(props: JSX.IntrinsicElements['video'] & VideoProps) {\n const {\n data,\n previewImageOptions,\n id = data.id,\n playsInline = true,\n controls = true,\n sourceProps = {},\n ...passthroughProps\n } = props;\n\n const posterUrl = shopifyImageLoader({\n src: data.previewImage?.url ?? '',\n ...previewImageOptions,\n });\n\n if (!data.sources) {\n throw new Error(`<Video/> requires a 'data.sources' array`);\n }\n\n return (\n // eslint-disable-next-line jsx-a11y/media-has-caption\n <video\n {...passthroughProps}\n id={id}\n playsInline={playsInline}\n controls={controls}\n poster={posterUrl}\n >\n {data.sources.map((source) => {\n if (!(source?.url && source?.mimeType)) {\n throw new Error(`<Video/> needs 'source.url' and 'source.mimeType'`);\n }\n return (\n <source\n {...sourceProps}\n key={source.url}\n src={source.url}\n type={source.mimeType}\n />\n );\n })}\n </video>\n );\n}\n"],"names":["Video","props","data","previewImageOptions","id","playsInline","controls","sourceProps","passthroughProps","posterUrl","shopifyImageLoader","src","previewImage","url","sources","Error","map","source","mimeType"],"mappings":";;;;;AAmBO,SAASA,MAAMC,OAAoD;;AAClE,QAAA;AAAA,IACJC;AAAAA,IACAC;AAAAA,IACAC,KAAKF,KAAKE;AAAAA,IACVC,cAAc;AAAA,IACdC,WAAW;AAAA,IACXC,cAAc,CANV;AAAA,OAODC;AAAAA,EACDP,IAAAA;AAEJ,QAAMQ,YAAYC,UAAAA,mBAAmB;AAAA,IACnCC,MAAKT,gBAAKU,iBAALV,mBAAmBW,QAAnBX,YAA0B;AAAA,IAC/B,GAAGC;AAAAA,EAAAA,CAF+B;AAKhC,MAAA,CAACD,KAAKY,SAAS;AACX,UAAA,IAAIC,MAAO,0CAAX;AAAA,EACP;AAED,wCAEE,SAAA;AAAA,IAAA,GACMP;AAAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQC;AAAAA,IALV,UAOGP,KAAKY,QAAQE,IAAKC,CAAW,WAAA;AAC5B,UAAI,GAAEA,iCAAQJ,SAAOI,iCAAQC,YAAW;AAChC,cAAA,IAAIH,MAAO,mDAAX;AAAA,MACP;AACD;WAEQR;AAAAA,QACJ,KAAKU,OAAOJ;AAAAA,QACZ,KAAKI,OAAOJ;AAAAA,QACZ,MAAMI,OAAOC;AAAAA,MAAAA,CALjB;AAAA,IAAA,CAJD;AAAA,EAAA,CAPH;AAsBH;;"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { shopifyImageLoader } from "./image-size.js";
|
|
2
|
+
import { createElement } from "react";
|
|
3
|
+
import { jsx } from "react/jsx-runtime";
|
|
4
|
+
function Video(props) {
|
|
5
|
+
var _a, _b;
|
|
6
|
+
const {
|
|
7
|
+
data,
|
|
8
|
+
previewImageOptions,
|
|
9
|
+
id = data.id,
|
|
10
|
+
playsInline = true,
|
|
11
|
+
controls = true,
|
|
12
|
+
sourceProps = {},
|
|
13
|
+
...passthroughProps
|
|
14
|
+
} = props;
|
|
15
|
+
const posterUrl = shopifyImageLoader({
|
|
16
|
+
src: (_b = (_a = data.previewImage) == null ? void 0 : _a.url) != null ? _b : "",
|
|
17
|
+
...previewImageOptions
|
|
18
|
+
});
|
|
19
|
+
if (!data.sources) {
|
|
20
|
+
throw new Error(`<Video/> requires a 'data.sources' array`);
|
|
21
|
+
}
|
|
22
|
+
return /* @__PURE__ */ jsx("video", {
|
|
23
|
+
...passthroughProps,
|
|
24
|
+
id,
|
|
25
|
+
playsInline,
|
|
26
|
+
controls,
|
|
27
|
+
poster: posterUrl,
|
|
28
|
+
children: data.sources.map((source) => {
|
|
29
|
+
if (!((source == null ? void 0 : source.url) && (source == null ? void 0 : source.mimeType))) {
|
|
30
|
+
throw new Error(`<Video/> needs 'source.url' and 'source.mimeType'`);
|
|
31
|
+
}
|
|
32
|
+
return /* @__PURE__ */ createElement("source", {
|
|
33
|
+
...sourceProps,
|
|
34
|
+
key: source.url,
|
|
35
|
+
src: source.url,
|
|
36
|
+
type: source.mimeType
|
|
37
|
+
});
|
|
38
|
+
})
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
export {
|
|
42
|
+
Video
|
|
43
|
+
};
|
|
44
|
+
//# sourceMappingURL=Video.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"Video.js","sources":["../../src/Video.tsx"],"sourcesContent":["import {type HTMLAttributes} from 'react';\nimport {shopifyImageLoader} from './image-size.js';\nimport type {Video as VideoType} from './storefront-api-types.js';\nimport type {PartialDeep} from 'type-fest';\n\nexport interface VideoProps {\n /** An object with fields that correspond to the Storefront API's [Video object](https://shopify.dev/api/storefront/latest/objects/video). */\n data: PartialDeep<VideoType, {recurseIntoArrays: true}>;\n /** An object of image size options for the video's `previewImage`. Uses `shopifyImageLoader` to generate the `poster` URL. */\n previewImageOptions?: Parameters<typeof shopifyImageLoader>[0];\n /** Props that will be passed to the `video` element's `source` children elements. */\n sourceProps?: HTMLAttributes<HTMLSourceElement> & {\n 'data-testid'?: string;\n };\n}\n\n/**\n * The `Video` component renders a `video` for the Storefront API's [Video object](https://shopify.dev/api/storefront/reference/products/video).\n */\nexport function Video(props: JSX.IntrinsicElements['video'] & VideoProps) {\n const {\n data,\n previewImageOptions,\n id = data.id,\n playsInline = true,\n controls = true,\n sourceProps = {},\n ...passthroughProps\n } = props;\n\n const posterUrl = shopifyImageLoader({\n src: data.previewImage?.url ?? '',\n ...previewImageOptions,\n });\n\n if (!data.sources) {\n throw new Error(`<Video/> requires a 'data.sources' array`);\n }\n\n return (\n // eslint-disable-next-line jsx-a11y/media-has-caption\n <video\n {...passthroughProps}\n id={id}\n playsInline={playsInline}\n controls={controls}\n poster={posterUrl}\n >\n {data.sources.map((source) => {\n if (!(source?.url && source?.mimeType)) {\n throw new Error(`<Video/> needs 'source.url' and 'source.mimeType'`);\n }\n return (\n <source\n {...sourceProps}\n key={source.url}\n src={source.url}\n type={source.mimeType}\n />\n );\n })}\n </video>\n );\n}\n"],"names":["Video","props","data","previewImageOptions","id","playsInline","controls","sourceProps","passthroughProps","posterUrl","shopifyImageLoader","src","previewImage","url","sources","Error","map","source","mimeType"],"mappings":";;;AAmBO,SAASA,MAAMC,OAAoD;;AAClE,QAAA;AAAA,IACJC;AAAAA,IACAC;AAAAA,IACAC,KAAKF,KAAKE;AAAAA,IACVC,cAAc;AAAA,IACdC,WAAW;AAAA,IACXC,cAAc,CANV;AAAA,OAODC;AAAAA,EACDP,IAAAA;AAEJ,QAAMQ,YAAYC,mBAAmB;AAAA,IACnCC,MAAKT,gBAAKU,iBAALV,mBAAmBW,QAAnBX,YAA0B;AAAA,IAC/B,GAAGC;AAAAA,EAAAA,CAF+B;AAKhC,MAAA,CAACD,KAAKY,SAAS;AACX,UAAA,IAAIC,MAAO,0CAAX;AAAA,EACP;AAED,6BAEE,SAAA;AAAA,IAAA,GACMP;AAAAA,IACJ;AAAA,IACA;AAAA,IACA;AAAA,IACA,QAAQC;AAAAA,IALV,UAOGP,KAAKY,QAAQE,IAAKC,CAAW,WAAA;AAC5B,UAAI,GAAEA,iCAAQJ,SAAOI,iCAAQC,YAAW;AAChC,cAAA,IAAIH,MAAO,mDAAX;AAAA,MACP;AACD;WAEQR;AAAAA,QACJ,KAAKU,OAAOJ;AAAAA,QACZ,KAAKI,OAAOJ;AAAAA,QACZ,MAAMI,OAAOC;AAAAA,MAAAA,CALjB;AAAA,IAAA,CAJD;AAAA,EAAA,CAPH;AAsBH;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperties(exports, { __esModule: { value: true }, [Symbol.toStringTag]: { value: "Module" } });
|
|
3
|
+
function flattenConnection(connection) {
|
|
4
|
+
if (connection.nodes) {
|
|
5
|
+
return connection.nodes;
|
|
6
|
+
}
|
|
7
|
+
if (connection.edges) {
|
|
8
|
+
return connection.edges.map((edge) => {
|
|
9
|
+
if (!(edge == null ? void 0 : edge.node)) {
|
|
10
|
+
throw new Error("Connection edges must contain nodes");
|
|
11
|
+
}
|
|
12
|
+
return edge.node;
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
return [];
|
|
16
|
+
}
|
|
17
|
+
exports.flattenConnection = flattenConnection;
|
|
18
|
+
//# sourceMappingURL=flatten-connection.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flatten-connection.cjs","sources":["../../src/flatten-connection.ts"],"sourcesContent":["import type {PartialDeep} from 'type-fest';\n\n/**\n * The `flattenConnection` utility transforms a connection object from the Storefront API (for example, [Product-related connections](https://shopify.dev/api/storefront/reference/products/product)) into a flat array of nodes.\n * The utility works with either `nodes` or `edges.node`.\n */\nexport function flattenConnection<T>(\n connection: PartialDeep<GraphQLConnection<T>, {recurseIntoArrays: true}>\n): PartialDeep<T, {recurseIntoArrays: true}>[] {\n if (connection.nodes) {\n return connection.nodes as PartialDeep<T, {recurseIntoArrays: true}>[];\n }\n\n if (connection.edges) {\n return connection.edges.map((edge) => {\n if (!edge?.node) {\n throw new Error('Connection edges must contain nodes');\n }\n return edge.node;\n });\n }\n\n if (__HYDROGEN_DEV__) {\n console.warn(\n `The connection did not contain either \"nodes\" or \"edges.node\". A empty array will be returned in its place.`\n );\n }\n\n return [];\n}\n\ninterface GraphQLConnection<T> {\n edges?: {node: T}[];\n nodes?: T[];\n}\n"],"names":[],"mappings":";;AAMO,SAAS,kBACd,YAC6C;AAC7C,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW;AAAA,EACpB;AAEA,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW,MAAM,IAAI,CAAC,SAAS;AAChC,UAAA,EAAC,6BAAM,OAAM;AACT,cAAA,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACA,aAAO,KAAK;AAAA,IAAA,CACb;AAAA,EACH;AAQA,SAAO;AACT;;"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
function flattenConnection(connection) {
|
|
2
|
+
if (connection.nodes) {
|
|
3
|
+
return connection.nodes;
|
|
4
|
+
}
|
|
5
|
+
if (connection.edges) {
|
|
6
|
+
return connection.edges.map((edge) => {
|
|
7
|
+
if (!(edge == null ? void 0 : edge.node)) {
|
|
8
|
+
throw new Error("Connection edges must contain nodes");
|
|
9
|
+
}
|
|
10
|
+
return edge.node;
|
|
11
|
+
});
|
|
12
|
+
}
|
|
13
|
+
return [];
|
|
14
|
+
}
|
|
15
|
+
export {
|
|
16
|
+
flattenConnection
|
|
17
|
+
};
|
|
18
|
+
//# sourceMappingURL=flatten-connection.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"flatten-connection.js","sources":["../../src/flatten-connection.ts"],"sourcesContent":["import type {PartialDeep} from 'type-fest';\n\n/**\n * The `flattenConnection` utility transforms a connection object from the Storefront API (for example, [Product-related connections](https://shopify.dev/api/storefront/reference/products/product)) into a flat array of nodes.\n * The utility works with either `nodes` or `edges.node`.\n */\nexport function flattenConnection<T>(\n connection: PartialDeep<GraphQLConnection<T>, {recurseIntoArrays: true}>\n): PartialDeep<T, {recurseIntoArrays: true}>[] {\n if (connection.nodes) {\n return connection.nodes as PartialDeep<T, {recurseIntoArrays: true}>[];\n }\n\n if (connection.edges) {\n return connection.edges.map((edge) => {\n if (!edge?.node) {\n throw new Error('Connection edges must contain nodes');\n }\n return edge.node;\n });\n }\n\n if (__HYDROGEN_DEV__) {\n console.warn(\n `The connection did not contain either \"nodes\" or \"edges.node\". A empty array will be returned in its place.`\n );\n }\n\n return [];\n}\n\ninterface GraphQLConnection<T> {\n edges?: {node: T}[];\n nodes?: T[];\n}\n"],"names":[],"mappings":"AAMO,SAAS,kBACd,YAC6C;AAC7C,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW;AAAA,EACpB;AAEA,MAAI,WAAW,OAAO;AACpB,WAAO,WAAW,MAAM,IAAI,CAAC,SAAS;AAChC,UAAA,EAAC,6BAAM,OAAM;AACT,cAAA,IAAI,MAAM,qCAAqC;AAAA,MACvD;AACA,aAAO,KAAK;AAAA,IAAA,CACb;AAAA,EACH;AAQA,SAAO;AACT;"}
|