/
col.tsx
268 lines (224 loc) · 8.13 KB
/
col.tsx
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { Component, ComponentInterface, Element, Listen, Prop } from '@stencil/core';
import { Mode } from '../../interface';
import { matchBreakpoint } from '../../utils/media';
const win = window as any;
const SUPPORTS_VARS = !!(win.CSS && win.CSS.supports && win.CSS.supports('--a: 0'));
const BREAKPOINTS = ['', 'xs', 'sm', 'md', 'lg', 'xl'];
@Component({
tag: 'ion-col',
styleUrl: 'col.scss',
shadow: true
})
export class Col implements ComponentInterface {
mode!: Mode;
@Prop({ context: 'window' }) win!: Window;
@Element() el!: HTMLStencilElement;
/**
* The amount to offset the column, in terms of how many columns it should shift to the end
* of the total available.
*/
@Prop() offset?: string;
/**
* The amount to offset the column for xs screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() offsetXs?: string;
/**
* The amount to offset the column for sm screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() offsetSm?: string;
/**
* The amount to offset the column for md screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() offsetMd?: string;
/**
* The amount to offset the column for lg screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() offsetLg?: string;
/**
* The amount to offset the column for xl screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() offsetXl?: string;
/**
* The amount to pull the column, in terms of how many columns it should shift to the start of
* the total available.
*/
@Prop() pull?: string;
/**
* The amount to pull the column for xs screens, in terms of how many columns it should shift
* to the start of the total available.
*/
@Prop() pullXs?: string;
/**
* The amount to pull the column for sm screens, in terms of how many columns it should shift
* to the start of the total available.
*/
@Prop() pullSm?: string;
/**
* The amount to pull the column for md screens, in terms of how many columns it should shift
* to the start of the total available.
*/
@Prop() pullMd?: string;
/**
* The amount to pull the column for lg screens, in terms of how many columns it should shift
* to the start of the total available.
*/
@Prop() pullLg?: string;
/**
* The amount to pull the column for xl screens, in terms of how many columns it should shift
* to the start of the total available.
*/
@Prop() pullXl?: string;
/**
* The amount to push the column, in terms of how many columns it should shift to the end
* of the total available.
*/
@Prop() push?: string;
/**
* The amount to push the column for xs screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() pushXs?: string;
/**
* The amount to push the column for sm screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() pushSm?: string;
/**
* The amount to push the column for md screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() pushMd?: string;
/**
* The amount to push the column for lg screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() pushLg?: string;
/**
* The amount to push the column for xl screens, in terms of how many columns it should shift
* to the end of the total available.
*/
@Prop() pushXl?: string;
/**
* The size of the column, in terms of how many columns it should take up out of the total
* available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() size?: string;
/**
* The size of the column for xs screens, in terms of how many columns it should take up out
* of the total available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() sizeXs?: string;
/**
* The size of the column for sm screens, in terms of how many columns it should take up out
* of the total available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() sizeSm?: string;
/**
* The size of the column for md screens, in terms of how many columns it should take up out
* of the total available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() sizeMd?: string;
/**
* The size of the column for lg screens, in terms of how many columns it should take up out
* of the total available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() sizeLg?: string;
/**
* The size of the column for xl screens, in terms of how many columns it should take up out
* of the total available. If `"auto"` is passed, the column will be the size of its content.
*/
@Prop() sizeXl?: string;
@Listen('window:resize')
onResize() {
this.el.forceUpdate();
}
// Loop through all of the breakpoints to see if the media query
// matches and grab the column value from the relevant prop if so
private getColumns(property: string) {
let matched;
for (const breakpoint of BREAKPOINTS) {
const matches = matchBreakpoint(this.win, breakpoint);
// Grab the value of the property, if it exists and our
// media query matches we return the value
const columns = (this as any)[property + breakpoint.charAt(0).toUpperCase() + breakpoint.slice(1)];
if (matches && columns !== undefined) {
matched = columns;
}
}
// Return the last matched columns since the breakpoints
// increase in size and we want to return the largest match
return matched;
}
private calculateSize() {
const columns = this.getColumns('size');
// If size wasn't set for any breakpoint
// or if the user set the size without a value
// it means we need to stick with the default and return
// e.g. <ion-col size-md>
if (!columns || columns === '') {
return;
}
// If the size is set to auto then don't calculate a size
const colSize = (columns === 'auto')
? 'auto'
// If CSS supports variables we should use the grid columns var
: SUPPORTS_VARS ? `calc(calc(${columns} / var(--ion-grid-columns, 12)) * 100%)`
// Convert the columns to a percentage by dividing by the total number
// of columns (12) and then multiplying by 100
: ((columns / 12) * 100) + '%';
return {
'flex': `0 0 ${colSize}`,
'width': `${colSize}`,
'max-width': `${colSize}`
};
}
// Called by push, pull, and offset since they use the same calculations
private calculatePosition(property: string, modifier: string) {
const columns = this.getColumns(property);
if (!columns) {
return;
}
// If the number of columns passed are greater than 0 and less than
// 12 we can position the column, else default to auto
const amount = SUPPORTS_VARS
// If CSS supports variables we should use the grid columns var
? `calc(calc(${columns} / var(--ion-grid-columns, 12)) * 100%)`
// Convert the columns to a percentage by dividing by the total number
// of columns (12) and then multiplying by 100
: (columns > 0 && columns < 12) ? (columns / 12 * 100) + '%' : 'auto';
return {
[modifier]: amount
};
}
private calculateOffset(isRTL: boolean) {
return this.calculatePosition('offset', isRTL ? 'margin-right' : 'margin-left');
}
private calculatePull(isRTL: boolean) {
return this.calculatePosition('pull', isRTL ? 'left' : 'right');
}
private calculatePush(isRTL: boolean) {
return this.calculatePosition('push', isRTL ? 'right' : 'left');
}
hostData() {
const isRTL = this.win.document.dir === 'rtl';
return {
class: {
[`${this.mode}`]: true
},
style: {
...this.calculateOffset(isRTL),
...this.calculatePull(isRTL),
...this.calculatePush(isRTL),
...this.calculateSize(),
}
};
}
render() {
return <slot></slot>;
}
}