@instructure/ui-responsive 11.7.3 → 11.7.4-pr-snapshot-1781695314229

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.
Files changed (40) hide show
  1. package/CHANGELOG.md +8 -0
  2. package/LICENSE.md +1 -0
  3. package/{lib/Responsive/v1/props.js → babel.config.cjs} +12 -11
  4. package/es/Responsive/v1/index.js +5 -4
  5. package/es/exports/a.js +7 -6
  6. package/es/utils/v1/ResponsivePropTypes.js +2 -1
  7. package/es/utils/v1/addElementQueryMatchListener.js +1 -1
  8. package/es/utils/v1/addMediaQueryMatchListener.js +2 -1
  9. package/package.json +11 -16
  10. package/src/Responsive/v1/index.tsx +5 -4
  11. package/src/exports/a.ts +6 -6
  12. package/src/utils/v1/ResponsivePropTypes.ts +2 -2
  13. package/src/utils/v1/addElementQueryMatchListener.ts +2 -2
  14. package/src/utils/v1/addMediaQueryMatchListener.ts +2 -2
  15. package/src/utils/v1/jsonToMediaQuery.ts +1 -1
  16. package/src/utils/v1/parseQuery.ts +1 -1
  17. package/tsconfig.build.tsbuildinfo +1 -1
  18. package/types/Responsive/v1/index.d.ts +2 -1
  19. package/types/Responsive/v1/index.d.ts.map +1 -1
  20. package/types/exports/a.d.ts +6 -6
  21. package/types/exports/a.d.ts.map +1 -1
  22. package/types/utils/v1/ResponsivePropTypes.d.ts +1 -1
  23. package/types/utils/v1/ResponsivePropTypes.d.ts.map +1 -1
  24. package/types/utils/v1/addElementQueryMatchListener.d.ts +2 -2
  25. package/types/utils/v1/addElementQueryMatchListener.d.ts.map +1 -1
  26. package/types/utils/v1/addMediaQueryMatchListener.d.ts +1 -1
  27. package/types/utils/v1/addMediaQueryMatchListener.d.ts.map +1 -1
  28. package/types/utils/v1/jsonToMediaQuery.d.ts +1 -1
  29. package/types/utils/v1/jsonToMediaQuery.d.ts.map +1 -1
  30. package/types/utils/v1/parseQuery.d.ts +1 -1
  31. package/types/utils/v1/parseQuery.d.ts.map +1 -1
  32. package/lib/Responsive/v1/index.js +0 -167
  33. package/lib/exports/a.js +0 -47
  34. package/lib/package.json +0 -1
  35. package/lib/utils/v1/QueryType.js +0 -5
  36. package/lib/utils/v1/ResponsivePropTypes.js +0 -69
  37. package/lib/utils/v1/addElementQueryMatchListener.js +0 -166
  38. package/lib/utils/v1/addMediaQueryMatchListener.js +0 -114
  39. package/lib/utils/v1/jsonToMediaQuery.js +0 -86
  40. package/lib/utils/v1/parseQuery.js +0 -88
@@ -1,167 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.Responsive = void 0;
7
- var _react = require("react");
8
- var _deepEqual = require("@instructure/ui-utils/lib/deepEqual.js");
9
- var _console = require("@instructure/console");
10
- var _addElementQueryMatchListener = require("../../utils/v1/addElementQueryMatchListener");
11
- var _addMediaQueryMatchListener = require("../../utils/v1/addMediaQueryMatchListener");
12
- var _findDOMNode = require("@instructure/ui-dom-utils/lib/findDOMNode.js");
13
- var _props = require("./props");
14
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
15
- /*
16
- * The MIT License (MIT)
17
- *
18
- * Copyright (c) 2015 - present Instructure, Inc.
19
- *
20
- * Permission is hereby granted, free of charge, to any person obtaining a copy
21
- * of this software and associated documentation files (the "Software"), to deal
22
- * in the Software without restriction, including without limitation the rights
23
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
24
- * copies of the Software, and to permit persons to whom the Software is
25
- * furnished to do so, subject to the following conditions:
26
- *
27
- * The above copyright notice and this permission notice shall be included in all
28
- * copies or substantial portions of the Software.
29
- *
30
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
31
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
32
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
33
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
34
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
35
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
36
- * SOFTWARE.
37
- */
38
-
39
- /**
40
- ---
41
- category: components
42
- ---
43
-
44
- **/class Responsive extends _react.Component {
45
- static displayName = "Responsive";
46
- static allowedProps = _props.allowedProps;
47
- static defaultProps = {
48
- children: null,
49
- match: 'element',
50
- props: null
51
- };
52
- ref = null;
53
- _matchListener = null;
54
- state = {
55
- matches: [],
56
- hasRendered: false
57
- };
58
- handleRef = el => {
59
- const {
60
- elementRef
61
- } = this.props;
62
- this.ref = el;
63
- if (typeof elementRef === 'function') {
64
- elementRef(el);
65
- }
66
- };
67
- componentDidMount() {
68
- (0, _console.logError)(!!(this.props.render || this.props.children), `[Responsive] must have either a \`render\` prop or \`children\` prop.`);
69
- if (this.props.match === 'element') {
70
- // Because Responsive renders an empty div initially, it always needs to
71
- // re-render with the children provided. If there are no matches the match
72
- // listener won't trigger an update, so we handle this update explicitly.
73
- const initialMatches = (0, _addElementQueryMatchListener.updateElementMatches)(this.props.query, this) || [];
74
- this.setState({
75
- matches: initialMatches,
76
- hasRendered: true
77
- });
78
- } else {
79
- this.setState({
80
- hasRendered: true
81
- });
82
- }
83
- this._matchListener = this.addMatchListener(this.props.query, this.updateMatches);
84
- }
85
- componentWillUnmount() {
86
- this.removeMatchListener();
87
- }
88
- componentDidUpdate(prevProps) {
89
- const {
90
- match,
91
- query
92
- } = this.props;
93
- if (match !== prevProps.match || !(0, _deepEqual.deepEqual)(query, prevProps.query)) {
94
- this.removeMatchListener();
95
- this._matchListener = this.addMatchListener(query, this.updateMatches, match);
96
- }
97
- }
98
- addMatchListener(query, updateMatches, match = this.props.match) {
99
- const matchListener = match === 'element' ? _addElementQueryMatchListener.addElementQueryMatchListener : _addMediaQueryMatchListener.addMediaQueryMatchListener;
100
- return matchListener(query, () => (0, _findDOMNode.findDOMNode)(this), updateMatches);
101
- }
102
- removeMatchListener() {
103
- if (this._matchListener) {
104
- this._matchListener.remove();
105
- }
106
- }
107
- updateMatches = (matches, cb) => {
108
- this.setState({
109
- matches
110
- }, () => {
111
- if (typeof cb === 'function') {
112
- cb();
113
- }
114
- });
115
- };
116
- mergeProps(matches, props) {
117
- if (!props) {
118
- return null;
119
- }
120
- const mergedProps = {};
121
- matches.forEach(match => {
122
- const matchProps = props[match];
123
-
124
- // Iterate over the props for the current match. If that the prop is
125
- // already in `mergedProps` that means that the prop was defined for
126
- // multiple breakpoints, and more than one of those breakpoints is being
127
- // currently applied so we log a warning.
128
- Object.keys(matchProps).forEach(prop => {
129
- const currentValue = mergedProps[prop];
130
- (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(' '));
131
- mergedProps[prop] = matchProps[prop];
132
- });
133
- });
134
- return mergedProps;
135
- }
136
- render() {
137
- const {
138
- matches,
139
- hasRendered
140
- } = this.state;
141
- const {
142
- props,
143
- render,
144
- children,
145
- display
146
- } = this.props;
147
- let renderFunc;
148
- // Responsive needs to render once to measure the dom and obtain matches.
149
- // Calling the render prop on this initial render can cause visual side
150
- // effects and is slower than rendering an empty div.
151
- if (hasRendered) {
152
- // Render via the children or render method, whichever is supplied. If
153
- // both are supplied, give preference to children.
154
- renderFunc = children || render;
155
- }
156
- return (0, _jsxRuntime.jsx)("div", {
157
- ref: this.handleRef,
158
- style: {
159
- display
160
- },
161
- "data-cid": this.props['data-cid'],
162
- children: renderFunc && renderFunc(this.mergeProps(matches, props), matches)
163
- });
164
- }
165
- }
166
- exports.Responsive = Responsive;
167
- var _default = exports.default = Responsive;
package/lib/exports/a.js DELETED
@@ -1,47 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "Responsive", {
7
- enumerable: true,
8
- get: function () {
9
- return _v.Responsive;
10
- }
11
- });
12
- Object.defineProperty(exports, "ResponsivePropTypes", {
13
- enumerable: true,
14
- get: function () {
15
- return _ResponsivePropTypes.ResponsivePropTypes;
16
- }
17
- });
18
- Object.defineProperty(exports, "addElementQueryMatchListener", {
19
- enumerable: true,
20
- get: function () {
21
- return _addElementQueryMatchListener.addElementQueryMatchListener;
22
- }
23
- });
24
- Object.defineProperty(exports, "addMediaQueryMatchListener", {
25
- enumerable: true,
26
- get: function () {
27
- return _addMediaQueryMatchListener.addMediaQueryMatchListener;
28
- }
29
- });
30
- Object.defineProperty(exports, "jsonToMediaQuery", {
31
- enumerable: true,
32
- get: function () {
33
- return _jsonToMediaQuery.jsonToMediaQuery;
34
- }
35
- });
36
- Object.defineProperty(exports, "parseQuery", {
37
- enumerable: true,
38
- get: function () {
39
- return _parseQuery.parseQuery;
40
- }
41
- });
42
- var _v = require("../Responsive/v1");
43
- var _addElementQueryMatchListener = require("../utils/v1/addElementQueryMatchListener");
44
- var _addMediaQueryMatchListener = require("../utils/v1/addMediaQueryMatchListener");
45
- var _parseQuery = require("../utils/v1/parseQuery");
46
- var _jsonToMediaQuery = require("../utils/v1/jsonToMediaQuery");
47
- var _ResponsivePropTypes = require("../utils/v1/ResponsivePropTypes");
package/lib/package.json DELETED
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
@@ -1,69 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.ResponsivePropTypes = void 0;
7
- var _jsonToMediaQuery = require("./jsonToMediaQuery");
8
- /*
9
- * The MIT License (MIT)
10
- *
11
- * Copyright (c) 2015 - present Instructure, Inc.
12
- *
13
- * Permission is hereby granted, free of charge, to any person obtaining a copy
14
- * of this software and associated documentation files (the "Software"), to deal
15
- * in the Software without restriction, including without limitation the rights
16
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
17
- * copies of the Software, and to permit persons to whom the Software is
18
- * furnished to do so, subject to the following conditions:
19
- *
20
- * The above copyright notice and this permission notice shall be included in all
21
- * copies or substantial portions of the Software.
22
- *
23
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
24
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
26
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
28
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
- * SOFTWARE.
30
- */
31
-
32
- const ResponsivePropTypes = exports.ResponsivePropTypes = {
33
- /**
34
- * Verify that the given prop is a properly formatted query object
35
- * with exactly one key and one corresponding valid value of type
36
- * string or number. The object key should be one of `minWidth`,
37
- * `maxWidth`, `minHeight`, or `maxHeight`. Note that key values
38
- * should be camel cased.
39
- *
40
- *
41
- * The following are examples of valid query objects:
42
- *
43
- * ```js
44
- * { minWidth: 350 }
45
- * { maxHeight: '400px' }
46
- * { maxWidth: '20rem' }
47
- * ```
48
- *
49
- * The following are examples of invalid query objects:
50
- *
51
- * ```js
52
- * { minwidth: 300 } // should be minWidth (case sensitive)
53
- * { minHeight: 200, maxHeight: 300 } // should consist of exactly one key
54
- * ```
55
- * @param {Object} props - object containing the component props
56
- * @param {string} propName - name of the given prop
57
- * @param {string} componentName - name of the component
58
- * @returns {Error} if prop is an invalid query
59
- */
60
- validQuery(props, propName, componentName) {
61
- try {
62
- (0, _jsonToMediaQuery.jsonToMediaQuery)(props[propName]);
63
- return null;
64
- } catch (e) {
65
- return new Error(`Invalid query prop supplied to \`${componentName}\`. ${e.message}`);
66
- }
67
- }
68
- };
69
- var _default = exports.default = ResponsivePropTypes;
@@ -1,166 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.addElementQueryMatchListener = void 0;
7
- exports.updateElementMatches = updateElementMatches;
8
- var _parseQuery = require("./parseQuery");
9
- var _findDOMNode = require("@instructure/ui-dom-utils/lib/findDOMNode.js");
10
- var _getBoundingClientRect = require("@instructure/ui-dom-utils/lib/getBoundingClientRect.js");
11
- var _debounce = require("@instructure/debounce");
12
- /*
13
- * The MIT License (MIT)
14
- *
15
- * Copyright (c) 2015 - present Instructure, Inc.
16
- *
17
- * Permission is hereby granted, free of charge, to any person obtaining a copy
18
- * of this software and associated documentation files (the "Software"), to deal
19
- * in the Software without restriction, including without limitation the rights
20
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
21
- * copies of the Software, and to permit persons to whom the Software is
22
- * furnished to do so, subject to the following conditions:
23
- *
24
- * The above copyright notice and this permission notice shall be included in all
25
- * copies or substantial portions of the Software.
26
- *
27
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
28
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
29
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
30
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
31
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
32
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33
- * SOFTWARE.
34
- */
35
-
36
- /**
37
- * ---
38
- * category: utilities/layout
39
- * ---
40
- * Given an object of named queries, listens for changes in the
41
- * element size and notifies which queries match via a function
42
- * callback. The callback method is only called when the query
43
- * matches change, not on all element resizes. (If you are looking
44
- * to call a method on all element resizes use
45
- * [ResizeObserver](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) instead)
46
- *
47
- *
48
- * This function shares an interface with
49
- * [addMediaQueryMatchListener](addMediaQueryMatchListener)
50
- * so that they can be used interchangeably.
51
- *
52
- * The [Responsive](Responsive) component with the `match` prop
53
- * set to `element` utilizes this function. This is a low level
54
- * utility method and, in most cases, [Responsive](Responsive)
55
- * should be used instead.
56
- *
57
- * ```js-code
58
- * class MyComponent extends Component {
59
- * state = {
60
- * matches: []
61
- * }
62
- *
63
- * _listener = null
64
- *
65
- * componentDidMount () {
66
- * const query = {myFirstQuery: { minWidth: 100 }, mySecondQuery: { maxHeight: '30rem'}}
67
- * const el = findDOMNode(this)
68
- *
69
- * this._listener = addElementQueryMatchListener(query, el, this.updateMatches)
70
- * }
71
- *
72
- * componentWillUnmount () {
73
- * if (this._listener) {
74
- * this._listener.remove()
75
- * }
76
- * }
77
- *
78
- * updateMatches = (matches) => {
79
- * this.setState({ matches })
80
- * }
81
- * ...
82
- * }
83
- * ```
84
- * @module addElementQueryMatchListener
85
- * @param {Object} query - object consisting of names and query objects
86
- * @param {Node|Window|React.ReactElement|React.Component|function} el - a DOM node or a function returning a DOM node
87
- * @param {function} cb - called with an array of the names of the currently
88
- * matching queries whenever a matching query changes
89
- * @returns {function} remove() function to call to remove the listener
90
- */
91
- const addElementQueryMatchListener = (query, el, cb) => {
92
- const node = typeof el === 'function' ? el() : el;
93
- const {
94
- width,
95
- height
96
- } = (0, _getBoundingClientRect.getBoundingClientRect)(node);
97
- let matches = [];
98
- const update = size => {
99
- const newMatches = updateElementMatches(query, node, matches, size);
100
- if (newMatches) {
101
- matches = newMatches;
102
- cb(matches);
103
- }
104
- return;
105
- };
106
- const debounced = (0, _debounce.debounce)(update, 100, {
107
- leading: false,
108
- trailing: true
109
- });
110
- const elementResizeListener = new ResizeObserver(entries => {
111
- for (const entry of entries) {
112
- const {
113
- width: newWidth,
114
- height: newHeight
115
- } = entry.contentRect;
116
- if (width !== newWidth) {
117
- debounced({
118
- width: newWidth,
119
- height: newHeight
120
- });
121
- }
122
- }
123
- });
124
- elementResizeListener.observe(node);
125
- update({
126
- width,
127
- height
128
- });
129
- return {
130
- remove() {
131
- if (elementResizeListener) {
132
- elementResizeListener.disconnect();
133
- }
134
- if (debounced) {
135
- debounced.cancel();
136
- }
137
- }
138
- };
139
- };
140
- exports.addElementQueryMatchListener = addElementQueryMatchListener;
141
- function updateElementMatches(query, el, matches = [], size) {
142
- const node = (0, _findDOMNode.findDOMNode)(el);
143
- let {
144
- width,
145
- height
146
- } = size || (0, _getBoundingClientRect.getBoundingClientRect)(node);
147
-
148
- // round dimensions to make sure matcher can match queries with integer values
149
- width = Math.floor(width);
150
- height = Math.floor(height);
151
- const matchingQueries = (0, _parseQuery.parseQuery)(query, node)({
152
- width,
153
- height
154
- });
155
- const newMatches = Object.keys(matchingQueries).filter(key => matchingQueries[key]).map(key => key);
156
-
157
- // only return matches if they have changed
158
- if (matches.length !== newMatches.length) {
159
- return newMatches;
160
- }
161
- if (matches.filter(match => newMatches.indexOf(match) === -1).length > 0) {
162
- return newMatches;
163
- }
164
- return null;
165
- }
166
- var _default = exports.default = addElementQueryMatchListener;
@@ -1,114 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = exports.addMediaQueryMatchListener = void 0;
7
- var _matchMedia = require("@instructure/ui-dom-utils/lib/matchMedia.js");
8
- var _jsonToMediaQuery = require("./jsonToMediaQuery");
9
- /*
10
- * The MIT License (MIT)
11
- *
12
- * Copyright (c) 2015 - present Instructure, Inc.
13
- *
14
- * Permission is hereby granted, free of charge, to any person obtaining a copy
15
- * of this software and associated documentation files (the "Software"), to deal
16
- * in the Software without restriction, including without limitation the rights
17
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
- * copies of the Software, and to permit persons to whom the Software is
19
- * furnished to do so, subject to the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be included in all
22
- * copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
- * SOFTWARE.
31
- */
32
-
33
- /**
34
- * ---
35
- * category: utilities/layout
36
- * ---
37
- * Given an object of named queries, listens for changes in the
38
- * window size and notifies which queries match via a function
39
- * callback. The callback method is only called when the query
40
- * matches change, not on all window resizes.
41
- *
42
- * This function shares an interface with
43
- * [addElementQueryMatchListener](addElementQueryMatchListener)
44
- * so that they can be used interchangeably.
45
- *
46
- * The [Responsive](Responsive) component with the `match` prop
47
- * set to `media` utilizes this function. This is a low level utility
48
- * method and, in most cases, [Responsive](Responsive) should be
49
- * used instead.
50
- *
51
- * ```js-code
52
- * class MyComponent extends Component {
53
- * state = {
54
- * matches: []
55
- * }
56
- *
57
- * _listener = null
58
- *
59
- * componentDidMount () {
60
- * const query = {myFirstQuery: { minWidth: 100 }, mySecondQuery: { maxHeight: '30rem'}}
61
- * const el = findDOMNode(this)
62
- *
63
- * this._listener = addMediaQueryMatchListener(query, el, this.updateMatches)
64
- * }
65
- *
66
- * componentWillUnmount () {
67
- * if (this._listener) {
68
- * this._listener.remove()
69
- * }
70
- * }
71
- *
72
- * updateMatches = (matches) => {
73
- * this.setState({ matches })
74
- * }
75
- * ...
76
- * }
77
- * ```
78
- * @module addMediaQueryMatchListener
79
- * @param {Object} query - object consisting of names and query objects
80
- * @param {Node|Window|React.ReactElement|React.Component|function} el - a DOM node or a function returning a DOM node
81
- * @param {function} cb - called with an array of the names of the currently matching queries whenever a matching query changes
82
- * @param {object} matchMedia - called with an array of the names of the currently matching queries whenever a matching query changes
83
- * @returns {function} remove() function to call to remove the listener
84
- */
85
- const addMediaQueryMatchListener = (query, el, cb, matchMedia = _matchMedia.matchMedia) => {
86
- const node = typeof el === 'function' ? el() : el;
87
- const updateMediaMatches = mediaQueryLists => {
88
- const matches = Object.keys(mediaQueryLists).filter(key => mediaQueryLists[key].matches).map(key => key);
89
- cb(matches);
90
- };
91
- const mediaQueryLists = {};
92
- const listenerCallback = () => {
93
- updateMediaMatches(mediaQueryLists);
94
- };
95
- Object.keys(query).forEach(key => {
96
- const mediaQueryList = matchMedia((0, _jsonToMediaQuery.jsonToMediaQuery)(query[key], node), node);
97
- if (mediaQueryList) {
98
- mediaQueryList.addListener(listenerCallback);
99
- mediaQueryLists[key] = mediaQueryList;
100
- }
101
- });
102
- updateMediaMatches(mediaQueryLists);
103
- return {
104
- remove() {
105
- if (mediaQueryLists) {
106
- Object.keys(mediaQueryLists).forEach(key => {
107
- mediaQueryLists[key].removeListener(listenerCallback);
108
- });
109
- }
110
- }
111
- };
112
- };
113
- exports.addMediaQueryMatchListener = addMediaQueryMatchListener;
114
- var _default = exports.default = addMediaQueryMatchListener;
@@ -1,86 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.jsonToMediaQuery = jsonToMediaQuery;
8
- var _px = require("@instructure/ui-utils/lib/px.js");
9
- /*
10
- * The MIT License (MIT)
11
- *
12
- * Copyright (c) 2015 - present Instructure, Inc.
13
- *
14
- * Permission is hereby granted, free of charge, to any person obtaining a copy
15
- * of this software and associated documentation files (the "Software"), to deal
16
- * in the Software without restriction, including without limitation the rights
17
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
18
- * copies of the Software, and to permit persons to whom the Software is
19
- * furnished to do so, subject to the following conditions:
20
- *
21
- * The above copyright notice and this permission notice shall be included in all
22
- * copies or substantial portions of the Software.
23
- *
24
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
25
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
26
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
27
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
28
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
29
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30
- * SOFTWARE.
31
- */
32
-
33
- /**
34
- * ---
35
- * category: utilities/layout
36
- * ---
37
- *
38
- * Given a valid json query object, converts it to a standard media query
39
- * string. Valid queries should be an object consisting of condition:breakpoint
40
- * pairs with the following condition types `minWidth`, `maxWidth`, `minHeight`,
41
- * or `maxHeight` where breakpoint value is either a string or a number.
42
- *
43
- * Example input
44
- * ```js-code
45
- * { minWidth: 350, maxWidth: 600 }
46
- * ```
47
- * Example output
48
- * ```js-code
49
- * '(min-width: 350px) and (max-Width: 600px)'
50
- * ```
51
- * @module jsonToMediaQuery
52
- * @param {Object} query - an object consisting of the query type and value
53
- * @param {Document | Window | Node | React.ReactElement | React.Component} el - component or DOM node which will be passed to the pixel conversion if the unit type is `em`
54
- * @returns {string} media query string
55
- */
56
- function jsonToMediaQuery(query, el) {
57
- // Ensure the query is of the correct form
58
- const keys = Object.keys(query);
59
- if (keys.length > 4) {
60
- throw new Error('Expected maximum 4 keys in query object.');
61
- }
62
- let mediaQuery = '';
63
- const QUERY_SEPARATOR = 'and';
64
- for (const key of keys) {
65
- const validKeys = ['minHeight', 'maxHeight', 'minWidth', 'maxWidth'];
66
- if (validKeys.indexOf(key) === -1) {
67
- throw new Error(`Invalid key \`${key}\` in query object. Valid keys should consist of one of the following: ` + `${validKeys.join(', ')} (case sensitive)`);
68
- }
69
- const value = query[key];
70
-
71
- // TS catches this, but we want to throw error in runtime too
72
- if (typeof value !== 'string' && typeof value !== 'number') {
73
- throw new Error('The value of the query object must be a string or number.');
74
- }
75
- if (!value) {
76
- throw new Error('No value supplied for query object');
77
- }
78
- mediaQuery += `(${hyphenateQueryKey(key)}: ${(0, _px.px)(value, el)}px) ${QUERY_SEPARATOR} `;
79
- }
80
- return mediaQuery.slice(0, -QUERY_SEPARATOR.length - 2);
81
- }
82
- function hyphenateQueryKey(key) {
83
- const lowerCaseKey = key.toLowerCase();
84
- return lowerCaseKey.slice(0, 3) + '-' + lowerCaseKey.slice(3);
85
- }
86
- var _default = exports.default = jsonToMediaQuery;