@shopify/hydrogen-react
Version:
React components, hooks, and utilities for creating custom Shopify storefronts
47 lines (46 loc) • 1.48 kB
JavaScript
import { createContext, useMemo, useContext } from "react";
import { SFAPI_VERSION } from "./storefront-api-constants.mjs";
import { jsx } from "react/jsx-runtime";
const ShopifyContext = createContext({
storeDomain: "test.myshopify.com",
storefrontToken: "abc123",
storefrontApiVersion: SFAPI_VERSION,
country: {
isoCode: "US"
},
language: {
isoCode: "EN"
},
locale: "EN-US"
});
function ShopifyProvider({
children,
shopifyConfig
}) {
if (!shopifyConfig) {
throw new Error(`The 'shopifyConfig' prop must be passed to '<ShopifyProvider/>'`);
}
if (shopifyConfig.storefrontApiVersion !== SFAPI_VERSION) {
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.`);
}
const finalConfig = useMemo(() => ({
...shopifyConfig,
storeDomain: shopifyConfig.storeDomain.replace(/^https?:\/\//, "")
}), [shopifyConfig]);
return /* @__PURE__ */ jsx(ShopifyContext.Provider, {
value: finalConfig,
children
});
}
function useShop() {
const shopContext = useContext(ShopifyContext);
if (!shopContext) {
throw new Error(`'useShop()' must be a descendent of <ShopifyProvider/>`);
}
return shopContext;
}
export {
ShopifyProvider,
useShop
};
//# sourceMappingURL=ShopifyProvider.mjs.map