-
Notifications
You must be signed in to change notification settings - Fork 37
/
DialogEffects.ts
198 lines (178 loc) · 6.13 KB
/
DialogEffects.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import { getLuminance, Theme, useTheme } from '@material-ui/core'
import { useEffect } from 'react'
import { getFlagshipMetadata, isFlagshipApp } from 'cozy-device-helper'
import { useWebviewIntent } from 'cozy-intent'
import {
FlagshipUI,
ThemeColor,
parseArg
} from '../hooks/useSetFlagshipUi/useSetFlagshipUI'
interface DialogEffectsOptions {
cozybar?: Element | null
fullscreen?: boolean
immersive?: boolean
sidebar?: HTMLElement | Element | null
rootModal?: HTMLElement | Element | null
theme: Theme
}
export enum DOMStrings {
BackgroundDefault = 'default',
BackgroundPaper = 'paper',
CozyBarBackground = 'background-color',
CozyBarClass = '.coz-bar-wrapper',
CozyBarPrimaryClass = 'coz-theme-primary',
DialogClass = '.MuiPaper-root',
OverlayActive = 'rgba(0, 0, 0, 0.5)',
OverlayTransparent = 'transparent',
RootModalColor = 'color',
SidebarID = 'sidebar'
}
const LUMINANCE_BREAKPOINT = 0.5
export const makeOnMount = ({
cozybar,
fullscreen,
sidebar,
rootModal,
theme
}: DialogEffectsOptions): FlagshipUI => {
const hasBottomBackground = !rootModal
const hasTopBackground = cozybar && !rootModal
return fullscreen
? {
bottomBackground: theme.palette.background.paper,
bottomTheme: ThemeColor.Dark,
topBackground: theme.palette.background.paper,
topTheme: ThemeColor.Dark
}
: {
bottomBackground: hasBottomBackground
? theme.palette.background[
sidebar
? DOMStrings.BackgroundDefault
: DOMStrings.BackgroundPaper
]
: undefined,
bottomTheme: ThemeColor.Light,
bottomOverlay: DOMStrings.OverlayActive,
topOverlay: DOMStrings.OverlayActive,
topBackground: hasTopBackground
? getComputedStyle(cozybar).getPropertyValue(
DOMStrings.CozyBarBackground
)
: undefined,
topTheme: ThemeColor.Light
}
}
export const makeOnUnmount = ({
rootModal,
theme,
immersive,
sidebar,
cozybar
}: DialogEffectsOptions): FlagshipUI => {
const hasDarkRoot =
rootModal &&
getLuminance(
getComputedStyle(rootModal).getPropertyValue(DOMStrings.RootModalColor)
) < LUMINANCE_BREAKPOINT
const hasBottomBackground = !rootModal
const hasDarkBottomTheme = hasDarkRoot || !immersive
const hasTopBackground = cozybar && !rootModal
const hasDarkTopTheme =
hasDarkRoot ||
(!immersive &&
!(cozybar && cozybar.classList.contains(DOMStrings.CozyBarPrimaryClass)))
return {
bottomBackground: hasBottomBackground
? theme.palette.background[
sidebar ? DOMStrings.BackgroundDefault : DOMStrings.BackgroundPaper
]
: undefined,
bottomTheme: hasDarkBottomTheme ? ThemeColor.Dark : ThemeColor.Light,
bottomOverlay: DOMStrings.OverlayTransparent,
topOverlay: DOMStrings.OverlayTransparent,
topBackground: hasTopBackground
? getComputedStyle(cozybar).getPropertyValue(DOMStrings.CozyBarBackground)
: undefined,
topTheme: hasDarkTopTheme ? ThemeColor.Dark : ThemeColor.Light
}
}
const makeCaller = (
fullScreen: boolean,
hasAnotherDialog: boolean,
immersive: boolean
): string =>
[
'cozy-ui/Dialog',
fullScreen ? '--fullScreen' : '',
hasAnotherDialog ? '--stacked' : '',
immersive ? '--immersive' : ''
].join('')
const getRootModal = (): HTMLElement | null => {
const modals = document.querySelectorAll(DOMStrings.DialogClass)
/**
* If we have more than one modal, we are in a stacked dialog scenario.
* In this case we want to have access to the DOM element of the root modal.
* This will allow us to apply the correct background color if a root modal exists, for instance.
*/
return modals.length > 1 ? (modals[0] as HTMLElement) : null
}
const useHook = (open: boolean, fullscreen?: boolean): void => {
const theme = useTheme()
const cozybar = document.querySelector(DOMStrings.CozyBarClass)
const sidebar = document.getElementById(DOMStrings.SidebarID)
const rootModal = getRootModal()
const immersive = Boolean(getFlagshipMetadata().immersive)
useDialogSetFlagshipUI(
open,
makeOnMount({ fullscreen, theme, sidebar, rootModal, cozybar }),
makeOnUnmount({ rootModal, theme, immersive, sidebar, cozybar }),
makeCaller(!!fullscreen, !!rootModal, immersive)
)
}
/**
* Custom version of useSetFlagshipUi() that is aware of the Dialog component.
*
* The difference here is that we send messages to the Native app when a props change.
* In the original version, we send the mount message as soon as the component is rendered.
*
* Dialog can be rendered but hidden, so we need to wait for the open prop to be true
*/
export const useDialogSetFlagshipUI = (
open: boolean,
onMount: FlagshipUI,
onUnmount?: FlagshipUI,
caller?: string
): void => {
const webviewIntent = useWebviewIntent()
useEffect(() => {
if (open)
parseArg(webviewIntent, onMount, `${caller || 'unknown'} (onOpenMount)`)
return () => {
/**
* As we are listening to the open prop, we still want to send an unmount message when the prop changes to false.
* To avoid false positives, we need to ensure the component is currently showing.
* We do that by checking if value of open during this cleanup cycle is false,
* if it is, that means the component is currently showing and is in the process of hiding.
*
* Note that this will also handle abrupt unmounting, as in hiding the dialog without using the open prop.
*/
if (open === false || open === undefined) return
parseArg(
webviewIntent,
onUnmount,
`${caller || 'unknown'} (onOpenUnmount)`
)
}
/**
* We don't want to listen to onMount/onUnmount arguments
* It will create far too many unwanted calls
* We only care about webviewIntent or open props presence,
* Open should always be present, webviewIntent is more uncertain
*/
}, [open, webviewIntent]) // eslint-disable-line react-hooks/exhaustive-deps
}
export const useDialogEffects = isFlagshipApp()
? useHook
: // eslint-disable-next-line @typescript-eslint/no-empty-function
(): void => {}