Skip to content
This repository has been archived by the owner on Dec 19, 2024. It is now read-only.

Commit

Permalink
Add RTL to polymer 3 (#141)
Browse files Browse the repository at this point in the history
RTL implementation
  • Loading branch information
Elliott Marquez authored Mar 18, 2019
1 parent 096c55d commit c2c556c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
10 changes: 10 additions & 0 deletions demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ <h3>Toggle buttons can be checked and disabled</h3>
</template>
</demo-snippet>
<h3>Toggle buttons can be set RTL</h3>
<demo-snippet class="centered-demo">
<template>
<paper-toggle-button dir="rtl">Toggle</paper-toggle-button>
<paper-toggle-button dir="rtl" checked>Toggle</paper-toggle-button>
<paper-toggle-button dir="rtl" disabled>Disabled</paper-toggle-button>
<paper-toggle-button dir="rtl" invalid>Invalid</paper-toggle-button>
</template>
</demo-snippet>
<h3>Toggle buttons can hide the ripple effect using the <i>noink</i> attribute</h3>
<demo-snippet class="centered-demo">
<template>
Expand Down
40 changes: 37 additions & 3 deletions paper-toggle-button.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ Polymer({
position: absolute;
top: -3px;
left: 0;
right: auto;
height: 20px;
width: 20px;
border-radius: 50%;
Expand All @@ -109,6 +110,13 @@ Polymer({
@apply --paper-toggle-button-unchecked-button;
}
:host(:dir(rtl)) .toggle-button,
/* Shady DOM workaround */
:host([dir="rtl"]) .toggle-button {
right: 0;
left: auto;
}
.toggle-button.dragging {
-webkit-transition: none;
transition: none;
Expand All @@ -131,6 +139,13 @@ Polymer({
transform: translate(16px, 0);
}
:host(:dir(rtl)):host([checked]) .toggle-button,
/* Shady DOM workaround */
:host([dir="rtl"][checked]) .toggle-button {
-webkit-transform: translate(-16px, 0);
transform: translate(-16px, 0);
}
:host([checked]:not([disabled])) .toggle-button {
background-color: var(--paper-toggle-button-checked-button-color, var(--primary-color));
Expand Down Expand Up @@ -225,6 +240,12 @@ Polymer({

listeners: {track: '_ontrack'},

__calculateIsRtl: function() {
const compStyle = window.getComputedStyle(this);
const dir = compStyle.direction;
return dir === 'rtl';
},

attached: function() {
afterNextRender(this, function() {
setTouchAction(this, 'pan-y');
Expand All @@ -243,6 +264,7 @@ Polymer({
},

_trackStart: function(track) {
this._isRtl = this.__calculateIsRtl()
this._width = this.$.toggleBar.offsetWidth / 2;
/*
* keep an track-only check state to keep the dragging behavior smooth
Expand All @@ -254,10 +276,22 @@ Polymer({

_trackMove: function(track) {
var dx = track.dx;
this._x = Math.min(
this._width, Math.max(0, this._trackChecked ? this._width + dx : dx));

if (this._isRtl) {
this._x = Math.max(
-this._width, Math.min(0, this._trackChecked ? -this._width + dx : dx));
} else {
this._x = Math.min(
this._width, Math.max(0, this._trackChecked ? this._width + dx : dx));
}

this.translate3d(this._x + 'px', 0, 0, this.$.toggleButton);
this._userActivate(this._x > (this._width / 2));

if (this._isRtl) {
this._userActivate(this._x < (-this._width / 2));
} else {
this._userActivate(this._x > (this._width / 2));
}
},

_trackEnd: function(track) {
Expand Down

0 comments on commit c2c556c

Please sign in to comment.