@khanacademy/wonder-blocks-grid 1.0.27 → 1.0.30

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/CHANGELOG.md ADDED
@@ -0,0 +1,20 @@
1
+ # @khanacademy/wonder-blocks-grid
2
+
3
+ ## 1.0.30
4
+
5
+ ### Patch Changes
6
+
7
+ - @khanacademy/wonder-blocks-core@4.3.1
8
+
9
+ ## 1.0.29
10
+
11
+ ### Patch Changes
12
+
13
+ - Updated dependencies [246a921d]
14
+ - @khanacademy/wonder-blocks-core@4.3.0
15
+
16
+ ## 1.0.28
17
+
18
+ ### Patch Changes
19
+
20
+ - @khanacademy/wonder-blocks-core@4.2.1
package/dist/es/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Component, createElement, Children } from 'react';
1
+ import * as React 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';
@@ -32,32 +32,12 @@ const flexBasis = size => {
32
32
  };
33
33
  };
34
34
 
35
- /**
36
- * A Cell is a container whose width is set based on the width of the
37
- * specified columns at the current grid size. You will specify the number
38
- * of columns that you want this component to span at each grid size.
39
- * This component should only be used as a child of a [Row](#row).
40
- *
41
- * This component renders a [View](#view) that
42
- * uses Flex Box and has a [flex-basis](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis)
43
- * of the specified "width" and [flex-shrink](https://developer.mozilla.org/en-US/docs/Web/CSS/flex-shrink)
44
- * of 0.
45
- *
46
- * By default (with no properties specified) it will display at all
47
- * grid sizes. If you specify the `smallCols`, `mediumCols`, `largeCols`, or
48
- * `cols` props then the component will only be shown at those grid sizes and
49
- * using the specified column width.
50
- */
51
- class Cell extends Component {
35
+ class Cell extends React.Component {
52
36
  static isClassOf(instance) {
53
37
  return instance && instance.type && instance.type.__IS_CELL__;
54
38
  }
55
39
 
56
40
  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
41
  if (!props.smallCols && !props.mediumCols && !props.largeCols && !props.cols) {
62
42
  return undefined;
63
43
  } else if (props.smallCols && mediaSize === "small") {
@@ -70,14 +50,10 @@ class Cell extends Component {
70
50
  return props.cols(mediaSize);
71
51
  } else if (props.cols) {
72
52
  return props.cols;
73
- } // If nothing applies then we return null (usually resulting
74
- // in the component not being rendered)
75
-
53
+ }
76
54
 
77
55
  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
-
56
+ }
81
57
 
82
58
  static shouldDisplay(props, mediaSize) {
83
59
  const cols = Cell.getCols(props, mediaSize);
@@ -89,7 +65,7 @@ class Cell extends Component {
89
65
  children,
90
66
  style
91
67
  } = this.props;
92
- return /*#__PURE__*/createElement(MediaLayout, null, ({
68
+ return React.createElement(MediaLayout, null, ({
93
69
  mediaSize,
94
70
  mediaSpec
95
71
  }) => {
@@ -97,15 +73,14 @@ class Cell extends Component {
97
73
 
98
74
  if (!spec) {
99
75
  throw new Error(`mediaSpec.${mediaSize} is undefined`);
100
- } // Get the settings for this particular size of grid
101
-
76
+ }
102
77
 
103
78
  const {
104
79
  totalColumns,
105
80
  gutterWidth,
106
81
  marginWidth
107
82
  } = spec;
108
- const cols = Cell.getCols(this.props, mediaSize); // If no columns are specified then we just don't render this cell
83
+ const cols = Cell.getCols(this.props, mediaSize);
109
84
 
110
85
  if (cols === undefined || cols === null || cols === 0) {
111
86
  return null;
@@ -113,31 +88,16 @@ class Cell extends Component {
113
88
 
114
89
  if (cols > totalColumns) {
115
90
  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.
91
+ }
132
92
 
93
+ const contentWidth = `(100% - ${gutterWidth * (totalColumns - 1)}px - ${marginWidth * 2}px)`;
94
+ const calcWidth = `calc(${contentWidth} * ${cols / totalColumns} + ${gutterWidth * (cols - 1)}px)`;
133
95
  const contents = typeof children === "function" ? children({
134
96
  mediaSize,
135
97
  totalColumns,
136
98
  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, {
99
+ }) : children;
100
+ return React.createElement(View, {
141
101
  style: [styles.cellFixed, flexBasis(calcWidth), style]
142
102
  }, contents);
143
103
  });
@@ -152,22 +112,9 @@ Cell.defaultProps = {
152
112
  };
153
113
  Cell.__IS_CELL__ = true;
154
114
 
155
- /**
156
- * Gutter is a component whose width is set based on the size of grid currently
157
- * being displayed. Used for spacing out cells from each other. The gutter
158
- * itself doesn't hold any content, it just spaces it out.
159
- *
160
- * Gutters are inserted automatically inside of a [Row](#row) in-between Cells.
161
- * You may only need to use Gutters if you're manually building your own
162
- * sub-grid, or some-such (this should be relatively rare).
163
- *
164
- * By default (with no properties specified) it will display at all
165
- * grid sizes. If you specify the `small`, `medium`, or `large`
166
- * props then the component will only be shown at those grid sizes.
167
- */
168
- class Gutter extends Component {
115
+ class Gutter extends React.Component {
169
116
  render() {
170
- return /*#__PURE__*/createElement(MediaLayout, null, ({
117
+ return React.createElement(MediaLayout, null, ({
171
118
  mediaSize,
172
119
  mediaSpec
173
120
  }) => {
@@ -185,7 +132,7 @@ class Gutter extends Component {
185
132
  return null;
186
133
  }
187
134
 
188
- return /*#__PURE__*/createElement(Strut, {
135
+ return React.createElement(Strut, {
189
136
  size: gutterWidth
190
137
  });
191
138
  });
@@ -196,31 +143,13 @@ Gutter.defaultProps = {
196
143
  mediaQuery: "all"
197
144
  };
198
145
 
199
- /**
200
- * A Row holds all of the Cells that make up the contents of the grid. A row
201
- * also provides the margins on the sides and inserts the gutter spacing
202
- * in-between the cells. Typically this component will hold a [Cell](#cell),
203
- * but it can also include any elements that could fit in a
204
- * [flexbox](https://developer.mozilla.org/en-US/docs/Web/CSS/CSS_Flexible_Box_Layout/Basic_Concepts_of_Flexbox).
205
- *
206
- * This component will automatically attempt to insert [Gutters](#gutter)
207
- * in-between all child elements. Additionally, it'll perform some basic checks
208
- * to ensure that no impossible layouts are accidentally generated.
209
- *
210
- * Typically this component will be used as a child of a [Grid](#grid-1),
211
- * but it's not a requirement, you can use it as a descendant, as well.
212
- *
213
- * By default (with no properties specified) it will display at all
214
- * grid sizes. If you specify the `small`, `medium`, or `large`
215
- * props then the component will only be shown at those grid sizes.
216
- */
217
- class Row extends Component {
146
+ class Row extends React.Component {
218
147
  render() {
219
148
  const {
220
149
  style,
221
150
  children
222
151
  } = this.props;
223
- return /*#__PURE__*/createElement(MediaLayout, null, ({
152
+ return React.createElement(MediaLayout, null, ({
224
153
  mediaSize,
225
154
  mediaSpec
226
155
  }) => {
@@ -235,36 +164,26 @@ class Row extends Component {
235
164
  maxWidth,
236
165
  totalColumns
237
166
  } = spec;
238
- const shouldDisplay = queryMatchesSize(this.props.mediaQuery, mediaSize); // Don't render the row if it's been disabled at this size
167
+ const shouldDisplay = queryMatchesSize(this.props.mediaQuery, mediaSize);
239
168
 
240
169
  if (!shouldDisplay) {
241
170
  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
-
171
+ }
245
172
 
246
173
  const contents = typeof children === "function" ? children({
247
174
  mediaSize,
248
175
  totalColumns
249
176
  }) : 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, {
177
+ const filteredContents = React.Children.toArray(contents).filter(item => Cell.isClassOf(item) ? Cell.shouldDisplay(item.props, mediaSize) : true).reduce((acc, elem, index) => [].concat(acc, [elem, React.createElement(Gutter, {
256
178
  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, {
179
+ })]), []).slice(0, -1);
180
+ return React.createElement(View, {
262
181
  style: [styles.row, !!maxWidth && styles.rowMaxWidth, !!maxWidth && {
263
182
  maxWidth
264
183
  }, style]
265
- }, /*#__PURE__*/createElement(Strut, {
184
+ }, React.createElement(Strut, {
266
185
  size: marginWidth
267
- }), filteredContents, /*#__PURE__*/createElement(Strut, {
186
+ }), filteredContents, React.createElement(Strut, {
268
187
  size: marginWidth
269
188
  }));
270
189
  });