UNPKG

@formidable-webview/webshell

Version:

🔥 Craft Robust React Native WebView-based components with ease.

40 lines (35 loc) • 2.6 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HandleHashChangeFeature = void 0; var _FeatureBuilder = _interopRequireDefault(require("../FeatureBuilder")); function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; } /* babel-plugin-inline-import './HandleHashChangeFeature.webjs' */ var linkPressScript = "function HandleHashChangeFeature(context) {\n var options = context.options || {};\n var shouldResetHashOnEvent = !!options.shouldResetHashOnEvent;\n var handleHashChangeEvent = context.makeCallbackSafe(function () {\n var newhash = document.location.hash;\n var name = newhash.substr(1);\n var targetEl =\n document.getElementById(name) ||\n document.querySelector('a[name=\"' + name + '\"]');\n shouldResetHashOnEvent && (document.location.hash = '');\n if (targetEl) {\n var rect = targetEl.getBoundingClientRect();\n var targetElementBoundingRect = {\n x: rect.x,\n y: rect.y,\n top: rect.top,\n left: rect.left,\n bottom: rect.bottom,\n right: rect.right,\n width: rect.width,\n height: rect.height\n };\n context.postMessageToShell({\n hash: newhash,\n targetElementBoundingRect: targetElementBoundingRect\n });\n }\n });\n window.addEventListener('hashchange', handleHashChangeEvent);\n}\n"; var defaultOptions = { shouldResetHashOnEvent: false }; /** * This feature provide `onDOMHashChange` prop with payloads of type {@link HashChangeEvent} to intercept hashchange events, triggered when the hash fragment of the URL changes. * An event will be triggered when the user clicks on anchors like this: * * ```html * <a href="#subresource">Let's Jump to Subresource</a> * ``` * * Or when JavaScript code imperatively changes the hash fragment of current location. * The feature can be customized with {@link HandleHashChangeOptions}. * See {@link https://developer.mozilla.org/en-US/docs/Web/API/Window/hashchange_event | MDN—hashchange event}. * * For an example, read {@link https://formidable-webview.github.io/webshell//docs/autoheight#handling-hashchange-events | Handling hashchange events}. * * @public */ var HandleHashChangeFeature = new _FeatureBuilder["default"]({ script: linkPressScript, defaultOptions: defaultOptions, identifier: 'org.formidable-webview/webshell.handle-hash-change' }).withShellHandler('onDOMHashChange').build(); exports.HandleHashChangeFeature = HandleHashChangeFeature; //# sourceMappingURL=HandleHashChangeFeature.js.map