-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathIconButton.js
162 lines (135 loc) · 4.06 KB
/
IconButton.js
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
import { ELEMENT_STYLE_TYPE } from '../core/customTypes.js';
import TooltipTriggerMixin from '../mixins/TooltipTriggerMixin.js';
import Button from './Button.js';
/* https://m3.material.io/components/icon-buttons/specs */
export default Button
.extend()
.mixin(TooltipTriggerMixin)
.set({
_allowedTypes: ['button', 'submit', 'reset', 'checkbox', 'file'],
})
.observe({
_ariaPressed: {
get({ type, checked, indeterminate }) {
if (type !== 'checkbox') return null;
return indeterminate ? 'mixed' : (checked ? 'true' : 'false');
},
},
_isToggle({ type }) {
return type === 'checkbox';
},
autoTooltip: { empty: true },
})
.expressions({
iconVariation({ checked, outlined, _isToggle }) {
if (!checked && (_isToggle || outlined)) return null;
return 'filled';
},
})
.childEvents({
control: {
keydown(event) {
if (event.key !== 'Enter' && event.key !== ' ') return;
const input = /** @type {HTMLInputElement} */ (event.currentTarget);
if (input.type !== 'checkbox') return;
event.stopImmediatePropagation();
event.stopPropagation();
event.preventDefault();
if (input.disabled) return;
input.click();
},
},
})
.recompose(({ refs: { icon, control, outline } }) => {
icon.removeAttribute('mdw-if');
control.setAttribute('aria-pressed', '{_ariaPressed}');
outline.setAttribute('toggle', '{_isToggle}');
outline.setAttribute('selected', '{checked}');
})
.observe({
_styles: {
...ELEMENT_STYLE_TYPE,
get({ checked, disabled, filled, outlined, _isToggle }) {
if (!_isToggle) return null;
if (disabled) return null;
if (outlined) {
if (checked) {
return {
backgroundColor: 'rgb(var(--mdw-bg))',
};
}
return {
color: 'rgb(var(--mdw-color__on-surface-variant))',
};
}
if (checked) return null; // All else uses checked state
if (filled == null) return null;
return {
backgroundColor: 'rgb(var(--mdw-color__surface-container-highest))',
color: filled === 'tonal'
? 'rgb(var(--mdw-color__on-surface-variant))'
: 'rgb(var(--mdw-bg))',
};
},
},
})
.css`
:host {
--mdw-shape__size: var(--mdw-shape__full);
--mdw-ink: rgb(var(--mdw-color__on-surface-variant));
align-items: center;
justify-content: center;
min-block-size: 1em;
min-inline-size: 1em;
padding: max(8px, calc(8px + (var(--mdw-density) * 2px)));
font-size: 24px;
transition-property: background-color, box-shadow;
}
:host(:where([type="checkbox"])) {
--mdw-ink: var(--mdw-color__primary);
}
/** Filled | Filled Checked */
:host(:where([filled])) {
--mdw-ink: var(--mdw-color__on-primary);
--mdw-bg: var(--mdw-color__primary);
}
/** Tonal | Tonal Checked */
:host(:where([filled="tonal"])) {
--mdw-ink: var(--mdw-color__on-secondary-container);
--mdw-bg: var(--mdw-color__secondary-container);
}
/** Outlined | Outlined Unchecked */
:host(:where([outlined])) {
--mdw-ink: var(--mdw-color__on-surface-variant);
background-color: transparent;
}
/** Outlined | Outlined Unchecked */
:host(:where([outlined][type="checkbox"])) {
--mdw-bg: var(--mdw-color__inverse-surface);
--mdw-ink: var(--mdw-color__inverse-on-surface);
}
#slot { display: none; }
#icon {
pointer-events: none;
font-size: inherit;
/* stylelint-disable-next-line liberty/use-logical-spec */
transition-property: color, inline-size, width;
}
#state {
z-index:1;
}
#ripple-container {
z-index:2;
}
#outline[focused] {
opacity: 1;
color: inherit;
}
#outline[focused][toggle] {
color: rgb(var(--mdw-color__outline));
}
#outline[selected]:not([disabled]) {
opacity: 0;
}
`
.autoRegister('mdw-icon-button');