Skip to content

Commit

Permalink
Feat/support sdk backdrop (#272)
Browse files Browse the repository at this point in the history
  • Loading branch information
rustin01 authored Dec 26, 2024
1 parent b811760 commit 71cbe73
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 13 deletions.
1 change: 1 addition & 0 deletions packages/sdk/src/lib/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const RPC_SERVICE_NAME = 'hibit-id-rpc'
export const IFRAME_CONTAINER_ID = 'hibit-id-app'
export const IFRAME_BACKDROP_ID = 'hibit-id-app-backdrop'
export const CONTROLLER_CONTAINER_ID = 'hibit-id-controller'
12 changes: 11 additions & 1 deletion packages/sdk/src/lib/dom/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

#hibit-id-app {
position: fixed;
z-index: 9998;
z-index: 9995;
border-radius: 1rem;
overflow: hidden;
}
Expand All @@ -45,3 +45,13 @@
height: 100%;
border: none;
}

#hibit-id-app-backdrop {
position: fixed;
top: 0;
left: 0;
width: 100vw;
height: 100vh;
background: rgba(0, 0, 0, 0.5);
z-index: 9990;
}
28 changes: 18 additions & 10 deletions packages/sdk/src/lib/dom/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CONTROLLER_CONTAINER_ID, IFRAME_CONTAINER_ID } from "../constants"
import { CONTROLLER_CONTAINER_ID, IFRAME_BACKDROP_ID, IFRAME_CONTAINER_ID } from "../constants"
import { HibitIdChainId } from "../enums"
import { FixDevMode, HibitEnv, Language } from "../types"
import { clamp, getHibitIdUrl } from "../utils"
Expand Down Expand Up @@ -153,15 +153,15 @@ export class HibitIdController {
export class HibitIdIframe {
public iframe: HTMLIFrameElement
public readonly isDesktop: boolean
private container: HTMLDivElement
private _container: HTMLDivElement
private _visible = false

constructor(env: HibitEnv, chains: HibitIdChainId[] = [], urlAppendix: string = '', lang: Language | '' = '', fixDevMode: FixDevMode = 'unset' ) {
this.isDesktop = window.innerWidth > 576
const existed = document.getElementById(IFRAME_CONTAINER_ID)
if (existed) {
this.container = existed as HTMLDivElement
this.iframe = this.container.querySelector('iframe') as HTMLIFrameElement
this._container = existed as HTMLDivElement
this.iframe = this._container.querySelector('iframe') as HTMLIFrameElement
return
}
const container = document.createElement('div')
Expand All @@ -171,7 +171,7 @@ export class HibitIdIframe {
iframe.allow='clipboard-write; publickey-credentials-get *; publickey-credentials-create *'
container.appendChild(iframe)
document.body.appendChild(container)
this.container = container
this._container = container
this.iframe = iframe
this.hide()
}
Expand All @@ -184,13 +184,14 @@ export class HibitIdIframe {
Object.keys(style).forEach((key) => {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
this.container.style[key] = style[key]
this._container.style[key] = style[key]
})
}

public show = (
mode: 'fullscreen' | 'centered' | 'floating',
floatingPos?: { right: number, bottom: number }
floatingPos?: { right: number, bottom: number },
withBackdrop?: boolean
) => {
if (mode === 'fullscreen') {
this.updateStyle({
Expand Down Expand Up @@ -230,16 +231,23 @@ export class HibitIdIframe {
})
}
}
if (withBackdrop && !document.getElementById(IFRAME_BACKDROP_ID)) {
const backdrop = document.createElement('div')
backdrop.id = IFRAME_BACKDROP_ID
document.body.appendChild(backdrop)
}
this._visible = true
}

public hide = () => {
this.container.style.width = '0'
this.container.style.height = '0'
this._container.style.width = '0'
this._container.style.height = '0'
document.getElementById(IFRAME_BACKDROP_ID)?.remove()
this._visible = false
}

public destroy = () => {
this.container?.remove()
this.hide()
this._container?.remove()
}
}
3 changes: 2 additions & 1 deletion packages/sdk/src/lib/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,8 @@ export class HibitIdWallet {
right: window.innerWidth - controllerRect.right,
bottom: window.innerHeight - controllerRect.top + 20
}
: undefined
: undefined,
this._options.embedMode === 'background',
);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/src/test/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const App: FC = () => {
env: 'dev',
chains: [],
defaultChain: HibitIdChainId.EthereumSepolia,
embedMode: 'float',
embedMode: 'background',
})
setWallet(wallet)
const handleChainChanged = (chainId: HibitIdChainId) => setChainId(chainId)
Expand Down

0 comments on commit 71cbe73

Please sign in to comment.