Skip to content

Commit 548435c

Browse files
Add built-in scroll animation support
1 parent 4995f6c commit 548435c

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ exports.fabTap = function(args) {
6969
| backColor | X | X | Sets the background color of the button | None
7070
| icon | X | X | Supports the same image source options that NativeScript images support | Required on android
7171
| rippleColor| X | | Ripple color on lollipop devices, it will fill the FAB on pre-lollipop devices | None
72+
| hideOnSwipeOfView| X | X | Directs the fab to animate itself in and out on scroll | Pass it the name of the view to monitor for a scroll event example: hideOnSwipeOfView="userListView"
73+
| hideAnimationDuration| X | X | How many milliseconds it takes for the button to hide. | Default of not set: 300ms
7274

7375
## NativeScript Compatibility
7476

demo/app/main-page.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ var platformModule = require("platform");
55
var color = require("color");
66

77
var users = [
8+
{ name: 'Billy Bob' },
9+
{ name: 'Tweeder' },
10+
{ name: 'Mox' },
11+
{ name: 'Coach' },
12+
{ name: 'Lance' },
13+
{ name: 'Johnson' },
14+
{ name: 'William' },
15+
{ name: 'Franklin' },
816
{ name: 'Billy Bob' },
917
{ name: 'Tweeder' },
1018
{ name: 'Mox' },
@@ -32,5 +40,5 @@ function pageLoaded(args) {
3240
exports.pageLoaded = pageLoaded;
3341

3442
exports.fabTap = function(args){
35-
viewModel.users.push({ name: "Gary"});
43+
viewModel.users.unshift({ name: "Gary"});
3644
}

demo/app/main-page.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
<ActionBar title="Native FAB" backgroundColor="#3F51B5" color="#fff" />
55
</Page.actionBar>
66
<grid-layout rows="auto, *">
7-
<list-view row="1" items="{{ users }}">
7+
<list-view id="userList" row="1" items="{{ users }}">
88
<list-view.itemTemplate>
99
<label text="{{ name }}" textWrap="true" fontSize="15" margin="20" />
1010
</list-view.itemTemplate>
@@ -15,6 +15,7 @@
1515
icon="res://ic_add_white"
1616
tap="fabTap"
1717
rippleColor="#ffffff"
18-
class="fab-button" />
18+
class="fab-button"
19+
hideOnSwipeOfView="userList" />
1920
</grid-layout>
2021
</Page>

fab-common.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var view = require("ui/core/view");
22
var color = require("color");
3+
var frameModule = require("ui/frame");
34
var dObservable = require("ui/core/dependency-observable");
45
var proxy = require("ui/core/proxy");
56

@@ -9,6 +10,37 @@ var FloatingActionButton = (function (_super) {
910
function FloatingActionButton() {
1011
_super.call(this);
1112
}
13+
14+
FloatingActionButton.prototype.onLoaded = function () {
15+
_super.prototype.onLoaded.call(this);
16+
var fab = this;
17+
var viewToAttachTo = this.hideOnSwipeOfView;
18+
var swipeItem = this.page.getViewById(viewToAttachTo);
19+
20+
if(swipeItem !== undefined){
21+
var duration = (this.hideAnimationDuration == undefined) ? 300 : this.hideAnimationDuration;
22+
23+
//Wire up action
24+
swipeItem.on("swipe", function (args) {
25+
26+
//if scrolling down (swipe up) -- hide FAB
27+
if (args.direction === 4) {
28+
fab.animate({
29+
translate: { x: 0, y: 200 },
30+
opacity: 0,
31+
duration: duration
32+
});
33+
} //if scrolling up (swipe down) -- show FAB
34+
else if (args.direction === 8) {
35+
fab.animate({
36+
translate: { x: 0, y: 0 },
37+
opacity: 1,
38+
duration: duration
39+
});
40+
};
41+
});
42+
}
43+
};
1244

1345
Object.defineProperty(FloatingActionButton.prototype, "rippleColor", {
1446
get: function () {
@@ -41,7 +73,9 @@ var FloatingActionButton = (function (_super) {
4173
FloatingActionButton.iconProperty = new dObservable.Property("icon", "FloatingActionButton", new proxy.PropertyMetadata(0, dObservable.PropertyMetadataSettings.AffectsLayout));
4274
FloatingActionButton.rippleColorProperty = new dObservable.Property("rippleColor", "FloatingActionButton", new proxy.PropertyMetadata(0, dObservable.PropertyMetadataSettings.AffectsLayout));
4375

76+
4477
return FloatingActionButton;
4578
})(view.View);
4679

80+
4781
exports.Fab = FloatingActionButton;

0 commit comments

Comments
 (0)