Skip to content

Commit

Permalink
fix: connect-button inside container with backdrop-filter (#257)
Browse files Browse the repository at this point in the history
Fixes issue #249
  • Loading branch information
dawidsowardx authored Sep 20, 2024
1 parent b0afda3 commit aa56ef6
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 1 deletion.
7 changes: 6 additions & 1 deletion examples/simple-dapp/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@
<title>Simple dApp</title>
</head>
<body>
<header><radix-connect-button /></header>
<style>
.header {
backdrop-filter: blur(10px);
}
</style>
<header class="header"><radix-connect-button /></header>
<div id="app"></div>

<script type="module" src="/src/main.ts"></script>
Expand Down
47 changes: 47 additions & 0 deletions packages/connect-button/src/components/connect-button.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ export class ConnectButton extends LitElement {

pristine = true
initialBodyOverflow: string
initialBackdropFilter: string
parentElementWithBackdropFilter: HTMLElement | null

windowClickEventHandler: (event: MouseEvent) => void

Expand All @@ -117,6 +119,8 @@ export class ConnectButton extends LitElement {
constructor() {
super()
this.initialBodyOverflow = document.body.style.overflow
this.initialBackdropFilter = ''
this.parentElementWithBackdropFilter = null
this.injectFontCSS()
this.windowClickEventHandler = (event) => {
if (!this.showPopoverMenu) return
Expand Down Expand Up @@ -170,6 +174,7 @@ export class ConnectButton extends LitElement {
this.pristine = false
this.showPopoverMenu = !this.showPopoverMenu
this.toggleBodyOverflow()
this.toggleParentBackdropFilter()
if (this.showPopoverMenu) {
this.dispatchEvent(
new CustomEvent('onShowPopover', {
Expand All @@ -188,6 +193,48 @@ export class ConnectButton extends LitElement {
: this.initialBodyOverflow
}

private toggleParentBackdropFilter() {
const OPACITY_TRANSITION_DURATION = 180
if (!this.isMobile) return

if (!this.showPopoverMenu && this.parentElementWithBackdropFilter) {
setTimeout(() => {
this.parentElementWithBackdropFilter?.style.setProperty(
'backdrop-filter',
this.initialBackdropFilter,
)
this.initialBackdropFilter = ''
this.parentElementWithBackdropFilter = null
}, OPACITY_TRANSITION_DURATION)

return
} else {
const parent = this.findParentWithBackdropFilter(this)
if (parent === null) {
return
}
this.initialBackdropFilter = parent.backdropFilter
this.parentElementWithBackdropFilter = parent.element

parent.element.style.backdropFilter = 'none'
}
}

private findParentWithBackdropFilter(
element: HTMLElement | null,
): { element: HTMLElement; backdropFilter: string } | null {
if (!element) {
return null
}
const style = window.getComputedStyle(element)
const backdropFilter = style.getPropertyValue('backdrop-filter')
if (backdropFilter !== 'none') {
return { element, backdropFilter }
} else {
return this.findParentWithBackdropFilter(element.parentElement)
}
}

private connectButtonTemplate() {
const buttonText = this.connected ? this.personaLabel : 'Connect'

Expand Down

0 comments on commit aa56ef6

Please sign in to comment.