ColorSchemeManager
is a service script that gives the ability to configure and control color scheme behavior.
Install the module and its dependencies.
With NPM:
npm install @mevbg/color-scheme-manager
With Yarn:
yarn add @mevbg/color-scheme-manager
To use the manager you need to go through the following three steps:
First things first. Import the module as follows:
import ColorSchemeManager from '@mevbg/color-scheme-manager';
The imported module has only an init
method. It takes one argument and that is a config object which has two properties. Here they are:
const config = {
systemSchemes: true,
defaultScheme: 'light'
};
A Boolean
that determines whether the logic should be mixed up with the operating system color scheme or not.
Default value: true
A String
that determines which color scheme among light & dark should be the default one when initialized for the first time.
NOTE This setting is used only when the manager is not bound to the system color schemes. Otherwise, it won't be applicable to the logic as the initial color will be determined by the current system color scheme or by a color scheme that might have been set in a cookie already.
Default value: light
Now that we have our config
, let's initialize the module and store the manager instance that will be returned.
const colorSchemeManager = ColorSchemeManager.init(config);
NOTE You can only have one instance of the manager. If you initialize a second one, the first instance will be returned.
// contains the value of the current color scheme
colorSchemeManager.currentColorScheme
// contains the value of the initial color scheme of the site
// at the time the manager was initialized
colorSchemeManager.initialScheme
// contains the value of default color scheme
colorSchemeManager.defaultScheme
// contains the value of the opposite color scheme to the current one
colorSchemeManager.oppositeColorScheme
NOTE There are few more properties when
systemSchemes
is set to betrue
that can be seen below.
// contains the value of the initial system color scheme
// at the time the manager was initialized
colorSchemeManager.initialSystemScheme
// contains the value of the system color scheme
// at the moment of accessing this property
colorSchemeManager.currentSystemScheme
// contains the value of the manually set color scheme (if such)
colorSchemeManager.cookieScheme
// Sets a given color scheme
colorSchemeManager.setColorScheme(scheme) // 'light' | 'dark'
// Toggles between light and dark color schemes
colorSchemeManager.toggle()
// Sets light color scheme
colorSchemeManager.setLight()
// Sets dark color scheme
colorSchemeManager.setDark()
The manager dispatches only one custom event to the document: color-scheme-change
.
This event gets dispatched on every color scheme change, regardless the cause. It returns the name of the changed color scheme.
document.addEventListener('color-scheme-change', (event) => {
const { scheme } = event.detail;
console.log(scheme) // light | dark
});
Here is an explanation to the behavior of the manager, based on all possible cases, divided into two groups:
- when the manager is initialized;
- when the manager reacts to a change.
NOTE A color scheme that is already set in a cookie takes priority in all initialization cases, regardless the manager is bound to the system color schemes or not.
this.initialScheme = this.cookieScheme || this.currentSystemScheme;
When the manager is bound to the system color schemes, there isn't going to be a defaultScheme
as it's only used to determine the initial color scheme and in this case the initial one will be determined by the current system color scheme or by a color scheme that might have been set in a cookie already. If no such, the current system color scheme will be used.
this.initialScheme =
CookieService.get(COLOR_SCHEME_COOKIE_NAME) || this.defaultScheme;
As mentioned, a color scheme set in a cookie takes priority. If no such, the defaultScheme
value will be used.
NOTE Once a cookie is set, the
defaultScheme
will never be used again.
A change may occur by a shift in the system color scheme, or by a user action throughout some of the provided methods.
NOTE Regardless of which type of change occurred, it takes precedence over the other if the other was leading at the time of the change.
If the system color scheme gets changed it will force the manager to set the new system color scheme as the new current color scheme, regardless its previous value was set by a cookie or by the old system color scheme.
And if the previous value of the current color scheme was set by a cookie, the manager will accept that the system color scheme takes precedence and the cookie will be removed, as it won't be necessary anymore due to its value that will be the same as the new system color scheme.
If the user change the current color scheme to be the opposite of the system color scheme, a colorScheme
cookie will be set with that value and that will be the current color scheme that will take precedence and will also be set as initial one at next initializations.
And in case the user is switching back from having a cookie with an opposite value to the system color scheme, so to return the leading role to the system, then the cookie will be removed and the system color scheme will take its precedence back.
A change may occur only by a user action throughout some of the provided methods and the new color scheme value will always be stored in a cookie.