@instructure/ui-dom-utils 8.55.2-snapshot-0 → 9.0.0

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/types/index.d.ts CHANGED
@@ -2,11 +2,9 @@ export { addEventListener } from './addEventListener';
2
2
  export { addInputModeListener } from './addInputModeListener';
3
3
  export { addPositionChangeListener } from './addPositionChangeListener';
4
4
  export type { PositionChangeListenerType } from './addPositionChangeListener';
5
- export { addResizeListener } from './addResizeListener';
6
5
  export { canUseDOM } from './canUseDOM';
7
6
  export { contains } from './contains';
8
7
  export { containsActiveElement } from './containsActiveElement';
9
- export { elementMatches } from './elementMatches';
10
8
  export { findDOMNode } from './findDOMNode';
11
9
  export { findFocusable } from './findFocusable';
12
10
  export { findTabbable } from './findTabbable';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAA;AACvE,YAAY,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAA;AAC7E,OAAO,EAAE,iBAAiB,EAAE,MAAM,qBAAqB,CAAA;AACvD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,EAAE,cAAc,EAAE,MAAM,kBAAkB,CAAA;AACjD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,YAAY,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AACxE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAyBA,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAA;AAC7D,OAAO,EAAE,yBAAyB,EAAE,MAAM,6BAA6B,CAAA;AACvE,YAAY,EAAE,0BAA0B,EAAE,MAAM,6BAA6B,CAAA;AAC7E,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AACrC,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,YAAY,EAAE,QAAQ,EAAE,MAAM,yBAAyB,CAAA;AACvD,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAA;AAC7C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,gBAAgB,EAAE,MAAM,oBAAoB,CAAA;AACrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,eAAe,EAAE,MAAM,mBAAmB,CAAA;AACnD,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAA;AACvC,OAAO,EAAE,aAAa,EAAE,MAAM,iBAAiB,CAAA;AAC/C,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAA;AAC3C,OAAO,EAAE,qBAAqB,EAAE,MAAM,yBAAyB,CAAA;AAC/D,YAAY,EAAE,yBAAyB,EAAE,MAAM,yBAAyB,CAAA;AACxE,OAAO,EAAE,kBAAkB,EAAE,MAAM,sBAAsB,CAAA;AACzD,OAAO,EAAE,UAAU,EAAE,MAAM,cAAc,CAAA"}
@@ -1,64 +0,0 @@
1
- var _div;
2
- /*
3
- * The MIT License (MIT)
4
- *
5
- * Copyright (c) 2015 - present Instructure, Inc.
6
- *
7
- * Permission is hereby granted, free of charge, to any person obtaining a copy
8
- * of this software and associated documentation files (the "Software"), to deal
9
- * in the Software without restriction, including without limitation the rights
10
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
- * copies of the Software, and to permit persons to whom the Software is
12
- * furnished to do so, subject to the following conditions:
13
- *
14
- * The above copyright notice and this permission notice shall be included in all
15
- * copies or substantial portions of the Software.
16
- *
17
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
- * SOFTWARE.
24
- */
25
-
26
- import React from 'react';
27
- import { render, waitFor } from '@testing-library/react';
28
- import '@testing-library/jest-dom';
29
- import { addResizeListener } from '../addResizeListener';
30
- const mockRect = {
31
- top: 0,
32
- left: 0,
33
- right: 0,
34
- bottom: 0,
35
- width: 0,
36
- height: 0,
37
- x: 0,
38
- y: 0,
39
- toJSON: jest.fn()
40
- };
41
- describe('addResizeListener', () => {
42
- it('should provide a remove method', async () => {
43
- const callback = jest.fn();
44
- Element.prototype.getBoundingClientRect = jest.fn(() => mockRect);
45
- const _render = render(_div || (_div = /*#__PURE__*/React.createElement("div", null))),
46
- container = _render.container;
47
- const node = container.firstChild;
48
- const listener = addResizeListener(node, callback);
49
-
50
- // Manually trigger a size change (since JSDOM won't)
51
- Element.prototype.getBoundingClientRect = jest.fn(() => {
52
- return {
53
- ...mockRect,
54
- width: 200
55
- };
56
- });
57
- await waitFor(() => {
58
- expect(callback).toHaveBeenCalledTimes(1);
59
- expect(typeof listener.remove).toBe('function');
60
- });
61
- listener.remove();
62
- jest.restoreAllMocks();
63
- });
64
- });
@@ -1,93 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { findDOMNode } from './findDOMNode';
26
- import { getBoundingClientRect } from './getBoundingClientRect';
27
- import { requestAnimationFrame } from './requestAnimationFrame';
28
- /**
29
- * ---
30
- * category: utilities/DOM
31
- * ---
32
- * ### This utility is deprecated since version __8.0__ and will be permanently deleted in version 9.0.
33
- * *Please use the native [DOM ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) instead of this.*
34
- *
35
- *
36
- * Example usage of the `ResizeObserver`:
37
- * ```js-code
38
- * const observer = new ResizeObserver((entries) => {
39
- * for (let entry of entries) {
40
- * const size = {
41
- * width: entry.contentRect.width
42
- * }
43
- *
44
- * if (size.width !== origSize.width) {
45
- * // call your handler function here
46
- * this._debounced(size)
47
- * }
48
- * }
49
- * })
50
- *
51
- * observer.observe(content)
52
- * ```
53
- *
54
- * Adds a listener to an element and calls a specified handler function whenever the size changes.
55
- * @deprecated since version 8.0
56
- * @module
57
- *
58
- *
59
- * @param { Node | Window | React.ReactElement | React.Component | function } el - component or DOM node
60
- * @param {function} handler - function to run when resize occurs
61
- * @param dimensions
62
- * @returns {function} remove - cancel the listener and no longer execute the handler function
63
- */
64
- function addResizeListener(el, handler, dimensions = ['width']) {
65
- const node = findDOMNode(el);
66
- let origSize = getBoundingClientRect(node);
67
- let cancelled = false;
68
- let raf;
69
- const checkDimensions = () => {
70
- if (cancelled) {
71
- return;
72
- }
73
- const boundingRect = getBoundingClientRect(node);
74
- const size = {
75
- width: boundingRect.width,
76
- height: boundingRect.height
77
- };
78
- if (dimensions.some(dim => size[dim] != origSize[dim]) && typeof handler === 'function') {
79
- handler(size);
80
- }
81
- origSize = size;
82
- raf = requestAnimationFrame(checkDimensions);
83
- };
84
- checkDimensions();
85
- return {
86
- remove() {
87
- cancelled = true;
88
- raf.cancel();
89
- }
90
- };
91
- }
92
- export default addResizeListener;
93
- export { addResizeListener };
@@ -1,46 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { findDOMNode } from './findDOMNode';
26
- /**
27
- * ---
28
- * category: utilities/DOM
29
- * ---
30
- *
31
- * Polyfill for Element.matches (https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)
32
- * DEPRECATED This polyfill is only needed for old browsers and will be removed in InstUI v9
33
- * @module elementMatches
34
- * @param el - component or DOM node
35
- * @param selectorString - a string representing the selector to test
36
- * @returns if the element would be selected by the specified selector string
37
- */
38
- function elementMatches(el, selectorString) {
39
- const node = el && findDOMNode(el);
40
- if (!node) return false;
41
- // this cast is in a separate line to circumvent a react-docgen bug
42
- const elem = node;
43
- return elem.matches(selectorString);
44
- }
45
- export default elementMatches;
46
- export { elementMatches };
@@ -1,66 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- var _react = _interopRequireDefault(require("react"));
5
- var _react2 = require("@testing-library/react");
6
- require("@testing-library/jest-dom");
7
- var _addResizeListener = require("../addResizeListener");
8
- var _div;
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
- const mockRect = {
33
- top: 0,
34
- left: 0,
35
- right: 0,
36
- bottom: 0,
37
- width: 0,
38
- height: 0,
39
- x: 0,
40
- y: 0,
41
- toJSON: jest.fn()
42
- };
43
- describe('addResizeListener', () => {
44
- it('should provide a remove method', async () => {
45
- const callback = jest.fn();
46
- Element.prototype.getBoundingClientRect = jest.fn(() => mockRect);
47
- const _render = (0, _react2.render)(_div || (_div = /*#__PURE__*/_react.default.createElement("div", null))),
48
- container = _render.container;
49
- const node = container.firstChild;
50
- const listener = (0, _addResizeListener.addResizeListener)(node, callback);
51
-
52
- // Manually trigger a size change (since JSDOM won't)
53
- Element.prototype.getBoundingClientRect = jest.fn(() => {
54
- return {
55
- ...mockRect,
56
- width: 200
57
- };
58
- });
59
- await (0, _react2.waitFor)(() => {
60
- expect(callback).toHaveBeenCalledTimes(1);
61
- expect(typeof listener.remove).toBe('function');
62
- });
63
- listener.remove();
64
- jest.restoreAllMocks();
65
- });
66
- });
@@ -1,99 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.addResizeListener = addResizeListener;
7
- exports.default = void 0;
8
- var _findDOMNode = require("./findDOMNode");
9
- var _getBoundingClientRect = require("./getBoundingClientRect");
10
- var _requestAnimationFrame = require("./requestAnimationFrame");
11
- /*
12
- * The MIT License (MIT)
13
- *
14
- * Copyright (c) 2015 - present Instructure, Inc.
15
- *
16
- * Permission is hereby granted, free of charge, to any person obtaining a copy
17
- * of this software and associated documentation files (the "Software"), to deal
18
- * in the Software without restriction, including without limitation the rights
19
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
20
- * copies of the Software, and to permit persons to whom the Software is
21
- * furnished to do so, subject to the following conditions:
22
- *
23
- * The above copyright notice and this permission notice shall be included in all
24
- * copies or substantial portions of the Software.
25
- *
26
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
29
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
30
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
31
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32
- * SOFTWARE.
33
- */
34
-
35
- /**
36
- * ---
37
- * category: utilities/DOM
38
- * ---
39
- * ### This utility is deprecated since version __8.0__ and will be permanently deleted in version 9.0.
40
- * *Please use the native [DOM ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) instead of this.*
41
- *
42
- *
43
- * Example usage of the `ResizeObserver`:
44
- * ```js-code
45
- * const observer = new ResizeObserver((entries) => {
46
- * for (let entry of entries) {
47
- * const size = {
48
- * width: entry.contentRect.width
49
- * }
50
- *
51
- * if (size.width !== origSize.width) {
52
- * // call your handler function here
53
- * this._debounced(size)
54
- * }
55
- * }
56
- * })
57
- *
58
- * observer.observe(content)
59
- * ```
60
- *
61
- * Adds a listener to an element and calls a specified handler function whenever the size changes.
62
- * @deprecated since version 8.0
63
- * @module
64
- *
65
- *
66
- * @param { Node | Window | React.ReactElement | React.Component | function } el - component or DOM node
67
- * @param {function} handler - function to run when resize occurs
68
- * @param dimensions
69
- * @returns {function} remove - cancel the listener and no longer execute the handler function
70
- */
71
- function addResizeListener(el, handler, dimensions = ['width']) {
72
- const node = (0, _findDOMNode.findDOMNode)(el);
73
- let origSize = (0, _getBoundingClientRect.getBoundingClientRect)(node);
74
- let cancelled = false;
75
- let raf;
76
- const checkDimensions = () => {
77
- if (cancelled) {
78
- return;
79
- }
80
- const boundingRect = (0, _getBoundingClientRect.getBoundingClientRect)(node);
81
- const size = {
82
- width: boundingRect.width,
83
- height: boundingRect.height
84
- };
85
- if (dimensions.some(dim => size[dim] != origSize[dim]) && typeof handler === 'function') {
86
- handler(size);
87
- }
88
- origSize = size;
89
- raf = (0, _requestAnimationFrame.requestAnimationFrame)(checkDimensions);
90
- };
91
- checkDimensions();
92
- return {
93
- remove() {
94
- cancelled = true;
95
- raf.cancel();
96
- }
97
- };
98
- }
99
- var _default = exports.default = addResizeListener;
@@ -1,52 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.elementMatches = elementMatches;
8
- var _findDOMNode = require("./findDOMNode");
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/DOM
36
- * ---
37
- *
38
- * Polyfill for Element.matches (https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)
39
- * DEPRECATED This polyfill is only needed for old browsers and will be removed in InstUI v9
40
- * @module elementMatches
41
- * @param el - component or DOM node
42
- * @param selectorString - a string representing the selector to test
43
- * @returns if the element would be selected by the specified selector string
44
- */
45
- function elementMatches(el, selectorString) {
46
- const node = el && (0, _findDOMNode.findDOMNode)(el);
47
- if (!node) return false;
48
- // this cast is in a separate line to circumvent a react-docgen bug
49
- const elem = node;
50
- return elem.matches(selectorString);
51
- }
52
- var _default = exports.default = elementMatches;
@@ -1,65 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import React from 'react'
26
- import { render, waitFor } from '@testing-library/react'
27
- import '@testing-library/jest-dom'
28
- import { addResizeListener } from '../addResizeListener'
29
-
30
- const mockRect = {
31
- top: 0,
32
- left: 0,
33
- right: 0,
34
- bottom: 0,
35
- width: 0,
36
- height: 0,
37
- x: 0,
38
- y: 0,
39
- toJSON: jest.fn()
40
- }
41
-
42
- describe('addResizeListener', () => {
43
- it('should provide a remove method', async () => {
44
- const callback = jest.fn()
45
- Element.prototype.getBoundingClientRect = jest.fn(() => mockRect as DOMRect)
46
-
47
- const { container } = render(<div />)
48
- const node = container.firstChild as HTMLDivElement
49
-
50
- const listener = addResizeListener(node, callback)
51
-
52
- // Manually trigger a size change (since JSDOM won't)
53
- Element.prototype.getBoundingClientRect = jest.fn(() => {
54
- return { ...mockRect, width: 200 } as DOMRect
55
- })
56
-
57
- await waitFor(() => {
58
- expect(callback).toHaveBeenCalledTimes(1)
59
- expect(typeof listener.remove).toBe('function')
60
- })
61
-
62
- listener.remove()
63
- jest.restoreAllMocks()
64
- })
65
- })
@@ -1,116 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { findDOMNode } from './findDOMNode'
26
- import { getBoundingClientRect, RectType } from './getBoundingClientRect'
27
- import {
28
- requestAnimationFrame,
29
- RequestAnimationFrameType
30
- } from './requestAnimationFrame'
31
- import { UIElement } from '@instructure/shared-types'
32
-
33
- type ResizeListenerType = { remove: () => void }
34
- type DimensionTypes = 'width' | 'height'
35
-
36
- /**
37
- * ---
38
- * category: utilities/DOM
39
- * ---
40
- * ### This utility is deprecated since version __8.0__ and will be permanently deleted in version 9.0.
41
- * *Please use the native [DOM ResizeObserver API](https://developer.mozilla.org/en-US/docs/Web/API/ResizeObserver) instead of this.*
42
- *
43
- *
44
- * Example usage of the `ResizeObserver`:
45
- * ```js-code
46
- * const observer = new ResizeObserver((entries) => {
47
- * for (let entry of entries) {
48
- * const size = {
49
- * width: entry.contentRect.width
50
- * }
51
- *
52
- * if (size.width !== origSize.width) {
53
- * // call your handler function here
54
- * this._debounced(size)
55
- * }
56
- * }
57
- * })
58
- *
59
- * observer.observe(content)
60
- * ```
61
- *
62
- * Adds a listener to an element and calls a specified handler function whenever the size changes.
63
- * @deprecated since version 8.0
64
- * @module
65
- *
66
- *
67
- * @param { Node | Window | React.ReactElement | React.Component | function } el - component or DOM node
68
- * @param {function} handler - function to run when resize occurs
69
- * @param dimensions
70
- * @returns {function} remove - cancel the listener and no longer execute the handler function
71
- */
72
- function addResizeListener(
73
- el: UIElement,
74
- handler: (dimensions: Record<DimensionTypes, number>) => any,
75
- dimensions: DimensionTypes[] = ['width']
76
- ): ResizeListenerType {
77
- const node = findDOMNode(el)
78
- let origSize: Partial<RectType> = getBoundingClientRect(node)
79
- let cancelled = false
80
- let raf: RequestAnimationFrameType
81
-
82
- const checkDimensions = () => {
83
- if (cancelled) {
84
- return
85
- }
86
-
87
- const boundingRect = getBoundingClientRect(node)
88
- const size: Record<DimensionTypes, number> = {
89
- width: boundingRect.width,
90
- height: boundingRect.height
91
- }
92
-
93
- if (
94
- dimensions.some((dim) => size[dim] != origSize[dim]) &&
95
- typeof handler === 'function'
96
- ) {
97
- handler(size)
98
- }
99
-
100
- origSize = size
101
-
102
- raf = requestAnimationFrame(checkDimensions)
103
- }
104
-
105
- checkDimensions()
106
-
107
- return {
108
- remove() {
109
- cancelled = true
110
- raf.cancel()
111
- }
112
- }
113
- }
114
-
115
- export default addResizeListener
116
- export { addResizeListener }
@@ -1,49 +0,0 @@
1
- /*
2
- * The MIT License (MIT)
3
- *
4
- * Copyright (c) 2015 - present Instructure, Inc.
5
- *
6
- * Permission is hereby granted, free of charge, to any person obtaining a copy
7
- * of this software and associated documentation files (the "Software"), to deal
8
- * in the Software without restriction, including without limitation the rights
9
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
- * copies of the Software, and to permit persons to whom the Software is
11
- * furnished to do so, subject to the following conditions:
12
- *
13
- * The above copyright notice and this permission notice shall be included in all
14
- * copies or substantial portions of the Software.
15
- *
16
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
- * SOFTWARE.
23
- */
24
-
25
- import { findDOMNode } from './findDOMNode'
26
- import type { UIElement } from '@instructure/shared-types'
27
-
28
- /**
29
- * ---
30
- * category: utilities/DOM
31
- * ---
32
- *
33
- * Polyfill for Element.matches (https://developer.mozilla.org/en-US/docs/Web/API/Element/matches)
34
- * DEPRECATED This polyfill is only needed for old browsers and will be removed in InstUI v9
35
- * @module elementMatches
36
- * @param el - component or DOM node
37
- * @param selectorString - a string representing the selector to test
38
- * @returns if the element would be selected by the specified selector string
39
- */
40
- function elementMatches(el: UIElement, selectorString: string) {
41
- const node = el && findDOMNode(el)
42
- if (!node) return false
43
- // this cast is in a separate line to circumvent a react-docgen bug
44
- const elem: Element = node as Element
45
- return elem.matches(selectorString)
46
- }
47
-
48
- export default elementMatches
49
- export { elementMatches }
@@ -1,2 +0,0 @@
1
- import '@testing-library/jest-dom';
2
- //# sourceMappingURL=addResizeListener.test.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"addResizeListener.test.d.ts","sourceRoot":"","sources":["../../src/__new-tests__/addResizeListener.test.tsx"],"names":[],"mappings":"AA0BA,OAAO,2BAA2B,CAAA"}