forked from googlearchive/paper-button
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-button.html
211 lines (171 loc) · 5.84 KB
/
paper-button.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
<!--
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<!--
@group Paper Elements
`paper-button` is a button containing text or an image. When the user touches
the button, a ripple effect emanates from the point of contact.
A `paper-button` may be flat or raised. A raised button behaves like a piece
of paper resting on another sheet, and lifts up upon press. Flat buttons do
not raise up. Add the `raisedButton` attribute to make a raised button.
Example:
<paper-button label="flat button"></paper-button>
<paper-button label="raised button" raisedButton></paper-button>
A button should be styled with a background color, text color, ripple color
and hover color.
To style the background, text and hover color, apply the `background` and
`color` CSS properties to the button. To style the ripple color, apply the
`color` CSS property to the `#ripple` element in the button's shadow root:
/* Style #my-button blue with white text and darker blue ink. */
#my-button {
background: #4285f4;
color: #fff;
}
#my-button:hover {
background: #2a56c6;
}
#my-button::shadow #ripple {
color: #2a56c6;
}
@element paper-button
@extends paper-focusable
-->
<link href="../polymer/polymer.html" rel="import">
<link href="../core-icon/core-icon.html" rel="import">
<link href="../paper-focusable/paper-focusable.html" rel="import">
<link href="../paper-ripple/paper-ripple.html" rel="import">
<link href="../paper-shadow/paper-shadow.html" rel="import">
<polymer-element name="paper-button" extends="paper-focusable" attributes="label raisedButton iconSrc icon"
role="button">
<template>
<link href="paper-button.css" rel="stylesheet">
<template if="{{raisedButton}}">
<div fit id="shadow-container">
<paper-shadow id="shadow" z="{{z}}" animated></paper-shadow>
</div>
</template>
<div id="clip">
<!-- <div id="focusBg"></div> -->
<paper-ripple id="ripple"></paper-ripple>
<div id="content">
<template if="{{iconSrc || icon}}">
<core-icon id="icon" src="{{iconSrc}}" icon="{{icon}}"></core-icon>
</template>
<template if="{{label}}">
<span>{{label}}</span>
</template>
</div>
</div>
</template>
<script>
Polymer('paper-button', {
publish: {
/**
* The label of the button.
*
* @attribute label
* @type string
* @default ''
*/
label: '',
/**
* If true, the button will be styled as a "raised" button.
*
* @attribute raisedButton
* @type boolean
* @default false
*/
raisedButton: {value: false, reflect: true},
/**
* (optional) The URL of an image for an icon to use in the button.
* Should not use `icon` property if you are using this property.
*
* @attribute iconSrc
* @type string
* @default ''
*/
iconSrc: '',
/**
* (optional) Specifies the icon name or index in the set of icons
* available in the icon set. If using this property, load the icon
* set separately where the icon is used. Should not use `src`
* if you are using this property.
*
* @attribute icon
* @type string
* @default ''
*/
icon: ''
},
z: 1,
attached: function() {
if (this.textContent && !this.textContent.match(/\s+/)) {
console.warn('Using textContent to label the button is deprecated. Use the "label" property instead');
this.label = this.textContent;
}
},
activeChanged: function() {
this.super();
if (this.active) {
// FIXME: remove when paper-ripple can have a default 'down' state.
if (!this.lastEvent) {
var rect = this.getBoundingClientRect();
this.lastEvent = {
x: rect.left + rect.width / 2,
y: rect.top + rect.height / 2
}
}
this.$.ripple.downAction(this.lastEvent);
} else {
this.$.ripple.upAction();
}
this.adjustZ();
},
focusedChanged: function() {
this.super();
this.adjustZ();
},
disabledChanged: function() {
this.super();
this.adjustZ();
},
// waitForSpillCompleted: function(callback) {
// this.async(callback, null, (this.$.ink.spillCompleted ? 0 : this.duration));
// },
// resetInk: function() {
// this.active = false;
// this.$.ink.reset();
// },
insideButton: function(x, y) {
var rect = this.getBoundingClientRect();
return (rect.left <= x) && (x <= rect.right) && (rect.top <= y) && (y <= rect.bottom);
},
adjustZ: function() {
if (this.focused) {
this.classList.add('paper-shadow-animate-z-1-z-2');
} else {
this.classList.remove('paper-shadow-animate-z-1-z-2');
if (this.active) {
this.z = 2;
} else if (this.disabled) {
this.z = 0;
} else {
this.z = 1;
}
}
},
downAction: function(e) {
this.super(e);
this.lastEvent = e;
},
labelChanged: function() {
this.setAttribute('aria-label', this.label);
}
});
</script>
</polymer-element>