@instructure/ui-responsive 8.33.1 → 8.33.2-snapshot-5
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 +8 -0
- package/es/Responsive/index.js +15 -30
- package/es/Responsive/props.js +1 -0
- package/es/ResponsivePropTypes.js +1 -1
- package/es/addElementQueryMatchListener.js +8 -23
- package/es/addMediaQueryMatchListener.js +1 -8
- package/es/index.js +1 -0
- package/es/jsonToMediaQuery.js +2 -10
- package/es/parseQuery.js +12 -15
- package/lib/Responsive/index.js +13 -38
- package/lib/Responsive/props.js +1 -4
- package/lib/ResponsivePropTypes.js +1 -3
- package/lib/addElementQueryMatchListener.js +8 -28
- package/lib/addMediaQueryMatchListener.js +0 -10
- package/lib/index.js +0 -6
- package/lib/jsonToMediaQuery.js +2 -11
- package/lib/parseQuery.js +12 -17
- package/package.json +11 -11
- package/tsconfig.build.tsbuildinfo +1 -1
package/CHANGELOG.md
CHANGED
|
@@ -3,6 +3,14 @@
|
|
|
3
3
|
All notable changes to this project will be documented in this file.
|
|
4
4
|
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
|
|
5
5
|
|
|
6
|
+
## [8.33.2-snapshot-5](https://github.com/instructure/instructure-ui/compare/v8.33.1...v8.33.2-snapshot-5) (2023-01-23)
|
|
7
|
+
|
|
8
|
+
**Note:** Version bump only for package @instructure/ui-responsive
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
|
|
6
14
|
## [8.33.1](https://github.com/instructure/instructure-ui/compare/v8.33.0...v8.33.1) (2023-01-06)
|
|
7
15
|
|
|
8
16
|
**Note:** Version bump only for package @instructure/ui-responsive
|
package/es/Responsive/index.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
|
+
|
|
24
25
|
import React, { Component } from 'react';
|
|
25
26
|
import { deepEqual } from '@instructure/ui-utils';
|
|
26
27
|
import { logError as error, warn } from '@instructure/console';
|
|
@@ -28,13 +29,13 @@ import { addElementQueryMatchListener, updateElementMatches } from '../addElemen
|
|
|
28
29
|
import { addMediaQueryMatchListener } from '../addMediaQueryMatchListener';
|
|
29
30
|
import { findDOMNode } from '@instructure/ui-dom-utils';
|
|
30
31
|
import { allowedProps, propTypes } from './props';
|
|
32
|
+
|
|
31
33
|
/**
|
|
32
34
|
---
|
|
33
35
|
category: components
|
|
34
36
|
---
|
|
35
37
|
@tsProps
|
|
36
38
|
**/
|
|
37
|
-
|
|
38
39
|
class Responsive extends Component {
|
|
39
40
|
constructor() {
|
|
40
41
|
super(...arguments);
|
|
@@ -44,16 +45,13 @@ class Responsive extends Component {
|
|
|
44
45
|
matches: [],
|
|
45
46
|
hasRendered: false
|
|
46
47
|
};
|
|
47
|
-
|
|
48
48
|
this.handleRef = el => {
|
|
49
49
|
const elementRef = this.props.elementRef;
|
|
50
50
|
this.ref = el;
|
|
51
|
-
|
|
52
51
|
if (typeof elementRef === 'function') {
|
|
53
52
|
elementRef(el);
|
|
54
53
|
}
|
|
55
54
|
};
|
|
56
|
-
|
|
57
55
|
this.updateMatches = (matches, cb) => {
|
|
58
56
|
this.setState({
|
|
59
57
|
matches
|
|
@@ -64,10 +62,8 @@ class Responsive extends Component {
|
|
|
64
62
|
});
|
|
65
63
|
};
|
|
66
64
|
}
|
|
67
|
-
|
|
68
65
|
componentDidMount() {
|
|
69
66
|
error(!!(this.props.render || this.props.children), `[Responsive] must have either a \`render\` prop or \`children\` prop.`);
|
|
70
|
-
|
|
71
67
|
if (this.props.match === 'element') {
|
|
72
68
|
// Because Responsive renders an empty div initially, it always needs to
|
|
73
69
|
// re-render with the children provided. If there are no matches the match
|
|
@@ -82,49 +78,42 @@ class Responsive extends Component {
|
|
|
82
78
|
hasRendered: true
|
|
83
79
|
});
|
|
84
80
|
}
|
|
85
|
-
|
|
86
81
|
this._matchListener = this.addMatchListener(this.props.query, this.updateMatches);
|
|
87
82
|
}
|
|
88
|
-
|
|
89
83
|
componentWillUnmount() {
|
|
90
84
|
this.removeMatchListener();
|
|
91
85
|
}
|
|
92
|
-
|
|
93
86
|
componentDidUpdate(prevProps) {
|
|
94
87
|
const _this$props = this.props,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
match = _this$props.match,
|
|
89
|
+
query = _this$props.query;
|
|
98
90
|
if (match !== prevProps.match || !deepEqual(query, prevProps.query)) {
|
|
99
91
|
this.removeMatchListener();
|
|
100
92
|
this._matchListener = this.addMatchListener(query, this.updateMatches, match);
|
|
101
93
|
}
|
|
102
94
|
}
|
|
103
|
-
|
|
104
95
|
addMatchListener(query, updateMatches) {
|
|
105
96
|
let match = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : this.props.match;
|
|
106
97
|
const matchListener = match === 'element' ? addElementQueryMatchListener : addMediaQueryMatchListener;
|
|
107
98
|
return matchListener(query, () => findDOMNode(this), updateMatches);
|
|
108
99
|
}
|
|
109
|
-
|
|
110
100
|
removeMatchListener() {
|
|
111
101
|
if (this._matchListener) {
|
|
112
102
|
this._matchListener.remove();
|
|
113
103
|
}
|
|
114
104
|
}
|
|
115
|
-
|
|
116
105
|
mergeProps(matches, props) {
|
|
117
106
|
if (!props) {
|
|
118
107
|
return null;
|
|
119
108
|
}
|
|
120
|
-
|
|
121
109
|
const mergedProps = {};
|
|
122
110
|
matches.forEach(match => {
|
|
123
|
-
const matchProps = props[match];
|
|
111
|
+
const matchProps = props[match];
|
|
112
|
+
|
|
113
|
+
// Iterate over the props for the current match. If that the prop is
|
|
124
114
|
// already in `mergedProps` that means that the prop was defined for
|
|
125
115
|
// multiple breakpoints, and more than one of those breakpoints is being
|
|
126
116
|
// currently applied so we log a warning.
|
|
127
|
-
|
|
128
117
|
Object.keys(matchProps).forEach(prop => {
|
|
129
118
|
const currentValue = mergedProps[prop];
|
|
130
119
|
warn(!(prop in mergedProps), [`[Responsive] The prop \`${prop}\` is defined at 2 or more breakpoints`, `which are currently applied at the same time. Its current value, \`${currentValue}\`,`, `will be overwritten as \`${matchProps[prop]}\`.`].join(' '));
|
|
@@ -133,26 +122,24 @@ class Responsive extends Component {
|
|
|
133
122
|
});
|
|
134
123
|
return mergedProps;
|
|
135
124
|
}
|
|
136
|
-
|
|
137
125
|
render() {
|
|
138
126
|
const _this$state = this.state,
|
|
139
|
-
|
|
140
|
-
|
|
127
|
+
matches = _this$state.matches,
|
|
128
|
+
hasRendered = _this$state.hasRendered;
|
|
141
129
|
const _this$props2 = this.props,
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
let renderFunc;
|
|
130
|
+
props = _this$props2.props,
|
|
131
|
+
render = _this$props2.render,
|
|
132
|
+
children = _this$props2.children,
|
|
133
|
+
display = _this$props2.display;
|
|
134
|
+
let renderFunc;
|
|
135
|
+
// Responsive needs to render once to measure the dom and obtain matches.
|
|
147
136
|
// Calling the render prop on this initial render can cause visual side
|
|
148
137
|
// effects and is slower than rendering an empty div.
|
|
149
|
-
|
|
150
138
|
if (hasRendered) {
|
|
151
139
|
// Render via the children or render method, whichever is supplied. If
|
|
152
140
|
// both are supplied, give preference to children.
|
|
153
141
|
renderFunc = children || render;
|
|
154
142
|
}
|
|
155
|
-
|
|
156
143
|
return /*#__PURE__*/React.createElement("div", {
|
|
157
144
|
ref: this.handleRef,
|
|
158
145
|
style: {
|
|
@@ -160,9 +147,7 @@ class Responsive extends Component {
|
|
|
160
147
|
}
|
|
161
148
|
}, renderFunc && renderFunc(this.mergeProps(matches, props), matches));
|
|
162
149
|
}
|
|
163
|
-
|
|
164
150
|
}
|
|
165
|
-
|
|
166
151
|
Responsive.displayName = "Responsive";
|
|
167
152
|
Responsive.allowedProps = allowedProps;
|
|
168
153
|
Responsive.propTypes = propTypes;
|
package/es/Responsive/props.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
|
+
|
|
24
25
|
import { jsonToMediaQuery } from './jsonToMediaQuery';
|
|
25
26
|
const ResponsivePropTypes = {
|
|
26
27
|
/**
|
|
@@ -58,7 +59,6 @@ const ResponsivePropTypes = {
|
|
|
58
59
|
return new Error(`Invalid query prop supplied to \`${componentName}\`. ${e.message}`);
|
|
59
60
|
}
|
|
60
61
|
}
|
|
61
|
-
|
|
62
62
|
};
|
|
63
63
|
export default ResponsivePropTypes;
|
|
64
64
|
export {
|
|
@@ -24,7 +24,6 @@
|
|
|
24
24
|
import { parseQuery } from './parseQuery';
|
|
25
25
|
import { findDOMNode, getBoundingClientRect } from '@instructure/ui-dom-utils';
|
|
26
26
|
import { debounce } from '@instructure/debounce';
|
|
27
|
-
|
|
28
27
|
/**
|
|
29
28
|
* ---
|
|
30
29
|
* category: utilities/layout
|
|
@@ -82,24 +81,18 @@ import { debounce } from '@instructure/debounce';
|
|
|
82
81
|
*/
|
|
83
82
|
const addElementQueryMatchListener = (query, el, cb) => {
|
|
84
83
|
const node = typeof el === 'function' ? el() : el;
|
|
85
|
-
|
|
86
84
|
const _getBoundingClientRec = getBoundingClientRect(node),
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
85
|
+
width = _getBoundingClientRec.width,
|
|
86
|
+
height = _getBoundingClientRec.height;
|
|
90
87
|
let matches = [];
|
|
91
|
-
|
|
92
88
|
const update = size => {
|
|
93
89
|
const newMatches = updateElementMatches(query, node, matches, size);
|
|
94
|
-
|
|
95
90
|
if (newMatches) {
|
|
96
91
|
matches = newMatches;
|
|
97
92
|
cb(matches);
|
|
98
93
|
}
|
|
99
|
-
|
|
100
94
|
return;
|
|
101
95
|
};
|
|
102
|
-
|
|
103
96
|
const debounced = debounce(update, 100, {
|
|
104
97
|
leading: false,
|
|
105
98
|
trailing: true
|
|
@@ -107,9 +100,8 @@ const addElementQueryMatchListener = (query, el, cb) => {
|
|
|
107
100
|
const elementResizeListener = new ResizeObserver(entries => {
|
|
108
101
|
for (const entry of entries) {
|
|
109
102
|
const _entry$contentRect = entry.contentRect,
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
103
|
+
newWidth = _entry$contentRect.width,
|
|
104
|
+
newHeight = _entry$contentRect.height;
|
|
113
105
|
if (width !== newWidth) {
|
|
114
106
|
debounced({
|
|
115
107
|
width: newWidth,
|
|
@@ -128,40 +120,33 @@ const addElementQueryMatchListener = (query, el, cb) => {
|
|
|
128
120
|
if (elementResizeListener) {
|
|
129
121
|
elementResizeListener.disconnect();
|
|
130
122
|
}
|
|
131
|
-
|
|
132
123
|
if (debounced) {
|
|
133
124
|
debounced.cancel();
|
|
134
125
|
}
|
|
135
126
|
}
|
|
136
|
-
|
|
137
127
|
};
|
|
138
128
|
};
|
|
139
|
-
|
|
140
129
|
function updateElementMatches(query, el) {
|
|
141
130
|
let matches = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : [];
|
|
142
131
|
let size = arguments.length > 3 ? arguments[3] : void 0;
|
|
143
132
|
const node = findDOMNode(el);
|
|
144
|
-
|
|
145
133
|
const _ref = size || getBoundingClientRect(node),
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
134
|
+
width = _ref.width,
|
|
135
|
+
height = _ref.height;
|
|
149
136
|
const matchingQueries = parseQuery(query, node)({
|
|
150
137
|
width,
|
|
151
138
|
height
|
|
152
139
|
});
|
|
153
|
-
const newMatches = Object.keys(matchingQueries).filter(key => matchingQueries[key]).map(key => key);
|
|
140
|
+
const newMatches = Object.keys(matchingQueries).filter(key => matchingQueries[key]).map(key => key);
|
|
154
141
|
|
|
142
|
+
// only return matches if they have changed
|
|
155
143
|
if (matches.length !== newMatches.length) {
|
|
156
144
|
return newMatches;
|
|
157
145
|
}
|
|
158
|
-
|
|
159
146
|
if (matches.filter(match => newMatches.indexOf(match) === -1).length > 0) {
|
|
160
147
|
return newMatches;
|
|
161
148
|
}
|
|
162
|
-
|
|
163
149
|
return null;
|
|
164
150
|
}
|
|
165
|
-
|
|
166
151
|
export default addElementQueryMatchListener;
|
|
167
152
|
export { addElementQueryMatchListener, updateElementMatches };
|
|
@@ -23,6 +23,7 @@
|
|
|
23
23
|
*/
|
|
24
24
|
import { matchMedia as defaultMatchMedia } from '@instructure/ui-dom-utils';
|
|
25
25
|
import { jsonToMediaQuery } from './jsonToMediaQuery';
|
|
26
|
+
|
|
26
27
|
/**
|
|
27
28
|
* ---
|
|
28
29
|
* category: utilities/layout
|
|
@@ -75,25 +76,19 @@ import { jsonToMediaQuery } from './jsonToMediaQuery';
|
|
|
75
76
|
* @param {object} matchMedia - called with an array of the names of the currently matching queries whenever a matching query changes
|
|
76
77
|
* @returns {function} remove() function to call to remove the listener
|
|
77
78
|
*/
|
|
78
|
-
|
|
79
79
|
const addMediaQueryMatchListener = function (query, el, cb) {
|
|
80
80
|
let matchMedia = arguments.length > 3 && arguments[3] !== void 0 ? arguments[3] : defaultMatchMedia;
|
|
81
81
|
const node = typeof el === 'function' ? el() : el;
|
|
82
|
-
|
|
83
82
|
const updateMediaMatches = mediaQueryLists => {
|
|
84
83
|
const matches = Object.keys(mediaQueryLists).filter(key => mediaQueryLists[key].matches).map(key => key);
|
|
85
84
|
cb(matches);
|
|
86
85
|
};
|
|
87
|
-
|
|
88
86
|
const mediaQueryLists = {};
|
|
89
|
-
|
|
90
87
|
const listenerCallback = () => {
|
|
91
88
|
updateMediaMatches(mediaQueryLists);
|
|
92
89
|
};
|
|
93
|
-
|
|
94
90
|
Object.keys(query).forEach(key => {
|
|
95
91
|
const mediaQueryList = matchMedia(jsonToMediaQuery(query[key], node), node);
|
|
96
|
-
|
|
97
92
|
if (mediaQueryList) {
|
|
98
93
|
mediaQueryList.addListener(listenerCallback);
|
|
99
94
|
mediaQueryLists[key] = mediaQueryList;
|
|
@@ -108,9 +103,7 @@ const addMediaQueryMatchListener = function (query, el, cb) {
|
|
|
108
103
|
});
|
|
109
104
|
}
|
|
110
105
|
}
|
|
111
|
-
|
|
112
106
|
};
|
|
113
107
|
};
|
|
114
|
-
|
|
115
108
|
export default addMediaQueryMatchListener;
|
|
116
109
|
export { addMediaQueryMatchListener };
|
package/es/index.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
|
+
|
|
24
25
|
export { Responsive } from './Responsive';
|
|
25
26
|
export { addElementQueryMatchListener } from './addElementQueryMatchListener';
|
|
26
27
|
export { addMediaQueryMatchListener } from './addMediaQueryMatchListener';
|
package/es/jsonToMediaQuery.js
CHANGED
|
@@ -22,7 +22,6 @@
|
|
|
22
22
|
* SOFTWARE.
|
|
23
23
|
*/
|
|
24
24
|
import { px } from '@instructure/ui-utils';
|
|
25
|
-
|
|
26
25
|
/**
|
|
27
26
|
* ---
|
|
28
27
|
* category: utilities/layout
|
|
@@ -49,35 +48,28 @@ import { px } from '@instructure/ui-utils';
|
|
|
49
48
|
function jsonToMediaQuery(query, el) {
|
|
50
49
|
// Ensure the query is of the correct form
|
|
51
50
|
const keys = Object.keys(query);
|
|
52
|
-
|
|
53
51
|
if (keys.length !== 1) {
|
|
54
52
|
throw new Error('Expected exactly one key in query object.');
|
|
55
53
|
}
|
|
56
|
-
|
|
57
54
|
const key = keys[0];
|
|
58
55
|
const validKeys = ['minHeight', 'maxHeight', 'minWidth', 'maxWidth'];
|
|
59
|
-
|
|
60
56
|
if (validKeys.indexOf(key) === -1) {
|
|
61
57
|
throw new Error(`Invalid key \`${key}\` in query object. Valid keys should consist of one of the following: ` + `${validKeys.join(', ')} (case sensitive)`);
|
|
62
58
|
}
|
|
59
|
+
const value = query[key];
|
|
63
60
|
|
|
64
|
-
|
|
65
|
-
|
|
61
|
+
// TS catches this, but we want to throw error in runtime too
|
|
66
62
|
if (typeof value !== 'string' && typeof value !== 'number') {
|
|
67
63
|
throw new Error('The value of the query object must be a string or number.');
|
|
68
64
|
}
|
|
69
|
-
|
|
70
65
|
if (!value) {
|
|
71
66
|
throw new Error('No value supplied for query object');
|
|
72
67
|
}
|
|
73
|
-
|
|
74
68
|
return `(${hyphenateQueryKey(key)}: ${px(value, el)}px)`;
|
|
75
69
|
}
|
|
76
|
-
|
|
77
70
|
function hyphenateQueryKey(key) {
|
|
78
71
|
const lowerCaseKey = key.toLowerCase();
|
|
79
72
|
return lowerCaseKey.slice(0, 3) + '-' + lowerCaseKey.slice(3);
|
|
80
73
|
}
|
|
81
|
-
|
|
82
74
|
export default jsonToMediaQuery;
|
|
83
75
|
export { jsonToMediaQuery };
|
package/es/parseQuery.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
-
|
|
3
2
|
/*
|
|
4
3
|
* The MIT License (MIT)
|
|
5
4
|
*
|
|
@@ -24,7 +23,6 @@ import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
|
24
23
|
* SOFTWARE.
|
|
25
24
|
*/
|
|
26
25
|
import { px } from '@instructure/ui-utils';
|
|
27
|
-
|
|
28
26
|
/**
|
|
29
27
|
* ---
|
|
30
28
|
* category: utilities/layout
|
|
@@ -42,14 +40,15 @@ import { px } from '@instructure/ui-utils';
|
|
|
42
40
|
* true false if it matches
|
|
43
41
|
*/
|
|
44
42
|
function parseQuery(query, el) {
|
|
45
|
-
const rules = {};
|
|
43
|
+
const rules = {};
|
|
46
44
|
|
|
45
|
+
// converting values to numerical values and adding initial values to all query keys
|
|
47
46
|
Object.keys(query).forEach(selectorName => {
|
|
48
47
|
const _query$selectorName = query[selectorName],
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
minWidth = _query$selectorName.minWidth,
|
|
49
|
+
maxWidth = _query$selectorName.maxWidth,
|
|
50
|
+
minHeight = _query$selectorName.minHeight,
|
|
51
|
+
maxHeight = _query$selectorName.maxHeight;
|
|
53
52
|
rules[selectorName] = {
|
|
54
53
|
minWidth: minWidth && px(minWidth, el) || 0,
|
|
55
54
|
maxWidth: maxWidth && px(maxWidth, el) || Infinity,
|
|
@@ -59,22 +58,20 @@ function parseQuery(query, el) {
|
|
|
59
58
|
});
|
|
60
59
|
return function (_ref) {
|
|
61
60
|
let width = _ref.width,
|
|
62
|
-
|
|
61
|
+
height = _ref.height;
|
|
63
62
|
const selectorMap = {};
|
|
64
63
|
Object.entries(rules).forEach(_ref2 => {
|
|
65
64
|
let _ref3 = _slicedToArray(_ref2, 2),
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
65
|
+
selectorName = _ref3[0],
|
|
66
|
+
query = _ref3[1];
|
|
69
67
|
const minWidth = query.minWidth,
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
maxWidth = query.maxWidth,
|
|
69
|
+
minHeight = query.minHeight,
|
|
70
|
+
maxHeight = query.maxHeight;
|
|
73
71
|
selectorMap[selectorName] = minWidth <= width && width <= maxWidth && minHeight <= height && height <= maxHeight;
|
|
74
72
|
});
|
|
75
73
|
return selectorMap;
|
|
76
74
|
};
|
|
77
75
|
}
|
|
78
|
-
|
|
79
76
|
export default parseQuery;
|
|
80
77
|
export { parseQuery };
|
package/lib/Responsive/index.js
CHANGED
|
@@ -1,26 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireWildcard = require("@babel/runtime/helpers/interopRequireWildcard").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.default = exports.Responsive = void 0;
|
|
9
|
-
|
|
10
8
|
var _react = _interopRequireWildcard(require("react"));
|
|
11
|
-
|
|
12
9
|
var _deepEqual = require("@instructure/ui-utils/lib/deepEqual.js");
|
|
13
|
-
|
|
14
10
|
var _console = require("@instructure/console");
|
|
15
|
-
|
|
16
11
|
var _addElementQueryMatchListener = require("../addElementQueryMatchListener");
|
|
17
|
-
|
|
18
12
|
var _addMediaQueryMatchListener = require("../addMediaQueryMatchListener");
|
|
19
|
-
|
|
20
13
|
var _findDOMNode = require("@instructure/ui-dom-utils/lib/findDOMNode.js");
|
|
21
|
-
|
|
22
14
|
var _props = require("./props");
|
|
23
|
-
|
|
24
15
|
/*
|
|
25
16
|
* The MIT License (MIT)
|
|
26
17
|
*
|
|
@@ -60,16 +51,13 @@ class Responsive extends _react.Component {
|
|
|
60
51
|
matches: [],
|
|
61
52
|
hasRendered: false
|
|
62
53
|
};
|
|
63
|
-
|
|
64
54
|
this.handleRef = el => {
|
|
65
55
|
const elementRef = this.props.elementRef;
|
|
66
56
|
this.ref = el;
|
|
67
|
-
|
|
68
57
|
if (typeof elementRef === 'function') {
|
|
69
58
|
elementRef(el);
|
|
70
59
|
}
|
|
71
60
|
};
|
|
72
|
-
|
|
73
61
|
this.updateMatches = (matches, cb) => {
|
|
74
62
|
this.setState({
|
|
75
63
|
matches
|
|
@@ -80,10 +68,8 @@ class Responsive extends _react.Component {
|
|
|
80
68
|
});
|
|
81
69
|
};
|
|
82
70
|
}
|
|
83
|
-
|
|
84
71
|
componentDidMount() {
|
|
85
72
|
(0, _console.logError)(!!(this.props.render || this.props.children), `[Responsive] must have either a \`render\` prop or \`children\` prop.`);
|
|
86
|
-
|
|
87
73
|
if (this.props.match === 'element') {
|
|
88
74
|
// Because Responsive renders an empty div initially, it always needs to
|
|
89
75
|
// re-render with the children provided. If there are no matches the match
|
|
@@ -98,49 +84,42 @@ class Responsive extends _react.Component {
|
|
|
98
84
|
hasRendered: true
|
|
99
85
|
});
|
|
100
86
|
}
|
|
101
|
-
|
|
102
87
|
this._matchListener = this.addMatchListener(this.props.query, this.updateMatches);
|
|
103
88
|
}
|
|
104
|
-
|
|
105
89
|
componentWillUnmount() {
|
|
106
90
|
this.removeMatchListener();
|
|
107
91
|
}
|
|
108
|
-
|
|
109
92
|
componentDidUpdate(prevProps) {
|
|
110
93
|
const _this$props = this.props,
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
94
|
+
match = _this$props.match,
|
|
95
|
+
query = _this$props.query;
|
|
114
96
|
if (match !== prevProps.match || !(0, _deepEqual.deepEqual)(query, prevProps.query)) {
|
|
115
97
|
this.removeMatchListener();
|
|
116
98
|
this._matchListener = this.addMatchListener(query, this.updateMatches, match);
|
|
117
99
|
}
|
|
118
100
|
}
|
|
119
|
-
|
|
120
101
|
addMatchListener(query, updateMatches) {
|
|
121
102
|
let match = arguments.length > 2 && arguments[2] !== void 0 ? arguments[2] : this.props.match;
|
|
122
103
|
const matchListener = match === 'element' ? _addElementQueryMatchListener.addElementQueryMatchListener : _addMediaQueryMatchListener.addMediaQueryMatchListener;
|
|
123
104
|
return matchListener(query, () => (0, _findDOMNode.findDOMNode)(this), updateMatches);
|
|
124
105
|
}
|
|
125
|
-
|
|
126
106
|
removeMatchListener() {
|
|
127
107
|
if (this._matchListener) {
|
|
128
108
|
this._matchListener.remove();
|
|
129
109
|
}
|
|
130
110
|
}
|
|
131
|
-
|
|
132
111
|
mergeProps(matches, props) {
|
|
133
112
|
if (!props) {
|
|
134
113
|
return null;
|
|
135
114
|
}
|
|
136
|
-
|
|
137
115
|
const mergedProps = {};
|
|
138
116
|
matches.forEach(match => {
|
|
139
|
-
const matchProps = props[match];
|
|
117
|
+
const matchProps = props[match];
|
|
118
|
+
|
|
119
|
+
// Iterate over the props for the current match. If that the prop is
|
|
140
120
|
// already in `mergedProps` that means that the prop was defined for
|
|
141
121
|
// multiple breakpoints, and more than one of those breakpoints is being
|
|
142
122
|
// currently applied so we log a warning.
|
|
143
|
-
|
|
144
123
|
Object.keys(matchProps).forEach(prop => {
|
|
145
124
|
const currentValue = mergedProps[prop];
|
|
146
125
|
(0, _console.warn)(!(prop in mergedProps), [`[Responsive] The prop \`${prop}\` is defined at 2 or more breakpoints`, `which are currently applied at the same time. Its current value, \`${currentValue}\`,`, `will be overwritten as \`${matchProps[prop]}\`.`].join(' '));
|
|
@@ -149,26 +128,24 @@ class Responsive extends _react.Component {
|
|
|
149
128
|
});
|
|
150
129
|
return mergedProps;
|
|
151
130
|
}
|
|
152
|
-
|
|
153
131
|
render() {
|
|
154
132
|
const _this$state = this.state,
|
|
155
|
-
|
|
156
|
-
|
|
133
|
+
matches = _this$state.matches,
|
|
134
|
+
hasRendered = _this$state.hasRendered;
|
|
157
135
|
const _this$props2 = this.props,
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
let renderFunc;
|
|
136
|
+
props = _this$props2.props,
|
|
137
|
+
render = _this$props2.render,
|
|
138
|
+
children = _this$props2.children,
|
|
139
|
+
display = _this$props2.display;
|
|
140
|
+
let renderFunc;
|
|
141
|
+
// Responsive needs to render once to measure the dom and obtain matches.
|
|
163
142
|
// Calling the render prop on this initial render can cause visual side
|
|
164
143
|
// effects and is slower than rendering an empty div.
|
|
165
|
-
|
|
166
144
|
if (hasRendered) {
|
|
167
145
|
// Render via the children or render method, whichever is supplied. If
|
|
168
146
|
// both are supplied, give preference to children.
|
|
169
147
|
renderFunc = children || render;
|
|
170
148
|
}
|
|
171
|
-
|
|
172
149
|
return /*#__PURE__*/_react.default.createElement("div", {
|
|
173
150
|
ref: this.handleRef,
|
|
174
151
|
style: {
|
|
@@ -176,9 +153,7 @@ class Responsive extends _react.Component {
|
|
|
176
153
|
}
|
|
177
154
|
}, renderFunc && renderFunc(this.mergeProps(matches, props), matches));
|
|
178
155
|
}
|
|
179
|
-
|
|
180
156
|
}
|
|
181
|
-
|
|
182
157
|
exports.Responsive = Responsive;
|
|
183
158
|
Responsive.displayName = "Responsive";
|
|
184
159
|
Responsive.allowedProps = _props.allowedProps;
|
package/lib/Responsive/props.js
CHANGED
|
@@ -1,16 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
|
|
3
3
|
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
|
|
4
|
-
|
|
5
4
|
Object.defineProperty(exports, "__esModule", {
|
|
6
5
|
value: true
|
|
7
6
|
});
|
|
8
7
|
exports.propTypes = exports.allowedProps = void 0;
|
|
9
|
-
|
|
10
8
|
var _propTypes = _interopRequireDefault(require("prop-types"));
|
|
11
|
-
|
|
12
9
|
var _ResponsivePropTypes = require("../ResponsivePropTypes");
|
|
13
|
-
|
|
14
10
|
/*
|
|
15
11
|
* The MIT License (MIT)
|
|
16
12
|
*
|
|
@@ -34,6 +30,7 @@ var _ResponsivePropTypes = require("../ResponsivePropTypes");
|
|
|
34
30
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
35
31
|
* SOFTWARE.
|
|
36
32
|
*/
|
|
33
|
+
|
|
37
34
|
const propTypes = {
|
|
38
35
|
match: _propTypes.default.oneOf(['element', 'media']),
|
|
39
36
|
query: _propTypes.default.objectOf(_ResponsivePropTypes.ResponsivePropTypes.validQuery).isRequired,
|
|
@@ -4,9 +4,7 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = exports.ResponsivePropTypes = void 0;
|
|
7
|
-
|
|
8
7
|
var _jsonToMediaQuery = require("./jsonToMediaQuery");
|
|
9
|
-
|
|
10
8
|
/*
|
|
11
9
|
* The MIT License (MIT)
|
|
12
10
|
*
|
|
@@ -30,6 +28,7 @@ var _jsonToMediaQuery = require("./jsonToMediaQuery");
|
|
|
30
28
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
31
29
|
* SOFTWARE.
|
|
32
30
|
*/
|
|
31
|
+
|
|
33
32
|
const ResponsivePropTypes = {
|
|
34
33
|
/**
|
|
35
34
|
* Verify that the given prop is a properly formatted query object
|
|
@@ -66,7 +65,6 @@ const ResponsivePropTypes = {
|
|
|
66
65
|
return new Error(`Invalid query prop supplied to \`${componentName}\`. ${e.message}`);
|
|
67
66
|
}
|
|
68
67
|
}
|
|
69
|
-
|
|
70
68
|
};
|
|
71
69
|
exports.ResponsivePropTypes = ResponsivePropTypes;
|
|
72
70
|
var _default = ResponsivePropTypes;
|