Add/Remove iron-selected class based on the paper-tab selection - polymer

I have started learning Polymer & lit-element and was working on active & inactive status grid where i have used paper-tabs with iron-pages.
Question: When I switch between the paper-tabs, the iron-selected class is adding to the paper-tab links but not getting added to the iron-pages content.
How to make the iron-selected class work with the iron-pages content?
Any solution would be great!
constructor() {
super();
this.currentPage = 0;
}
render() {
return html `
<div class="card">
<paper-tabs scrollable selected=${this.currentPage}>
<paper-tab>Tab 1</paper-tab>
<paper-tab>Tab 2</paper-tab>
<paper-tab>Tab 3</paper-tab>
</paper-tabs>
<iron-pages selected=${this.currentPage}>
<div>some story for tab 1</div>
<div>some story for tab 2</div>
<div>some story for tab 3</div>
</iron-pages>
</div>
`;
}
}

Unlike Polymer, lit-html doesn't have a two way data binding mechanism so you have to take care of updating the currentPage property in the selected-changed event:
render() {
return html`
<div class="card">
<paper-tabs scrollable
selected=${this.currentPage /* This is unidirectional */}
#selected-changed=${e => this.currentPage = e.detail.value}>
<paper-tab>Tab 1</paper-tab>
<paper-tab>Tab 2</paper-tab>
<paper-tab>Tab 3</paper-tab>
</paper-tabs>
<iron-pages selected=${this.currentPage}>
<div>some story for tab 1</div>
<div>some story for tab 2</div>
<div>some story for tab 3</div>
</iron-pages>
</div>
`;
}

Related

Similar attribute like drawer-toggle but does not toggle the drawer

Is there anything like drawer-toggle except that it doesn't toggle the menu? I want to add a similar attribute to paper-icon-button to show/hide the icon button based on whether the drawer is shown or not. I know I can get the boolean from app-drawer-layout.narrow, but my code is not exactly like the following and it's not easy to have an reference to <app-drawer-layout>. The following is just an example of providing the context of what I meant by drawer-toggle.
<app-drawer-layout>
<app-drawer>
drawer-content
</app-drawer>
<app-header-layout>
<app-header>
<app-toolbar>
<paper-icon-button icon="close" drawer-toggle></paper-icon-button>
<div main-title>App name</div>
</app-toolbar>
</app-header>
main content
</app-header-layout>
</app-drawer-layout>
You can bind a boolean property to the app-drawer-layout's narrow property and use this with an observer to show/hide the paper-icon-button. Example:
<dom-module id="test-app">
<template>
<style>
.hide {
display: none;
}
</style>
<app-drawer-layout fullbleed narrow="{{visible}}">
<app-drawer id="drawer">
drawer content
</app-drawer>
<app-header-layout>
<app-header>
<app-toolbar>
<paper-icon-button id="button" icon="menu" on-tap="_onTap"></paper-icon-button>
<div class="title" main-title>App name</div>
</app-toolbar>
</app-header>
<div>
main content
</div>
</app-header-layout>
</app-drawer-layout>
</template>
<script>
Polymer({
is: 'test-app',
properties: {
visible: {
type: Boolean,
observer: '_visibleChanged'
}
},
_onTap: function() {
console.log(this.visible);
this.$.drawer.toggle();
},
_visibleChanged: function(value) {
this.toggleClass('hide', !value, this.$.button);
}
});
</script>
</dom-module>
Note that now you need to manually handle the drawer toggle (_onTap function).

How to reveal <app-header condenses reveals fixed> immediately when slide back up without scrolling to top

Referring to the following code:-
<app-header-layout has-scrolling-region>
<app-header condenses reveals fixed effects="waterfall">
<app-toolbar style="height: 48px;">
<paper-icon-button icon="menu" drawer-toggle></paper-icon-button>
<div title>My App</div>
</app-toolbar>
<app-toolbar primary style="height: 48px;">
<paper-tabs attr-for-selected="name" fallback-selection="tab-1" selected={{selected}} style="width: 100%" noink scrollable>
<paper-tab name="tab-1">Tab 1</paper-tab>
<paper-tab name="tab-2">Tab 2</paper-tab>
</paper-tabs>
</app-toolbar>
</app-header>
<iron-pages role="main" selected="[[page]]" attr-for-selected="name">
<my-view1 name="view1"></my-view1>
<my-view2 name="view2"></my-view2>
<my-view3 name="view3"></my-view3>
</iron-pages>
</app-header-layout>
I intend to fixed the paper-tabs, and reveal the first app-toolbar immediately when slide back up. Currently I have to scroll to top for the first app-toolbar to reveal itself.
Does anybody knoow what's the proper way to achieve it? Thanks!
Just want the share that the problem has been fixed in 0.9.1
https://github.com/PolymerElements/app-layout/issues/232

paper-ripple- fill sibling element in paper-toolbar

I would like the paper-ripple effect to fill<div class='title bottom'> also. Any ideas on how to make it do that?
<body unresolved>
<paper-header-panel shadow="true"
mode="waterfall-tall"
class="fit">
<paper-toolbar>
<paper-ripple fit></paper-ripple>
<div class='title'></div>
<paper-tabs noink="true" selected="0">
<!-- only HOME gets route. Others will but down below -->
<paper-tab class="route" onclick="page('/')">
HOME
</paper-tab>
<paper-tab onclick="page('/resume')">
RESUME
</paper-tab>
<paper-tab>
CONTACT
</paper-tab>
</paper-tabs>
<div class='title bottom'>
<h1 id="name-title">Foo<p> bar<p></h1>
</div>
</paper-toolbar>
UPDATE...plunker example condenses paper-header-panel in non waterfall mode:
<paper-header-panel
mode="waterfall-tall"
class="fit">
<div>
<paper-toolbar>
<paper-tabs>
<!-- only HOME gets route. Others will but down below -->
<paper-tab class="route" onclick="page('/')">
HOME
</paper-tab>
</paper-tabs>
</paper-toolbar>
<paper-ripple></paper-ripple>
</div>
</paper-header-panel>
paper-toolbar has three vertically stacked divs. So having a paper-ripple that takes the whole space is not an option here. What you can do is to wrap the paper-toolbar with a parent div and have the paper-ripple sit at the same level as the paper-toolbar.
Note that this parent div needs to have a paper-header class so that the paper-header-panel knows it's the header and will assign proper styling to it. Also, to constraint the paper-ripple, this parent div needs to be relative position.
The last change you need to make is to manually give the paper-toolbar a tall class since it's no longer the paper-header-panel's direct children.
<paper-header-panel mode="waterfall-tall" class="fit">
<div class="paper-header relative">
<paper-toolbar class="tall">
<div class="title"></div>
<paper-tabs noink selected="0">
<paper-tab class="route" onclick="page('/')">HOME</paper-tab>
<paper-tab onclick="page('/resume')">RESUME</paper-tab>
<paper-tab>CONTACT</paper-tab>
</paper-tabs>
<div class="title bottom">
<h1 id="name-title">Foo<p> bar<p></h1>
</div>
</paper-toolbar>
<paper-ripple></paper-ripple>
</div>
</paper-header-panel>
Check out this plunker.

Polymer paper-tab select first tab programmatically

Our paper-tabs are generated dynamically, how can we select the first tab by default and also fire the change event so that the iron page content is visible.
<paper-tabs id="scrollableTabs" selected={{selected}} scrollable >
<template is="dom-repeat" items="[[liveQueues]]" as="queue" >
<paper-tab on-click="listLiveTokens" >[[queue.queueName]]</paper-tab>
</template>
</paper-tabs>
list: function() {
this.loading=!this.loading;
this.$.list.url = "http://apicall";
var tmp = this.$.list.generateRequest();
console.log(tmp)
},
this script fetches the tab content from the api call
<iron-pages selected="{{selected}}">
<template is="dom-repeat" items="[[liveQueues]]" as="queue" >
<paper-material elevation="1">
<iron-list items="[[queueTokens]]" as="token">
<template>
<div>
<div class="item" tabindex="0">
<span class="avatar" >[[token.tokenNumber]]</span>
<a href$="{{_getDetailsLink(token.tokenId,token.tokenNumber,token.userName,token.statusType,token.userMobile,token.joinDate)}}">
<div class="pad">
<div class="primary">[[token.userName]]</div>
<div class="secondary">[[token.userMobile]]</div>
<div class="secondary dim">[[token.notes]]</div>
<div class="secondary dim">[[token.joinTime]]</div>
</div>
</a>
<iron-icon icon$="[[iconForItem(sminq)]]"></iron-icon>
</div>
</div>
</template>
</iron-list>
</paper-material>
</template>
</iron-pages>
An event will fire every time the dom-repeat changes. You could listen to changes and set selected to 0.
<template is="dom-repeat" items="[[liveQueues]]" as="queue" on-dom-change="setSelected">
In your prototype you define the function.
setSelected: function() {
this.$.scrollableTabs.selected = 0;
}
Note that the function is called every time you update liveQueues and you may not want to reset the tabs every time.
The iron pages should update automatically, if you have data-binding to selected.
Well, I think that ready function is the way to initialize !
ready: function() {
this.selected = 0;
}

Polymer 1.0 a/custom element under toolbar clicking bug

I am encountering some problems with a custom element created by me, called <little-game></little-game>.
This is <little-game></little-game> template code :
<template>
<a href="{{link}}">
<paper-material elevation="1">
<paper-ripple></paper-ripple>
<iron-image src="{{img_url}}"></iron-image>
<div id="description">{{name}}</div>
<div id="category">{{category}}</div>
</paper-material>
</a></template>
And the :host css of this element:
:host {
display: inline-block;
text-decoration: none;
z-index:1;
}
Those <little-game></little-game> elements are displayed in a page and inside this page i have a <paper-scroll-header-panel> and a <paper-toolbar>. The problem is when i scroll down and the .tall <paper-toolbar> gets smaller, i can click through the <paper-toolbar> on <little-game>/<paper-ripple> element.
<paper-ripple> css :
paper-ripple {
z-index:1;}
mainToolbar html :
<paper-toolbar id="mainToolbar" class="tall">
<paper-icon-button id="paperToggle" icon="menu" paper-drawer-toggle></paper-icon-button>
<span class="flex"></span>
<!-- Toolbar icons -->
<!--paper-icon-button icon="refresh"></paper-icon-button-->
<paper-icon-button icon="more-vert"></paper-icon-button>
<!-- Application name -->
<div class="middle middle-container center horizontal layout">
<div class="app-name">App title</div>
</div>
<!-- Application sub title -->
<div class="bottom bottom-container center horizontal layout">
<div class="bottom-title paper-font-subhead">App subtitle</div>
</div>
</paper-toolbar>
mainToolbar css :
#mainToolbar {
z-index:3;}
So the main problem is about that i can click the <little-game></little-game> element through the toolbar.
There is an image to understand what i am talking about in a better way:
I think you need to cancel the tap event from propagating through, try adding an on-tap event handler on the paper-toolbar e.g.
<paper-toolbar id="mainToolbar" class="tall" on-tap="{{cancelEvent}}">
then add the function to cancel it
cancelEvent: function(event) {
event.stopPropagation();
}