Skip to content

Commit

Permalink
refactor(dark mode): change global dark mode implementation (#73)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Dark mode configuration should be set in a so-called dark mode object
  • Loading branch information
YoeriNijs committed Aug 1, 2023
1 parent 282b753 commit fd9e42b
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
18 changes: 11 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ following:
- `style`: just plain css.
- `href`: a link to a remote stylesheet. Thus, a href should start with 'http'. Also, it is possible to pass
integrity and crossorigin values (see below).
- `darkModeEnabled` (optional): global method to initialize app-wide dark mode (see [dark mode](#dark-mode)).
- `darkMode` (optional): global config object to initialize app-wide dark mode (see [dark mode](#dark-mode)).
- `pipes` (optional): custom pipes (see [create custom pipes](#create-custom-pipes))

For instance, if you want to use [Bulma](https://bulma.io), just add the following:
Expand Down Expand Up @@ -1025,7 +1025,7 @@ dark apps more than light apps, and for some it is even a necessity to use an ap
### Set up dark mode

Luckily, Vienna comes with dark mode out of the box. There are two ways to enable dark mode. The easiest way is to set
up dark mode in your application config by using the so-called `darkModeEnabled` hook. It is just a method that should
up dark mode in your application config by using the so-called `isDarkModeEnabled` hook. It is just a method that should
return true or false. If it returns true, dark mode will be enabled for the whole application. For instance:

`application.ts`
Expand All @@ -1034,9 +1034,11 @@ return true or false. If it returns true, dark mode will be enabled for the whol
@VApplication({
declarations: [],
routes: [],
darkModeEnabled: () => {
// ... Some custom logic here.
return true;
darkMode: {
isDarkModeEnabled: () => {
// ... Some custom logic here.
return true;
}
}
})
export class Application {}
Expand Down Expand Up @@ -1106,8 +1108,10 @@ config:
@VApplication({
declarations: [],
routes: [],
darkModeEnabled: () => {...},
darkModeClassOverride: 'my-custom-dark-mode-class'
darkMode: {
isDarkModeEnabled: () => {...},
darkModeClassOverride: 'my-custom-dark-mode-class'
}
})
export class Application {}
```
Expand Down
4 changes: 2 additions & 2 deletions src/core/application/v-application-config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import {VGlobalStyles} from "./v-global-styles";
import {VApplicationPlugins} from "./v-application-plugins";
import {VPipeTransform} from "../pipe/v-pipe-transform";
import {VI18nConfig} from "./v-i18n-config";
import {VDarkModeOptions} from "./v-dark-mode-options";

export interface VApplicationConfig {
declarations: Type<VComponentType>[];
routes: VRoute[];
routeNotFoundStrategy?: VRouteNotFoundStrategy | VRouteNotFoundRedirect;
rootElementSelector?: string;
globalStyles?: VGlobalStyles;
darkModeEnabled?: () => boolean;
darkModeClassOverride?: string;
darkMode?: VDarkModeOptions;
plugins?: VApplicationPlugins;
pipes?: Type<VPipeTransform>[];
i18n?: VI18nConfig;
Expand Down
10 changes: 8 additions & 2 deletions src/core/application/v-application.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ export function VApplication(config: VApplicationConfig) {
}

private initializeDarkMode(): void {
const isDarkModeEnabledInConfig = config.darkModeEnabled ? config.darkModeEnabled() : false;
if (!config.darkMode) {
return;
}

const isDarkModeEnabledInConfig = config.darkMode.isDarkModeEnabled
? config.darkMode.isDarkModeEnabled()
: false;
if (isDarkModeEnabledInConfig) {
this._darkModeService.enableDarkMode();
} else {
this._darkModeService.disableDarkMode();
}

const darkModeClassOverride = config.darkModeClassOverride;
const darkModeClassOverride = config.darkMode.darkModeClassOverride;
if (darkModeClassOverride) {
this._darkModeService.overrideGlobalDarkModeClass(darkModeClassOverride);
}
Expand Down
4 changes: 4 additions & 0 deletions src/core/application/v-dark-mode-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface VDarkModeOptions {
isDarkModeEnabled?: () => boolean;
darkModeClassOverride?: string;
}
1 change: 1 addition & 0 deletions src/core/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Public Vienna framework api
export * from './application/v-application';
export * from './application/v-application-config';
export * from './application/v-dark-mode-options';
export * from './application/v-global-inline-style';
export * from './application/v-global-style-link';
export * from './application/v-global-styles';
Expand Down

0 comments on commit fd9e42b

Please sign in to comment.