Skip to content

Instantly share code, notes, and snippets.

@rigor789
Last active September 13, 2024 15:19
Show Gist options
  • Save rigor789/0b1cc867e6ab77977930dea7355b1eff to your computer and use it in GitHub Desktop.
Save rigor789/0b1cc867e6ab77977930dea7355b1eff to your computer and use it in GitHub Desktop.
NativeScript utility to toggle a global css class. For example this would be useful to a manual dark/light mode toggle.
import { Application, CSSUtils } from '@nativescript/core';
function toggleGlobalClass(className: string) {
const rootView = Application.getRootView();
const enabled = rootView.cssClasses.has(className);
if (enabled) {
CSSUtils.removeSystemCssClass(className);
rootView.cssClasses.delete(className);
// also remove from all modals
rootView._getRootModalViews().forEach(view => {
view.cssClasses.delete(className);
view._onCssStateChange();
});
} else {
CSSUtils.pushToSystemCssClasses(className);
rootView.cssClasses.add(className);
// also add to all modals
rootView._getRootModalViews().forEach(view => {
view.cssClasses.add(className);
view._onCssStateChange();
});
}
rootView._onCssStateChange();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment