Get started 🚀
Installation​
- Using hosted file
- Using npm package
For the iframe to work you might need to acquire an API Key
- Vanilla JS
- React
- Vue
ts
import { setup } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.3'const iframe = document.querySelector('iframe#your-id')const client = setup(iframe, {Â apiKey: '<your-api-key>' })
ts
import { setup } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.3'const iframe = document.querySelector('iframe#your-id')const client = setup(iframe, {Â apiKey: '<your-api-key>' })
The client
won't be properly set if the iframe is not yet loaded.
If you have an Error about it not being loaded call setup on the onload event of the iframe
ts
import { typeEventer ,setup } from '@unionavatars/iframe'Âletclient :Eventer iframe .onload = () => {client =setup (iframe , {apiKey : '<your-api-key>'})}
ts
import { typeEventer ,setup } from '@unionavatars/iframe'Âletclient :Eventer iframe .onload = () => {client =setup (iframe , {apiKey : '<your-api-key>'})}
The variable client
can be used to do plain requests, or to listen for events.
For a more detailed list of what can be done you can refer to the API docs.
With that client
you can do things like login
, change the logo with set-logo
, or list the avatars with avatars
.
The calls are done with the request
method of the client
.
ts
constavatars = awaitclient .request ('avatars')
ts
constavatars = awaitclient .request ('avatars')
html
<script type="module">import { Provider, Iframe, useIframeContext } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.1/react'function App () {const { eventer } = useIframeContext()const onClick = () => { eventer.request('goto', 'Register') }return <button onClick={onClick}>Go to register</button>}const reactRoot = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)reactRoot.render(<Provider apiKey="<your-api-key>"><Iframe src="https://iframe.unionavatars.com" /><App /></Provider>)</script>
html
<script type="module">import { Provider, Iframe, useIframeContext } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.1/react'function App () {const { eventer } = useIframeContext()const onClick = () => { eventer.request('goto', 'Register') }return <button onClick={onClick}>Go to register</button>}const reactRoot = ReactDOM.createRoot(document.getElementById('root') as HTMLElement)reactRoot.render(<Provider apiKey="<your-api-key>"><Iframe src="https://iframe.unionavatars.com" /><App /></Provider>)</script>
html
<script type="module">import { createApp, defineComponent } from 'vue'import { Plugin, Iframe, useIframeContext } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.1/vue'const component = defineComponent({template: `<Iframe src="https://iframe.unionavatars.com" /><button @click="goToRegister">Go to register</button>`,setup () {const ctx = useIframeContext()return {goToRegister () {ctx?.eventer?.request('goto', 'Register')}}}})const app = createApp(component)app.use(Plugin({ apiKey: '<your-api-key>' }))app.mount('#root')</script>
html
<script type="module">import { createApp, defineComponent } from 'vue'import { Plugin, Iframe, useIframeContext } from 'https://cdn.skypack.dev/@unionavatars/iframe@v0.3.1/vue'const component = defineComponent({template: `<Iframe src="https://iframe.unionavatars.com" /><button @click="goToRegister">Go to register</button>`,setup () {const ctx = useIframeContext()return {goToRegister () {ctx?.eventer?.request('goto', 'Register')}}}})const app = createApp(component)app.use(Plugin({ apiKey: '<your-api-key>' }))app.mount('#root')</script>
For the iframe to work you might need to acquire an API Key
Create a new react project with Vite
shell
yarn create vite example
shell
yarn create vite example
Include the npm package for the @unionavatars/iframe
.
shell
yarn add @unionavatars/iframe
shell
yarn add @unionavatars/iframe
Go to the src/main.tsx
file and paste this.
- Vanilla JS
- React
- Vue
ts
import {setup } from '@unionavatars/iframe'Âconstiframe =document .querySelector ('iframe#id') asHTMLIFrameElement constclient =setup (iframe , {ÂapiKey : '<your-api-key>' })Âconstbutton =document .querySelector ('button#goto-register') asHTMLButtonElement button .onclick = () => {client .request ('goto', 'Register')}
ts
import {setup } from '@unionavatars/iframe'Âconstiframe =document .querySelector ('iframe#id') asHTMLIFrameElement constclient =setup (iframe , {ÂapiKey : '<your-api-key>' })Âconstbutton =document .querySelector ('button#goto-register') asHTMLButtonElement button .onclick = () => {client .request ('goto', 'Register')}
tsx
import {ÂcreateRoot } from 'react-dom/client'import {Provider ,Iframe ,useIframeContext } from '@unionavatars/iframe/react'ÂfunctionApp () {const {eventer } =useIframeContext ()constonClick = () => {eventer ?.request ('goto', 'Register') }return <button onClick ={onClick }>Go to register</button >}Âconstnode =document .getElementById ('root') asHTMLElement constroot =createRoot (node )root .render (<Provider apiKey ="<your-api-key>"><Iframe src ="https://iframe.unionavatars.com" /><App /></Provider >)
tsx
import {ÂcreateRoot } from 'react-dom/client'import {Provider ,Iframe ,useIframeContext } from '@unionavatars/iframe/react'ÂfunctionApp () {const {eventer } =useIframeContext ()constonClick = () => {eventer ?.request ('goto', 'Register') }return <button onClick ={onClick }>Go to register</button >}Âconstnode =document .getElementById ('root') asHTMLElement constroot =createRoot (node )root .render (<Provider apiKey ="<your-api-key>"><Iframe src ="https://iframe.unionavatars.com" /><App /></Provider >)
ts
import {createApp ,defineComponent } from 'vue'import {Plugin ,Iframe ,useIframeContext } from '@unionavatars/iframe/vue'Âconstcomponent =defineComponent ({template : `<Iframe src="https://iframe.unionavatars.com" /><button @click="goToRegister">Go to register</button>`,setup () {constctx =useIframeContext ()return {goToRegister () {ctx ?.eventer ?.request ('goto', 'Register')}}}})Âconstapp =createApp (component )app .use (Plugin ({ÂapiKey : '<your-api-key>'}))app .mount ('#root')
ts
import {createApp ,defineComponent } from 'vue'import {Plugin ,Iframe ,useIframeContext } from '@unionavatars/iframe/vue'Âconstcomponent =defineComponent ({template : `<Iframe src="https://iframe.unionavatars.com" /><button @click="goToRegister">Go to register</button>`,setup () {constctx =useIframeContext ()return {goToRegister () {ctx ?.eventer ?.request ('goto', 'Register')}}}})Âconstapp =createApp (component )app .use (Plugin ({ÂapiKey : '<your-api-key>'}))app .mount ('#root')
Press the button to verify it's working
Examples​
Some more detailed examples are hosted here: https://github.com/LinkingRealities/iframe-examples
Getting to know more about the API​
More information on what can be done with the client
inside the Api section.