@instructure/ui-react-utils 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-react-utils
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-react-utils
@@ -1,10 +1,3 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.getDisplayName = getDisplayName;
8
1
  /*
9
2
  * The MIT License (MIT)
10
3
  *
@@ -29,17 +22,16 @@ exports.getDisplayName = getDisplayName;
29
22
  * SOFTWARE.
30
23
  */
31
24
 
32
- /**
33
- * ---
34
- * category: utilities/react
35
- * ---
36
- * Get the displayName of a React component.
37
- * needs a babel plugin to work https://github.com/facebook/react/issues/4915 !!
38
- * @module getDisplayName
39
- * @param ReactComponent
40
- * @returns the component displayName
41
- */
42
- function getDisplayName(ReactComponent) {
43
- return typeof ReactComponent === 'string' ? ReactComponent : ReactComponent.displayName || ReactComponent.name;
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
+ ]
44
37
  }
45
- var _default = exports.default = getDisplayName;
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@instructure/ui-react-utils",
3
- "version": "11.6.0",
3
+ "version": "11.6.1-pr-snapshot-1779894247159",
4
+ "type": "module",
4
5
  "description": "A React utility library made by Instructure Inc.",
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",
@@ -17,17 +17,17 @@
17
17
  "@babel/runtime": "^7.27.6",
18
18
  "@emotion/is-prop-valid": "^1.3.1",
19
19
  "hoist-non-react-statics": "^3.3.2",
20
- "@instructure/shared-types": "11.6.0",
21
- "@instructure/ui-decorator": "11.6.0",
22
- "@instructure/console": "11.6.0",
23
- "@instructure/ui-dom-utils": "11.6.0",
24
- "@instructure/ui-utils": "11.6.0"
20
+ "@instructure/console": "11.6.1-pr-snapshot-1779894247159",
21
+ "@instructure/shared-types": "11.6.1-pr-snapshot-1779894247159",
22
+ "@instructure/ui-decorator": "11.6.1-pr-snapshot-1779894247159",
23
+ "@instructure/ui-dom-utils": "11.6.1-pr-snapshot-1779894247159",
24
+ "@instructure/ui-utils": "11.6.1-pr-snapshot-1779894247159"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@testing-library/jest-dom": "^6.6.3",
28
28
  "@testing-library/react": "15.0.7",
29
29
  "vitest": "^3.2.2",
30
- "@instructure/ui-babel-preset": "11.6.0"
30
+ "@instructure/ui-babel-preset": "11.6.1-pr-snapshot-1779894247159"
31
31
  },
32
32
  "peerDependencies": {
33
33
  "react": ">=18 <=19",
@@ -41,10 +41,8 @@
41
41
  ".": {
42
42
  "types": "./types/index.d.ts",
43
43
  "import": "./es/index.js",
44
- "require": "./lib/index.js",
45
44
  "default": "./es/index.js"
46
45
  },
47
- "./lib/*": "./lib/*",
48
46
  "./es/*": "./es/*",
49
47
  "./types/*": "./types/*",
50
48
  "./package.json": "./package.json",
@@ -54,7 +52,7 @@
54
52
  "lint": "ui-scripts lint",
55
53
  "lint:fix": "ui-scripts lint --fix",
56
54
  "clean": "ui-scripts clean",
57
- "build": "ui-scripts build --modules es,cjs",
55
+ "build": "ui-scripts build",
58
56
  "build:watch": "ui-scripts build --watch",
59
57
  "build:types": "tsc -p tsconfig.build.json"
60
58
  }
@@ -1,50 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.defaultDeterministicIDMap = exports.DeterministicIdContext = void 0;
8
- var _react = _interopRequireDefault(require("react"));
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
- const instUIInstanceCounter = '__INSTUI_GLOBAL_INSTANCE_COUNTER__';
34
-
35
- /**
36
- * Returns a global (window-level) instance counter map.
37
- * This needs to be global so that IDs are unique across application instances,
38
- * e.g. in module federation applications are loaded as a .js blob, this method
39
- * makes sure that there are no duplicate IDs across instances.
40
- */
41
- function generateInstanceCounterMap() {
42
- if (globalThis[instUIInstanceCounter]) {
43
- return globalThis[instUIInstanceCounter];
44
- }
45
- const map = new Map();
46
- globalThis[instUIInstanceCounter] = map;
47
- return map;
48
- }
49
- const defaultDeterministicIDMap = exports.defaultDeterministicIDMap = generateInstanceCounterMap();
50
- const DeterministicIdContext = exports.DeterministicIdContext = /*#__PURE__*/_react.default.createContext(defaultDeterministicIDMap);
@@ -1,54 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.DeterministicIdContextProvider = void 0;
7
- var _DeterministicIdContext = require("./DeterministicIdContext");
8
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
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: components/contexts
36
- * ---
37
- * WARNING: providing the `instanceCounterMap` prop will result in unexpected behaviour. DO NOT USE IT!
38
- *
39
- * DEPRECATED: the `instanceCounterMap` prop is deprecated. You don't need to supply the
40
- * `instanceCounterMap` to the component. It handles it internally.
41
- *
42
- * This utility component is for wrapping components with `DeterministicIdContext.Provider`
43
- * See detailed documentation about how to use it: [InstUISettingsProvider](/#InstUISettingsProvider)
44
- */
45
- const DeterministicIdContextProvider = ({
46
- children,
47
- instanceCounterMap = _DeterministicIdContext.defaultDeterministicIDMap
48
- }) => {
49
- return (0, _jsxRuntime.jsx)(_DeterministicIdContext.DeterministicIdContext.Provider, {
50
- value: instanceCounterMap,
51
- children: children
52
- });
53
- };
54
- exports.DeterministicIdContextProvider = DeterministicIdContextProvider;
@@ -1,26 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "DeterministicIdContext", {
7
- enumerable: true,
8
- get: function () {
9
- return _DeterministicIdContext.DeterministicIdContext;
10
- }
11
- });
12
- Object.defineProperty(exports, "DeterministicIdContextProvider", {
13
- enumerable: true,
14
- get: function () {
15
- return _DeterministicIdContextProvider.DeterministicIdContextProvider;
16
- }
17
- });
18
- Object.defineProperty(exports, "withDeterministicId", {
19
- enumerable: true,
20
- get: function () {
21
- return _withDeterministicId.withDeterministicId;
22
- }
23
- });
24
- var _DeterministicIdContextProvider = require("./DeterministicIdContextProvider");
25
- var _DeterministicIdContext = require("./DeterministicIdContext");
26
- var _withDeterministicId = require("./withDeterministicId");
@@ -1,75 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.withDeterministicId = exports.default = void 0;
8
- var _react = require("react");
9
- var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
10
- var _DeterministicIdContext = require("./DeterministicIdContext");
11
- var _decorator = require("@instructure/ui-decorator/lib/decorator.js");
12
- var _generateId = require("@instructure/ui-utils/lib/generateId.js");
13
- var _console = require("@instructure/console");
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
- * This decorator is used to enable the decorated class to use the `DeterministicIdContext` which is needed
41
- * for deterministic id generation.
42
- *
43
- * The context is there for the users to pass an `instanceCounterMap` Map which is then used
44
- * in the child components to deterministically create ids for them based on the `instanceCounterMap`.
45
- * Read more about it here: [SSR guide](https://instructure.design/#server-side-rendering)
46
- */
47
- const withDeterministicId = exports.withDeterministicId = (0, _decorator.decorator)(ComposedComponent => {
48
- const WithDeterministicId = /*#__PURE__*/(0, _react.forwardRef)((props, ref) => {
49
- const componentName = ComposedComponent.componentId || ComposedComponent.displayName || ComposedComponent.name;
50
- const instanceCounterMap = (0, _react.useContext)(_DeterministicIdContext.DeterministicIdContext);
51
- const deterministicId = (instanceName = componentName) => (0, _generateId.generateId)(instanceName, instanceCounterMap);
52
- if (props.deterministicId) {
53
- (0, _console.warn)(false, `Manually passing the "deterministicId" property is not allowed on the ${componentName} component.\n`, props.deterministicId);
54
- }
55
- return (0, _jsxRuntime.jsx)(ComposedComponent, {
56
- ref: ref,
57
- deterministicId: deterministicId,
58
- ...props
59
- });
60
- });
61
- (0, _hoistNonReactStatics.default)(WithDeterministicId, ComposedComponent);
62
-
63
- // These static fields exist on InstUI components
64
- //@ts-expect-error fix this
65
- WithDeterministicId.allowedProps = ComposedComponent.allowedProps;
66
-
67
- // added so it can be tested with ReactTestUtils
68
- // more info: https://github.com/facebook/react/issues/13455
69
- WithDeterministicId.originalType = ComposedComponent.originalType || ComposedComponent;
70
- if (process.env.NODE_ENV !== 'production') {
71
- WithDeterministicId.displayName = `WithDeterministicId(${ComposedComponent.displayName})`;
72
- }
73
- return WithDeterministicId;
74
- });
75
- var _default = exports.default = withDeterministicId;
@@ -1,57 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.callRenderProp = callRenderProp;
8
- exports.default = void 0;
9
- var _react = _interopRequireDefault(require("react"));
10
- /*
11
- * The MIT License (MIT)
12
- *
13
- * Copyright (c) 2015 - present Instructure, Inc.
14
- *
15
- * Permission is hereby granted, free of charge, to any person obtaining a copy
16
- * of this software and associated documentation files (the "Software"), to deal
17
- * in the Software without restriction, including without limitation the rights
18
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
- * copies of the Software, and to permit persons to whom the Software is
20
- * furnished to do so, subject to the following conditions:
21
- *
22
- * The above copyright notice and this permission notice shall be included in all
23
- * copies or substantial portions of the Software.
24
- *
25
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- * SOFTWARE.
32
- */
33
-
34
- /**
35
- * ---
36
- * category: utilities/react
37
- * ---
38
- * Evaluate a provided value to create a renderable React element.
39
- * @module callRenderProp
40
- * @param value
41
- * @param props
42
- */
43
- function callRenderProp(value, props = {}) {
44
- if (typeof value === 'function') {
45
- // In react 16, `createElement` accepts a function. In react 15 we get an
46
- // error on rendering the result. Evaluate the function here if it is not a
47
- // react component.
48
- // fat arrow functions don't have a prototype
49
- if (!(value.prototype && value.prototype.isReactComponent)) {
50
- return value(props);
51
- }
52
- // TODO type 'value' properly
53
- return /*#__PURE__*/_react.default.createElement(value, props);
54
- }
55
- return value;
56
- }
57
- var _default = exports.default = callRenderProp;
@@ -1,62 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.ensureSingleChild = ensureSingleChild;
8
- var _react = require("react");
9
- var _safeCloneElement = require("./safeCloneElement");
10
- var _jsxRuntime = require("@emotion/react/jsx-runtime");
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/react
38
- * ---
39
- *
40
- * Ensure a single child. If it is a child of length 1, return a
41
- * cloned instance of the child. If it is a child of length > 1,
42
- * wrap in a span and return the child. Return null if child has
43
- * no length.
44
- * @module ensureSingleChild
45
- * @param child
46
- * @param props props for child
47
- * @returns cloned instance for a single child, or children wrapped in a span
48
- */function ensureSingleChild(child, props = {}) {
49
- const childCount = _react.Children.count(child);
50
- if (childCount === 0) {
51
- return null;
52
- } else if (typeof child === 'string' && child.length > 0 || childCount > 1) {
53
- return (0, _jsxRuntime.jsx)("span", {
54
- ...props,
55
- children: child
56
- });
57
- } else {
58
- // TODO: check that we can only end up here if child is ReactElement
59
- return (0, _safeCloneElement.safeCloneElement)(Array.isArray(child) ? child[0] : child, props);
60
- }
61
- }
62
- var _default = exports.default = ensureSingleChild;
@@ -1,78 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.getElementType = getElementType;
8
- var _console = require("@instructure/console");
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
- * A props object that is searched for some fields to determine the
35
- * element's type.
36
- */
37
-
38
- /**
39
- * ---
40
- * category: utilities/react
41
- * ---
42
- * Get the React element type for a component. It uses the following logic:
43
- * 1. type defined by the `as` prop
44
- * 2. type returned by the `getDefault()` parameter
45
- * 3. `<a>` if it has a `href` or `to` prop
46
- * 4. `<button>` if it has an `onClick` prop
47
- * 5. the component's defaultProp's `as` field
48
- * 6. `<span>` if none of the above
49
- *
50
- * @module getElementType
51
- * @param Component
52
- * @param props
53
- * @param getDefault an optional function that returns the default element type
54
- * @returns the element type
55
- */
56
- function getElementType(Component, props, getDefault) {
57
- var _Component$defaultPro, _Component$defaultPro2;
58
- if (props.as && props.as !== ((_Component$defaultPro = Component.defaultProps) === null || _Component$defaultPro === void 0 ? void 0 : _Component$defaultPro.as)) {
59
- return props.as;
60
- }
61
- if (typeof getDefault === 'function') {
62
- return getDefault();
63
- }
64
- if (props.href) {
65
- return 'a';
66
- }
67
- if (props.to) {
68
- (0, _console.logWarn)(
69
- // if to prop is used without as
70
- !props.as, `[${Component.displayName}] \`as\` prop should be provided when using \`to\``);
71
- return 'a';
72
- }
73
- if (typeof props.onClick === 'function') {
74
- return 'button';
75
- }
76
- return ((_Component$defaultPro2 = Component.defaultProps) === null || _Component$defaultPro2 === void 0 ? void 0 : _Component$defaultPro2.as) || 'span';
77
- }
78
- var _default = exports.default = getElementType;
@@ -1,60 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.getInteraction = getInteraction;
7
- /*
8
- * The MIT License (MIT)
9
- *
10
- * Copyright (c) 2015 - present Instructure, Inc.
11
- *
12
- * Permission is hereby granted, free of charge, to any person obtaining a copy
13
- * of this software and associated documentation files (the "Software"), to deal
14
- * in the Software without restriction, including without limitation the rights
15
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
- * copies of the Software, and to permit persons to whom the Software is
17
- * furnished to do so, subject to the following conditions:
18
- *
19
- * The above copyright notice and this permission notice shall be included in all
20
- * copies or substantial portions of the Software.
21
- *
22
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
- * SOFTWARE.
29
- */
30
-
31
- /**
32
- * ---
33
- * category: utilities/react
34
- * ---
35
- * Parse the component props for the interaction type. It will return one of 'enabled', 'disabled', or 'readonly'.
36
- * This is useful for form elements where consumers are able to either use the interaction prop as specified or the
37
- * native html disabled or readonly attributes
38
- * @module getInteraction
39
- * @param args extra arguments
40
- * @returns The calculated interaction type
41
- */
42
- function getInteraction({
43
- props = {},
44
- interactionTypes = ['disabled', 'readonly']
45
- } = {}) {
46
- const interaction = props.interaction,
47
- disabled = props.disabled,
48
- readOnly = props.readOnly;
49
-
50
- // interaction is the source of truth when it is provided
51
- if (interaction) return interaction;
52
-
53
- // if there's no interaction specified, use disabled or readOnly if they are defined as props (note, disabled will win
54
- // over readOnly if both are provided)
55
- if (interactionTypes.includes('disabled') && disabled) return 'disabled';
56
- if (interactionTypes.includes('readonly') && readOnly) return 'readonly';
57
-
58
- // We were provided with nothing, use enabled by default
59
- return 'enabled';
60
- }
package/lib/index.js DELETED
@@ -1,101 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- Object.defineProperty(exports, "DeterministicIdContext", {
7
- enumerable: true,
8
- get: function () {
9
- return _DeterministicIdContext.DeterministicIdContext;
10
- }
11
- });
12
- Object.defineProperty(exports, "DeterministicIdContextProvider", {
13
- enumerable: true,
14
- get: function () {
15
- return _DeterministicIdContext.DeterministicIdContextProvider;
16
- }
17
- });
18
- Object.defineProperty(exports, "callRenderProp", {
19
- enumerable: true,
20
- get: function () {
21
- return _callRenderProp.callRenderProp;
22
- }
23
- });
24
- Object.defineProperty(exports, "ensureSingleChild", {
25
- enumerable: true,
26
- get: function () {
27
- return _ensureSingleChild.ensureSingleChild;
28
- }
29
- });
30
- Object.defineProperty(exports, "getDisplayName", {
31
- enumerable: true,
32
- get: function () {
33
- return _getDisplayName.getDisplayName;
34
- }
35
- });
36
- Object.defineProperty(exports, "getElementType", {
37
- enumerable: true,
38
- get: function () {
39
- return _getElementType.getElementType;
40
- }
41
- });
42
- Object.defineProperty(exports, "getInteraction", {
43
- enumerable: true,
44
- get: function () {
45
- return _getInteraction.getInteraction;
46
- }
47
- });
48
- Object.defineProperty(exports, "matchComponentTypes", {
49
- enumerable: true,
50
- get: function () {
51
- return _matchComponentTypes.matchComponentTypes;
52
- }
53
- });
54
- Object.defineProperty(exports, "omitProps", {
55
- enumerable: true,
56
- get: function () {
57
- return _omitProps.omitProps;
58
- }
59
- });
60
- Object.defineProperty(exports, "passthroughProps", {
61
- enumerable: true,
62
- get: function () {
63
- return _passthroughProps.passthroughProps;
64
- }
65
- });
66
- Object.defineProperty(exports, "pickProps", {
67
- enumerable: true,
68
- get: function () {
69
- return _pickProps.pickProps;
70
- }
71
- });
72
- Object.defineProperty(exports, "safeCloneElement", {
73
- enumerable: true,
74
- get: function () {
75
- return _safeCloneElement.safeCloneElement;
76
- }
77
- });
78
- Object.defineProperty(exports, "windowMessageListener", {
79
- enumerable: true,
80
- get: function () {
81
- return _windowMessageListener.windowMessageListener;
82
- }
83
- });
84
- Object.defineProperty(exports, "withDeterministicId", {
85
- enumerable: true,
86
- get: function () {
87
- return _DeterministicIdContext.withDeterministicId;
88
- }
89
- });
90
- var _callRenderProp = require("./callRenderProp");
91
- var _ensureSingleChild = require("./ensureSingleChild");
92
- var _getDisplayName = require("./getDisplayName");
93
- var _getElementType = require("./getElementType");
94
- var _getInteraction = require("./getInteraction");
95
- var _matchComponentTypes = require("./matchComponentTypes");
96
- var _omitProps = require("./omitProps");
97
- var _passthroughProps = require("./passthroughProps");
98
- var _pickProps = require("./pickProps");
99
- var _safeCloneElement = require("./safeCloneElement");
100
- var _windowMessageListener = require("./windowMessageListener");
101
- var _DeterministicIdContext = require("./DeterministicIdContext");
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.matchComponentTypes = matchComponentTypes;
8
- var _getDisplayName = require("./getDisplayName");
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/react
36
- * ---
37
- * Check if a React component instance (React element) matches one of the
38
- * specified types.
39
- *
40
- * @module matchComponentTypes
41
- * @param componentInstance
42
- * @param types an array of React components
43
- * @returns true if the component matches at least one of the types
44
- */
45
- function matchComponentTypes(componentInstance, types = []) {
46
- if (componentInstance && componentInstance.type) {
47
- const displayNames = types.map(type => (0, _getDisplayName.getDisplayName)(type));
48
- return displayNames.indexOf((0, _getDisplayName.getDisplayName)(componentInstance.type)) >= 0;
49
- } else {
50
- return false;
51
- }
52
- }
53
- var _default = exports.default = matchComponentTypes;
package/lib/omitProps.js DELETED
@@ -1,72 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.omitProps = omitProps;
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
- /**
33
- * ---
34
- * category: utilities/react
35
- * ---
36
- * Return an object with the remaining props after the given props are omitted.
37
- *
38
- * Automatically excludes the following props:
39
- * `theme`, `children`, `className`, `style`, `styles`, `makeStyles`,
40
- * `themeOverride`, `deterministicId`
41
- * @module omitProps
42
- * @param props The object to process
43
- * @param propsToOmit list disallowed prop keys or an object whose
44
- * keys will be omitted. If not specified the automatically excluded
45
- * props will be removed.
46
- * @param exclude an optional array of disallowed prop names to omit
47
- * @returns props object without the excluded props
48
- * @module omitProps
49
- */
50
- function omitProps(props, propsToOmit, exclude) {
51
- const propKeys = Array.isArray(propsToOmit) ? propsToOmit : Object.keys(propsToOmit || {});
52
- const keysToOmit = exclude ? propKeys.concat(exclude) : propKeys;
53
- return omit(props, keysToOmit);
54
- }
55
- const hasOwnProperty = Object.prototype.hasOwnProperty;
56
- const omit = (originalObject, keysToOmit) => {
57
- // code based on babel's _objectWithoutProperties
58
- const newObject = {};
59
- for (const key in originalObject) {
60
- // special case because we always want to omit these and === is faster than
61
- // concat'ing them in
62
- if (key === 'theme' || key === 'children' || key === 'className' || key === 'style' || key === 'styles' || key === 'makeStyles' || key === 'themeOverride' || key === 'deterministicId') {
63
- continue;
64
- }
65
- if (keysToOmit.includes(key) || !hasOwnProperty.call(originalObject, key)) {
66
- continue;
67
- }
68
- newObject[key] = originalObject[key];
69
- }
70
- return newObject;
71
- };
72
- var _default = exports.default = omitProps;
package/lib/package.json DELETED
@@ -1 +0,0 @@
1
- {"type":"commonjs"}
@@ -1,53 +0,0 @@
1
- "use strict";
2
-
3
- var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
4
- Object.defineProperty(exports, "__esModule", {
5
- value: true
6
- });
7
- exports.default = void 0;
8
- exports.passthroughProps = passthroughProps;
9
- var _isPropValid = _interopRequireDefault(require("@emotion/is-prop-valid"));
10
- /*
11
- * The MIT License (MIT)
12
- *
13
- * Copyright (c) 2015 - present Instructure, Inc.
14
- *
15
- * Permission is hereby granted, free of charge, to any person obtaining a copy
16
- * of this software and associated documentation files (the "Software"), to deal
17
- * in the Software without restriction, including without limitation the rights
18
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
19
- * copies of the Software, and to permit persons to whom the Software is
20
- * furnished to do so, subject to the following conditions:
21
- *
22
- * The above copyright notice and this permission notice shall be included in all
23
- * copies or substantial portions of the Software.
24
- *
25
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
29
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
30
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
31
- * SOFTWARE.
32
- */
33
- // list of "valid" props https://github.com/emotion-js/emotion/blob/master/packages/is-prop-valid/src/props.js
34
-
35
- /**
36
- * Removes disallowed keys from the given object. Allowed keys: Valid props for
37
- * HTML or SVG elements (see https://github.com/emotion-js/emotion/tree/main/packages/is-prop-valid)
38
- *
39
- * Disallowed is anything else and 'style', 'styles', 'className', 'children',
40
- * 'makeStyles', 'deterministicId'
41
- * @param props The props to process
42
- */
43
- function passthroughProps(props) {
44
- const validProps = {};
45
- Object.keys(props)
46
- // style and className need to be explicitly passed through
47
- // styles and makeStyle can not pass through
48
- .filter(propName => (0, _isPropValid.default)(propName) && propName !== 'style' && propName !== 'className' && propName !== 'children' && propName !== 'styles' && propName !== 'makeStyles' && propName !== 'deterministicId').forEach(propName => {
49
- validProps[propName] = props[propName];
50
- });
51
- return validProps;
52
- }
53
- var _default = exports.default = passthroughProps;
package/lib/pickProps.js DELETED
@@ -1,64 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.pickProps = pickProps;
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
- /**
33
- * ---
34
- * category: utilities/react
35
- * ---
36
- * Return a props object with only specified propTypes.
37
- *
38
- * @param props React component props
39
- * @param propTypesOrAllowedPropList React component propTypes or the list of allowed prop keys
40
- * @param include an optional array of prop names to include
41
- * @returns props object with only the included props
42
- * @module pickProps
43
- */
44
- function pickProps(props, propTypesOrAllowedPropList, include) {
45
- const propKeys = Array.isArray(propTypesOrAllowedPropList) ? propTypesOrAllowedPropList : Object.keys(propTypesOrAllowedPropList || {});
46
- const combined = include ? propKeys.concat(include) : propKeys;
47
- return pick(props, combined);
48
- }
49
-
50
- // this was the fastest implementation from testing: https://jsperf.com/pick-props
51
- function pick(obj, keys) {
52
- const res = {};
53
- const len = keys.length;
54
- let idx = -1;
55
- let key;
56
- while (++idx < len) {
57
- key = keys[idx];
58
- if (key in obj) {
59
- res[key] = obj[key];
60
- }
61
- }
62
- return res;
63
- }
64
- var _default = exports.default = pickProps;
@@ -1,105 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.safeCloneElement = safeCloneElement;
8
- var _react = require("react");
9
- var _console = require("@instructure/console");
10
- var _createChainedFunction = require("@instructure/ui-utils/lib/createChainedFunction.js");
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/react
38
- * ---
39
- * Clones a React element without overwriting refs.
40
- * @param element The element to clone
41
- * @param props Props of the element
42
- * @param children
43
- */
44
- function safeCloneElement(element, props, ...children) {
45
- const cloneRef = props.ref;
46
- // Support both React 18 (element.ref) and React 19+ (element.props.ref)
47
- // TypeScript's ReactElement type does not always include a 'ref' property,
48
- // so we use 'as any' to safely access it for React 18 compatibility.
49
- let isReact19OrNewer = true;
50
- const versArr = _react.version.split('.', 1);
51
- if (versArr.length > 0 && parseInt(versArr[0], 10) < 19) {
52
- isReact19OrNewer = false;
53
- }
54
- // this needs to happen in such tricky way because React 18 and earlier
55
- // throw warning when one tries to access element.props.ref, see
56
- // https://github.com/facebook/react/blob/18-3-1/packages/react/src/jsx/ReactJSXElement.js#L108
57
- const originalRef = isReact19OrNewer ? element.props.ref : element.ref;
58
- const originalRefIsAFunction = typeof originalRef === 'function';
59
- const cloneRefIsFunction = typeof cloneRef === 'function';
60
- const mergedProps = {
61
- ...props
62
- };
63
- if (element.props.style && props.style) {
64
- // merge with existing styles
65
- mergedProps.style = {
66
- ...element.props.style,
67
- ...props.style
68
- };
69
- }
70
-
71
- // prevent overriding existing keys
72
- mergedProps.key = element.key || props.key;
73
-
74
- // Add chained function to preserve existing event handlers
75
- Object.keys(props).forEach(prop => {
76
- // If prop looks like an event handler "on*" and either
77
- // props[props] or element.props[prop] is a function create a chained function.
78
- // If only one is a function it will just use that function with no extra overhead.
79
- // This is necessary in cases where props[prop] is `null` or `undefined` which would
80
- // otherwise unwantedly override element.props[prop].
81
- if (prop.indexOf('on') === 0 && (typeof props[prop] === 'function' || typeof element.props[prop] === 'function')) {
82
- ;
83
- mergedProps[prop] = (0, _createChainedFunction.createChainedFunction)(element.props[prop], props[prop]);
84
- }
85
- });
86
- if (originalRef == null || cloneRef == null) {
87
- return /*#__PURE__*/(0, _react.cloneElement)(element, mergedProps, ...children);
88
- }
89
- (0, _console.logWarn)(originalRefIsAFunction, `Cloning an element with a ref that will be overwritten because the ref \
90
- is not a function. Use a composable callback-style ref instead. \
91
- Ignoring ref: ${originalRef}`);
92
- return /*#__PURE__*/(0, _react.cloneElement)(element, {
93
- ...mergedProps,
94
- ref(component) {
95
- if (cloneRefIsFunction) {
96
- ;
97
- cloneRef(component);
98
- } else {
99
- cloneRef.current = component;
100
- }
101
- originalRefIsAFunction && originalRef(component);
102
- }
103
- }, ...children);
104
- }
105
- var _default = exports.default = safeCloneElement;
@@ -1,111 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = void 0;
7
- exports.origin = origin;
8
- exports.windowMessageListener = void 0;
9
- var _decorator = require("@instructure/ui-decorator/lib/decorator.js");
10
- var _ownerWindow = require("@instructure/ui-dom-utils/lib/ownerWindow.js");
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/react
38
- * ---
39
- * A decorator or higher order component that provides methods
40
- * for cross-origin communication (between iframes/windows).
41
- *
42
- * see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
43
- * @module windowMessageListener
44
- * @param {Function} messageHandler a handler for messages receieved by the component
45
- * @param {Function} validSource an optional function that would restrict message handling to a specified source.
46
- * @returns {Function} a function that decorates a React component with the behavior
47
- */
48
- const windowMessageListener = exports.windowMessageListener = (0, _decorator.decorator)((ComposedComponent, messageHandler, validSource) => {
49
- var _Class;
50
- return _Class = class extends ComposedComponent {
51
- constructor(...args) {
52
- super(...args);
53
- this.handleMessage = e => {
54
- if (this.sourceIsValid(e.source) && e.origin === origin(this) && e.data) {
55
- messageHandler.call(this, e.data);
56
- }
57
- };
58
- }
59
- componentDidMount() {
60
- const win = (0, _ownerWindow.ownerWindow)(this);
61
- win.addEventListener('message', this.handleMessage, false);
62
- if (super.componentDidMount) {
63
- super.componentDidMount();
64
- }
65
- }
66
- componentWillUnmount() {
67
- const win = (0, _ownerWindow.ownerWindow)(this);
68
- win.removeEventListener('message', this.handleMessage, false);
69
- if (super.componentDidMount) {
70
- super.componentDidMount();
71
- }
72
- }
73
- sourceIsValid(eventSource) {
74
- const expectedSource = typeof validSource === 'function' ? validSource.call(this) : validSource;
75
- if (!expectedSource) {
76
- return true;
77
- } else if (eventSource) {
78
- const sourceFrame = eventSource.frameElement;
79
- const sourceName = sourceFrame ? sourceFrame.getAttribute('name') : null;
80
- return sourceName === expectedSource;
81
- } else {
82
- return false;
83
- }
84
- }
85
- }, _Class.postMessage = function (target, message, origin) {
86
- target.postMessage(message, origin);
87
- }, _Class;
88
- });
89
-
90
- /**
91
- * Return the origin of the owner window of the DOM element
92
- *
93
- * see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
94
- *
95
- * @param {DOMElement} node
96
- * @returns {String} the origin
97
- */
98
- function origin(node) {
99
- const ownWindow = (0, _ownerWindow.ownerWindow)(node);
100
- const location = ownWindow.location;
101
- if (location.protocol === 'file:') {
102
- return '*';
103
- } else if (location.origin) {
104
- return location.origin;
105
- } else if (location.port) {
106
- return `${location.protocol}//${location.hostname}:${location.port}`;
107
- } else {
108
- return `${location.protocol}//${location.hostname}`;
109
- }
110
- }
111
- var _default = exports.default = windowMessageListener;