Skip to content

Commit bc646d2

Browse files
committed
fix(snackbar): fixed view option and added anchorView #243
1 parent e78f5b9 commit bc646d2

File tree

4 files changed

+26
-19
lines changed

4 files changed

+26
-19
lines changed

demo-vue/app/examples/SnackBar.vue

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@
99
<StackLayout id="innerView" ref="innerView" backgroundColor="gray">
1010
<MDButton id="show_snack" text="show snack" @tap="onTap" />
1111
<MDButton id="show_snack_action" text="show snack action" @tap="onTap" />
12-
<MDButton id="show_snack_action_color" text="show snack action color" @tap="onTap" />
12+
<MDButton ref="show_snack_action_color" id="show_snack_action_color" text="show snack action color" @tap="onTap" />
1313
</StackLayout>
1414
<!-- </Page> -->
1515
<!-- </Frame> -->
16-
<MDBottomNavigationBar row="1" activeColor="red">
16+
<MDBottomNavigationBar row="1" activeColor="red" ref="bottomBar">
1717
<MDBottomNavigationTab title="First" icon="res://ic_home" activeColor="green" />
1818
<MDBottomNavigationTab title="Second" icon="res://ic_view_list" isSelectable="false" />
1919
<MDBottomNavigationTab title="Third" icon="res://ic_menu" inactiveColor="brown" />
@@ -51,7 +51,7 @@ export default Vue.extend({
5151
console.log('onTap', objId, obj);
5252
switch (objId) {
5353
case 'show_snack': {
54-
showSnack({ message: 'this is test snack!', textColor: 'yellow', view: this.$refs.innerView.nativeView });
54+
showSnack({ message: 'this is test snack!', textColor: 'yellow', anchorView: this.$refs.bottomBar.nativeView });
5555
break;
5656
}
5757
case 'show_snack_action': {

src/snackbar/snackbar-common.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,16 @@ export interface SnackBarOptions {
4343
isRTL?: boolean;
4444

4545
/**
46-
* *Android Only*
4746
* The View to which the snackbar will be attached. Useful with modals.
4847
* Default to Frame.topmost().currentPage
4948
*/
5049
view?: View;
50+
51+
/**
52+
* The view you want to "anchor the snack above".
53+
* The snackbar will appear above that view
54+
*/
55+
anchorView?: View;
5156
}
5257

5358
export enum DismissReasons {

src/snackbar/snackbar.android.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -54,31 +54,28 @@ export class SnackBar extends SnackBarBase {
5454
}
5555
const page = attachView instanceof Page ? attachView : attachView.page;
5656
let nView = (page.nativeViewProtected as android.view.View).getParent();
57-
if (page.hasActionBar) {
58-
nView = nView.getParent();
59-
}
6057
let nCoordinatorLayout: androidx.coordinatorlayout.widget.CoordinatorLayout = (page as any).nCoordinatorLayout;
6158
if (!nCoordinatorLayout && !(nView instanceof androidx.coordinatorlayout.widget.CoordinatorLayout) && nView instanceof android.view.ViewGroup) {
6259
nCoordinatorLayout = new androidx.coordinatorlayout.widget.CoordinatorLayout(attachView._context);
6360

6461
if (options.view) {
6562
const nAttachedView = options.view.nativeViewProtected as android.view.View;
6663
const params = new android.widget.FrameLayout.LayoutParams(nAttachedView.getWidth(), nAttachedView.getHeight());
67-
const myArray = Array.create('int', 2);
68-
nView.getLocationOnScreen(myArray);
69-
const otherArray = Array.create('int', 2);
70-
options.view.nativeViewProtected.getLocationOnScreen(otherArray);
71-
72-
params.topMargin = otherArray[1] - myArray[1];
73-
(nView as any).addView(nCoordinatorLayout, params);
64+
params.gravity = android.view.Gravity.BOTTOM;
65+
const locationArray = Array.create('int', 2);
66+
options.view.nativeViewProtected.getLocationOnScreen(locationArray);
67+
params.bottomMargin = nView.getHeight() - locationArray[1] - nAttachedView.getHeight();
68+
nView.addView(nCoordinatorLayout, params);
7469
} else {
7570
(nView as any).addView(nCoordinatorLayout, new android.view.ViewGroup.LayoutParams(android.view.ViewGroup.LayoutParams.MATCH_PARENT, android.view.ViewGroup.LayoutParams.MATCH_PARENT));
7671
}
7772

7873
nView = nCoordinatorLayout;
7974
}
8075
this._snackbar = com.google.android.material.snackbar.Snackbar.make(nView as any, options.message, options.hideDelay);
81-
76+
if (options.anchorView) {
77+
this._snackbar.setAnchorView(options.anchorView.nativeViewProtected);
78+
}
8279
// this._snackbar.setText(options.message);
8380
// this._snackbar.setDuration(options.hideDelay);
8481

src/snackbar/snackbar.ios.ts

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ export class SnackBar extends SnackBarBase {
5454
button.inkColor = getRippleColor(options.actionTextColor);
5555
});
5656

57-
// message.buttonTextColor =
5857
}
5958

6059
if (options.backgroundColor && Color.isValid(options.backgroundColor)) {
@@ -77,11 +76,17 @@ export class SnackBar extends SnackBarBase {
7776
nAttachedView = viewController.view;
7877
}
7978

80-
8179
if (nAttachedView) {
82-
SnackBar._snackbarManager.setPresentationHostView(nAttachedView);
80+
message.presentationHostViewOverride = nAttachedView;
81+
}
82+
83+
if (options.anchorView) {
84+
const totalHeight = nAttachedView? (nAttachedView.frame.origin.y + nAttachedView.frame.size.height): UIApplication.sharedApplication.keyWindow.rootViewController.view.bounds.size.height;
85+
const result = (options.anchorView.nativeViewProtected as UIView).convertPointToView(CGPointZero, nAttachedView);
86+
SnackBar._snackbarManager.setBottomOffset(totalHeight - result.y );
8387
} else {
84-
SnackBar._snackbarManager.setPresentationHostView(null);
88+
SnackBar._snackbarManager.setBottomOffset(0);
89+
8590
}
8691
}
8792

0 commit comments

Comments
 (0)