1<!--
2@license
3Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
4This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7Code distributed by Google as part of the polymer project is also
8subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9-->
10
11<link rel="import" href="../polymer/polymer.html">
12<link rel="import" href="../paper-behaviors/paper-checked-element-behavior.html">
13<link rel="import" href="../paper-styles/default-theme.html">
14<link rel="import" href="../iron-flex-layout/iron-flex-layout.html">
15
16<!--
17Material design: [Radio button](https://www.google.com/design/spec/components/selection-controls.html#selection-controls-radio-button)
18
19`paper-radio-button` is a button that can be either checked or unchecked.
20User can tap the radio button to check or uncheck it.
21
22Use a `<paper-radio-group>` to group a set of radio buttons.  When radio buttons
23are inside a radio group, exactly one radio button in the group can be checked
24at any time.
25
26Example:
27
28    <paper-radio-button></paper-radio-button>
29    <paper-radio-button>Item label</paper-radio-button>
30
31### Styling
32
33The following custom properties and mixins are available for styling:
34
35Custom property | Description | Default
36----------------|-------------|----------
37`--paper-radio-button-unchecked-background-color` | Radio button background color when the input is not checked | `transparent`
38`--paper-radio-button-unchecked-color` | Radio button color when the input is not checked | `--primary-text-color`
39`--paper-radio-button-unchecked-ink-color` | Selected/focus ripple color when the input is not checked | `--primary-text-color`
40`--paper-radio-button-checked-color` | Radio button color when the input is checked | `--primary-color`
41`--paper-radio-button-checked-ink-color` | Selected/focus ripple color when the input is checked | `--primary-color`
42`--paper-radio-button-size` | Size of the radio button | `16px`
43`--paper-radio-button-ink-size` | Size of the ripple | `48px`
44`--paper-radio-button-label-color` | Label color | `--primary-text-color`
45`--paper-radio-button-label-spacing` | Spacing between the label and the button | `10px`
46`--paper-radio-button-radio-container` | A mixin applied to the internal radio container | `{}`
47`--paper-radio-button-label` | A mixin applied to the internal label | `{}`
48`--paper-radio-button-label-checked` | A mixin applied to the internal label when the radio button is checked | `{}`
49
50This element applies the mixin `--paper-font-common-base` but does not import `paper-styles/typography.html`.
51In order to apply the `Roboto` font to this element, make sure you've imported `paper-styles/typography.html`.
52
53@group Paper Elements
54@element paper-radio-button
55@hero hero.svg
56@demo demo/index.html
57-->
58
59<dom-module id="paper-radio-button">
60  <template strip-whitespace>
61    <style>
62      :host {
63        display: inline-block;
64        line-height: 0;
65        white-space: nowrap;
66        cursor: pointer;
67        @apply(--paper-font-common-base);
68        --calculated-paper-radio-button-size: var(--paper-radio-button-size, 16px);
69        /* -1px is a sentinel for the default and is replace in `attached`. */
70        --calculated-paper-radio-button-ink-size: var(--paper-radio-button-ink-size, -1px);
71      }
72
73      :host(:focus) {
74        outline: none;
75      }
76
77      #radioContainer {
78        @apply(--layout-inline);
79        @apply(--layout-center-center);
80        position: relative;
81        width: var(--calculated-paper-radio-button-size);
82        height: var(--calculated-paper-radio-button-size);
83        vertical-align: middle;
84
85        @apply(--paper-radio-button-radio-container);
86      }
87
88      #ink {
89        position: absolute;
90        top: 50%;
91        left: 50%;
92        right: auto;
93        width: var(--calculated-paper-radio-button-ink-size);
94        height: var(--calculated-paper-radio-button-ink-size);
95        color: var(--paper-radio-button-unchecked-ink-color, --primary-text-color);
96        opacity: 0.6;
97        pointer-events: none;
98        -webkit-transform: translate(-50%, -50%);
99        transform: translate(-50%, -50%);
100      }
101
102      #ink[checked] {
103        color: var(--paper-radio-button-checked-ink-color, --primary-color);
104      }
105
106      #offRadio, #onRadio {
107        position: absolute;
108        box-sizing: border-box;
109        top: 0;
110        left: 0;
111        width: 100%;
112        height: 100%;
113        border-radius: 50%;
114      }
115
116      #offRadio {
117        border: 2px solid var(--paper-radio-button-unchecked-color, --primary-text-color);
118        background-color: var(--paper-radio-button-unchecked-background-color, transparent);
119        transition: border-color 0.28s;
120      }
121
122      #onRadio {
123        background-color: var(--paper-radio-button-checked-color, --primary-color);
124        -webkit-transform: scale(0);
125        transform: scale(0);
126        transition: -webkit-transform ease 0.28s;
127        transition: transform ease 0.28s;
128        will-change: transform;
129      }
130
131      :host([checked]) #offRadio {
132        border-color: var(--paper-radio-button-checked-color, --primary-color);
133      }
134
135      :host([checked]) #onRadio {
136        -webkit-transform: scale(0.5);
137        transform: scale(0.5);
138      }
139
140      #radioLabel {
141        line-height: normal;
142        position: relative;
143        display: inline-block;
144        vertical-align: middle;
145        margin-left: var(--paper-radio-button-label-spacing, 10px);
146        white-space: normal;
147        color: var(--paper-radio-button-label-color, --primary-text-color);
148
149        @apply(--paper-radio-button-label);
150      }
151
152      :host([checked]) #radioLabel {
153        @apply(--paper-radio-button-label-checked);
154      }
155
156      :host-context([dir="rtl"]) #radioLabel {
157        margin-left: 0;
158        margin-right: var(--paper-radio-button-label-spacing, 10px);
159      }
160
161      #radioLabel[hidden] {
162        display: none;
163      }
164
165      /* disabled state */
166
167      :host([disabled]) #offRadio {
168        border-color: var(--paper-radio-button-unchecked-color, --primary-text-color);
169        opacity: 0.5;
170      }
171
172      :host([disabled][checked]) #onRadio {
173        background-color: var(--paper-radio-button-unchecked-color, --primary-text-color);
174        opacity: 0.5;
175      }
176
177      :host([disabled]) #radioLabel {
178        /* slightly darker than the button, so that it's readable */
179        opacity: 0.65;
180      }
181    </style>
182
183    <div id="radioContainer">
184      <div id="offRadio"></div>
185      <div id="onRadio"></div>
186    </div>
187
188    <div id="radioLabel"><content></content></div>
189  </template>
190
191  <script>
192    Polymer({
193      is: 'paper-radio-button',
194
195      behaviors: [
196        Polymer.PaperCheckedElementBehavior
197      ],
198
199      hostAttributes: {
200        role: 'radio',
201        'aria-checked': false,
202        tabindex: 0
203      },
204
205      properties: {
206        /**
207         * Fired when the checked state changes due to user interaction.
208         *
209         * @event change
210         */
211
212        /**
213         * Fired when the checked state changes.
214         *
215         * @event iron-change
216         */
217
218        ariaActiveAttribute: {
219          type: String,
220          value: 'aria-checked'
221        }
222      },
223
224      ready: function() {
225        this._rippleContainer = this.$.radioContainer;
226      },
227
228      attached: function() {
229        var inkSize = this.getComputedStyleValue('--calculated-paper-radio-button-ink-size').trim();
230        // If unset, compute and set the default `--paper-radio-button-ink-size`.
231        if (inkSize === '-1px') {
232          var size = parseFloat(this.getComputedStyleValue('--calculated-paper-radio-button-size').trim());
233          var defaultInkSize = Math.floor(3 * size);
234
235          // The button and ripple need to have the same parity so that their
236          // centers align.
237          if (defaultInkSize % 2 !== size % 2) {
238            defaultInkSize++;
239          }
240
241          this.customStyle['--paper-radio-button-ink-size'] = defaultInkSize + 'px';
242          this.updateStyles();
243        }
244      },
245    })
246  </script>
247</dom-module>
248