UNPKG

crowdriff-story-network-gallery-react

Version:

A Component that allows the embedding of a CrowdRiff Story Network gallery in a React app

68 lines (50 loc) 3.39 kB
"use strict"; function _typeof(obj) { "@babel/helpers - typeof"; if (typeof Symbol === "function" && typeof Symbol.iterator === "symbol") { _typeof = function _typeof(obj) { return typeof obj; }; } else { _typeof = function _typeof(obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; }; } return _typeof(obj); } Object.defineProperty(exports, "__esModule", { value: true }); exports["default"] = void 0; var _react = _interopRequireWildcard(require("react")); function _getRequireWildcardCache(nodeInterop) { if (typeof WeakMap !== "function") return null; var cacheBabelInterop = new WeakMap(); var cacheNodeInterop = new WeakMap(); return (_getRequireWildcardCache = function _getRequireWildcardCache(nodeInterop) { return nodeInterop ? cacheNodeInterop : cacheBabelInterop; })(nodeInterop); } function _interopRequireWildcard(obj, nodeInterop) { if (!nodeInterop && obj && obj.__esModule) { return obj; } if (obj === null || _typeof(obj) !== "object" && typeof obj !== "function") { return { "default": obj }; } var cache = _getRequireWildcardCache(nodeInterop); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (key !== "default" && Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj["default"] = obj; if (cache) { cache.set(obj, newObj); } return newObj; } var CrowdRiffStoryGallery = function CrowdRiffStoryGallery(props) { var galleryId = props.galleryId; (0, _react.useEffect)(function () { // set the script source based on environment // The script to load story galleries in the current page DOM. var galleryLoadScript = 'https://g.amp.travel/v2/js'; // variable to see if the script has already been appended to the page. var existingScript = document.getElementById('cr__init-JS'); // If the existingScript variable is true, do not continue to execute the useEffect, return. if (existingScript) { return; } // Gallery configurations var q = []; var a = window.amptravel = function (a, p) { window.amptravel.q.push({ a: a, p: p }); }; a.q = q; // Create script dom element var e = document.createElement('script'); // Set the script src to the gallery load script e.setAttribute('src', galleryLoadScript); // Add an ID to the script so it can be easily selected. e.id = 'cr__init-JS'; // Append script tag to document head. This will initiate the gallery load script document.head.appendChild(e); // Component unmount return function () { // If the element variable 'e' exists, remove the element from the dom. // This will allow the script to rerun on route changes on React sites. if (e) { e.remove(); } }; }); return ( /*#__PURE__*/ // HTML markup for gallery _react["default"].createElement("div", { "data-amp-travel-gallery-id": galleryId, "data-amp-travel": "gallery-container" }) ); }; var _default = CrowdRiffStoryGallery; exports["default"] = _default;