Skip to content

Commit

Permalink
Add example project
Browse files Browse the repository at this point in the history
  • Loading branch information
vunam committed Aug 13, 2024
1 parent 9ea92bf commit 93befb7
Show file tree
Hide file tree
Showing 10 changed files with 216 additions and 42 deletions.
18 changes: 15 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,21 @@ Events are sent two way, parent frame to child frame and child frame to parent f
![Event Flow](./docs/images/event_flow.svg "Event FLow Diagram")
Parent document

## Example
## Example Project

In the main document:
An example is included in the project in the `/example` folder. Run the following command to see it in action:

```bash
yarn start:demo
```

Go to `http://localhost:3030` to see the demo app.

![Example](./docs/images/example.png "Example")

## Example Code

In the parent document:

```typescript
const state = {
Expand All @@ -38,7 +50,7 @@ const myAPI = new ParentFrame({
child: document.querySelector("iframe"),
methods: {
updateCounter: function () {
state.counter = state.counter++;
state.counter++;
this.send("counterUpdated", {
counter: state.counter,
});
Expand Down
Binary file added docs/images/example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 8 additions & 8 deletions docs/working-with-frame-events.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,16 @@ new ParentFrame(options);

### Options

| Name | Type | |
| --------- | ------------------- | ---------- |
| child | `HTMLIFrameElement` | `required` |
| methods | `object` | |
| listeners | `string[]` | |
| scripts | `string[]` | |
| Name | Type | |
| ------------------ | ------------------- | ---------- |
| childFrameNode | `HTMLIFrameElement` | `required` |
| methods | `object` | |
| listeners | `string[]` | |
| scripts | `string[]` | |

#### child
#### childFrameNode

A child is a `HTMLIFrameElement` that is embedding a document with a ChildFrame instance into the parent document. This iframe must be attached to the DOM and ready to receive events.
A childFrameNode is a `HTMLIFrameElement` that is embedding a document with a ChildFrame instance into the parent document. This iframe must be attached to the DOM and ready to receive events.

When building your iframe source you must specify the parent origin in order to establish a secure connection.

Expand Down
11 changes: 10 additions & 1 deletion example/assets/ChildFrame.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
<body>
<h1>Child Frame</h1>

<button>Increment counter</button>
<button>Increment counter on parent frame</button>

<h3>Messages received from parent frame:</h3>
<ul id="messages"></ul>

<script type="module" src="/child.js"></script>
<style>
body { font-family: Helvetica, sans-serif; }
button { border: none; border-radius: 5px; background: black; color: white; padding: 12px; margin: 0 0 16px 0;}
#counter { font-weight: 900; color: green; }
</style>
</body>
</html>
24 changes: 17 additions & 7 deletions example/assets/child.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
const { ChildFrame } = require("../../build/index");
const { ChildFrame } = require('../../build/index');

const addMessage = (text) => {
const messagesBox = document.getElementById('messages');
const message = document.createElement('li');
message.append(text);
messagesBox.append(message);
}

const frameEvents = new ChildFrame((data) => {
addMessage('Parent "Ready" event');

(function() {
const frameEvents = new ChildFrame();

document.querySelector("button").addEventListener("click", function () {
frameEvents.run.updateCounter();
frameEvents.listeners.parentClicked(() => {
addMessage('Parent click event');
});
})();

});

document.querySelector('button').addEventListener('click', (data) => {
frameEvents.run.updateCounter();
});
11 changes: 9 additions & 2 deletions example/assets/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,16 @@
<h1>Parent Frame</h1>
<p>Counter: <span id="counter">0</span></p>

<iframe id="childFrame" src="./ChildFrame.html?_origin=localhost:3030&_placement=TEST" width="500" height="500"></iframe>
<div>
<button id="sendParentEvent">Send Event From Parent</button>
</div>

<script type="text/javascript" src="/parent.js"></script>

<style>
body { font-family: Helvetica, sans-serif; }
button { border: none; border-radius: 5px; background: black; color: white; padding: 12px; margin: 0 0 16px 0;}
#counter { font-weight: 900; color: green; }
iframe { border: 1px solid #ccc; }
</style>
</body>
</html>
52 changes: 34 additions & 18 deletions example/assets/parent.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,47 @@
/* eslint-disable */

const { ParentFrame } = require("../../build/index");
const testDomain = 'http://localhost:3030'

(function() {
const { ParentFrame } = require('../../build/index');

const childFrameNode = document.createElement('iframe');

childFrameNode.src = `${testDomain}/ChildFrame.html?_origin=${testDomain}&_placement=TEST`

childFrameNode.width = 500;
childFrameNode.height = 360;

childFrameNode.onload = ({ target }) => {
const state = {
counter: 0,
};

console.log('test')
const element = document.getElementById("childFrame");
console.log(element)

new ParentFrame({
child: document.getElementById("childFrame"),
const parentFrame = new ParentFrame({
childFrameNode: target,
methods: {
updateCounter: function () {
state.counter = state.counter++;
this.send("counterUpdated", {
counter: state.counter,
});
updateCounter: () => {
state.counter++;

const counterElement = document.getElementById("counter");
const counterElement = document.getElementById('counter');
counterElement.innerHTML = state.counter;
console.log('!!!')
},
},
listeners: ["counterUpdated"],
scripts: [],
listeners: ['parentClicked'],
scripts: [`
<script>
const el = document.createElement("div");
el.innerHTML = "This text is injected by the Parent Frame script.";
el.style = "background: yellow";
document.body.prepend(el)
</script>
`],
});
})();

const sendEventButton = document.getElementById('sendParentEvent');
sendEventButton.onclick = () => {
parentFrame.send('parentClicked');
}
}


document.body.appendChild(childFrameNode);
2 changes: 1 addition & 1 deletion example/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,4 @@ module.exports = {
},
port: 3030,
},
};
};
4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"lint:fix": "eslint src/**/*.ts --fix",
"version:patch": "yarn version --patch",
"version:minor": "yarn version --minor",
"version:major": "yarn version --major"
"version:major": "yarn version --major",
"start:demo": "yarn build && yarn webpack-dev-server --config ./example/webpack.config.js"
},
"lint-staged": {
"**/*.ts": [
Expand Down Expand Up @@ -59,6 +60,7 @@
"tslib": "^2.6.3",
"typescript": "^5.5.3",
"webpack": "^5.93.0",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^5.0.4"
},
"dependencies": {},
Expand Down
Loading

0 comments on commit 93befb7

Please sign in to comment.