@zohodesk/a11y
Version:
In this Package, We Provide Some Basic Components For Accessibility.
64 lines (58 loc) • 1.79 kB
JavaScript
/**
* Sends a post message to the parent window containing information about a mouse event
* and the iframe's bounding data.
*
* @param {MouseEvent} event - The mouse event object containing details of the event
* like type, clientX, and clientY.
* @param {HTMLIFrameElement} iframe - The iframe element where the event originated,
* used to get its bounding rectangle information.
*/
import { POST_MESSAGE_TYPE } from "../data/constants";
export const handleReadingMaskPostMessage = (event, iframe) => {
const message = {
type: POST_MESSAGE_TYPE,
event: {
type: event.type,
x: event.clientX,
y: event.clientY,
iframeData: iframe.getBoundingClientRect()
}
};
window.parent.postMessage(message, '*');
};
/**
* Safely retrieves the document of an iframe's content window.
* @param {HTMLIFrameElement} iframe - The iframe element.
* @returns {Document|null} - The iframe's content window document or null if unavailable.
*/
export function getIframeDocument(iframe) {
if (iframe && iframe.contentWindow && iframe.contentWindow.document) {
return iframe.contentWindow.document;
}
return null;
}
export const throttle = (func, limit) => {
let lastFunc;
let lastRan;
return {
func() {
const context = this; // eslint-disable-next-line prefer-rest-params
const args = arguments;
if (!lastRan) {
func.apply(context, args);
lastRan = Date.now();
} else {
clearTimeout(lastFunc);
lastFunc = setTimeout(() => {
if (Date.now() - lastRan >= limit) {
func.apply(context, args);
lastRan = Date.now();
}
}, limit - (Date.now() - lastRan));
}
},
cancel() {
clearTimeout(lastFunc);
}
};
};