3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7 Code distributed by Google as part of the polymer project is also
8 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
11 <link rel=
"import" href=
"../polymer/polymer.html">
12 <link rel=
"import" href=
"../iron-a11y-keys-behavior/iron-a11y-keys-behavior.html">
13 <link rel=
"import" href=
"iron-control-state.html">
18 * @demo demo/index.html
19 * @polymerBehavior Polymer.IronButtonState
21 Polymer
.IronButtonStateImpl
= {
26 * If true, the user is currently holding down the button.
32 reflectToAttribute
: true,
33 observer
: '_pressedChanged'
37 * If true, the button toggles the active state with each tap or press
43 reflectToAttribute
: true
47 * If true, the button is a toggle and is currently in the active state.
53 reflectToAttribute
: true,
54 observer
: '_activeChanged'
58 * True if the element is currently being pressed by a "pointer," which
59 * is loosely defined as mouse or touch input (but specifically excluding
69 * True if the input device that caused the element to receive focus
72 receivedFocusFromKeyboard
: {
85 '_detectKeyboardFocus(focused)'
89 'enter:keydown': '_asyncClick',
90 'space:keydown': '_spaceKeyDownHandler',
91 'space:keyup': '_spaceKeyUpHandler',
94 _tapHandler: function() {
96 // a tap is needed to toggle the active state
97 this._userActivate(!this.active
);
103 _detectKeyboardFocus: function(focused
) {
104 this._setReceivedFocusFromKeyboard(!this.pointerDown
&& focused
);
107 // to emulate native checkbox, (de-)activations from a user interaction fire
109 _userActivate: function(active
) {
110 this.active
= active
;
114 _downHandler: function() {
115 this._setPointerDown(true);
116 this._setPressed(true);
117 this._setReceivedFocusFromKeyboard(false);
120 _upHandler: function() {
121 this._setPointerDown(false);
122 this._setPressed(false);
125 _spaceKeyDownHandler: function(event
) {
126 var keyboardEvent
= event
.detail
.keyboardEvent
;
127 keyboardEvent
.preventDefault();
128 keyboardEvent
.stopImmediatePropagation();
129 this._setPressed(true);
132 _spaceKeyUpHandler: function() {
136 this._setPressed(false);
139 // trigger click asynchronously, the asynchrony is useful to allow one
140 // event handler to unwind before triggering another event
141 _asyncClick: function() {
142 this.async(function() {
147 // any of these changes are considered a change to button state
149 _pressedChanged: function(pressed
) {
150 this._changedButtonState();
153 _activeChanged: function(active
) {
155 this.setAttribute('aria-pressed', active
? 'true' : 'false');
157 this.removeAttribute('aria-pressed');
159 this._changedButtonState();
162 _controlStateChanged: function() {
164 this._setPressed(false);
166 this._changedButtonState();
170 // provide hook for follow-on behaviors to react to button-state
172 _changedButtonState: function() {
173 if (this._buttonStateChanged
) {
174 this._buttonStateChanged(); // abstract
180 /** @polymerBehavior */
181 Polymer
.IronButtonState
= [
182 Polymer
.IronA11yKeysBehavior
,
183 Polymer
.IronButtonStateImpl