From the command prompt go to your app's root folder and execute:
tns plugin add nativescript-ng-shadow
If you want a quickstart, check out the demo app.
This is an Angular directive to make your life easier when it comes to native shadows.
Shadows are a very important part of Material design specification.It brings up the concept of elevation, which implies in the natural human way of perceiving objects raising up from the surface.
With this directive, you won't have to worry about all the aspects regarding shadowing on Android and on iOS.On the other hand, if you care about any details, just provide extra attributes and they will superseed the default ones.
However, running this on Android you will require the SDK to be greater or equal than 21 (Android 5.0 Lollipop or later), otherwise shadows will simply not be shown. There should be no problem running this on any version of iOS.
// ...
import { NgShadowModule } from 'nativescript-ng-shadow';
@NgModule({
imports: [
NgShadowModule,
// ...
],
// ...
})
export class MyModule { }
Simple attribute shadow
:
<Label shadow="2"></Label>
Of course you can property bind it:
<Label [shadow]="myCustomShadow"></Label>
To provide other details, combine the shadow
directive with other attributes:
<Label shadow [elevation]="myElevation" cornerRadius="5"></Label>
There are a couple of platform specific attributes you might want to use to customize your view. Bear in mind some of them clash with CSS styles applied to the same views. When it happens, the default behaviour on Android is the original HTML/CSS styles are lost in favor of the ones provided by this directive. On iOS, on the other hand, HTML/CSS pre-existent styles are regarded, consequently the shadow might not be applied.
The tip is avoid applying things like background color and border radius to the same view you intend to apply this directive. You are always able to nest views and get what you want. If not, please leave a message so we can try to help.
The table below list and describes all possible attributes as well as show which platform supports each one of them:
Attribute | Type | Platform | Description |
---|---|---|---|
shadow | string | number | AndroidData | IOSData | both | Directive attribute. Providing null or empty string with no elevation attribute, will switch off the shadow |
elevation | number | string | both | Determines the elevation of the view from the surface. It does all shadow related calculations. You might want to have a look at this enum of standard material design elevations. PS: Since version 2.0, it's calculated in DIPs (or DPs, density independent pixels) on Android, or PTs (points) on iOS. |
shape | string => 'RECTANGLE' | 'OVAL' | 'RING' | 'LINE' |
Android | Determines the shape of the view and overrides its format styles. |
bgcolor | string => color #RGB | Android | Determines view's background color and overrides its previous background. |
cornerRadius | number | Android | Determines view's corner radius (CSS border-radius) and overrides its previous style. PS: Since version 2.0, it's calculated in DIPs (or DPs, density independent pixels). |
translationZ | number | Android | Determines an extra distance (in DIP) to the surface. |
maskToBounds | boolean => default: false | iOS | Determines whether the shadow will be limited to the view margins. |
shadowColor | string => color #RGB | iOS | Determines shadow color. Shadow won't be applied if the view already has background. |
shadowOffset | number | iOS | Determines the distance in points (only on Y axis) of the shadow. Negative value shows the shadow above the view. |
shadowOpacity | number | iOS | From 0 to 1. Determines the opacity level of the shadow. |
shadowRadius | number | iOS | Determines the blurring effect in points of the shadow. The higher the more blurred. |
AndroidData
and IOSData
As you might have noticed the main shadow
attribute accepts object as argument. You'll be able to assign it in a property bind and it will override any possible separate attribute you might have defined:
import { AndroidData, ShapeEnum } from 'nativescript-ng-shadow';
// ...
export class MyComponent {
fabShadow: AndroidData = {
elevation: 6,
bgcolor: '#ff1744',
shape: ShapeEnum.OVAL,
};
// ...
}
In the template you can do:
<Label [shadow]="fabShadow" width="56" height="56"></Label>
If you want to be consistent with the Material Design specification but you're sick trying to memorize which elevation your view should have. We put together a list of pre-defined elevations:
If you don't even want to check it out every time you have to shadow a view, just import the Elevation
enum and enjoy :)
import { Elevation } from 'nativescript-ng-shadow';
class MyComponent {
// ...
ngOnInit(): init {
this.mySnackBar.elevation = Elevation.SNACKBAR;
}
// ...
}
Here are the list of improvements on version 2.0:
translationZ
will add extra distance to the surfaceand it's mainly used for animations.Android buttons are split into three categories:floating, raised and flat. Different from labels and other ui elements,each button category has its own state animator.So, when buttons are tapped, Android does affect their elevation(and z translation) in a way that Angular is not notified. At the end of tap animation, buttons get back toresting defaults (i.e. raised button's elevation
at 2dp and translationZ
at 0) overridingthe shadow stablished by this plugin.
Since version 2.1.0, this plugin replaces the default StateListAnimator
with onethat gets back to the values you provide for elevation
and translationZ
.
Feel free to fill an issue if you want the flexibility of defining your ownStateListAnimator
. The motivation so far was simply put this plugin to work withbuttons without changing the original state once they are clicked.
Apache License Version 2.0, January 2004
NativeScript 可以使用 Javascript,CSS, XML 创建真正的 Native 跨平台应用,支持 iOS Android,NativeScript 将您的跨平台代码翻译成目标平台的代码。 UI 使用 XML 描述,CSS 样式,在编译时将 UI 转化成本地原生代码,最终得到正在的 Native 原生应用。 Telerik 公开了用于创建安卓、iOS和Windows Unive
NativeScript Angular Shadow Directive Plugin This repo is a fork of @Especializa's NativeScript Shadow Directive.https://github.com/Especializa/nativescript-ng-shadow Installation From the command pro
NativeScript Command-Line Interface The NativeScript CLI lets you create, build, and deploy NativeScript-based apps on iOS and Android devices. Get it using: npm install -g nativescript What is Native
NativeScript-Snackbar �� �� �� NativeScript plugin for Material Design SnackBar component. Installation: NativeScript 7+:tns plugin add @nstudio/nativescript-snackbar NativeScript version prior to 7:t
Nativescript-Ripple This plugin aims to bring a native (or close to native) ripple implementation on Android and iOS. The android version uses a RippleDrawable and conserves the previous background, a
NativeScript-FloatingActionButton NativeScript plugin for Material Design Floating Action Button UI component. Installation Nativescript 7+: ns plugin add @nstudio/nativescript-floatingactionbutton Na