Last active
September 13, 2024 15:19
-
-
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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