Skip to content

Commit ee1485a

Browse files
authored
Merge pull request #4657 from codewithvk/private/codewithvk/pass_ui_theme_to_new
feat(setting): add dark/light iframe CSS vars
2 parents 9746668 + 1dfe15d commit ee1485a

File tree

2 files changed

+31
-12
lines changed

2 files changed

+31
-12
lines changed

src/components/CoolFrame.vue

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<input type="hidden" name="wopi_setting_base_url" :value="wopiSettingBaseUrl">
1414
<input type="hidden" name="iframe_type" :value="iframeType">
1515
<input type="hidden" name="css_variables" :value="cssVariables">
16+
<input type="hidden" name="theme" :value="theme">
1617
<input type="hidden" name="ui_theme" :value="uiTheme">
1718
</form>
1819
<iframe :id="iframeName"
@@ -26,7 +27,7 @@
2627

2728
<script>
2829
29-
import { generateCSSVarTokens, getUITheme } from '../helpers/coolParameters.js'
30+
import { generateCSSVarTokens, getCollaboraTheme, getUITheme } from '../helpers/coolParameters.js'
3031
import { languageToBCP47 } from '../helpers/index.js'
3132
import PostMessageService from '../services/postMessage.tsx'
3233
@@ -58,8 +59,9 @@ export default {
5859
return {
5960
iframeName: 'coolFrameIframe',
6061
formAction: '',
61-
cssVariables: generateCSSVarTokens(),
62+
cssVariables: generateCSSVarTokens(true),
6263
isIframeLoaded: false,
64+
theme: getCollaboraTheme(),
6365
uiTheme: getUITheme(),
6466
postMessage: null,
6567
}

src/helpers/coolParameters.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,13 @@ const createDataThemeDiv = (elementType, theme) => {
4747
document.body.appendChild(element)
4848
return element
4949
}
50-
const generateCSSVarTokens = () => {
50+
51+
const generateCSSVarTokens = (isSettingIframe = false) => {
5152
/* NC versus COOL */
5253
const cssVarMap = {
53-
'--color-primary-element-text': '--co-primary-text',
54-
'--color-primary-element': '--co-primary-element:--co-text-accent',
55-
'--color-primary-element-light': '--co-primary-light:--co-primary-element-light',
54+
'--color-primary-element-text': '--co-primary-text:--co-settings-btn-primary-text',
55+
'--color-primary-element': '--co-primary-element:--co-text-accent:--co-settings-btn-primary',
56+
'--color-primary-element-light': '--co-primary-light:--co-primary-element-light:--co-settings-btn-light',
5657
'--color-error': '--co-color-error',
5758
'--color-warning': '--co-color-warning',
5859
'--color-success': '--co-color-success',
@@ -61,18 +62,31 @@ const generateCSSVarTokens = () => {
6162
'--color-loading-light': '--co-loading-light',
6263
'--color-loading-dark': '--co-loading-dark',
6364
'--color-box-shadow': '--co-box-shadow',
64-
'--color-border': '--co-border',
65-
'--color-border-dark': '--co-border-dark',
65+
'--color-border': '--co-border:--co-settings-border',
66+
'--color-border-dark': '--co-border-dark:--co--settings-border-contrast',
6667
'--border-radius-pill': '--co-border-radius-pill',
68+
'--color-primary-element-light-text': '--co-settings-btn-light-text',
69+
'--color-main-text': '--co-settings-text',
70+
'--color-text-maxcontrast': '--co-settings-text-maxcontrast',
71+
'--color-main-background': '--co-settings-background',
72+
'--color-background-hover': '--co-settings-background-hover',
6773
}
6874
let str = ''
6975
const lightElement = createDataThemeDiv('div', 'light')
76+
const darkElement = createDataThemeDiv('div', 'dark')
77+
let selectedElement = lightElement
78+
79+
// Note: To style the iframe properly, we send appropriate CSS variables based on the current theme.
80+
// For example, if the current theme is dark, we send values corresponding to the dark theme.
81+
if (isSettingIframe) {
82+
selectedElement = getUITheme() === 'dark' ? darkElement : lightElement
83+
}
7084
try {
7185
for (const cssVarKey in cssVarMap) {
72-
let cStyle = window.getComputedStyle(lightElement).getPropertyValue(cssVarKey)
86+
let cStyle = window.getComputedStyle(selectedElement).getPropertyValue(cssVarKey)
7387
if (!cStyle) {
7488
// try suffix -dark instead
75-
cStyle = window.getComputedStyle(lightElement).getPropertyValue(cssVarKey + '-dark')
89+
cStyle = window.getComputedStyle(selectedElement).getPropertyValue(cssVarKey + '-dark')
7690
}
7791
if (!cStyle) continue // skip if it is not set
7892
const varNames = cssVarMap[cssVarKey].split(':')
@@ -84,8 +98,11 @@ const generateCSSVarTokens = () => {
8498
// Skip extracting css vars if we cannot access parent
8599
}
86100

87-
// New dark mode compatible way to hand over our Nextcloud variables in both light/dark to Collabora
88-
const darkElement = createDataThemeDiv('div', 'dark')
101+
if (isSettingIframe) {
102+
lightElement.remove()
103+
darkElement.remove()
104+
return str.replace(/["']/g, '\\\'')
105+
}
89106

90107
const handover = [
91108
'--color-main-background',

0 commit comments

Comments
 (0)