Skip to content

Instantly share code, notes, and snippets.

@girliemac
Last active September 1, 2016 14:47
Show Gist options
  • Save girliemac/920e46856a9a5decfe0f to your computer and use it in GitHub Desktop.
Save girliemac/920e46856a9a5decfe0f to your computer and use it in GitHub Desktop.
[Blog] Creating a Chat App with Material Design using Polymer
This is a gist for my blog/tutorial, "Creating a Chat App with Material Design using Polymer"
<!-- Import element -->
<link rel="import" href="paper-fab">
...
<!-- Use element -->
<paper-fab icon="send"></paper-fab>
<!DOCTYPE html>
<html>
<head>
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
</head>
<body>
...
<script src="bower_components/webcomponentsjs/webcomponents.min.js"></script>
<link rel="import" href="bower_components/core-scaffold/core-scaffold.html">
<link rel="import" href="bower_components/core-item/core-item.html">
<link rel="import" href="bower_components/paper-input/paper-input.html">
<link rel="import" href="bower_components/paper-fab/paper-fab.html">
<body fullbleed unresolved>
<core-scaffold>
<!-- Drawer panel -->
<core-header-panel navigation flex>
<core-toolbar class="tall">
<!-- an avatar and username will be here -->
</core-toolbar>
</core-header-panel>
<!-- App Title -->
<div tool layout horizontal flex>
<span flex>Kitteh Anonymous</span>
<core-icon icon="account-circle"></core-icon>
<span><!-- number of people online --></span>
</div>
<!-- Main content -->
<div flex>...</div>
</core-scaffold>
</body>
<!-- Main content -->
<div class="send-message" layout horizontal>
<paper-input flex label="Type message..." id="input" value="{{input}}"></paper-input>
<paper-fab icon="send" id="sendButton" on-tap="{{sendMyMessage}}"></paper-fab>
</div>
<body fullbleed unresolved>
<template is="auto-binding">
<core-scaffold>
...
</core-scaffold>
</template>
</body>
var template = document.querySelector('template[is=auto-binding]');
doSomething(template.input);
<template repeat="{{item in items}}">
<core-item icon="{{item.icon}}" label="{{item.title}}"></core-item>
</template>
template.items = [
{icon: 'cloud', title: 'PubNub'},
{icon: 'polymer', title: 'Polymer'},
{icon: 'favorite', title: 'Love'}
];
<polymer-element name="x-chat-list" attributes="avatar color username text">
<template>
<section class="user-list" layout horizontal>
<div class="avatar {{color}}" style="background-image: url({{avatar}})"></div>
<div flex>
<div class="username">{{username}}</div>
<div class="text">{{text}}</div>
</div>
</section>
</template>
<script>
Polymer('x-chat-list', { // set default values
avatar: '',
color: '',
username: '',
text: ''
});
</script>
</polymer-element>
<link rel="import" href="x-chat-list.html">
<div flex class="chat-list">
<template repeat="{{message in messages}}">
<x-chat-list color="{{message.color}}"
avatar="{{message.avatar}}"
username="{{message.uuid}}"
text="{{message.text}}"></x-chat-list>
</template>
</div>
<link rel="import" href="bower_components/pubnub-polymer/pubnub-element.html">
<core-pubnub
publish_key="your_pub_key"
subscribe_key="your_sub_key"
uuid="{{uuid}}"></core-pubnub>
var randomColor = function() {
var colors = ['navy', 'slate', 'olive', 'moss', 'chocolate',...];
return colors[(Math.random() * colors.length) >>> 0];
};
var randomCat = function() { ... };
var color = randomColor();
var cat = randomCat();
template.uuid = color + '-' + cat;
templtae.color = color;
template.avatar = 'images/' + cat + '.jpg';
<core-pubnub publish_key="demo" subscribe_key="demo">
<core-pubnub-publish id="pub" channel="polymer-chat" message="Hello">
</core-pubnub-publish>
</core-pubnub>
<paper-fab icon="send" on-tap="{{sendMyMessage}}"></paper-fab>
template.sendMyMessage = function(e) {
if(!template.input) return; // if the input field is empty, do nothing.
template.$.pub.message = {
uuid: uuid,
avatar: avatarUrl,
color: color,
text: template.input
};
template.$.pub.publish();
};
<core-pubnub-subscribe
channel="polymer-chat"
id="sub"
messages="{{messages}}"
on-callback="{{subscribeCallback}}">
<div flex class="chat-list">
<template repeat="{{message in messages}}">
<x-chat-list
color="{{message.color}}"
avatar="{{message.avatar}}"
username="{{message.uuid}}"
text="{{message.text}}"></x-chat-list>
</template>
</div>
template.subscribeCallback = function(e) {
template.async(function(){
var chatDiv = document.querySelector('.chat-list');
chatDiv.scrollTop = chatDiv.scrollHeight; // scroll to bottom
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment