This repository was archived by the owner on Nov 17, 2022. It is now read-only.
-
-
Couldn't load subscription status.
- Fork 447
feat: add dark mode article #1837
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
39339df refactor(styling): add system appearance classes
vchimev a0d6e7f feat: add dark mode article
vchimev 15a8466 refactor: typos
vchimev b2c3ab2 Update docs/ui/dark-mode.md
vchimev 3a92f85 Update docs/ui/dark-mode.md
vchimev edc5d9b Apply suggestions from code review
vchimev d896323 refactor(Android): default behavior
vchimev 3728182 refactor(Android): note config changes values
vchimev ee584e9 refactor(iOS): describe pre and post 6.2 behavior
vchimev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,89 @@ | ||
| --- | ||
| title: Dark Mode | ||
| description: The article describes the Dark Mode support in NativeScript. | ||
| position: 51 | ||
| tags: nativescript dark mode, nativescript styling, nativescript css | ||
| slug: dark-mode | ||
| --- | ||
| | ||
| # Overview | ||
| | ||
| Android 10 (API level 29) and iOS 13 introduce system-wide dark appearance for all user interface elements, known as [Dark theme](https://developer.android.com/guide/topics/ui/look-and-feel/darktheme) for Android and [Dark Mode](https://developer.apple.com/documentation/appkit/supporting_dark_mode_in_your_interface) for iOS. They have many benefits as to allow users to toggle their interface and focus on the application’s content based on ambient lighting conditions as well as to reduce power usage amount depending on the device's screen technology. | ||
| | ||
| ## Dark Theme for Android | ||
| | ||
| In order to support Dark theme for Android, the application's theme must inherit from a `DayNight` theme as explained [here](https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#supporting_dark_theme_in_your_app). | ||
| | ||
| However, Android 10 (API level 29) provides a feature for developers to quickly implement a Dark theme without explicitly setting a `DayNight` theme as mentioned. This features is called [Force Dark](https://developer.android.com/guide/topics/ui/look-and-feel/darktheme#force_dark). | ||
| | ||
| Since release 6.2, NativeScript creates new applications with opt-in Force Dark functionality by default to provide support for Dark theme out of the box. | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| The opt-in to Force Dark happens by setting `android:forceDarkAllowed` to `true` in the activity's theme. In particular, every project created by the NativeScript CLI 6.2 or later, contains the snippet below in the `<project>/app/App_Recoures/Android/src/main/res/values/styles.xml` file: | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ```xml | ||
| ... | ||
| | ||
| <!-- theme to use AFTER launch screen is loaded--> | ||
| <style name="AppThemeBase" parent="Theme.AppCompat.Light.NoActionBar"> | ||
| <item name="android:forceDarkAllowed">true</item> | ||
| <item name="toolbarStyle">@style/NativeScriptToolbarStyle</item> | ||
| | ||
| <item name="colorPrimary">@color/ns_primary</item> | ||
| <item name="colorPrimaryDark">@color/ns_primaryDark</item> | ||
| <item name="colorAccent">@color/ns_accent</item> | ||
| </style> | ||
| | ||
| <style name="AppTheme" parent="AppThemeBase"> | ||
| </style> | ||
| | ||
| ... | ||
| ``` | ||
| | ||
| > Important: Make sure to test your application carefully and thoroughly on both Light and Dark system appearance. | ||
| | ||
| > Note: If you want to update your project to opt-in to Force Dark, it is needed to put the following line in the `<project>/app/App_Recoures/Android/src/main/res/values/styles.xml` file as shown above: `<item name="android:forceDarkAllowed">true</item>`. | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ## Dark Mode for iOS | ||
| | ||
| For iOS, the Dark mode is opt-in by default and every application built with Xcode 11 or above tooling, automatically support both Light and Dark system appearances. | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| The support in NativeScript framework has come since version 6.2 where the complete set of native widgets has been adapted to work out of the box for the. | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| However, if the your application need extra time to work on its dark Mode support, you can opt out by including the `UIUserInterfaceStyle` key in the application's the `<project>/app/App_Recoures/iOS/Info.plist` file: | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ```xml | ||
| ... | ||
| | ||
| <key>UIUserInterfaceStyle</key> | ||
| <string>Light</string> | ||
| | ||
| ... | ||
| ``` | ||
| | ||
| > Important: Make sure to test your application carefully and thoroughly on both Light and Dark system appearance. | ||
| | ||
| ## How Does It Work | ||
| | ||
| Since version 6.2, NativeScript detects the system appearance of the running mobile operating system and applies a respective CSS class to the application's [root view](https://docs.nativescript.org/ui/styling#root-views-css-classes). In particular, if you run the application on iOS 13.0 in Light mode, a `.ns-light` CSS class will be assign to the root view of your application. In analogy, if you change the system appearance from Light to Dark, the `.ns-light` class will be replaced with `.ns-dark` and vice verse. | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| To illustrate this behavior, the sample CSS styles below will apint the Pages of your application in *light blue* color on Light system appearance and in *dark blue* on dark system appearance: | ||
vchimev marked this conversation as resolved. Outdated Show resolved Hide resolved | ||
| | ||
| ```css | ||
| .ns-light Page { | ||
vchimev marked this conversation as resolved. Show resolved Hide resolved | ||
| background-color: lightblue; | ||
| } | ||
| | ||
| .ns-dark Page { | ||
| background-color: darkblue; | ||
| } | ||
| ``` | ||
| | ||
| In addition, the application module provides `systemAppearance` property and `systemAppearanceChanged` event. Through, them you could easily detect the current system appearance mode and update application behavior or style on a change. | ||
| | ||
| ## NativeScript Theme Integration | ||
| | ||
| The NativeScript Theme provides beautiful look and feel of your application for both Light and Dark system appearances. Please, refer to the Theme article for more information. | ||
| | ||
| <!-- TODO: add a link to the theme article --> | ||
vchimev marked this conversation as resolved. Show resolved Hide resolved | ||
| | ||
| > Note: make sure to use `@nativescript/theme` package version 2.2.0 or above. | ||
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
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.