UNPKG

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