UNPKG

@shopify/checkout-sheet-kit

Version:

A React Native library for Shopify's Checkout Kit.

106 lines (100 loc) 4.45 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ShopifyCheckoutSheetProvider = ShopifyCheckoutSheetProvider; exports.default = void 0; exports.useShopifyCheckoutSheet = useShopifyCheckoutSheet; var _react = _interopRequireWildcard(require("react")); var _index = require("./index"); function _getRequireWildcardCache(e) { if ("function" != typeof WeakMap) return null; var r = new WeakMap(), t = new WeakMap(); return (_getRequireWildcardCache = function (e) { return e ? t : r; })(e); } function _interopRequireWildcard(e, r) { if (!r && e && e.__esModule) return e; if (null === e || "object" != typeof e && "function" != typeof e) return { default: e }; var t = _getRequireWildcardCache(r); if (t && t.has(e)) return t.get(e); var n = { __proto__: null }, a = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var u in e) if ("default" !== u && Object.prototype.hasOwnProperty.call(e, u)) { var i = a ? Object.getOwnPropertyDescriptor(e, u) : null; i && (i.get || i.set) ? Object.defineProperty(n, u, i) : n[u] = e[u]; } return n.default = e, t && t.set(e, n), n; } /* MIT License Copyright 2023 - Present, Shopify Inc. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ const noop = () => undefined; const ShopifyCheckoutSheetContext = /*#__PURE__*/_react.default.createContext({ addEventListener: noop, removeEventListeners: noop, setConfig: noop, getConfig: async () => undefined, preload: noop, present: noop, invalidate: noop, dismiss: noop, version: undefined }); function ShopifyCheckoutSheetProvider({ features, configuration, children }) { const instance = (0, _react.useRef)(null); if (!instance.current) { instance.current = new _index.ShopifyCheckoutSheet(configuration, features); } const addEventListener = (0, _react.useCallback)((eventName, callback) => { return instance.current?.addEventListener(eventName, callback); }, []); const removeEventListeners = (0, _react.useCallback)(eventName => { instance.current?.removeEventListeners(eventName); }, []); const present = (0, _react.useCallback)(checkoutUrl => { if (checkoutUrl) { instance.current?.present(checkoutUrl); } }, []); const preload = (0, _react.useCallback)(checkoutUrl => { if (checkoutUrl) { instance.current?.preload(checkoutUrl); } }, []); const invalidate = (0, _react.useCallback)(() => { instance.current?.invalidate(); }, []); const dismiss = (0, _react.useCallback)(() => { instance.current?.dismiss(); }, []); const setConfig = (0, _react.useCallback)(config => { instance.current?.setConfig(config); }, []); const getConfig = (0, _react.useCallback)(async () => { return instance.current?.getConfig(); }, []); const context = (0, _react.useMemo)(() => { return { addEventListener, dismiss, setConfig, getConfig, preload, present, invalidate, removeEventListeners, version: instance.current?.version }; }, [addEventListener, dismiss, removeEventListeners, getConfig, setConfig, preload, present, invalidate]); return /*#__PURE__*/_react.default.createElement(ShopifyCheckoutSheetContext.Provider, { value: context }, children); } function useShopifyCheckoutSheet() { return _react.default.useContext(ShopifyCheckoutSheetContext); } var _default = exports.default = ShopifyCheckoutSheetContext; //# sourceMappingURL=context.js.map