@khanacademy/wonder-blocks-grid 1.0.23 → 1.0.27

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/es/index.js CHANGED
@@ -1,167 +1,20 @@
1
- import { createElement, Component, Children } from 'react';
1
+ import { Component, createElement, Children } from 'react';
2
2
  import { MediaLayout, queryMatchesSize, Strut } from '@khanacademy/wonder-blocks-layout';
3
3
  import { View } from '@khanacademy/wonder-blocks-core';
4
4
  import { StyleSheet } from 'aphrodite';
5
5
 
6
- function _classCallCheck(instance, Constructor) {
7
- if (!(instance instanceof Constructor)) {
8
- throw new TypeError("Cannot call a class as a function");
9
- }
10
- }
11
-
12
- function _defineProperties(target, props) {
13
- for (var i = 0; i < props.length; i++) {
14
- var descriptor = props[i];
15
- descriptor.enumerable = descriptor.enumerable || false;
16
- descriptor.configurable = true;
17
- if ("value" in descriptor) descriptor.writable = true;
18
- Object.defineProperty(target, descriptor.key, descriptor);
19
- }
20
- }
21
-
22
- function _createClass(Constructor, protoProps, staticProps) {
23
- if (protoProps) _defineProperties(Constructor.prototype, protoProps);
24
- if (staticProps) _defineProperties(Constructor, staticProps);
25
- return Constructor;
26
- }
27
-
28
- function _defineProperty(obj, key, value) {
29
- if (key in obj) {
30
- Object.defineProperty(obj, key, {
31
- value: value,
32
- enumerable: true,
33
- configurable: true,
34
- writable: true
35
- });
36
- } else {
37
- obj[key] = value;
38
- }
39
-
40
- return obj;
41
- }
42
-
43
- function _inherits(subClass, superClass) {
44
- if (typeof superClass !== "function" && superClass !== null) {
45
- throw new TypeError("Super expression must either be null or a function");
46
- }
47
-
48
- subClass.prototype = Object.create(superClass && superClass.prototype, {
49
- constructor: {
50
- value: subClass,
51
- writable: true,
52
- configurable: true
53
- }
54
- });
55
- if (superClass) _setPrototypeOf(subClass, superClass);
56
- }
57
-
58
- function _getPrototypeOf(o) {
59
- _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) {
60
- return o.__proto__ || Object.getPrototypeOf(o);
61
- };
62
- return _getPrototypeOf(o);
63
- }
64
-
65
- function _setPrototypeOf(o, p) {
66
- _setPrototypeOf = Object.setPrototypeOf || function _setPrototypeOf(o, p) {
67
- o.__proto__ = p;
68
- return o;
69
- };
70
-
71
- return _setPrototypeOf(o, p);
72
- }
73
-
74
- function _isNativeReflectConstruct() {
75
- if (typeof Reflect === "undefined" || !Reflect.construct) return false;
76
- if (Reflect.construct.sham) return false;
77
- if (typeof Proxy === "function") return true;
78
-
79
- try {
80
- Date.prototype.toString.call(Reflect.construct(Date, [], function () {}));
81
- return true;
82
- } catch (e) {
83
- return false;
84
- }
85
- }
86
-
87
- function _assertThisInitialized(self) {
88
- if (self === void 0) {
89
- throw new ReferenceError("this hasn't been initialised - super() hasn't been called");
90
- }
91
-
92
- return self;
93
- }
94
-
95
- function _possibleConstructorReturn(self, call) {
96
- if (call && (typeof call === "object" || typeof call === "function")) {
97
- return call;
98
- }
99
-
100
- return _assertThisInitialized(self);
101
- }
102
-
103
- function _createSuper(Derived) {
104
- var hasNativeReflectConstruct = _isNativeReflectConstruct();
105
-
106
- return function _createSuperInternal() {
107
- var Super = _getPrototypeOf(Derived),
108
- result;
109
-
110
- if (hasNativeReflectConstruct) {
111
- var NewTarget = _getPrototypeOf(this).constructor;
112
-
113
- result = Reflect.construct(Super, arguments, NewTarget);
114
- } else {
115
- result = Super.apply(this, arguments);
116
- }
117
-
118
- return _possibleConstructorReturn(this, result);
119
- };
120
- }
121
-
122
- function _toConsumableArray(arr) {
123
- return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread();
124
- }
125
-
126
- function _arrayWithoutHoles(arr) {
127
- if (Array.isArray(arr)) return _arrayLikeToArray(arr);
128
- }
129
-
130
- function _iterableToArray(iter) {
131
- if (typeof Symbol !== "undefined" && Symbol.iterator in Object(iter)) return Array.from(iter);
132
- }
133
-
134
- function _unsupportedIterableToArray(o, minLen) {
135
- if (!o) return;
136
- if (typeof o === "string") return _arrayLikeToArray(o, minLen);
137
- var n = Object.prototype.toString.call(o).slice(8, -1);
138
- if (n === "Object" && o.constructor) n = o.constructor.name;
139
- if (n === "Map" || n === "Set") return Array.from(o);
140
- if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen);
141
- }
142
-
143
- function _arrayLikeToArray(arr, len) {
144
- if (len == null || len > arr.length) len = arr.length;
145
-
146
- for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i];
147
-
148
- return arr2;
149
- }
150
-
151
- function _nonIterableSpread() {
152
- throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.");
153
- }
154
-
155
- var WIDE_SCREEN = "@media (min-width: 1168px)";
156
- var styles = StyleSheet.create({
6
+ const WIDE_SCREEN = "@media (min-width: 1168px)";
7
+ const styles = StyleSheet.create({
157
8
  row: {
158
9
  flexDirection: "row",
159
10
  alignItems: "center",
160
11
  width: "100%"
161
12
  },
162
- rowMaxWidth: _defineProperty({}, WIDE_SCREEN, {
163
- margin: "0 auto"
164
- }),
13
+ rowMaxWidth: {
14
+ [WIDE_SCREEN]: {
15
+ margin: "0 auto"
16
+ }
17
+ },
165
18
  cellGrow: {
166
19
  flexGrow: 1
167
20
  },
@@ -170,7 +23,7 @@ var styles = StyleSheet.create({
170
23
  }
171
24
  });
172
25
 
173
- var flexBasis = function flexBasis(size) {
26
+ const flexBasis = size => {
174
27
  return {
175
28
  MsFlexBasis: size,
176
29
  MsFlexPreferredSize: size,
@@ -195,126 +48,109 @@ var flexBasis = function flexBasis(size) {
195
48
  * `cols` props then the component will only be shown at those grid sizes and
196
49
  * using the specified column width.
197
50
  */
198
- var Cell = /*#__PURE__*/function (_React$Component) {
199
- _inherits(Cell, _React$Component);
200
-
201
- var _super = _createSuper(Cell);
202
-
203
- function Cell() {
204
- _classCallCheck(this, Cell);
205
-
206
- return _super.apply(this, arguments);
51
+ class Cell extends Component {
52
+ static isClassOf(instance) {
53
+ return instance && instance.type && instance.type.__IS_CELL__;
207
54
  }
208
55
 
209
- _createClass(Cell, [{
210
- key: "render",
211
- value: function render() {
212
- var _this = this;
213
-
214
- var _this$props = this.props,
215
- children = _this$props.children,
216
- style = _this$props.style;
217
- return /*#__PURE__*/createElement(MediaLayout, null, function (_ref) {
218
- var mediaSize = _ref.mediaSize,
219
- mediaSpec = _ref.mediaSpec;
220
- var spec = mediaSpec[mediaSize];
221
-
222
- if (!spec) {
223
- throw new Error("mediaSpec.".concat(mediaSize, " is undefined"));
224
- } // Get the settings for this particular size of grid
225
-
226
-
227
- var totalColumns = spec.totalColumns,
228
- gutterWidth = spec.gutterWidth,
229
- marginWidth = spec.marginWidth;
230
- var cols = Cell.getCols(_this.props, mediaSize); // If no columns are specified then we just don't render this cell
231
-
232
- if (cols === undefined || cols === null || cols === 0) {
233
- return null;
234
- }
235
-
236
- if (cols > totalColumns) {
237
- throw new Error("Specified columns ".concat(cols, " is greater than the maximum ") + "".concat(totalColumns, " at the ").concat(mediaSize, " grid size."));
238
- } // We need to start by calculating the total width of all the "content"
239
- // We do this by starting with the full width (100%) and then
240
- // subtracting all of the gutter spaces inbetween the cells
241
- // (gutterWidth * (totalColumns - 1)) and the width of the two margins
242
- // (marginWidth * 2).
243
-
244
-
245
- var contentWidth = "(100% - ".concat(gutterWidth * (totalColumns - 1), "px - ").concat(marginWidth * 2, "px)"); // Now that we have the full width we can calculate the width of this
246
- // particular cell by multiplying the full width (allCellWidth) by
247
- // the ratio of this cell (cols / totalColumns). But we then need to
248
- // add back in the missing gutter widths:
249
- // (gutterWidth * (cols - 1)). This gives us to full width of
250
- // this particular cell.
251
-
252
- var calcWidth = "calc(".concat(contentWidth, " * ").concat(cols / totalColumns, " + ").concat(gutterWidth * (cols - 1), "px)"); // If the contents are a function then we call it with the mediaSize,
253
- // totalColumns, and cols properties and render the return value.
254
-
255
- var contents = typeof children === "function" ? children({
256
- mediaSize: mediaSize,
257
- totalColumns: totalColumns,
258
- cols: cols
259
- }) : children; // Render a fixed-width cell (flex-basis: size, flex-shrink: 0)
260
- // that matches the intended width of the cell
261
-
262
- return /*#__PURE__*/createElement(View, {
263
- style: [styles.cellFixed, flexBasis(calcWidth), style]
264
- }, contents);
265
- });
266
- }
267
- }], [{
268
- key: "isClassOf",
269
- value: function isClassOf(instance) {
270
- return instance && instance.type && instance.type.__IS_CELL__;
271
- }
272
- }, {
273
- key: "getCols",
274
- value: function getCols(props, mediaSize) {
275
- // If no option was specified then we just return undefined,
276
- // components may handle this case differently.
277
- // We go through all the ways in which a fixed width can be
278
- // specified and find the one that matches our current grid size.
279
- if (!props.smallCols && !props.mediumCols && !props.largeCols && !props.cols) {
280
- return undefined;
281
- } else if (props.smallCols && mediaSize === "small") {
282
- return props.smallCols;
283
- } else if (props.mediumCols && mediaSize === "medium") {
284
- return props.mediumCols;
285
- } else if (props.largeCols && mediaSize === "large") {
286
- return props.largeCols;
287
- } else if (typeof props.cols === "function") {
288
- return props.cols(mediaSize);
289
- } else if (props.cols) {
290
- return props.cols;
291
- } // If nothing applies then we return null (usually resulting
292
- // in the component not being rendered)
293
-
294
-
295
- return null;
296
- } // HACK(kevinb): we use a stack method here because we can't get a ref to
297
- // each cell in a row without cloning them all.
298
-
299
- }, {
300
- key: "shouldDisplay",
301
- value: function shouldDisplay(props, mediaSize) {
302
- var cols = Cell.getCols(props, mediaSize);
303
- return cols !== null && cols !== 0;
304
- }
305
- }]);
56
+ static getCols(props, mediaSize) {
57
+ // If no option was specified then we just return undefined,
58
+ // components may handle this case differently.
59
+ // We go through all the ways in which a fixed width can be
60
+ // specified and find the one that matches our current grid size.
61
+ if (!props.smallCols && !props.mediumCols && !props.largeCols && !props.cols) {
62
+ return undefined;
63
+ } else if (props.smallCols && mediaSize === "small") {
64
+ return props.smallCols;
65
+ } else if (props.mediumCols && mediaSize === "medium") {
66
+ return props.mediumCols;
67
+ } else if (props.largeCols && mediaSize === "large") {
68
+ return props.largeCols;
69
+ } else if (typeof props.cols === "function") {
70
+ return props.cols(mediaSize);
71
+ } else if (props.cols) {
72
+ return props.cols;
73
+ } // If nothing applies then we return null (usually resulting
74
+ // in the component not being rendered)
75
+
76
+
77
+ return null;
78
+ } // HACK(kevinb): we use a stack method here because we can't get a ref to
79
+ // each cell in a row without cloning them all.
80
+
81
+
82
+ static shouldDisplay(props, mediaSize) {
83
+ const cols = Cell.getCols(props, mediaSize);
84
+ return cols !== null && cols !== 0;
85
+ }
306
86
 
307
- return Cell;
308
- }(Component);
87
+ render() {
88
+ const {
89
+ children,
90
+ style
91
+ } = this.props;
92
+ return /*#__PURE__*/createElement(MediaLayout, null, ({
93
+ mediaSize,
94
+ mediaSpec
95
+ }) => {
96
+ const spec = mediaSpec[mediaSize];
97
+
98
+ if (!spec) {
99
+ throw new Error(`mediaSpec.${mediaSize} is undefined`);
100
+ } // Get the settings for this particular size of grid
101
+
102
+
103
+ const {
104
+ totalColumns,
105
+ gutterWidth,
106
+ marginWidth
107
+ } = spec;
108
+ const cols = Cell.getCols(this.props, mediaSize); // If no columns are specified then we just don't render this cell
109
+
110
+ if (cols === undefined || cols === null || cols === 0) {
111
+ return null;
112
+ }
113
+
114
+ if (cols > totalColumns) {
115
+ throw new Error(`Specified columns ${cols} is greater than the maximum ` + `${totalColumns} at the ${mediaSize} grid size.`);
116
+ } // We need to start by calculating the total width of all the "content"
117
+ // We do this by starting with the full width (100%) and then
118
+ // subtracting all of the gutter spaces inbetween the cells
119
+ // (gutterWidth * (totalColumns - 1)) and the width of the two margins
120
+ // (marginWidth * 2).
121
+
122
+
123
+ const contentWidth = `(100% - ${gutterWidth * (totalColumns - 1)}px - ${marginWidth * 2}px)`; // Now that we have the full width we can calculate the width of this
124
+ // particular cell by multiplying the full width (allCellWidth) by
125
+ // the ratio of this cell (cols / totalColumns). But we then need to
126
+ // add back in the missing gutter widths:
127
+ // (gutterWidth * (cols - 1)). This gives us to full width of
128
+ // this particular cell.
129
+
130
+ const calcWidth = `calc(${contentWidth} * ${cols / totalColumns} + ${gutterWidth * (cols - 1)}px)`; // If the contents are a function then we call it with the mediaSize,
131
+ // totalColumns, and cols properties and render the return value.
132
+
133
+ const contents = typeof children === "function" ? children({
134
+ mediaSize,
135
+ totalColumns,
136
+ cols
137
+ }) : children; // Render a fixed-width cell (flex-basis: size, flex-shrink: 0)
138
+ // that matches the intended width of the cell
139
+
140
+ return /*#__PURE__*/createElement(View, {
141
+ style: [styles.cellFixed, flexBasis(calcWidth), style]
142
+ }, contents);
143
+ });
144
+ }
309
145
 
310
- _defineProperty(Cell, "defaultProps", {
146
+ }
147
+ Cell.defaultProps = {
311
148
  smallCols: 0,
312
149
  mediumCols: 0,
313
150
  largeCols: 0,
314
151
  cols: 0
315
- });
316
-
317
- _defineProperty(Cell, "__IS_CELL__", true);
152
+ };
153
+ Cell.__IS_CELL__ = true;
318
154
 
319
155
  /**
320
156
  * Gutter is a component whose width is set based on the size of grid currently
@@ -329,50 +165,36 @@ _defineProperty(Cell, "__IS_CELL__", true);
329
165
  * grid sizes. If you specify the `small`, `medium`, or `large`
330
166
  * props then the component will only be shown at those grid sizes.
331
167
  */
332
- var Gutter = /*#__PURE__*/function (_React$Component) {
333
- _inherits(Gutter, _React$Component);
334
-
335
- var _super = _createSuper(Gutter);
336
-
337
- function Gutter() {
338
- _classCallCheck(this, Gutter);
339
-
340
- return _super.apply(this, arguments);
341
- }
342
-
343
- _createClass(Gutter, [{
344
- key: "render",
345
- value: function render() {
346
- var _this = this;
347
-
348
- return /*#__PURE__*/createElement(MediaLayout, null, function (_ref) {
349
- var mediaSize = _ref.mediaSize,
350
- mediaSpec = _ref.mediaSpec;
351
- var spec = mediaSpec[mediaSize];
352
-
353
- if (!spec) {
354
- throw new Error("mediaSpec.".concat(mediaSize, " is undefined"));
355
- }
356
-
357
- var gutterWidth = spec.gutterWidth;
358
-
359
- if (!queryMatchesSize(_this.props.mediaQuery, mediaSize)) {
360
- return null;
361
- }
362
-
363
- return /*#__PURE__*/createElement(Strut, {
364
- size: gutterWidth
365
- });
168
+ class Gutter extends Component {
169
+ render() {
170
+ return /*#__PURE__*/createElement(MediaLayout, null, ({
171
+ mediaSize,
172
+ mediaSpec
173
+ }) => {
174
+ const spec = mediaSpec[mediaSize];
175
+
176
+ if (!spec) {
177
+ throw new Error(`mediaSpec.${mediaSize} is undefined`);
178
+ }
179
+
180
+ const {
181
+ gutterWidth
182
+ } = spec;
183
+
184
+ if (!queryMatchesSize(this.props.mediaQuery, mediaSize)) {
185
+ return null;
186
+ }
187
+
188
+ return /*#__PURE__*/createElement(Strut, {
189
+ size: gutterWidth
366
190
  });
367
- }
368
- }]);
369
-
370
- return Gutter;
371
- }(Component);
191
+ });
192
+ }
372
193
 
373
- _defineProperty(Gutter, "defaultProps", {
194
+ }
195
+ Gutter.defaultProps = {
374
196
  mediaQuery: "all"
375
- });
197
+ };
376
198
 
377
199
  /**
378
200
  * A Row holds all of the Cells that make up the contents of the grid. A row
@@ -392,82 +214,65 @@ _defineProperty(Gutter, "defaultProps", {
392
214
  * grid sizes. If you specify the `small`, `medium`, or `large`
393
215
  * props then the component will only be shown at those grid sizes.
394
216
  */
395
- var Row = /*#__PURE__*/function (_React$Component) {
396
- _inherits(Row, _React$Component);
397
-
398
- var _super = _createSuper(Row);
399
-
400
- function Row() {
401
- _classCallCheck(this, Row);
402
-
403
- return _super.apply(this, arguments);
217
+ class Row extends Component {
218
+ render() {
219
+ const {
220
+ style,
221
+ children
222
+ } = this.props;
223
+ return /*#__PURE__*/createElement(MediaLayout, null, ({
224
+ mediaSize,
225
+ mediaSpec
226
+ }) => {
227
+ const spec = mediaSpec[mediaSize];
228
+
229
+ if (!spec) {
230
+ throw new Error(`mediaSpec.${mediaSize} is undefined`);
231
+ }
232
+
233
+ const {
234
+ marginWidth,
235
+ maxWidth,
236
+ totalColumns
237
+ } = spec;
238
+ const shouldDisplay = queryMatchesSize(this.props.mediaQuery, mediaSize); // Don't render the row if it's been disabled at this size
239
+
240
+ if (!shouldDisplay) {
241
+ return null;
242
+ } // If the contents are a function then we call it with the mediaSize and
243
+ // totalColumns properties and render the return value.
244
+
245
+
246
+ const contents = typeof children === "function" ? children({
247
+ mediaSize,
248
+ totalColumns
249
+ }) : children;
250
+ const filteredContents = Children.toArray(contents) // Go through all of the contents and pre-emptively remove anything
251
+ // that shouldn't be rendered.
252
+ .filter( // Flow doesn't let us check .type on a non-null React.Node so
253
+ // we have to cast it to any.
254
+ item => Cell.isClassOf(item) ? Cell.shouldDisplay(item.props, mediaSize) : true) // Intersperse Gutter elements between the cells.
255
+ .reduce((acc, elem, index) => [].concat(acc, [elem, /*#__PURE__*/createElement(Gutter, {
256
+ key: `gutter-${index}`
257
+ })]), []) // We only want gutters between each cell in the row. The reduce
258
+ // adds a gutter after every cell so we need to remove the last
259
+ // element which is an unnecessary gutteer.
260
+ .slice(0, -1);
261
+ return /*#__PURE__*/createElement(View, {
262
+ style: [styles.row, !!maxWidth && styles.rowMaxWidth, !!maxWidth && {
263
+ maxWidth
264
+ }, style]
265
+ }, /*#__PURE__*/createElement(Strut, {
266
+ size: marginWidth
267
+ }), filteredContents, /*#__PURE__*/createElement(Strut, {
268
+ size: marginWidth
269
+ }));
270
+ });
404
271
  }
405
272
 
406
- _createClass(Row, [{
407
- key: "render",
408
- value: function render() {
409
- var _this = this;
410
-
411
- var _this$props = this.props,
412
- style = _this$props.style,
413
- children = _this$props.children;
414
- return /*#__PURE__*/createElement(MediaLayout, null, function (_ref) {
415
- var mediaSize = _ref.mediaSize,
416
- mediaSpec = _ref.mediaSpec;
417
- var spec = mediaSpec[mediaSize];
418
-
419
- if (!spec) {
420
- throw new Error("mediaSpec.".concat(mediaSize, " is undefined"));
421
- }
422
-
423
- var marginWidth = spec.marginWidth,
424
- maxWidth = spec.maxWidth,
425
- totalColumns = spec.totalColumns;
426
- var shouldDisplay = queryMatchesSize(_this.props.mediaQuery, mediaSize); // Don't render the row if it's been disabled at this size
427
-
428
- if (!shouldDisplay) {
429
- return null;
430
- } // If the contents are a function then we call it with the mediaSize and
431
- // totalColumns properties and render the return value.
432
-
433
-
434
- var contents = typeof children === "function" ? children({
435
- mediaSize: mediaSize,
436
- totalColumns: totalColumns
437
- }) : children;
438
- var filteredContents = Children.toArray(contents). // Go through all of the contents and pre-emptively remove anything
439
- // that shouldn't be rendered.
440
- filter( // Flow doesn't let us check .type on a non-null React.Node so
441
- // we have to cast it to any.
442
- function (item) {
443
- return Cell.isClassOf(item) ? Cell.shouldDisplay(item.props, mediaSize) : true;
444
- }) // Intersperse Gutter elements between the cells.
445
- .reduce(function (acc, elem, index) {
446
- return [].concat(_toConsumableArray(acc), [elem, /*#__PURE__*/createElement(Gutter, {
447
- key: "gutter-".concat(index)
448
- })]);
449
- }, []) // We only want gutters between each cell in the row. The reduce
450
- // adds a gutter after every cell so we need to remove the last
451
- // element which is an unnecessary gutteer.
452
- .slice(0, -1);
453
- return /*#__PURE__*/createElement(View, {
454
- style: [styles.row, !!maxWidth && styles.rowMaxWidth, !!maxWidth && {
455
- maxWidth: maxWidth
456
- }, style]
457
- }, /*#__PURE__*/createElement(Strut, {
458
- size: marginWidth
459
- }), filteredContents, /*#__PURE__*/createElement(Strut, {
460
- size: marginWidth
461
- }));
462
- });
463
- }
464
- }]);
465
-
466
- return Row;
467
- }(Component);
468
-
469
- _defineProperty(Row, "defaultProps", {
273
+ }
274
+ Row.defaultProps = {
470
275
  mediaQuery: "all"
471
- });
276
+ };
472
277
 
473
278
  export { Cell, Row };