forked from PolymerElements/paper-checkbox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
paper-checkbox.html
161 lines (130 loc) · 4.66 KB
/
paper-checkbox.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
<!--
@license
Copyright (c) 2015 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
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-ripple/paper-ripple.html">
<link rel="import" href="../paper-styles/default-theme.html">
<link rel="import" href="../paper-behaviors/paper-radio-button-behavior.html">
<!--
`paper-checkbox` is a button that can be either checked or unchecked. User
can tap the checkbox to check or uncheck it. Usually you use checkboxes
to allow user to select multiple options from a set. If you have a single
ON/OFF option, avoid using a single checkbox and use `paper-toggle-button`
instead.
Example:
<paper-checkbox>label</paper-checkbox>
<paper-checkbox checked> label</paper-checkbox>
### Styling
The following custom properties and mixins are available for styling:
Custom property | Description | Default
----------------|-------------|----------
`--paper-checkbox-unchecked-color` | Checkbox color when the input is not checked | `--primary-text-color`
`--paper-checkbox-unchecked-ink-color` | Selected/focus ripple color when the input is not checked | `--primary-text-color`
`--paper-checkbox-checked-color` | Checkbox color when the input is checked | `--default-primary-color`
`--paper-checkbox-checked-ink-color` | Selected/focus ripple color when the input is checked | `--default-primary-color`
`--paper-checkbox-label-color` | Label color | `--primary-text-color`
@demo demo/index.html
-->
<style is="custom-style">
:root {
--paper-checkbox-unchecked-color: var(--primary-text-color);
--paper-checkbox-unchecked-ink-color: var(--primary-text-color);
--paper-checkbox-checked-color: var(--default-primary-color);
--paper-checkbox-checked-ink-color: var(--default-primary-color);
--paper-checkbox-label-color: var(--primary-text-color);
}
</style>
<dom-module id="paper-checkbox">
<link rel="import" type="css" href="paper-checkbox.css">
<template>
<div id="checkboxContainer">
<paper-ripple id="ink" class="circle" center checked$="[[checked]]"></paper-ripple>
<div id="checkbox" class$="[[_computeCheckboxClass(checked)]]">
<div id="checkmark" class$="[[_computeCheckmarkClass(checked)]]"></div>
</div>
</div>
<div id="checkboxLabel" aria-hidden="true"><content></content></div>
</template>
<script>
Polymer({
is: 'paper-checkbox',
behaviors: [
Polymer.PaperRadioButtonBehavior
],
hostAttributes: {
role: 'checkbox',
'aria-checked': false,
tabindex: 0
},
properties: {
/**
* Fired when the checked state changes due to user interaction.
*
* @event change
*/
/**
* Fired when the checked state changes.
*
* @event iron-change
*/
/**
* Gets or sets the state, `true` is checked and `false` is unchecked.
*/
checked: {
type: Boolean,
value: false,
reflectToAttribute: true,
notify: true,
observer: '_checkedChanged'
},
/**
* If true, the button toggles the active state with each tap or press
* of the spacebar.
*/
toggles: {
type: Boolean,
value: true,
reflectToAttribute: true
}
},
ready: function() {
if (Polymer.dom(this).textContent == '') {
this.$.checkboxLabel.hidden = true;
} else {
this.setAttribute('aria-label', Polymer.dom(this).textContent);
}
this._isReady = true;
},
// button-behavior hook
_buttonStateChanged: function() {
if (this.disabled) {
return;
}
if (this._isReady) {
this.checked = this.active;
}
},
_checkedChanged: function(checked) {
this.setAttribute('aria-checked', this.checked ? 'true' : 'false');
this.active = this.checked;
this.fire('iron-change');
},
_computeCheckboxClass: function(checked) {
if (checked) {
return 'checked';
}
},
_computeCheckmarkClass: function(checked) {
if (!checked) {
return 'hidden';
}
}
})
</script>
</dom-module>