Skip to content

Commit

Permalink
Adding missing changes
Browse files Browse the repository at this point in the history
  • Loading branch information
pgayvallet committed Feb 20, 2024
1 parent 8e3b644 commit 102c6f8
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ describe('getStylesheetPaths', () => {
darkMode: true,
themeVersion: 'v8',
baseHref: '/base-path/buildShaShort',
buildNum: 17,
})
).toMatchInlineSnapshot(`
Array [
Expand All @@ -83,7 +82,6 @@ describe('getStylesheetPaths', () => {
darkMode: false,
themeVersion: 'v8',
baseHref: '/base-path/buildShaShort',
buildNum: 69,
})
).toMatchInlineSnapshot(`
Array [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,7 @@ export const getScriptPaths = ({
}
};

export const getCommonStylesheetPaths = ({
baseHref,
}: {
baseHref: string;
}) => {
export const getCommonStylesheetPaths = ({ baseHref }: { baseHref: string }) => {
const bundlesHref = getBundlesHref(baseHref);
return [
`${bundlesHref}/kbn-ui-shared-deps-src/${UiSharedDepsSrc.cssDistFilename}`,
Expand All @@ -62,11 +58,9 @@ export const getThemeStylesheetPaths = ({
darkMode,
themeVersion,
baseHref,
buildNum,
}: {
darkMode: boolean;
themeVersion: UiSharedDepsNpm.ThemeVersion;
buildNum: number;
baseHref: string;
}) => {
const bundlesHref = getBundlesHref(baseHref);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,6 @@ export class RenderingService {
mode: this.coreContext.env.mode,
packageInfo: this.coreContext.env.packageInfo,
};
const buildNum = env.packageInfo.buildNum;
const staticAssetsHrefBase = http.staticAssets.getHrefBase();
const basePath = http.basePath.get(request);
const { serverBasePath, publicBaseUrl } = http.basePath;
Expand Down Expand Up @@ -191,7 +190,6 @@ export class RenderingService {
darkMode: mode,
themeVersion,
baseHref: staticAssetsHrefBase,
buildNum,
});
const commonStylesheetPaths = getCommonStylesheetPaths({
baseHref: staticAssetsHrefBase,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@
* Side Public License, v 1.
*/

import { Subject, ReplaySubject } from 'rxjs';
import { shareReplay, takeUntil } from 'rxjs/operators';
import { of } from 'rxjs';
import { _setDarkMode } from '@kbn/ui-theme';
import type { InjectedMetadataTheme } from '@kbn/core-injected-metadata-common-internal';
import type { InternalInjectedMetadataSetup } from '@kbn/core-injected-metadata-browser-internal';
Expand All @@ -22,45 +21,44 @@ export interface ThemeServiceSetupDeps {

/** @internal */
export class ThemeService {
private theme$ = new ReplaySubject<CoreTheme>(1);
private stop$ = new Subject<void>();

private contract?: ThemeServiceSetup;
private themeMetadata?: InjectedMetadataTheme;
private stylesheets: HTMLLinkElement[] = [];

public setup({ injectedMetadata }: ThemeServiceSetupDeps): ThemeServiceSetup {
const theme = injectedMetadata.getTheme();
this.themeMetadata = theme;
const themeMetadata = injectedMetadata.getTheme();
this.themeMetadata = themeMetadata;

if (theme.darkMode === 'system' && browsersSupportsSystemTheme()) {
const darkMode = systemThemeIsDark();
this.applyTheme(darkMode);
// onSystemThemeChange((mode) => this.applyTheme(mode));
let theme: CoreTheme;
if (themeMetadata.darkMode === 'system' && browsersSupportsSystemTheme()) {
theme = { darkMode: systemThemeIsDark() };
} else {
const darkMode = theme.darkMode === 'system' ? false : theme.darkMode;
this.applyTheme(darkMode);
const darkMode = themeMetadata.darkMode === 'system' ? false : themeMetadata.darkMode;
theme = { darkMode };
}

return {
theme$: this.theme$.pipe(takeUntil(this.stop$), shareReplay(1)),
this.applyTheme(theme);

this.contract = {
getTheme: () => theme,
theme$: of(theme),
};

return this.contract;
}

public start(): ThemeServiceStart {
if (!this.themeMetadata) {
if (!this.contract) {
throw new Error('setup must be called before start');
}

return {
theme$: this.theme$.pipe(takeUntil(this.stop$), shareReplay(1)),
};
return this.contract;
}

public stop() {
this.stop$.next();
}
public stop() {}

private applyTheme(darkMode: boolean) {
private applyTheme(theme: CoreTheme) {
const { darkMode } = theme;
this.stylesheets.forEach((stylesheet) => {
stylesheet.remove();
});
Expand All @@ -75,7 +73,6 @@ export class ThemeService {

_setDarkMode(darkMode);
updateKbnThemeTag(darkMode);
this.theme$.next({ darkMode });
}
}

Expand Down

0 comments on commit 102c6f8

Please sign in to comment.