4 * @demo demo/index.html
5 * @polymerBehavior Polymer.IronButtonState
7 Polymer.IronButtonStateImpl = {
12 * If true, the user is currently holding down the button.
18 reflectToAttribute: true,
19 observer: '_pressedChanged'
23 * If true, the button toggles the active state with each tap or press
29 reflectToAttribute: true
33 * If true, the button is a toggle and is currently in the active state.
39 reflectToAttribute: true,
40 observer: '_activeChanged'
44 * True if the element is currently being pressed by a "pointer," which
45 * is loosely defined as mouse or touch input (but specifically excluding
55 * True if the input device that caused the element to receive focus
58 receivedFocusFromKeyboard: {
71 '_detectKeyboardFocus(focused)'
75 'enter:keydown': '_asyncClick',
76 'space:keydown': '_spaceKeyDownHandler',
77 'space:keyup': '_spaceKeyUpHandler',
80 _tapHandler: function() {
82 // a tap is needed to toggle the active state
83 this._userActivate(!this.active);
89 _detectKeyboardFocus: function(focused) {
90 this._setReceivedFocusFromKeyboard(!this.pointerDown && focused);
93 // to emulate native checkbox, (de-)activations from a user interaction fire
95 _userActivate: function(active) {
100 _downHandler: function() {
101 this._setPointerDown(true);
102 this._setPressed(true);
103 this._setReceivedFocusFromKeyboard(false);
106 _upHandler: function() {
107 this._setPointerDown(false);
108 this._setPressed(false);
111 _spaceKeyDownHandler: function(event) {
112 var keyboardEvent = event.detail.keyboardEvent;
113 keyboardEvent.preventDefault();
114 keyboardEvent.stopImmediatePropagation();
115 this._setPressed(true);
118 _spaceKeyUpHandler: function() {
122 this._setPressed(false);
125 // trigger click asynchronously, the asynchrony is useful to allow one
126 // event handler to unwind before triggering another event
127 _asyncClick: function() {
128 this.async(function() {
133 // any of these changes are considered a change to button state
135 _pressedChanged: function(pressed) {
136 this._changedButtonState();
139 _activeChanged: function(active) {
141 this.setAttribute('aria-pressed', active ? 'true' : 'false');
143 this.removeAttribute('aria-pressed');
145 this._changedButtonState();
148 _controlStateChanged: function() {
150 this._setPressed(false);
152 this._changedButtonState();
156 // provide hook for follow-on behaviors to react to button-state
158 _changedButtonState: function() {
159 if (this._buttonStateChanged) {
160 this._buttonStateChanged(); // abstract
166 /** @polymerBehavior */
167 Polymer.IronButtonState = [
168 Polymer.IronA11yKeysBehavior,
169 Polymer.IronButtonStateImpl