Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

@nativescript/theme

NativeScript11.4kApache-2.03.1.0TypeScript support: included

NativeScript Core Theme

readme

NativeScript Theme

Usage

The core theme supports light and dark core styling and skins on top of that. To load the core theme styling, just load the core CSS and its default skin:

@import "@nativescript/theme/css/core.css";
@import "@nativescript/theme/css/default.css";

or alternatively SCSS:

@import "@nativescript/theme/core";
@import "@nativescript/theme/default";

If you want, you can choose from several different skins. To do that, you can change the second CSS/SCSS file to the preferred skin name:

@import "@nativescript/theme/css/core.css";
@import "@nativescript/theme/css/blue.css";

or

@import "@nativescript/theme/core";
@import "@nativescript/theme/blue";

In order to import just the Theme variables in one of your modules, use this:

@import "@nativescript/theme/scss/variables";

It will import just the variables and mixins, without any additional styling.

Here is the old list of Theme skins - the first two are now the Core default Light and Dark skins, the rest are all the Light skins available, listed by name.

Multiple Platforms

The theme will style your application using Element selectors - you don't need to add CSS classes on every element you need to style.

Additionally, if you need to create you own skin, you can start just from the core theme - it includes the sizing and initial styling of the components.

Usage before NativeScript 6.1

In order to use Theme 2.0 before {N} 6.1, you will also need to include a small JS file in your project:

import "@nativescript/theme";

This JS takes care of updating several classes on the app root elements, something that got included in tns-core-modules in {N} 6.1.

Setting Dark or Light mode

Setting the theme mode from light to dark is now easier - instead of loading a new file, just find the root view and set .ns-dark class to it - this will change all colorization to dark tones. For instance, if your page root is RadSideDrawer, just add a class to it, like this:

<drawer:RadSideDrawer class="ns-dark" xmlns:drawer="nativescript-ui-sidedrawer">
    ...
</drawer:RadSideDrawer>

If your root is a frame, you can do this

<Frame class="ns-dark" defaultPage="root"></Frame>

For Angular, if your root is a page-router-outlet, you can set the .ns-dark class on it - it will pass it down to the Frame it renders.

Setting Dark or Light mode from JavaScript

Setting the theme mode from JavaScript is also much easier now - just import the theme and call Theme.setMode() with your preferred mode - either Theme.Light or Theme.Dark, like this:

import Theme from "@nativescript/theme";

Theme.setMode(Theme.Dark); // Or Theme.Light

Keep in mind that in {N} 6.2 these changes will override the default system mode. To restore it, use Theme.Auto (available since Theme 2.3.0):

import Theme from "@nativescript/theme";

Theme.setMode(Theme.Auto);

Additionally there is another helper method - toggleMode, which can be used without parameters to just toggle the mode or with a boolean parameter to ensure light or dark mode is set:

import Theme from "@nativescript/theme";

Theme.toggleMode(); // to toggle between the modes

// or

Theme.toggleMode(true);  // to ensure dark mode
Theme.toggleMode(false); // to ensure light mode
A note of warning

Due to limitation in Playground the JS Theme API cannot be imported like an ES6 module in a TS/Angular projects. You'll have to resort to requiring it:

const Theme = require("@nativescript/theme");

Theme.setMode(Theme.Dark); // Or Theme.Light

More root classes

In addition to .ns-light and .ns-dark classes, the theme's little JavaScript file introduces .ns-root on the root element, .ns-android/.ns-ios depending on the current platform (which the theme extensively uses) and additionally .ns-portrait/.ns-landscape and .ns-phone/.ns-tablet, which should be self-explanatory. Of course .ns-portrait/.ns-landscape get updated on orientation change, so you can use it to show a two pane layout in landscape, for instance.

The newest addition is .ns-statusbar-transparent since 2.0.4 - add this class to your root element, if you have enabled transparent status bar in the OS and your ActionBar gets underneath it.

Keep in mind that Android APIs before 21 don't support transparent status bars and this will result in an undesired top ActionBar padding!

Using Theme variables

There are several functions and mixins in the core theme, that can be used in your projects, as long as you're using SASS/SCSS.

If you need to access specific theme variables like simple colors or sizes, do it through the const function:

Button {
    background-color: const(forest);
    height: const(btn-height);
}

You can get light/dark colors:

Button {
    color: light(primary);

    .ns-dark & {
        color: dark(primary);
    }
}

or alternatively set them both in one go - this mixin does the same as the upper example:

Button {
    @include colorize($color: primary);
}

You can darken/lighten a color depending on its background (darken for light theme and lighten for dark theme):

Button {
    @include colorize($contrasted-border-color: accent background 20%);
}

The above example uses the contrasted function to check the contrast level of the background and adjust the lightness of the accent color according to it.

CSS variables

The Theme now exports all its internal variables to custom CSS ones in the .ns-root and .ns-modal classes. This is also done for Kendo based skins. You can use them to inherit your styles from the Theme, if using CSS. A list of the supported custom CSS variables follows:

Simple Colors Constants Light Colors Dark Colors
--color-black --const-font-size --light-primary --dark-primary
--color-white --const-background-alt-10 --light-background --dark-background
--color-grey --const-btn-color-secondary --light-accent --dark-accent
--color-grey-light --const-btn-color-disabled --light-complementary-color --dark-complementary-color
--color-charcoal --const-btn-font-size --light-complementary --dark-complementary
--color-transparent --const-btn-min-width --light-background-alt-5 --dark-background-alt-5
--color-aqua --const-btn-height --light-background-alt-10 --dark-background-alt-10
--color-blue --const-btn-padding-x --light-background-alt-20 --dark-background-alt-20
--color-brown --const-btn-padding-y --light-secondary --dark-secondary
--color-forest --const-btn-margin-x --light-disabled --dark-disabled
--color-grey-dark --const-btn-margin-y --light-text-color --dark-text-color
--color-purple --const-btn-radius --light-headings-color --dark-headings-color
--color-lemon --const-headings-margin-bottom --light-tab-text-color --dark-tab-text-color
--color-lime --const-headings-font-weight --light-accent-dark --dark-accent-dark
--color-orange --const-border-width --light-accent-light --dark-accent-light
--color-ruby --const-border-radius --light-accent-transparent --dark-accent-transparent
--color-sky --const-border-radius-sm --light-primary-accent --dark-primary-accent
--color-error --const-border-radius-lg --light-background-accent --dark-background-accent
| --const-disabled-opacity --light-background-dark-accent --dark-background-dark-accent
| --const-icon-font-size --light-item-active-color --dark-item-active-color
| --const-icon-font-size-lg --light-item-active-background --dark-item-active-background
| --light-btn-color --dark-btn-color
| --light-item-active-icon-color --dark-item-active-icon-color
| --light-btn-color-inverse --dark-btn-color-inverse
| --light-btn-color-secondary --dark-btn-color-secondary

Use them like this:

.my-accented-class {
    color: var(--light-accent);
}

For now these CSS variables are not used internally, so changing them won't change the look of your skin. This is planned for after HSL color support comes in NativeScript 6.2.

Kendo UI ThemeBuilder support

The theme now supports inheriting the Kendo UI ThemeBuilder theme variables. Just head there, customize your Keno UI SASS theme and hit the Download button. You will get a ZIP with two files in it - the theme CSS that you can use to style your web app, and variables.scss - the one you need for your NativeScript theme. It will look something like this:

$base-theme:Default;
$skin-name:test;
$swatch-name:Default Purple;
$border-radius: 10px;
$accent: #bf70cc;
$info: #3e80ed;
$success: #5ec232;
$warning: #fdce3e;
$error: #d51923;
$text-color: #656565;
$bg-color: #ffffff;
$base-text: #656565;
$base-bg: #f6f6f6;
$hovered-text: #656565;
$hovered-bg: #ededed;
$selected-text: #ffffff;
$selected-bg: #bf70cc;
$series-a: #ff6358;
$series-b: #ffd246;
$series-c: #78d237;
$series-d: #28b4c8;
$series-e: #2d73f5;
$series-f: #aa46be;

Take this file, add the following under it:

// Uncomment this row, if you target compat styling:
// $compat: true;

@import '~@nativescript/theme/index';

And load the file after your core theme. It should just work™. If it doesn't - head for the issues section.

Theme classes and compatibility

The old generic theme classes have been retired to avoid clashes with user code. They now live in the .compat world - if you want to use them, you should load them separately, like this:

@import "~@nativescript/theme/core.compat";
@import "~@nativescript/theme/blue.compat";

As of 2.0, the theme now utilizes a simplified BEM approach for the new element classes, that might be needed here or there. For instance, the buttons need .-primary and .-outline modifiers, instead of the old .btn-primary and .btn-outline classes. All element classes (which are not needed by default, except if you want to style a component to look like another one) are namespaced, so for instance a button is .nt-button, an action bar is .nt-action-bar and a ListView is .nt-list-view.

Should I use sass or node-sass

Theme 2.0 is developed using SASS. The NPM package used was sass (formerly dart-sass), however it can be used with either sass or node-sass. The difference between them is that sass doesn't depend on a native module (unless you add fibers) and is a little slower (30%) than node-sass, however it doesn't need recompilation if you change Node versions often. If you decide to use it, you should update your webpack config and change the sass-loader options to load sass instead (node-sass is the default sass-loader implementation), like this:

{
    loader: "sass-loader",
    options: {
        implementation: require("sass")
    }
}

Screenshots


Contributing

Clone this repo, and then use the tns run command to launch the demo app on your device or emulator of choice.

tns run ios

// or

tns run android

If you’d like to toggle the color scheme from light to dark, open the sidedrawer, scroll down to "Themes" at the bottom, and toggle away.

Publishing

NOTE: Only authorized authors can publish updates.

IMPORTANT: Always make sure you have run the demo app in iOS or Android to verify any changes as well as ensure the latest css has been built before doing the following:

  • Bump version in package.json
  • Adjust nativescript-theme-core.md if any changes to the published README are needed.
npm run builder
cd nativescript-theme-core
npm publish

IMPORTANT: Never modify the contents of nativescript-theme-core folder directly. The builder creates that everytime and any change you make there will be overwritten.

Setup changelog generation:

npm install -g conventional-changelog-cli

Generate changelog workflow:

  1. Make changes
  2. Commit those changes - using these conventions.
  3. Make sure Travis turns green
  4. Bump version in package.json and nativescript-theme-core.json
  5. conventional-changelog -p angular -i CHANGELOG.md -s
  6. Commit package.json and CHANGELOG.md files
  7. Tag
  8. Push

Breaking changes


NOTE

NativeScript Theme 1.0 to 2.0 migration guide is available here.


  • The theme requires 2 files to be loaded - the core theme and a skin on top of it. Read Usage for more info.
  • The theme is now applied using Element selectors, if you need the old classes approach - it has moved to .compat CSS/SCSS files, e.g. core.compat.css and blue.compat.css.
  • Theme 2.0 replaces node-sass with sass which is more up to date feature-wise and doesn't have a native dependency. It does still work with node-sass though.

Awesome Contributors

NathanWalker TheOriginalJosh tjvantoll NathanaelA triniwiz sis0k0
NathanWalker TheOriginalJosh tjvantoll NathanaelA triniwiz sis0k0
sitefinitysteve vakrilov bradmartin firescript valentinstoychev enchev
sitefinitysteve vakrilov  bradmartin   firescript   valentinstoychev   enchev  
bundyo | | |
  bundyo   | | |

LICENSE

Apache 2.0

changelog

3.1.0 (2024-07-22)

Features

  • add custom palettes to _kendo-palettes

Fixes

  • remove warnings for new "Mixed Declarations"
  • layout height issue with Angular forms

2.3.4 (2020-12-05)

Fixes

  • setMode should work correctly in all cases
  • Disables system theme so that NS themes can work properly...
  • Theme should work on NS 6 & 7 properly

2.3.3 (2020-03-17)

Fixes

  • Fix wrong ActionBar colors, introduced with node-sass fix
  • Fix missing bootstrap bg-primary accented background-color

2.3.2 (2020-02-20)

Fixes

  • CSS variables output with node-sass is causing crashes

2.3.1 (2020-01-13)

Features

  • Add support for hard changing the mode in {N} 6.2+ (Theme.Light/Theme.Dark/Theme.Auto).
  • toggleMode now takes into account systemAppearance to decide the alternate mode (6.2+).

2.2.1 (2019-11-13)

Features

  • Add default style for modal dialogs

2.2.0 (2019-10-29)

Breaking changes

  • Separate default core styling again to reduce the core CSS with 20k

Features

  • Ship pre-parsed JSON files for all CSS (used with css2json-loader)
  • Return $accent input variable, use $focus as internal abstraction
  • Rename $ab-background and $ab-color to respectively $complementary and $complementary-color, keep backwards compatibility
  • Introduce $...-dark input colors to be able to override the main dark mode colors specifically

Fixes

  • Stop importing view-common in app, use view instead
  • Add more padding to action items
  • Make the dark complementary color closer to the light one
  • Make HtmlView white in dark mode, as its internal colors are currently not dependent on mode
  • Fix PickerField modal in dark mode
  • Fix SegmentedBar selection colors (it doesn't support changing the color in selected state)

2.1.2 (2019-10-16)

Features

  • Make the Button color modifiers work as such (applies to primary and outline buttons)

Fixes

  • Fix the iOS landscape custom ActionBar items displacement
  • Fix the not properly distributed custom ActionBar title and iOS buttons
  • Update blue skin accent color (to blue:)
  • Make the outline button borders use accent color
  • Return the thumb/-thumb classes for Listview (Playground compatibility)
  • Fix app-compat to use scoped packages
  • Make the default Image height only 20pt
  • Increase the side drawer header padding
  • Add -circle class to complement -rounded/rounded
  • Fix the esm to cjs transpilation (was breaking Angular projects in Playground)

Potential breaking changes

  • Remove border-radius-rounded variable - use border-radius-sm instead (they had 1pt difference).

2.1.1 (2019-10-14)

Features

  • Export CSS variables in Core CSS

Fixes

  • Fix divider colors
  • Add 5pt Button default padding
  • Remove default font-weight from .ns-root
  • Fix vertical SideDrawer header centering in iOS - remove height, use padding

Breaking changes

  • Remove default FontAwesome 4 rule as it doesn't belong in the theme. Leave it for compat mode.

2.1.0 (2019-10-11)

Version 2.0.24 Beta was promoted to stable and was published as a scoped @nativescript/theme package.

2.0.24 beta (2019-10-10)

Features

  • Add platform API classes in .ns-{platform}-{sdkVersion} format to the root element, which should allow targeting both platform and sdkVersion
  • Make .hr to depend on background color by default

Fixes

  • Fix SideDrawer styling to be more like in templates
  • Update Kendo skin colors to be more alike their web counterparts
  • Fix .ns-statusbar-transparent to not target or break API 19
  • Fix compat theme setting in app
  • Make sure app-compat to target compat theming with the variable

2.0.23 beta (2019-09-30)

Fixes

  • Don't set default colors to labels, as it overrides dialog colors in iOS
  • Make the ActionBar colors defaults so they can be overridden

2.0.22 beta (2019-09-26)

Fixes

  • Added a workaround for node-sass still not getting updated to 3.6.x - to be able to import variables as listed in the README
  • Fix large border-radius not applied in iOS, up the small to 4pt (to differ from Android defaults) (thanks @kulikala)
  • Remove the default Image margin - not needed and confusing
  • Sync app and app-compat, some housekeeping

2.0.21 beta (2019-09-21)

Features

  • Add focus state support for the RadDataForm
  • Adjust RadAutoCompleteTextView paddings after its CSS fixes in 5.1.1

Fixes

  • Remove bold from ActionBar title to make it consistent across OSes
  • Fix .action-bar-title in compat
  • Fix the missing Roboto font in iOS - font now set to sans-serif normal
  • Fix RadAutoCompleteTextView clear button in iOS

2.0.20 beta (2019-09-13)

Fixes

  • Fix a node-sass incompatibility

2.0.19 beta (2019-09-13)

Features

  • Export CSS variables to .ns-modal too

2.0.18 beta (2019-09-12)

Features

  • Apply a default skin over the Core theme, similar to core in 1.x
  • Add styling for custom ActionBar (GridLayout, Label and Buttons in the TitleView), check the example

Fixes

  • Fix some issues in the app
  • Move all colorization in the skins
  • Fix some issues in Slider, SearchBar, SegmentedBar
  • Fix the aqua accent

2.0.17 beta (2019-09-06)

Features

  • Export all variable maps to CSS variables. Check the README.md for a full list.
  • Revise font-sizes, make them the same for both platforms

Fixes

  • Fix the modal ActionBar with ns-statusbar-transparent
  • Fix the aqua skin colors
  • Revamp the Button animations to work in ActionBar
  • Return accent var in maps
  • Remove the ghost StackLayout padding in RadListView

2.0.16 beta (2019-09-04)

Features

  • Add .t-36 class

Fixes

  • Fix the dark DateTimePicker's missing icons
  • Fix a crash on orientation change

2.0.15 beta (2019-09-04)

Fixes

  • Fix TextField borders
  • Compat fixes

2.0.14 beta (2019-09-03)

Features

  • Add typings to make Angular projects in Playground happy

Fixes

  • Fix setMode resets the classes set manually on root

2.0.13 beta (2019-09-03)

Fixes

  • Workaround the folder issue in Playground

2.0.12 beta (2019-09-03)

Features

  • Add scss/variables folder with SCSS skin variables for easier import
  • Add variable map for generic variables, accessible with const()
  • Add toggleMode to the API with optional boolean state enforcer, used like this:
    import Theme from 'nativescript-theme-core';

    Theme.toggleMode(); // to toggle between the modes

    // or

    Theme.toggleMode(true);  // to ensure dark mode
    Theme.toggleMode(false); // to ensure light mode

Fixes

  • Fix dark mode for Switches and several other components
  • Fix ActionBar Label title background, fix dark buttons
  • Fix the colorize function to produce better organized code, to work with consts and to properly prepend .ns-dark on root classes

2.0.11 beta (2019-09-01)

Features

  • Add JS transpilation to work in non-transpiling projects and Playground

Fixes

  • Fix setMode in Playground
  • Move bootstrap-based skin outside of theme-core to make the theme importable in Playground

2.0.10 beta (2019-09-01)

Features

  • Revise the Button styling, now all buttons have elevation in Android

Fixes

  • Fix the missing root view crash
  • Only load root class modifier if no support in core modules
  • Fix the jumping iOS Switch
  • Fix the Switch styling

2.0.9 beta (2019-08-29)

Features

  • Add small API for setMode. Usage:

      import Theme from "nativescript-theme-core";
    
      Theme.setMode(Theme.Dark); // Or Theme.Light

    It would set light or dark mode even before the app is initialized.

Fixes

  • Simplify ClassList, use Set
  • Fix dark mode for TabView, BottomNavigation and Tabs if they are root views

2.0.8 beta (2019-08-28)

Fixes

  • Remove folder imports while the Playground has issues with them

2.0.7 beta (2019-08-28)

Fixes

  • Fix headings
  • Fix the RadAutoCompleteTextView iOS TokenClearButton
  • Fix compat .btn styling, other compat fixes

2.0.6 beta (2019-08-22)

Features

  • Add styling and demo for nativescript-ui-autocomplete

Maintenance

  • Fix the bootstrap links in the bootstrap-based theme (Playground compatibility)
  • Update dependencies

2.0.5 beta (2019-07-30)

Maintenance

  • Remove PostCSS loader, as it doesn't do anything at this point
  • Fix the repo URL

2.0.4 beta (2019-07-30)

Maintenance

  • All SASS imports are now from same folder qualifiers to allow working in a folder (e.g. in Playground)

2.0.0 beta (2019-07-17)

Breaking Changes

  • The theme is now applied using Element selectors, for instance Button, Label, etc. This means it will style the app it is added to directly, without the need to add classes to every element. The old classes are moved to .compat CSS/SCSS. New namespaced classes are introduced to avoid clashes with user CSS.
  • The theme supports a core light or dark theme and skins on top of it - 2 files should be imported for it to work. A simple change of a class on root level controls if the core theme is light or dark. Skins can be changed on the fly, without changing the core theme. However special steps are needed with the new Webpack workflow.
  • Theme 2.0 beta requires some JavaScript to be loaded which helps with the styling. It should be included in core modules before the theme goes final.
  • Theme 2.0 replaces node-sass with dart-sass which is more up to date feature-wise and doesn't have a native dependency.

Features

  • Introduced several classes that are set on the root view and used to control light/dark theme and other platform features.
  • Several helper SASS functions and mixins are introduced to ease app/component styling
  • Theme is now compatible with Kendo UI ThemeBuilder, allowing for shared Web/Mobile themes
  • Added styling for the new Tabs and BottomNavigation.
  • Added styling for several nativescript-ui components that are CSS stylable - RadListView, RadDataForm.
  • Added styling for nativescript-picker and nativescript-datetimepicker components.

1.0.4 (2017-04-18)

Maintenance

  • maintenance release

1.0.3 (2017-03-01)

Bug Fixes

  • ListView: dark skin ListView background (79d5d8e)

1.0.2 (2016-12-06)

Bug Fixes

  • 2.4.0 is minimum required version (b399a3d)
  • forms spacing when same controls are used in a vertical stack (ce9db89)

1.0.1 (2016-11-27)

Bug Fixes

  • ActionBar: action-bar-title vertical-align centered for android, plus name migrate in platform (e7a96c3)

Features

  • Colors: add color classes for color/background-color with demo page (997b336)

1.0.0 (2016-11-23)

Bug Fixes

  • add node-sass dep (453d8c0)
  • android button shadow/border, updated demo app to 2.4.0 (46369ba)
  • correct version in compiled css for published versions (d8ab630)
  • custom sass hook (69166e0)
  • dark skin for list view and custom actionbar (9ed6676)
  • demo styles no longer published with compiled css (4ab677d)
  • drawer for demo (#50) (346fef8)
  • gitignore (53ec861)
  • listview (109baea)
  • postinstall (19bdd3f)
  • print correct version on publish (571a41b)
  • switch disabled on (65df221)
  • tabs setup (a8d4f20)
  • text fixes (6c956b5)
  • theme page (6cbc4ad)
  • theme skins now apply properly (4979153)
  • ActionBar: action-bar-title (4e5eb2c)
  • js: Append theme sheet to app.css (#102) (15b9695)
  • ListView: item padding/margin fixes (b382028)

Features

  • ci: Set up travis build (#103) (64fbf30)
  • build support for distribution (#61) (1397438)
  • Organization: renaming and organization of theme files per discussion (08552a9)
  • forms and demo app improvements (#51) (e6eda4d)
  • Page: page level classes (cc625d8)
  • Skins: ActionBar skins with theme'd sets (#108) (3e7b2b8)
  • Skins: theme aware classes, also fixes to button styles for android (8580319)
  • actionbar improvements. modal to test out action-bar class on custom setup. also fixed errors with demo. (525200b)
  • base color variables, label classes, and beginning actionbar (063ac15)
  • multiples of 4 spacing utility classes, also heading variables (7c27856)
  • pages for forms, sliders, switches (3840497)
  • pages for other components (999fc9b)
  • platform specific base (ff13779)
  • text utility classes (d5b4b37)

BREAKING CHANGES

  • ActionBar: action-title > action-bar-title

    Before:

    <Label class=“action-title”></Label>

    After:

    <Label class=“action-bar-title”></Label>