Skip to content

Commit

Permalink
RTL-friendly align property for drawer
Browse files Browse the repository at this point in the history
  • Loading branch information
keanulee committed Feb 19, 2016
1 parent 3ab952e commit f4ddb58
Show file tree
Hide file tree
Showing 10 changed files with 91 additions and 35 deletions.
6 changes: 3 additions & 3 deletions app-drawer-layout/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ In narrow layout, the drawer will be stacked on top of the main content. The dra
in/out to hide/reveal the main content.


By default the drawer is on the left side of the screen:
By default the drawer is aligned to the start, which is left in LTR layouts:

```html
<app-drawer-layout>
Expand All @@ -19,11 +19,11 @@ By default the drawer is on the left side of the screen:
</app-drawer-layout>
```

A drawer on the right side of the screen:
Align the drawer at the end:

```html
<app-drawer-layout>
<app-drawer position="right">
<app-drawer align="end">
<!-- drawer content -->
</app-drawer>
<div>
Expand Down
6 changes: 3 additions & 3 deletions app-drawer-layout/app-drawer-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
In narrow layout, the drawer will be stacked on top of the main content. The drawer will slide
in/out to hide/reveal the main content.
By default the drawer is on the left side of the screen:
By default the drawer is aligned to the start, which is left in LTR layouts:
```html
<app-drawer-layout>
Expand All @@ -31,11 +31,11 @@
</app-drawer-layout>
```
A drawer on the right side of the screen:
Align the drawer at the end:
```html
<app-drawer-layout>
<app-drawer position="right">
<app-drawer align="end">
<!\-\- drawer content \-\->
</app-drawer>
<div>
Expand Down
2 changes: 1 addition & 1 deletion app-drawer-layout/demo/demo1.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

<app-drawer-layout>

<app-drawer position="left"></app-drawer>
<app-drawer></app-drawer>

<app-toolbar>
<paper-icon-button icon="menu" drawer-toggle></paper-icon-button>
Expand Down
4 changes: 2 additions & 2 deletions app-drawer-layout/demo/demo2.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,12 @@

<app-drawer swipe-open></app-drawer>

<app-drawer id="rightDrawer" position="right" swipe-open></app-drawer>
<app-drawer id="endDrawer" align="end" swipe-open></app-drawer>

<app-toolbar>
<paper-icon-button icon="menu" drawer-toggle></paper-icon-button>
<div title></div>
<paper-icon-button icon="info" onclick="document.getElementById('rightDrawer').toggle();"></paper-icon-button>
<paper-icon-button icon="info" onclick="document.getElementById('endDrawer').toggle();"></paper-icon-button>
</app-toolbar>

<sample-content size="100"></sample-content>
Expand Down
2 changes: 1 addition & 1 deletion app-drawer-layout/test/app-drawer-layout.html
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@
assert.equal(drawerLayout.$.contentContainer.style.marginLeft, '256px');
assert.equal(drawerLayout.$.contentContainer.style.marginRight, '');

drawer.position = 'right';
drawer.align = 'end';

window.setTimeout(function() {
assert.equal(drawerLayout.$.contentContainer.style.marginLeft, '');
Expand Down
6 changes: 3 additions & 3 deletions app-drawer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ app-drawer is a navigation drawer that can slide in from the left or right.

Example:

Add a drawer on the left side of the screen:
Align the drawer at the start, which is left in LTR layouts (default):

```html
<app-drawer opened></app-drawer>
```

Add a drawer on the right side of the screen:
Align the drawer at the end:

```html
<app-drawer position="right" opened></app-drawer>
<app-drawer align="end" opened></app-drawer>
```

To make the contents of the drawer scrollable, create a wrapper for the scroll
Expand Down
44 changes: 34 additions & 10 deletions app-drawer/app-drawer.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,16 @@
Example:
Add a drawer on the left side of the screen (default):
Align the drawer at the start, which is left in LTR layouts (default):
```html
<app-drawer opened></app-drawer>
```
Add a drawer on the right side of the screen:
Align the drawer at the end:
```html
<app-drawer position="right" opened></app-drawer>
<app-drawer align="end" opened></app-drawer>
```
To make the contents of the drawer scrollable, create a wrapper for the scroll
Expand Down Expand Up @@ -79,7 +79,7 @@
@apply(--app-drawer-content-container);
}

:host([position=right]) > #contentContainer {
#contentContainer[position=right] {
left: auto;
right: 0;

Expand All @@ -97,7 +97,7 @@
visibility: visible;
}

:host([swipe-open][position=right]) > #contentContainer::after {
:host([swipe-open]) > #contentContainer[position=right]::after {
left: auto;
right: 100%;
}
Expand Down Expand Up @@ -135,7 +135,7 @@

<div id="scrim" on-tap="_scrimTapHandler"></div>

<div id="contentContainer">
<div id="contentContainer" position$="{{position}}">
<content></content>
</div>

Expand Down Expand Up @@ -169,12 +169,22 @@
},

/**
* The position of the drawer on the screen ('left' or 'right').
* The alignment of the drawer on the screen ('start', 'end', 'left', or 'right').
* 'start' computes to left and 'end' to right in LTR layout and vice versa in RTL
* layout.
*/
align: {
type: String,
value: 'start',
},

/**
* The computed, read-only position of the drawer on the screen ('left' or 'right').
*/
position: {
type: String,
value: 'left',
reflectToAttribute: true
readOnly: true,
computed: '_computePosition(align)'
},

/**
Expand Down Expand Up @@ -252,6 +262,20 @@
}, 1);
},

_isRTL: function() {
return window.getComputedStyle(this).direction === 'rtl';
},

_computePosition: function() {
switch (this.align) {
case 'start':
return this._isRTL() ? 'right' : 'left';
case 'end':
return this._isRTL() ? 'left' : 'right';
}
return this.align;
},

_scrimTapHandler: function() {
if (this.persistent) {
return;
Expand Down Expand Up @@ -291,7 +315,7 @@
this.style.visibility = 'visible';

var rect = this.$.contentContainer.getBoundingClientRect();
if (this.position == 'left') {
if (this.position === 'left') {
this._translateOffset = rect.left;
} else {
this._translateOffset = rect.right - window.innerWidth;
Expand Down
2 changes: 1 addition & 1 deletion app-drawer/demo/demo2.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@

<sample-content size="100"></sample-content>

<app-drawer id="drawer" position="right" swipe-open>
<app-drawer id="drawer" align="end" swipe-open>
<div style="height: 100%; overflow: auto;">
<template is="dom-repeat" id="menu" items="[[items]]">
<paper-icon-item class="iconItem">
Expand Down
42 changes: 37 additions & 5 deletions app-drawer/test/app-drawer.html
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,45 @@
test('default values', function() {
assert.isFalse(drawer.opened);
assert.isFalse(drawer.persistent);
assert.equal(drawer.position, 'left');
assert.equal(drawer.align, 'start');
assert.isFalse(drawer.swipeOpen);
});

test('set scroll direction', function() {
assert.equal(drawer['__polymerGesturesTouchAction'], 'pan-y');
});

test('computed position', function() {
document.dir = 'ltr';
drawer.align = 'start';

assert.equal(drawer.position, 'left');

drawer.align = 'end';

assert.equal(drawer.position, 'right');

drawer.align = 'left';

assert.equal(drawer.position, 'left');

document.dir = 'rtl';
drawer.align = 'start';

assert.equal(drawer.position, 'right');

drawer.align = 'end';

assert.equal(drawer.position, 'left');

drawer.align = 'right';

assert.equal(drawer.position, 'right');
});

test('left drawer opens and closes', function() {
drawer.align = 'left';

var contentContainerClientRect = contentContainer.getBoundingClientRect();
assert.isTrue(contentContainerClientRect.right <= 0);

Expand All @@ -84,7 +114,7 @@
});

test('right drawer opens and closes', function() {
drawer.position = 'right';
drawer.align = 'right';

var contentContainerClientRect = contentContainer.getBoundingClientRect();
assert.isTrue(contentContainerClientRect.left >= window.innerWidth);
Expand Down Expand Up @@ -127,7 +157,7 @@
test('app-drawer-reset-layout', function(done) {
var listenerSpy = sinon.spy();
drawer.addEventListener('app-drawer-reset-layout', listenerSpy);
drawer.position = 'right';
drawer.align = 'right';

window.setTimeout(function() {
assert.isTrue(listenerSpy.called);
Expand Down Expand Up @@ -172,6 +202,7 @@
test('left drawer swiping', function() {
var drawerWidth = drawer.getWidth();
var halfWidth = drawerWidth / 2;
drawer.align = 'left';
drawer.fire('track', { state: 'start' });
drawer.fire('track', { state: 'track', dx: -halfWidth, ddx: -halfWidth });

Expand Down Expand Up @@ -210,7 +241,7 @@
test('right drawer swiping', function() {
var drawerWidth = drawer.getWidth();
var halfWidth = drawerWidth / 2;
drawer.position = 'right';
drawer.align = 'right';
drawer.fire('track', { state: 'start' });
drawer.fire('track', { state: 'track', dx: halfWidth, ddx: halfWidth });

Expand Down Expand Up @@ -268,6 +299,7 @@

test('left drawer flicking', function() {
var drawerWidth = drawer.getWidth();
drawer.align = 'left';
drawer.fire('track', { state: 'start' });
drawer.fire('track', { state: 'track', dx: 0, ddx: 0 });
drawer.fire('track', { state: 'end', dx: 0.1, ddx: 0.1 });
Expand Down Expand Up @@ -315,7 +347,7 @@

test('right drawer flicking', function() {
var drawerWidth = drawer.getWidth();
drawer.position = 'right';
drawer.align = 'right';
drawer.fire('track', { state: 'start' });
drawer.fire('track', { state: 'track', dx: 0, ddx: 0 });
drawer.fire('track', { state: 'end', dx: -0.1, ddx: -0.1 });
Expand Down
12 changes: 6 additions & 6 deletions demo/demo1.html
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@
background-color: #eee;
}

#leftDrawer {
#startDrawer {
--app-drawer-content-container: {
box-shadow: 1px 0 2px 1px rgba(0,0,0,0.18);
}
}

#rightDrawer {
#endDrawer {
--app-drawer-content-container: {
box-shadow: -1px 0 2px 1px rgba(0,0,0,0.18);
}
Expand Down Expand Up @@ -83,9 +83,9 @@
</app-toolbar>
</app-header>

<app-drawer id="leftDrawer" position="left"></app-drawer>
<app-drawer id="startDrawer" align="start"></app-drawer>

<app-drawer id="rightDrawer" position="right"></app-drawer>
<app-drawer id="endDrawer" align="end"></app-drawer>

<sample-content size="100"></sample-content>

Expand All @@ -95,11 +95,11 @@

(function(scope) {
scope.toggleLeft = function() {
this.$.leftDrawer.toggle();
this.$.startDrawer.toggle();
};

scope.toggleRight = function() {
this.$.rightDrawer.toggle();
this.$.endDrawer.toggle();
};
})(document.querySelector('#app'));

Expand Down

0 comments on commit f4ddb58

Please sign in to comment.