@instructure/ui-responsive 11.6.0 → 11.6.1-pr-snapshot-1779894247159

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 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
+ ## [11.6.1-pr-snapshot-1779894247159](https://github.com/instructure/instructure-ui/compare/v11.6.0...v11.6.1-pr-snapshot-1779894247159) (2026-05-27)
7
+
8
+ **Note:** Version bump only for package @instructure/ui-responsive
9
+
10
+
11
+
12
+
13
+
6
14
  # [11.6.0](https://github.com/instructure/instructure-ui/compare/v11.5.0...v11.6.0) (2026-02-18)
7
15
 
8
16
  **Note:** Version bump only for package @instructure/ui-responsive
@@ -1,9 +1,3 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.allowedProps = void 0;
7
1
  /*
8
2
  * The MIT License (MIT)
9
3
  *
@@ -28,8 +22,16 @@ exports.allowedProps = void 0;
28
22
  * SOFTWARE.
29
23
  */
30
24
 
31
- /**
32
- * Consists of an object where the keys match the breakpoint names used in the query. The values are objects with keys representing prop names and values representing prop values Ex. `{small: { myProp: 'fillscreen' }, large: { myProp: 'fillcontainer' }}`
33
- */
34
-
35
- const allowedProps = exports.allowedProps = ['match', 'query', 'props', 'render', 'children', 'display', 'elementRef'];
25
+ module.exports = {
26
+ presets: [
27
+ [
28
+ require('@instructure/ui-babel-preset'),
29
+ {
30
+ coverage: Boolean(process.env.COVERAGE),
31
+ esModules: Boolean(process.env.ES_MODULES),
32
+ removeConsole: process.env.NODE_ENV === 'production',
33
+ transformImports: Boolean(process.env.TRANSFORM_IMPORTS)
34
+ }
35
+ ]
36
+ ]
37
+ }
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@instructure/ui-responsive",
3
- "version": "11.6.0",
3
+ "version": "11.6.1-pr-snapshot-1779894247159",
4
+ "type": "module",
4
5
  "description": "A component that allows for rendering a component differently based on either the element or the viewport size",
5
6
  "author": "Instructure, Inc. Engineering and Product Design",
6
7
  "module": "./es/index.js",
7
- "main": "./lib/index.js",
8
8
  "types": "./types/index.d.ts",
9
9
  "repository": {
10
10
  "type": "git",
@@ -15,19 +15,19 @@
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
17
  "@babel/runtime": "^7.27.6",
18
- "@instructure/console": "11.6.0",
19
- "@instructure/shared-types": "11.6.0",
20
- "@instructure/ui-dom-utils": "11.6.0",
21
- "@instructure/ui-react-utils": "11.6.0",
22
- "@instructure/debounce": "11.6.0",
23
- "@instructure/ui-utils": "11.6.0"
18
+ "@instructure/console": "11.6.1-pr-snapshot-1779894247159",
19
+ "@instructure/debounce": "11.6.1-pr-snapshot-1779894247159",
20
+ "@instructure/shared-types": "11.6.1-pr-snapshot-1779894247159",
21
+ "@instructure/ui-dom-utils": "11.6.1-pr-snapshot-1779894247159",
22
+ "@instructure/ui-react-utils": "11.6.1-pr-snapshot-1779894247159",
23
+ "@instructure/ui-utils": "11.6.1-pr-snapshot-1779894247159"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@testing-library/jest-dom": "^6.6.3",
27
27
  "@testing-library/react": "15.0.7",
28
28
  "vitest": "^3.2.2",
29
- "@instructure/ui-color-utils": "11.6.0",
30
- "@instructure/ui-babel-preset": "11.6.0"
29
+ "@instructure/ui-babel-preset": "11.6.1-pr-snapshot-1779894247159",
30
+ "@instructure/ui-color-utils": "11.6.1-pr-snapshot-1779894247159"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": ">=18 <=19"
@@ -40,10 +40,8 @@
40
40
  ".": {
41
41
  "types": "./types/index.d.ts",
42
42
  "import": "./es/index.js",
43
- "require": "./lib/index.js",
44
43
  "default": "./es/index.js"
45
44
  },
46
- "./lib/*": "./lib/*",
47
45
  "./es/*": "./es/*",
48
46
  "./types/*": "./types/*",
49
47
  "./package.json": "./package.json",
@@ -53,7 +51,7 @@
53
51
  "lint": "ui-scripts lint",
54
52
  "lint:fix": "ui-scripts lint --fix",
55
53
  "clean": "ui-scripts clean",
56
- "build": "ui-scripts build --modules es,cjs",
54
+ "build": "ui-scripts build",
57
55
  "build:watch": "pnpm run ts:check -- --watch & ui-scripts build --watch",
58
56
  "build:types": "tsc -p tsconfig.build.json",
59
57
  "ts:check": "tsc -p tsconfig.build.json --noEmit --emitDeclarationOnly false"
package/lib/QueryType.js DELETED
@@ -1,5 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
@@ -1,165 +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("../addElementQueryMatchListener");
11
- var _addMediaQueryMatchListener = require("../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
- constructor(...args) {
46
- super(...args);
47
- this.ref = null;
48
- this._matchListener = null;
49
- this.state = {
50
- matches: [],
51
- hasRendered: false
52
- };
53
- this.handleRef = el => {
54
- const elementRef = this.props.elementRef;
55
- this.ref = el;
56
- if (typeof elementRef === 'function') {
57
- elementRef(el);
58
- }
59
- };
60
- this.updateMatches = (matches, cb) => {
61
- this.setState({
62
- matches
63
- }, () => {
64
- if (typeof cb === 'function') {
65
- cb();
66
- }
67
- });
68
- };
69
- }
70
- componentDidMount() {
71
- (0, _console.logError)(!!(this.props.render || this.props.children), `[Responsive] must have either a \`render\` prop or \`children\` prop.`);
72
- if (this.props.match === 'element') {
73
- // Because Responsive renders an empty div initially, it always needs to
74
- // re-render with the children provided. If there are no matches the match
75
- // listener won't trigger an update, so we handle this update explicitly.
76
- const initialMatches = (0, _addElementQueryMatchListener.updateElementMatches)(this.props.query, this) || [];
77
- this.setState({
78
- matches: initialMatches,
79
- hasRendered: true
80
- });
81
- } else {
82
- this.setState({
83
- hasRendered: true
84
- });
85
- }
86
- this._matchListener = this.addMatchListener(this.props.query, this.updateMatches);
87
- }
88
- componentWillUnmount() {
89
- this.removeMatchListener();
90
- }
91
- componentDidUpdate(prevProps) {
92
- const _this$props = this.props,
93
- match = _this$props.match,
94
- query = _this$props.query;
95
- if (match !== prevProps.match || !(0, _deepEqual.deepEqual)(query, prevProps.query)) {
96
- this.removeMatchListener();
97
- this._matchListener = this.addMatchListener(query, this.updateMatches, match);
98
- }
99
- }
100
- addMatchListener(query, updateMatches, match = this.props.match) {
101
- const matchListener = match === 'element' ? _addElementQueryMatchListener.addElementQueryMatchListener : _addMediaQueryMatchListener.addMediaQueryMatchListener;
102
- return matchListener(query, () => (0, _findDOMNode.findDOMNode)(this), updateMatches);
103
- }
104
- removeMatchListener() {
105
- if (this._matchListener) {
106
- this._matchListener.remove();
107
- }
108
- }
109
- mergeProps(matches, props) {
110
- if (!props) {
111
- return null;
112
- }
113
- const mergedProps = {};
114
- matches.forEach(match => {
115
- const matchProps = props[match];
116
-
117
- // Iterate over the props for the current match. If that the prop is
118
- // already in `mergedProps` that means that the prop was defined for
119
- // multiple breakpoints, and more than one of those breakpoints is being
120
- // currently applied so we log a warning.
121
- Object.keys(matchProps).forEach(prop => {
122
- const currentValue = mergedProps[prop];
123
- (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(' '));
124
- mergedProps[prop] = matchProps[prop];
125
- });
126
- });
127
- return mergedProps;
128
- }
129
- render() {
130
- const _this$state = this.state,
131
- matches = _this$state.matches,
132
- hasRendered = _this$state.hasRendered;
133
- const _this$props2 = this.props,
134
- props = _this$props2.props,
135
- render = _this$props2.render,
136
- children = _this$props2.children,
137
- display = _this$props2.display;
138
- let renderFunc;
139
- // Responsive needs to render once to measure the dom and obtain matches.
140
- // Calling the render prop on this initial render can cause visual side
141
- // effects and is slower than rendering an empty div.
142
- if (hasRendered) {
143
- // Render via the children or render method, whichever is supplied. If
144
- // both are supplied, give preference to children.
145
- renderFunc = children || render;
146
- }
147
- return (0, _jsxRuntime.jsx)("div", {
148
- ref: this.handleRef,
149
- style: {
150
- display
151
- },
152
- "data-cid": this.props['data-cid'],
153
- children: renderFunc && renderFunc(this.mergeProps(matches, props), matches)
154
- });
155
- }
156
- }
157
- exports.Responsive = Responsive;
158
- Responsive.displayName = "Responsive";
159
- Responsive.allowedProps = _props.allowedProps;
160
- Responsive.defaultProps = {
161
- children: null,
162
- match: 'element',
163
- props: null
164
- };
165
- var _default = exports.default = Responsive;
@@ -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,163 +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 _getBoundingClientRec = (0, _getBoundingClientRect.getBoundingClientRect)(node),
94
- width = _getBoundingClientRec.width,
95
- height = _getBoundingClientRec.height;
96
- let matches = [];
97
- const update = size => {
98
- const newMatches = updateElementMatches(query, node, matches, size);
99
- if (newMatches) {
100
- matches = newMatches;
101
- cb(matches);
102
- }
103
- return;
104
- };
105
- const debounced = (0, _debounce.debounce)(update, 100, {
106
- leading: false,
107
- trailing: true
108
- });
109
- const elementResizeListener = new ResizeObserver(entries => {
110
- for (const entry of entries) {
111
- const _entry$contentRect = entry.contentRect,
112
- newWidth = _entry$contentRect.width,
113
- newHeight = _entry$contentRect.height;
114
- if (width !== newWidth) {
115
- debounced({
116
- width: newWidth,
117
- height: newHeight
118
- });
119
- }
120
- }
121
- });
122
- elementResizeListener.observe(node);
123
- update({
124
- width,
125
- height
126
- });
127
- return {
128
- remove() {
129
- if (elementResizeListener) {
130
- elementResizeListener.disconnect();
131
- }
132
- if (debounced) {
133
- debounced.cancel();
134
- }
135
- }
136
- };
137
- };
138
- exports.addElementQueryMatchListener = addElementQueryMatchListener;
139
- function updateElementMatches(query, el, matches = [], size) {
140
- const node = (0, _findDOMNode.findDOMNode)(el);
141
- let _ref = size || (0, _getBoundingClientRect.getBoundingClientRect)(node),
142
- width = _ref.width,
143
- height = _ref.height;
144
-
145
- // round dimensions to make sure matcher can match queries with integer values
146
- width = Math.floor(width);
147
- height = Math.floor(height);
148
- const matchingQueries = (0, _parseQuery.parseQuery)(query, node)({
149
- width,
150
- height
151
- });
152
- const newMatches = Object.keys(matchingQueries).filter(key => matchingQueries[key]).map(key => key);
153
-
154
- // only return matches if they have changed
155
- if (matches.length !== newMatches.length) {
156
- return newMatches;
157
- }
158
- if (matches.filter(match => newMatches.indexOf(match) === -1).length > 0) {
159
- return newMatches;
160
- }
161
- return null;
162
- }
163
- 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;
package/lib/index.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 _Responsive.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 _Responsive = require("./Responsive");
43
- var _addElementQueryMatchListener = require("./addElementQueryMatchListener");
44
- var _addMediaQueryMatchListener = require("./addMediaQueryMatchListener");
45
- var _parseQuery = require("./parseQuery");
46
- var _jsonToMediaQuery = require("./jsonToMediaQuery");
47
- var _ResponsivePropTypes = require("./ResponsivePropTypes");
@@ -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;
package/lib/package.json DELETED
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
package/lib/parseQuery.js DELETED
@@ -1,85 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.parseQuery = parseQuery;
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
- * An object consisting of query names and boolean true false if it matches
35
- */
36
-
37
- /**
38
- * ---
39
- * category: utilities/layout
40
- * ---
41
- *
42
- * Given a query and an element, return a function that takes
43
- * size as an argument and returns an object consisting of
44
- * query names and a boolean true false if it matches.
45
- * @module parseQuery
46
- * @param {Object} query - an object consisting of query names
47
- * mapped to individual queries
48
- * @param {Document | Window | Node | null} el - component or DOM node
49
- * @returns {function} takes size {width, height} as an argument
50
- * and returns an object consisting of query names and boolean
51
- * true false if it matches
52
- */
53
- function parseQuery(query, el) {
54
- const rules = {};
55
-
56
- // converting values to numerical values and adding initial values to all query keys
57
- Object.keys(query).forEach(selectorName => {
58
- const _query$selectorName = query[selectorName],
59
- minWidth = _query$selectorName.minWidth,
60
- maxWidth = _query$selectorName.maxWidth,
61
- minHeight = _query$selectorName.minHeight,
62
- maxHeight = _query$selectorName.maxHeight;
63
- rules[selectorName] = {
64
- minWidth: minWidth && (0, _px.px)(minWidth, el) || 0,
65
- maxWidth: maxWidth && (0, _px.px)(maxWidth, el) || Infinity,
66
- minHeight: minHeight && (0, _px.px)(minHeight, el) || 0,
67
- maxHeight: maxHeight && (0, _px.px)(maxHeight, el) || Infinity
68
- };
69
- });
70
- return function ({
71
- width,
72
- height
73
- }) {
74
- const selectorMap = {};
75
- Object.entries(rules).forEach(([selectorName, query]) => {
76
- const minWidth = query.minWidth,
77
- maxWidth = query.maxWidth,
78
- minHeight = query.minHeight,
79
- maxHeight = query.maxHeight;
80
- selectorMap[selectorName] = minWidth <= width && width <= maxWidth && minHeight <= height && height <= maxHeight;
81
- });
82
- return selectorMap;
83
- };
84
- }
85
- var _default = exports.default = parseQuery;