Skip to content

Commit 40451b6

Browse files
committed
fix: detect UC dialog open state during initialization
1 parent e1eefe3 commit 40451b6

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

src/services/usercentrics-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/** State management singleton for internal use. This should not be used directly in applications. */
22

33
import { UCUICMPEvent, UCUICMPEventType } from '../types.js'
4-
import { hasUserInteracted, IS_BROWSER } from '../utils.js'
4+
import { hasUserInteracted, IS_BROWSER, isOpen } from '../utils.js'
55

66
export type ServiceState = {
77
hasInteracted: boolean
@@ -56,7 +56,7 @@ const initializeState = () => {
5656

5757
if (!state.isInitialized && IS_BROWSER && 'UC_UI' in window) {
5858
shouldUpdate = true
59-
state = { ...state, isInitialized: true }
59+
state = { ...state, isInitialized: true, isOpen: isOpen() }
6060
}
6161

6262
const hasInteracted = hasUserInteracted()

src/utils.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,3 +152,16 @@ export const updateLanguage = (countryCode: string) => {
152152
;(window as UCWindow).UC_UI?.updateLanguage?.(countryCode)
153153
}
154154
}
155+
156+
/**
157+
* Returns true if the Usercentrics dialog is currently open.
158+
*/
159+
export const isOpen = (): boolean => {
160+
if (IS_BROWSER) {
161+
const usercentricsRoot = document.getElementById('usercentrics-root')
162+
const dialog = usercentricsRoot?.shadowRoot?.querySelector('[role="dialog"]')
163+
return !!dialog
164+
}
165+
166+
return false
167+
}

0 commit comments

Comments
 (0)