Skip to content

Commit

Permalink
Release 1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
davychhouk committed Nov 28, 2022
1 parent ca124ff commit 266e11f
Show file tree
Hide file tree
Showing 28 changed files with 66 additions and 50 deletions.
13 changes: 7 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,11 @@ In `App1`:

```javascript
import { useEffect, useState } from "react";
import { attachParamsToUrl, Iframe, initRequester } from "react-postmessage";
import {
attachParamsToUrl,
Iframe,
initRequester,
} from "@chhoukdavy/react-postmessage";
import "./App.css";

const URL = "http://localhost:3001";
Expand Down Expand Up @@ -104,7 +108,7 @@ import {
cleanUp,
postMessage,
signalClose,
} from "react-postmessage";
} from "@chhoukdavy/react-postmessage";
import "./App.css";

type HookData = {
Expand All @@ -122,10 +126,7 @@ function App() {
hook: setData,
checkOrigin: true,
});

return () => {
cleanUp();
};
return cleanUp();
}, [fromOrigin]);

return (
Expand Down
1 change: 1 addition & 0 deletions example/app1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"eject": "react-scripts eject"
},
"dependencies": {
"@chhoukdavy/react-postmessage": "^1.0.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-postmessage": "link:../..",
Expand Down
12 changes: 8 additions & 4 deletions example/app1/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
import { useEffect, useState } from "react";
import { attachParamsToUrl, Iframe, initRequester } from "react-postmessage";
import {
attachParamsToUrl,
initRequester,
Iframe,
} from "@chhoukdavy/react-postmessage";
import "./App.css";

const URL = "http://localhost:3001";
const testData = {
const TEST_DATA = {
test: "sth from app1",
};

Expand All @@ -18,8 +22,8 @@ function App() {
useEffect(() => {
initRequester<Data>({
url: URL,
data: TEST_DATA,
checkOrigin: true,
data: testData,
hook: setData,
close: () => setShow(false),
});
Expand All @@ -28,7 +32,7 @@ function App() {
const formedURL = attachParamsToUrl(URL, [
{
name: "fromOrigin",
value: "http://localhost:3000",
value: window?.location?.origin,
},
]);

Expand Down
5 changes: 5 additions & 0 deletions example/app1/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,11 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@chhoukdavy/react-postmessage@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@chhoukdavy/react-postmessage/-/react-postmessage-1.0.1.tgz#bf5c9fb5092acf27b25fe591897fb24dc95a442d"
integrity sha512-UmlNeAbmpGGajg5EN/xBuTtRcMkNLL2B0IxUp5OGzymLVcQj5j2drKVDtiCgV2gvwbsOd8nWnBdaDk+FGJeNjg==

"@csstools/normalize.css@*":
version "12.0.0"
resolved "https://registry.yarnpkg.com/@csstools/normalize.css/-/normalize.css-12.0.0.tgz#a9583a75c3f150667771f30b60d9f059473e62c4"
Expand Down
1 change: 1 addition & 0 deletions example/app2/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"eject": "react-scripts eject"
},
"dependencies": {
"@chhoukdavy/react-postmessage": "^1.0.1",
"react": "18.2.0",
"react-dom": "18.2.0",
"react-postmessage": "link:../..",
Expand Down
7 changes: 2 additions & 5 deletions example/app2/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
cleanUp,
postMessage,
signalClose,
} from "react-postmessage";
} from "@chhoukdavy/react-postmessage";
import "./App.css";

type HookData = {
Expand All @@ -22,10 +22,7 @@ function App() {
hook: setData,
checkOrigin: true,
});

return () => {
cleanUp();
};
return cleanUp();
}, [fromOrigin]);

return (
Expand Down
5 changes: 5 additions & 0 deletions example/app2/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1082,6 +1082,11 @@
resolved "https://registry.yarnpkg.com/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz#75a2e8b51cb758a7553d6804a5932d7aace75c39"
integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw==

"@chhoukdavy/react-postmessage@^1.0.1":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@chhoukdavy/react-postmessage/-/react-postmessage-1.0.1.tgz#bf5c9fb5092acf27b25fe591897fb24dc95a442d"
integrity sha512-UmlNeAbmpGGajg5EN/xBuTtRcMkNLL2B0IxUp5OGzymLVcQj5j2drKVDtiCgV2gvwbsOd8nWnBdaDk+FGJeNjg==

"@csstools/normalize.css@*":
version "12.0.0"
resolved "https://registry.yarnpkg.com/@csstools/normalize.css/-/normalize.css-12.0.0.tgz#a9583a75c3f150667771f30b60d9f059473e62c4"
Expand Down
2 changes: 1 addition & 1 deletion lib/cjs/components/Iframe.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="react" />
declare type Props = {
type Props = {
url: string;
height?: number | string | undefined;
width?: number | string | undefined;
Expand Down
2 changes: 1 addition & 1 deletion lib/cjs/components/Iframe.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ var react_1 = __importDefault(require("react"));
var constants_1 = require("../constants");
var Iframe = function (_a) {
var url = _a.url, _b = _a.height, height = _b === void 0 ? 450 : _b, _c = _a.width, width = _c === void 0 ? 450 : _c;
return (react_1.default.createElement("iframe", { id: constants_1.IFRAME_ID, src: url, width: width, height: height }));
return (react_1.default.createElement("iframe", { id: constants_1.TARGET_ID, src: url, width: width, height: height }));
};
exports.Iframe = Iframe;
2 changes: 1 addition & 1 deletion lib/cjs/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export declare const IFRAME_ID = "react-postmessage-iframe";
export declare const TARGET_ID = "@chhoukdavy/react-postmessage-target-window-id";
export declare const TYPES: {
handshake: string;
request: string;
Expand Down
4 changes: 2 additions & 2 deletions lib/cjs/constants.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.TYPES = exports.IFRAME_ID = void 0;
exports.IFRAME_ID = "react-postmessage-iframe";
exports.TYPES = exports.TARGET_ID = void 0;
exports.TARGET_ID = "@chhoukdavy/react-postmessage-target-window-id";
exports.TYPES = {
handshake: "handshake",
request: "request",
Expand Down
2 changes: 1 addition & 1 deletion lib/cjs/utils/params.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export declare const getOriginFromUrl: (url: string) => string;
export declare const getParam: (href: string, name: string) => string | null;
declare type Param = {
type Param = {
name: string;
value: string | undefined;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/cjs/utils/params.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ Object.defineProperty(exports, "__esModule", { value: true });
exports.attachParamsToUrl = exports.getParam = exports.getOriginFromUrl = void 0;
var getOriginFromUrl = function (url) {
var _a;
return (_a = new URL(url)) === null || _a === void 0 ? void 0 : _a.origin;
var cleanedHref = url === null || url === void 0 ? void 0 : url.replace("#", "");
return (_a = new URL(cleanedHref)) === null || _a === void 0 ? void 0 : _a.origin;
};
exports.getOriginFromUrl = getOriginFromUrl;
var getParam = function (href, name) {
Expand Down
4 changes: 2 additions & 2 deletions lib/cjs/utils/utilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare type RequesterArgs = {
type RequesterArgs = {
url: string;
checkOrigin?: boolean;
};
Expand All @@ -7,7 +7,7 @@ export declare function initRequester<Data>({ url, checkOrigin, data, hook, clos
hook?: (args: Data) => void;
close?: () => void;
}): void;
declare type ResponderArgs = {
type ResponderArgs = {
fromOrigin: string;
setFromOrigin: (o: string) => void;
checkOrigin?: boolean;
Expand Down
6 changes: 3 additions & 3 deletions lib/cjs/utils/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ var params_1 = require("./params");
function initRequester(_a) {
var url = _a.url, checkOrigin = _a.checkOrigin, data = _a.data, hook = _a.hook, close = _a.close;
if (window && document) {
var iframeElement = document === null || document === void 0 ? void 0 : document.getElementById(constants_1.IFRAME_ID);
var iframeElement = document === null || document === void 0 ? void 0 : document.getElementById(constants_1.TARGET_ID);
var iframeWindow_1 = iframeElement === null || iframeElement === void 0 ? void 0 : iframeElement.contentWindow;
/**
* Register listener
Expand Down Expand Up @@ -63,7 +63,7 @@ function initReceiver(_a) {
var type = (_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.type;
var payload = (_b = e === null || e === void 0 ? void 0 : e.data) === null || _b === void 0 ? void 0 : _b.payload;
if (type === constants_1.TYPES.request && payload) {
hook(payload);
hook && hook(payload);
return;
}
}, false);
Expand All @@ -72,8 +72,8 @@ function initReceiver(_a) {
*/
var origin_1 = (0, params_1.getParam)((_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.href, "fromOrigin");
if (origin_1) {
setFromOrigin(origin_1);
(_c = window === null || window === void 0 ? void 0 : window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({ type: constants_1.TYPES.handshake }, origin_1);
setFromOrigin(origin_1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/esm/components/Iframe.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/// <reference types="react" />
declare type Props = {
type Props = {
url: string;
height?: number | string | undefined;
width?: number | string | undefined;
Expand Down
4 changes: 2 additions & 2 deletions lib/esm/components/Iframe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React from "react";
import { IFRAME_ID } from "../constants";
import { TARGET_ID } from "../constants";
export var Iframe = function (_a) {
var url = _a.url, _b = _a.height, height = _b === void 0 ? 450 : _b, _c = _a.width, width = _c === void 0 ? 450 : _c;
return (React.createElement("iframe", { id: IFRAME_ID, src: url, width: width, height: height }));
return (React.createElement("iframe", { id: TARGET_ID, src: url, width: width, height: height }));
};
2 changes: 1 addition & 1 deletion lib/esm/constants.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export declare const IFRAME_ID = "react-postmessage-iframe";
export declare const TARGET_ID = "@chhoukdavy/react-postmessage-target-window-id";
export declare const TYPES: {
handshake: string;
request: string;
Expand Down
2 changes: 1 addition & 1 deletion lib/esm/constants.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export var IFRAME_ID = "react-postmessage-iframe";
export var TARGET_ID = "@chhoukdavy/react-postmessage-target-window-id";
export var TYPES = {
handshake: "handshake",
request: "request",
Expand Down
2 changes: 1 addition & 1 deletion lib/esm/utils/params.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
export declare const getOriginFromUrl: (url: string) => string;
export declare const getParam: (href: string, name: string) => string | null;
declare type Param = {
type Param = {
name: string;
value: string | undefined;
};
Expand Down
3 changes: 2 additions & 1 deletion lib/esm/utils/params.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export var getOriginFromUrl = function (url) {
var _a;
return (_a = new URL(url)) === null || _a === void 0 ? void 0 : _a.origin;
var cleanedHref = url === null || url === void 0 ? void 0 : url.replace("#", "");
return (_a = new URL(cleanedHref)) === null || _a === void 0 ? void 0 : _a.origin;
};
export var getParam = function (href, name) {
var _a;
Expand Down
4 changes: 2 additions & 2 deletions lib/esm/utils/utilities.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
declare type RequesterArgs = {
type RequesterArgs = {
url: string;
checkOrigin?: boolean;
};
Expand All @@ -7,7 +7,7 @@ export declare function initRequester<Data>({ url, checkOrigin, data, hook, clos
hook?: (args: Data) => void;
close?: () => void;
}): void;
declare type ResponderArgs = {
type ResponderArgs = {
fromOrigin: string;
setFromOrigin: (o: string) => void;
checkOrigin?: boolean;
Expand Down
8 changes: 4 additions & 4 deletions lib/esm/utils/utilities.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { IFRAME_ID, TYPES } from "../constants";
import { TARGET_ID, TYPES } from "../constants";
import { getOriginFromUrl, getParam } from "./params";
export function initRequester(_a) {
var url = _a.url, checkOrigin = _a.checkOrigin, data = _a.data, hook = _a.hook, close = _a.close;
if (window && document) {
var iframeElement = document === null || document === void 0 ? void 0 : document.getElementById(IFRAME_ID);
var iframeElement = document === null || document === void 0 ? void 0 : document.getElementById(TARGET_ID);
var iframeWindow_1 = iframeElement === null || iframeElement === void 0 ? void 0 : iframeElement.contentWindow;
/**
* Register listener
Expand Down Expand Up @@ -59,7 +59,7 @@ export function initReceiver(_a) {
var type = (_a = e === null || e === void 0 ? void 0 : e.data) === null || _a === void 0 ? void 0 : _a.type;
var payload = (_b = e === null || e === void 0 ? void 0 : e.data) === null || _b === void 0 ? void 0 : _b.payload;
if (type === TYPES.request && payload) {
hook(payload);
hook && hook(payload);
return;
}
}, false);
Expand All @@ -68,8 +68,8 @@ export function initReceiver(_a) {
*/
var origin_1 = getParam((_b = window === null || window === void 0 ? void 0 : window.location) === null || _b === void 0 ? void 0 : _b.href, "fromOrigin");
if (origin_1) {
setFromOrigin(origin_1);
(_c = window === null || window === void 0 ? void 0 : window.parent) === null || _c === void 0 ? void 0 : _c.postMessage({ type: TYPES.handshake }, origin_1);
setFromOrigin(origin_1);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@chhoukdavy/react-postmessage",
"version": "1.0.1",
"version": "1.0.2",
"author": "Davy CHHOUK",
"scripts": {
"build": "yarn build:esm && yarn build:cjs",
Expand Down
4 changes: 2 additions & 2 deletions src/components/Iframe.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from "react";
import { IFRAME_ID } from "../constants";
import { TARGET_ID } from "../constants";

type Props = {
url: string;
Expand All @@ -12,5 +12,5 @@ export const Iframe = ({
height = 450,
width = 450,
}: Props): JSX.Element => (
<iframe id={IFRAME_ID} src={url} width={width} height={height} />
<iframe id={TARGET_ID} src={url} width={width} height={height} />
);
2 changes: 1 addition & 1 deletion src/constants.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const IFRAME_ID = "react-postmessage-iframe";
export const TARGET_ID = "@chhoukdavy/react-postmessage-target-window-id";
export const TYPES = {
handshake: "handshake",
request: "request",
Expand Down
4 changes: 2 additions & 2 deletions src/utils/params.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const getOriginFromUrl = (url: string) => {
return new URL(url)?.origin;
const cleanedHref = url?.replace("#", "");
return new URL(cleanedHref)?.origin;
};

export const getParam = (href: string, name: string): string | null => {
Expand Down Expand Up @@ -36,6 +37,5 @@ export const attachParamsToUrl = (url: string, params: Param[]): string => {
}
}
});

return attachedUrl;
};
8 changes: 4 additions & 4 deletions src/utils/utilities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { IFRAME_ID, TYPES } from "../constants";
import { TARGET_ID, TYPES } from "../constants";
import { getOriginFromUrl, getParam } from "./params";

type RequesterArgs = {
Expand All @@ -18,7 +18,7 @@ export function initRequester<Data>({
}) {
if (window && document) {
const iframeElement = document?.getElementById(
IFRAME_ID
TARGET_ID
) as HTMLIFrameElement;
const iframeWindow = iframeElement?.contentWindow;

Expand Down Expand Up @@ -92,7 +92,7 @@ export function initReceiver<Data>({
const type = e?.data?.type;
const payload = e?.data?.payload;
if (type === TYPES.request && payload) {
hook(payload);
hook && hook(payload);
return;
}
},
Expand All @@ -104,8 +104,8 @@ export function initReceiver<Data>({
*/
const origin = getParam(window?.location?.href, "fromOrigin");
if (origin) {
setFromOrigin(origin);
window?.parent?.postMessage({ type: TYPES.handshake }, origin);
setFromOrigin(origin);
}
}
}
Expand Down

0 comments on commit 266e11f

Please sign in to comment.