UNPKG

6.3 kBJavaScriptView Raw
1"use strict";
2var __assign = (this && this.__assign) || function () {
3 __assign = Object.assign || function(t) {
4 for (var s, i = 1, n = arguments.length; i < n; i++) {
5 s = arguments[i];
6 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7 t[p] = s[p];
8 }
9 return t;
10 };
11 return __assign.apply(this, arguments);
12};
13var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
14 if (k2 === undefined) k2 = k;
15 Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
16}) : (function(o, m, k, k2) {
17 if (k2 === undefined) k2 = k;
18 o[k2] = m[k];
19}));
20var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
21 Object.defineProperty(o, "default", { enumerable: true, value: v });
22}) : function(o, v) {
23 o["default"] = v;
24});
25var __importStar = (this && this.__importStar) || function (mod) {
26 if (mod && mod.__esModule) return mod;
27 var result = {};
28 if (mod != null) for (var k in mod) if (k !== "default" && Object.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
29 __setModuleDefault(result, mod);
30 return result;
31};
32var __rest = (this && this.__rest) || function (s, e) {
33 var t = {};
34 for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
35 t[p] = s[p];
36 if (s != null && typeof Object.getOwnPropertySymbols === "function")
37 for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
38 if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
39 t[p[i]] = s[p[i]];
40 }
41 return t;
42};
43var __importDefault = (this && this.__importDefault) || function (mod) {
44 return (mod && mod.__esModule) ? mod : { "default": mod };
45};
46Object.defineProperty(exports, "__esModule", { value: true });
47exports.Modal = void 0;
48var react_1 = __importStar(require("react"));
49var react_compose_1 = __importDefault(require("@shopify/react-compose"));
50var polaris_1 = require("@shopify/polaris");
51var app_bridge_1 = require("@shopify/app-bridge");
52var actions_1 = require("@shopify/app-bridge/actions");
53var modal_1 = require("../store/reducers/embeddedApp/modal");
54var withFeature_1 = __importDefault(require("../withFeature"));
55var Frame_1 = __importDefault(require("../Frame"));
56var hooks_1 = require("../hooks");
57var appUrl_1 = require("./utilities/appUrl");
58var style = {
59 border: 'none',
60 width: '100%',
61 flex: '1',
62 display: 'flex',
63};
64var DEFAULT_IFRAME_CONTENT_HEIGHT = 400;
65var SMALL_IFRAME_CONTENT_HEIGHT = 150;
66/**
67 * Renders a Polaris Modal with the Context set to `Modal`
68 * When the `location` is defined, renders Frame component as the modal content
69 * @public
70 * @requires HostContext
71 * */
72function Modal(props) {
73 var _a = hooks_1.useHostContext(), app = _a.app, config = _a.config;
74 var _b = props.actions, close = _b.close, clickFooterButton = _b.clickFooterButton, _c = props.store, _d = _c.content, content = _d === void 0 ? null : _d, height = _c.height, id = _c.id, location = _c.location, open = _c.open, primaryAction = _c.primaryAction, secondaryActions = _c.secondaryActions, size = _c.size, _e = _c.title, title = _e === void 0 ? '' : _e, extraProps = __rest(props, ["actions", "store"]);
75 var modalStyles = __assign(__assign({}, style), { height: (height || getIframeHeight(size)) + "px" });
76 var url = react_1.useMemo(function () {
77 if (!location) {
78 return;
79 }
80 var apiKey = config.apiKey, handle = config.handle, url = config.url;
81 return appUrl_1.buildAppUrl({ handle: handle, apiKey: apiKey, url: url, pathname: location, search: takeSearch(location) });
82 }, [location]);
83 // There is a bug in Polaris for the iframe height calculation
84 // It returns null when the modal is not opened and is never re-calculated
85 if (!open) {
86 return null;
87 }
88 var frameContent = url ? (react_1.default.createElement(Frame_1.default, __assign({}, extraProps, { config: config, style: modalStyles, context: app_bridge_1.Context.Modal, app: app, title: title, url: url.href, onUrlChange: updateIframeUrl }))) : (content);
89 var isLarge = size === actions_1.Modal.Size.Large;
90 var isSectioned = url === undefined;
91 var polarisPrimaryAction = primaryAction ? createButton(primaryAction) : undefined;
92 var polarisSecondaryActions = modalSecondaryActions(secondaryActions);
93 function handleClose() {
94 close({ id: id });
95 }
96 return (react_1.default.createElement(polaris_1.Modal, { large: isLarge, title: title, open: open, onClose: handleClose, primaryAction: polarisPrimaryAction, secondaryActions: polarisSecondaryActions, sectioned: isSectioned }, frameContent));
97 function createButton(button) {
98 var label = button.label, id = button.id, disabled = button.disabled, style = button.style, loading = button.loading;
99 var onAction = function () { return clickFooterButton(id); };
100 return {
101 content: label,
102 disabled: disabled,
103 destructive: style === actions_1.Button.Style.Danger,
104 onAction: onAction,
105 loading: loading,
106 };
107 }
108 function modalSecondaryActions(actions) {
109 if (!Array.isArray(actions) || !actions.length) {
110 return;
111 }
112 return actions.map(createButton);
113 }
114}
115exports.Modal = Modal;
116function getIframeHeight(size) {
117 var isFullScreen = size === actions_1.Modal.Size.Full;
118 if (isFullScreen) {
119 return;
120 }
121 if (size === actions_1.Modal.Size.Small) {
122 return SMALL_IFRAME_CONTENT_HEIGHT;
123 }
124 return DEFAULT_IFRAME_CONTENT_HEIGHT;
125}
126/**
127 * Take the search parameters from a given URL
128 */
129function takeSearch(url) {
130 var match = url.match(/\?[^#]+/);
131 if (match === null) {
132 return undefined;
133 }
134 return match[0];
135}
136function updateIframeUrl(iframe, newUrl) {
137 if (!iframe || !iframe.contentWindow) {
138 return;
139 }
140 iframe.contentWindow.location.replace(newUrl);
141}
142/**
143 * The Modal feature with its reducer, actions and UI component
144 * @public
145 * */
146exports.default = react_compose_1.default(withFeature_1.default(modal_1.feature))(Modal);