-
Notifications
You must be signed in to change notification settings - Fork 0
/
option.ts
243 lines (218 loc) · 6.44 KB
/
option.ts
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
import {TemplateResult, html} from 'lit-html';
import {Constructor} from './index';
import {DeepObject} from '../utility/deepObject';
import {SelectiveEditor} from '../selective/editor';
import {classMap} from 'lit-html/directives/class-map.js';
import {ifDefined} from 'lit-html/directives/if-defined.js';
import {repeat} from 'lit-html/directives/repeat.js';
import {styleMap} from 'lit-html/directives/style-map.js';
/**
* Orientation for colors gradient.
*/
export enum ColorsOrientation {
Vertical = 'vertical',
Horizontal = 'horizontal',
Slope = 'slope',
}
export interface ColorsConfig {
colors: Array<string>;
/**
* Use a smooth color gradient?
*/
isSmooth?: boolean;
/**
* Orientation for the color gradient.
*/
orientation?: ColorsOrientation;
}
export interface Option {
/**
* Color for the color hint.
*/
color?: string;
/**
* Gradient color hint.
*/
gradient?: ColorsConfig;
/**
* Value when the option is selected.
*/
value: any;
/**
* Label for the option.
*/
label: string;
}
export interface OptionUiComponent {
templateOption(editor: SelectiveEditor, option: Option): TemplateResult;
}
export interface OptionUIConfig {
handleBlur: (evt: Event) => void;
handleInput: (evt: Event) => void;
isMulti?: boolean;
isOptionSelected: (option: Option) => boolean;
showColorHint?: boolean;
}
export function OptionMixin<TBase extends Constructor>(Base: TBase) {
return class OptionClass extends Base {
ariaLabelForOptionDot(config: OptionUIConfig, option: Option): string {
if (option.color) {
return option.color;
}
if (!option.gradient || !option.gradient.colors.length) {
return option.label;
}
return option.gradient.colors.join(', ');
}
classesForOptions(
config: OptionUIConfig,
options: Array<Option>
): Record<string, boolean> {
return {
selective__options: true,
'selective__options--color-hint': this.hasColorHints(options),
'selective__options--few': options.length > 4,
'selective__options--many': options.length > 11,
'selective__options--multi': config.isMulti || false,
};
}
classesForOption(
config: OptionUIConfig,
option: Option
): Record<string, boolean> {
return {
selective__options__option: true,
'selective__options__option--selected': config.isOptionSelected(option),
'selective__options__option--color-hint': Boolean(option.color),
'selective__options__option--color-hint-gradient': Boolean(
option.gradient
),
};
}
/**
* Are there color hints?
*/
hasColorHints(options: Array<Option>): boolean {
for (const option of options) {
if (option.color || option.gradient) {
return true;
}
}
return false;
}
stylesForOptionDot(
config: OptionUIConfig,
option: Option
): Record<string, string> {
if (option.color) {
return {
backgroundColor: option.color,
};
}
if (!option.gradient || !option.gradient.colors.length) {
return {};
}
const gradient = option.gradient;
let orientationAngle = '0deg';
if (gradient.orientation === ColorsOrientation.Horizontal) {
orientationAngle = '90deg';
} else if (gradient.orientation === ColorsOrientation.Slope) {
orientationAngle = '45deg';
}
const isSmooth = gradient.isSmooth || false;
const breakpoints: Array<string> = [`${gradient.colors[0]} 0%`];
const numBreakpoints = isSmooth
? gradient.colors.length - 1
: gradient.colors.length;
let breakpoint = Math.floor(100 / numBreakpoints);
let lastColor = null;
for (const color of gradient.colors) {
if (!lastColor) {
lastColor = color;
continue;
}
if (!isSmooth) {
breakpoints.push(`${lastColor} ${breakpoint}%`);
}
breakpoints.push(`${color} ${breakpoint}%`);
breakpoint += breakpoint;
lastColor = color;
}
if (!isSmooth) {
breakpoints.push(`${gradient.colors[gradient.colors.length - 1]} 100%`);
}
return {
backgroundImage: `linear-gradient(${orientationAngle}, ${breakpoints.join(
', '
)})`,
};
}
templateColorSwatch(
editor: SelectiveEditor,
data: DeepObject,
config: OptionUIConfig,
option: Option
): TemplateResult {
if (!option.color && !option.gradient) {
return html``;
}
return html`<div
class="selective__swatch"
aria-label=${this.ariaLabelForOptionDot(config, option)}
style=${styleMap(this.stylesForOptionDot(config, option))}
></div>`;
}
templateOption(
editor: SelectiveEditor,
data: DeepObject,
config: OptionUIConfig,
option: Option
): TemplateResult {
let icon = '';
if (config.isOptionSelected(option)) {
icon = config.isMulti ? 'check_box' : 'radio_button_checked';
} else {
icon = config.isMulti
? 'check_box_outline_blank'
: 'radio_button_unchecked';
}
return html`<div
class=${classMap(this.classesForOption(config, option))}
aria-checked=${config.isOptionSelected(option)}
data-value=${option.value}
tabindex="0"
role=${config.isMulti ? 'checkbox' : 'radio'}
@blur=${config.handleBlur}
@click=${config.handleInput}
@keypress=${(evt: KeyboardEvent) => {
if (evt.code === 'Space') {
evt.preventDefault();
config.handleInput(evt);
}
}}
>
<span class="material-icons">${icon}</span>
${this.templateColorSwatch(editor, data, config, option)}
<label>${option.label || option.value || '(Empty)'}</label>
</div>`;
}
templateOptions(
editor: SelectiveEditor,
data: DeepObject,
config: OptionUIConfig,
options: Array<Option>
): TemplateResult {
// TODO: Convert to a different UI when there are a lot of options.
return html`<div
class=${classMap(this.classesForOptions(config, options))}
role=${ifDefined(config.isMulti ? undefined : 'radiogroup')}
>
${repeat(
options,
option => option.value,
option => this.templateOption(editor, data, config, option)
)}
</div>`;
}
};
}