Skip to content

Commit 8b43ada

Browse files
committed
feat: add theme support to AUIApplication and bridge components
1 parent b1d31bc commit 8b43ada

3 files changed

Lines changed: 34 additions & 3 deletions

File tree

apps/sensenet/docs/auiapplications.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ window.sensenetAdminApp = {
7676
hash: string
7777
params: Record<string, string>
7878
}
79+
theme: 'light' | 'dark'
7980
fetch(input: string, init?: {
8081
method?: string
8182
headers?: Record<string, string>
@@ -130,6 +131,21 @@ window.sensenetAdminApp.location.params.userId
130131

131132
The `params` object is built from the parent route query string with `Object.fromEntries(new URLSearchParams(location.search))`.
132133

134+
## Admin UI Theme
135+
136+
The bridge also exposes the current Admin UI theme mode. Use this instead of trying to inspect parent styles from the sandboxed iframe:
137+
138+
```js
139+
const theme = window.sensenetAdminApp.theme
140+
document.documentElement.dataset.theme = theme
141+
142+
if (theme === 'dark') {
143+
// Render dark-friendly colors.
144+
}
145+
```
146+
147+
The value is always either `"light"` or `"dark"`, matching the Admin UI view option.
148+
133149
## URL Rules
134150

135151
Use repository-relative URLs when possible:

apps/sensenet/src/components/content/AUIApplicationView.tsx

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
import { CircularProgress, createStyles, makeStyles, Theme, Typography } from '@material-ui/core'
1+
import { CircularProgress, createStyles, makeStyles, Theme, Typography, useTheme } from '@material-ui/core'
22
import { ODataFieldParameter } from '@sensenet/client-core'
33
import { GenericContent } from '@sensenet/default-content-types'
44
import { CurrentContentContext, useLogger, useRepository } from '@sensenet/hooks-react'
55
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react'
66
import { useLocation } from 'react-router-dom'
7-
import { AUIApplicationBridgeLocation, createBridgeLocation } from './auiapplication-bridge'
7+
import {
8+
AUIApplicationBridgeLocation,
9+
AUIApplicationBridgeTheme,
10+
createBridgeLocation,
11+
createBridgeTheme,
12+
} from './auiapplication-bridge'
813

914
export const AUI_APPLICATION_CONTENT_TYPE = 'AUIApplication'
1015

@@ -67,6 +72,7 @@ const createApplicationDocument = (
6772
adminUiUrl: string,
6873
content: AUIApplicationContent | undefined,
6974
location: AUIApplicationBridgeLocation,
75+
theme: AUIApplicationBridgeTheme,
7076
) => {
7177
const baseUrl = getApplicationBaseUrl(repositoryUrl, content?.Path ?? '')
7278
const bootstrap = `<base href="${escapeAttribute(baseUrl)}">
@@ -145,6 +151,7 @@ const createApplicationDocument = (
145151
Type: content?.Type,
146152
})},
147153
location: ${makeSafeScriptJson(location)},
154+
theme: ${makeSafeScriptJson(theme)},
148155
fetch: (input, init = {}) => {
149156
const requestId = \`\${Date.now()}-\${Math.random().toString(36).slice(2)}\`;
150157
@@ -222,6 +229,7 @@ const useStyles = makeStyles((theme: Theme) =>
222229

223230
export const AUIApplicationView: React.FC = () => {
224231
const classes = useStyles()
232+
const theme = useTheme<Theme>()
225233
const frameRef = useRef<HTMLIFrameElement>(null)
226234
const repository = useRepository()
227235
const logger = useLogger('AUIApplicationView')
@@ -335,6 +343,7 @@ export const AUIApplicationView: React.FC = () => {
335343
() => createBridgeLocation(adminUiUrl, routerLocation),
336344
[adminUiUrl, routerLocation.hash, routerLocation.pathname, routerLocation.search],
337345
)
346+
const bridgeTheme = useMemo(() => createBridgeTheme(theme.palette.type), [theme.palette.type])
338347
const srcDoc = useMemo(
339348
() =>
340349
createApplicationDocument(
@@ -343,8 +352,9 @@ export const AUIApplicationView: React.FC = () => {
343352
adminUiUrl,
344353
content,
345354
bridgeLocation,
355+
bridgeTheme,
346356
),
347-
[adminUiUrl, applicationHtml, bridgeLocation, content, repository.configuration.repositoryUrl],
357+
[adminUiUrl, applicationHtml, bridgeLocation, bridgeTheme, content, repository.configuration.repositoryUrl],
348358
)
349359

350360
if (isLoading) {

apps/sensenet/src/components/content/auiapplication-bridge.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ export type AUIApplicationBridgeLocation = {
66
params: Record<string, string>
77
}
88

9+
export type AUIApplicationBridgeTheme = 'light' | 'dark'
10+
911
export const createBridgeLocation = (
1012
adminUiUrl: string,
1113
location: Pick<Location, 'pathname' | 'search' | 'hash'>,
@@ -23,3 +25,6 @@ export const createBridgeLocation = (
2325
params,
2426
}
2527
}
28+
29+
export const createBridgeTheme = (theme: string | undefined): AUIApplicationBridgeTheme =>
30+
theme === 'dark' ? 'dark' : 'light'

0 commit comments

Comments
 (0)