@instructure/ui-react-utils 11.7.8 → 11.7.9-pr-snapshot-1792147648392

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (42) hide show
  1. package/CHANGELOG.md +13 -0
  2. package/es/DeterministicIdContext/DeterministicIdContext.js +2 -38
  3. package/es/DeterministicIdContext/DeterministicIdContextProvider.js +7 -46
  4. package/es/DeterministicIdContext/index.js +1 -25
  5. package/es/DeterministicIdContext/useDeterministicId.js +3 -65
  6. package/es/DeterministicIdContext/withDeterministicId.js +21 -65
  7. package/es/callRenderProp.js +7 -45
  8. package/es/ensureSingleChild.js +13 -53
  9. package/es/getDisplayName.js +2 -36
  10. package/es/getElementType.js +18 -68
  11. package/es/getInteraction.js +10 -56
  12. package/es/index.js +1 -26
  13. package/es/matchComponentTypes.js +7 -43
  14. package/es/omitProps.js +15 -60
  15. package/es/passthroughProps.js +6 -42
  16. package/es/pickProps.js +14 -52
  17. package/es/safeCloneElement.js +40 -89
  18. package/es/windowMessageListener.js +52 -96
  19. package/package.json +10 -13
  20. package/src/getDisplayName.ts +1 -1
  21. package/src/omitProps.ts +1 -1
  22. package/tsconfig.build.json +0 -3
  23. package/tsconfig.build.tsbuildinfo +1 -1
  24. package/types/getDisplayName.d.ts +1 -1
  25. package/lib/DeterministicIdContext/DeterministicIdContext.js +0 -47
  26. package/lib/DeterministicIdContext/DeterministicIdContextProvider.js +0 -54
  27. package/lib/DeterministicIdContext/index.js +0 -33
  28. package/lib/DeterministicIdContext/useDeterministicId.js +0 -74
  29. package/lib/DeterministicIdContext/withDeterministicId.js +0 -80
  30. package/lib/callRenderProp.js +0 -57
  31. package/lib/ensureSingleChild.js +0 -62
  32. package/lib/getDisplayName.js +0 -45
  33. package/lib/getElementType.js +0 -77
  34. package/lib/getInteraction.js +0 -62
  35. package/lib/index.js +0 -107
  36. package/lib/matchComponentTypes.js +0 -53
  37. package/lib/omitProps.js +0 -72
  38. package/lib/package.json +0 -1
  39. package/lib/passthroughProps.js +0 -53
  40. package/lib/pickProps.js +0 -64
  41. package/lib/safeCloneElement.js +0 -105
  42. package/lib/windowMessageListener.js +0 -110
package/es/index.js CHANGED
@@ -1,28 +1,3 @@
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
- /* list utils in alphabetical order */
26
1
  export { callRenderProp } from './callRenderProp.js';
27
2
  export { ensureSingleChild } from './ensureSingleChild.js';
28
3
  export { getDisplayName } from './getDisplayName.js';
@@ -34,4 +9,4 @@ export { passthroughProps } from './passthroughProps.js';
34
9
  export { pickProps } from './pickProps.js';
35
10
  export { safeCloneElement } from './safeCloneElement.js';
36
11
  export { windowMessageListener } from './windowMessageListener.js';
37
- export { DeterministicIdContext, DeterministicIdContextProvider, withDeterministicId, useDeterministicId } from './DeterministicIdContext/index.js';
12
+ export { DeterministicIdContext, DeterministicIdContextProvider, withDeterministicId, useDeterministicId } from './DeterministicIdContext/index.js';
@@ -1,47 +1,11 @@
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
1
  import { getDisplayName } from './getDisplayName.js';
26
- /**
27
- * ---
28
- * category: utilities/react
29
- * ---
30
- * Check if a React component instance (React element) matches one of the
31
- * specified types.
32
- *
33
- * @module matchComponentTypes
34
- * @param componentInstance
35
- * @param types an array of React components
36
- * @returns true if the component matches at least one of the types
37
- */
38
2
  function matchComponentTypes(componentInstance, types = []) {
39
- if (componentInstance && componentInstance.type) {
40
- const displayNames = types.map(type => getDisplayName(type));
41
- return displayNames.indexOf(getDisplayName(componentInstance.type)) >= 0;
42
- } else {
43
- return false;
44
- }
3
+ if (componentInstance && componentInstance.type) {
4
+ const displayNames = types.map((type)=>getDisplayName(type));
5
+ return displayNames.indexOf(getDisplayName(componentInstance.type)) >= 0;
6
+ } else {
7
+ return false;
8
+ }
45
9
  }
46
10
  export default matchComponentTypes;
47
- export { matchComponentTypes };
11
+ export { matchComponentTypes };
package/es/omitProps.js CHANGED
@@ -1,66 +1,21 @@
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
- /**
26
- * ---
27
- * category: utilities/react
28
- * ---
29
- * Return an object with the remaining props after the given props are omitted.
30
- *
31
- * Automatically excludes the following props:
32
- * `theme`, `children`, `className`, `style`, `styles`, `makeStyles`,
33
- * `themeOverride`, `deterministicId`
34
- * @module omitProps
35
- * @param props The object to process
36
- * @param propsToOmit list disallowed prop keys or an object whose
37
- * keys will be omitted. If not specified the automatically excluded
38
- * props will be removed.
39
- * @param exclude an optional array of disallowed prop names to omit
40
- * @returns props object without the excluded props
41
- * @module omitProps
42
- */
43
1
  function omitProps(props, propsToOmit, exclude) {
44
- const propKeys = Array.isArray(propsToOmit) ? propsToOmit : Object.keys(propsToOmit || {});
45
- const keysToOmit = exclude ? propKeys.concat(exclude) : propKeys;
46
- return omit(props, keysToOmit);
2
+ const propKeys = Array.isArray(propsToOmit) ? propsToOmit : Object.keys(propsToOmit || {});
3
+ const keysToOmit = exclude ? propKeys.concat(exclude) : propKeys;
4
+ return omit(props, keysToOmit);
47
5
  }
48
6
  const hasOwnProperty = Object.prototype.hasOwnProperty;
49
- const omit = (originalObject, keysToOmit) => {
50
- // code based on babel's _objectWithoutProperties
51
- const newObject = {};
52
- for (const key in originalObject) {
53
- // special case because we always want to omit these and === is faster than
54
- // concat'ing them in
55
- if (key === 'theme' || key === 'children' || key === 'className' || key === 'style' || key === 'styles' || key === 'makeStyles' || key === 'themeOverride' || key === 'deterministicId') {
56
- continue;
7
+ const omit = (originalObject, keysToOmit)=>{
8
+ const newObject = {};
9
+ for(const key in originalObject){
10
+ if (key === 'theme' || key === 'children' || key === 'className' || key === 'style' || key === 'styles' || key === 'makeStyles' || key === 'themeOverride' || key === 'deterministicId') {
11
+ continue;
12
+ }
13
+ if (keysToOmit.includes(key) || !hasOwnProperty.call(originalObject, key)) {
14
+ continue;
15
+ }
16
+ newObject[key] = originalObject[key];
57
17
  }
58
- if (keysToOmit.includes(key) || !hasOwnProperty.call(originalObject, key)) {
59
- continue;
60
- }
61
- newObject[key] = originalObject[key];
62
- }
63
- return newObject;
18
+ return newObject;
64
19
  };
65
20
  export default omitProps;
66
- export { omitProps };
21
+ export { omitProps };
@@ -1,46 +1,10 @@
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
- // list of "valid" props https://github.com/emotion-js/emotion/blob/master/packages/is-prop-valid/src/props.js
25
1
  import isPropValid from '@emotion/is-prop-valid';
26
-
27
- /**
28
- * Removes disallowed keys from the given object. Allowed keys: Valid props for
29
- * HTML or SVG elements (see https://github.com/emotion-js/emotion/tree/main/packages/is-prop-valid)
30
- *
31
- * Disallowed is anything else and 'style', 'styles', 'className', 'children',
32
- * 'makeStyles', 'deterministicId'
33
- * @param props The props to process
34
- */
35
2
  function passthroughProps(props) {
36
- const validProps = {};
37
- Object.keys(props)
38
- // style and className need to be explicitly passed through
39
- // styles and makeStyle can not pass through
40
- .filter(propName => isPropValid(propName) && propName !== 'style' && propName !== 'className' && propName !== 'children' && propName !== 'styles' && propName !== 'makeStyles' && propName !== 'deterministicId').forEach(propName => {
41
- validProps[propName] = props[propName];
42
- });
43
- return validProps;
3
+ const validProps = {};
4
+ Object.keys(props).filter((propName)=>isPropValid(propName) && propName !== 'style' && propName !== 'className' && propName !== 'children' && propName !== 'styles' && propName !== 'makeStyles' && propName !== 'deterministicId').forEach((propName)=>{
5
+ validProps[propName] = props[propName];
6
+ });
7
+ return validProps;
44
8
  }
45
9
  export { passthroughProps };
46
- export default passthroughProps;
10
+ export default passthroughProps;
package/es/pickProps.js CHANGED
@@ -1,58 +1,20 @@
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
- /**
26
- * ---
27
- * category: utilities/react
28
- * ---
29
- * Return a props object with only specified propTypes.
30
- *
31
- * @param props React component props
32
- * @param propTypesOrAllowedPropList React component propTypes or the list of allowed prop keys
33
- * @param include an optional array of prop names to include
34
- * @returns props object with only the included props
35
- * @module pickProps
36
- */
37
1
  function pickProps(props, propTypesOrAllowedPropList, include) {
38
- const propKeys = Array.isArray(propTypesOrAllowedPropList) ? propTypesOrAllowedPropList : Object.keys(propTypesOrAllowedPropList || {});
39
- const combined = include ? propKeys.concat(include) : propKeys;
40
- return pick(props, combined);
2
+ const propKeys = Array.isArray(propTypesOrAllowedPropList) ? propTypesOrAllowedPropList : Object.keys(propTypesOrAllowedPropList || {});
3
+ const combined = include ? propKeys.concat(include) : propKeys;
4
+ return pick(props, combined);
41
5
  }
42
-
43
- // this was the fastest implementation from testing: https://jsperf.com/pick-props
44
6
  function pick(obj, keys) {
45
- const res = {};
46
- const len = keys.length;
47
- let idx = -1;
48
- let key;
49
- while (++idx < len) {
50
- key = keys[idx];
51
- if (key in obj) {
52
- res[key] = obj[key];
7
+ const res = {};
8
+ const len = keys.length;
9
+ let idx = -1;
10
+ let key;
11
+ while(++idx < len){
12
+ key = keys[idx];
13
+ if (key in obj) {
14
+ res[key] = obj[key];
15
+ }
53
16
  }
54
- }
55
- return res;
17
+ return res;
56
18
  }
57
19
  export default pickProps;
58
- export { pickProps };
20
+ export { pickProps };
@@ -1,99 +1,50 @@
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
1
  import { cloneElement, version } from 'react';
26
2
  import { logWarn as warn } from '@instructure/console';
27
3
  import { createChainedFunction } from '@instructure/ui-utils';
28
- /**
29
- * ---
30
- * category: utilities/react
31
- * ---
32
- * Clones a React element without overwriting refs.
33
- * @param element The element to clone
34
- * @param props Props of the element
35
- * @param children
36
- */
37
4
  function safeCloneElement(element, props, ...children) {
38
- const cloneRef = props.ref;
39
- // Support both React 18 (element.ref) and React 19+ (element.props.ref)
40
- // TypeScript's ReactElement type does not always include a 'ref' property,
41
- // so we use 'as any' to safely access it for React 18 compatibility.
42
- let isReact19OrNewer = true;
43
- const versArr = version.split('.', 1);
44
- if (versArr.length > 0 && parseInt(versArr[0], 10) < 19) {
45
- isReact19OrNewer = false;
46
- }
47
- // this needs to happen in such tricky way because React 18 and earlier
48
- // throw warning when one tries to access element.props.ref, see
49
- // https://github.com/facebook/react/blob/18-3-1/packages/react/src/jsx/ReactJSXElement.js#L108
50
- const originalRef = isReact19OrNewer ? element.props.ref : element.ref;
51
- const originalRefIsAFunction = typeof originalRef === 'function';
52
- const cloneRefIsFunction = typeof cloneRef === 'function';
53
- const mergedProps = {
54
- ...props
55
- };
56
- if (element.props.style && props.style) {
57
- // merge with existing styles
58
- mergedProps.style = {
59
- ...element.props.style,
60
- ...props.style
5
+ const cloneRef = props.ref;
6
+ let isReact19OrNewer = true;
7
+ const versArr = version.split('.', 1);
8
+ if (versArr.length > 0 && parseInt(versArr[0], 10) < 19) {
9
+ isReact19OrNewer = false;
10
+ }
11
+ const originalRef = isReact19OrNewer ? element.props.ref : element.ref;
12
+ const originalRefIsAFunction = typeof originalRef === 'function';
13
+ const cloneRefIsFunction = typeof cloneRef === 'function';
14
+ const mergedProps = {
15
+ ...props
61
16
  };
62
- }
63
-
64
- // prevent overriding existing keys
65
- mergedProps.key = element.key || props.key;
66
-
67
- // Add chained function to preserve existing event handlers
68
- Object.keys(props).forEach(prop => {
69
- // If prop looks like an event handler "on*" and either
70
- // props[props] or element.props[prop] is a function create a chained function.
71
- // If only one is a function it will just use that function with no extra overhead.
72
- // This is necessary in cases where props[prop] is `null` or `undefined` which would
73
- // otherwise unwantedly override element.props[prop].
74
- if (prop.indexOf('on') === 0 && (typeof props[prop] === 'function' || typeof element.props[prop] === 'function')) {
75
- ;
76
- mergedProps[prop] = createChainedFunction(element.props[prop], props[prop]);
17
+ if (element.props.style && props.style) {
18
+ mergedProps.style = {
19
+ ...element.props.style,
20
+ ...props.style
21
+ };
22
+ }
23
+ mergedProps.key = element.key || props.key;
24
+ Object.keys(props).forEach((prop)=>{
25
+ if (prop.indexOf('on') === 0 && (typeof props[prop] === 'function' || typeof element.props[prop] === 'function')) {
26
+ ;
27
+ mergedProps[prop] = createChainedFunction(element.props[prop], props[prop]);
28
+ }
29
+ });
30
+ if (originalRef == null || cloneRef == null) {
31
+ return cloneElement(element, mergedProps, ...children);
77
32
  }
78
- });
79
- if (originalRef == null || cloneRef == null) {
80
- return /*#__PURE__*/cloneElement(element, mergedProps, ...children);
81
- }
82
- warn(originalRefIsAFunction, `Cloning an element with a ref that will be overwritten because the ref \
33
+ warn(originalRefIsAFunction, `Cloning an element with a ref that will be overwritten because the ref \
83
34
  is not a function. Use a composable callback-style ref instead. \
84
35
  Ignoring ref: ${originalRef}`);
85
- return /*#__PURE__*/cloneElement(element, {
86
- ...mergedProps,
87
- ref(component) {
88
- if (cloneRefIsFunction) {
89
- ;
90
- cloneRef(component);
91
- } else {
92
- cloneRef.current = component;
93
- }
94
- originalRefIsAFunction && originalRef(component);
95
- }
96
- }, ...children);
36
+ return cloneElement(element, {
37
+ ...mergedProps,
38
+ ref (component) {
39
+ if (cloneRefIsFunction) {
40
+ ;
41
+ cloneRef(component);
42
+ } else {
43
+ cloneRef.current = component;
44
+ }
45
+ originalRefIsAFunction && originalRef(component);
46
+ }
47
+ }, ...children);
97
48
  }
98
49
  export default safeCloneElement;
99
- export { safeCloneElement };
50
+ export { safeCloneElement };
@@ -1,103 +1,59 @@
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
1
  import { decorator } from '@instructure/ui-decorator';
25
2
  import { ownerWindow } from '@instructure/ui-dom-utils';
26
-
27
- /**
28
- * ---
29
- * category: utilities/react
30
- * ---
31
- * A decorator or higher order component that provides methods
32
- * for cross-origin communication (between iframes/windows).
33
- *
34
- * see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
35
- * @module windowMessageListener
36
- * @param {Function} messageHandler a handler for messages receieved by the component
37
- * @param {Function} validSource an optional function that would restrict message handling to a specified source.
38
- * @returns {Function} a function that decorates a React component with the behavior
39
- */
40
- const windowMessageListener = decorator((ComposedComponent, messageHandler, validSource) => {
41
- return class extends ComposedComponent {
42
- static postMessage = function (target, message, origin) {
43
- target.postMessage(message, origin);
3
+ const windowMessageListener = decorator((ComposedComponent, messageHandler, validSource)=>{
4
+ return class extends ComposedComponent {
5
+ static{
6
+ this.postMessage = function(target, message, origin) {
7
+ target.postMessage(message, origin);
8
+ };
9
+ }
10
+ componentDidMount() {
11
+ const win = ownerWindow(this);
12
+ win.addEventListener('message', this.handleMessage, false);
13
+ if (super.componentDidMount) {
14
+ super.componentDidMount();
15
+ }
16
+ }
17
+ componentWillUnmount() {
18
+ const win = ownerWindow(this);
19
+ win.removeEventListener('message', this.handleMessage, false);
20
+ if (super.componentDidMount) {
21
+ super.componentDidMount();
22
+ }
23
+ }
24
+ sourceIsValid(eventSource) {
25
+ const expectedSource = typeof validSource === 'function' ? validSource.call(this) : validSource;
26
+ if (!expectedSource) {
27
+ return true;
28
+ } else if (eventSource) {
29
+ const sourceFrame = eventSource.frameElement;
30
+ const sourceName = sourceFrame ? sourceFrame.getAttribute('name') : null;
31
+ return sourceName === expectedSource;
32
+ } else {
33
+ return false;
34
+ }
35
+ }
36
+ constructor(...args){
37
+ super(...args), this.handleMessage = (e)=>{
38
+ if (this.sourceIsValid(e.source) && e.origin === origin(this) && e.data) {
39
+ messageHandler.call(this, e.data);
40
+ }
41
+ };
42
+ }
44
43
  };
45
- componentDidMount() {
46
- const win = ownerWindow(this);
47
- win.addEventListener('message', this.handleMessage, false);
48
- if (super.componentDidMount) {
49
- super.componentDidMount();
50
- }
51
- }
52
- componentWillUnmount() {
53
- const win = ownerWindow(this);
54
- win.removeEventListener('message', this.handleMessage, false);
55
- if (super.componentDidMount) {
56
- super.componentDidMount();
57
- }
58
- }
59
- sourceIsValid(eventSource) {
60
- const expectedSource = typeof validSource === 'function' ? validSource.call(this) : validSource;
61
- if (!expectedSource) {
62
- return true;
63
- } else if (eventSource) {
64
- const sourceFrame = eventSource.frameElement;
65
- const sourceName = sourceFrame ? sourceFrame.getAttribute('name') : null;
66
- return sourceName === expectedSource;
67
- } else {
68
- return false;
69
- }
70
- }
71
- handleMessage = e => {
72
- if (this.sourceIsValid(e.source) && e.origin === origin(this) && e.data) {
73
- messageHandler.call(this, e.data);
74
- }
75
- };
76
- };
77
44
  });
78
-
79
- /**
80
- * Return the origin of the owner window of the DOM element
81
- *
82
- * see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage
83
- *
84
- * @param {DOMElement} node
85
- * @returns {String} the origin
86
- */
87
45
  function origin(node) {
88
- const ownWindow = ownerWindow(node);
89
- const {
90
- location
91
- } = ownWindow;
92
- if (location.protocol === 'file:') {
93
- return '*';
94
- } else if (location.origin) {
95
- return location.origin;
96
- } else if (location.port) {
97
- return `${location.protocol}//${location.hostname}:${location.port}`;
98
- } else {
99
- return `${location.protocol}//${location.hostname}`;
100
- }
46
+ const ownWindow = ownerWindow(node);
47
+ const { location } = ownWindow;
48
+ if (location.protocol === 'file:') {
49
+ return '*';
50
+ } else if (location.origin) {
51
+ return location.origin;
52
+ } else if (location.port) {
53
+ return `${location.protocol}//${location.hostname}:${location.port}`;
54
+ } else {
55
+ return `${location.protocol}//${location.hostname}`;
56
+ }
101
57
  }
102
58
  export default windowMessageListener;
103
- export { origin, windowMessageListener };
59
+ export { origin, windowMessageListener };
package/package.json CHANGED
@@ -1,10 +1,10 @@
1
1
  {
2
2
  "name": "@instructure/ui-react-utils",
3
- "version": "11.7.8",
3
+ "version": "11.7.9-pr-snapshot-1792147648392",
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",
@@ -14,19 +14,18 @@
14
14
  "bugs": "https://github.com/instructure/instructure-ui/issues",
15
15
  "license": "MIT",
16
16
  "dependencies": {
17
- "@babel/runtime": "^7.29.7",
18
17
  "@emotion/is-prop-valid": "^1.4.0",
18
+ "@swc/helpers": "^0.5.23",
19
19
  "hoist-non-react-statics": "^3.3.2",
20
- "@instructure/shared-types": "11.7.8",
21
- "@instructure/console": "11.7.8",
22
- "@instructure/ui-utils": "11.7.8",
23
- "@instructure/ui-dom-utils": "11.7.8",
24
- "@instructure/ui-decorator": "11.7.8"
20
+ "@instructure/console": "11.7.9-pr-snapshot-1792147648392",
21
+ "@instructure/ui-decorator": "11.7.9-pr-snapshot-1792147648392",
22
+ "@instructure/ui-utils": "11.7.9-pr-snapshot-1792147648392",
23
+ "@instructure/ui-dom-utils": "11.7.9-pr-snapshot-1792147648392",
24
+ "@instructure/shared-types": "11.7.9-pr-snapshot-1792147648392"
25
25
  },
26
26
  "devDependencies": {
27
27
  "@types/hoist-non-react-statics": "^3.3.7",
28
- "vitest": "^5.0.0",
29
- "@instructure/ui-babel-preset": "11.7.8"
28
+ "vitest": "^5.0.0"
30
29
  },
31
30
  "peerDependencies": {
32
31
  "react": ">=18 <=19",
@@ -41,10 +40,8 @@
41
40
  "src": "./src/index.ts",
42
41
  "types": "./types/index.d.ts",
43
42
  "import": "./es/index.js",
44
- "require": "./lib/index.js",
45
43
  "default": "./es/index.js"
46
44
  },
47
- "./lib/*": "./lib/*",
48
45
  "./es/*": "./es/*",
49
46
  "./types/*": "./types/*",
50
47
  "./package.json": "./package.json",
@@ -54,7 +51,7 @@
54
51
  "lint": "ui-scripts lint",
55
52
  "lint:fix": "ui-scripts lint --fix",
56
53
  "clean": "ui-scripts clean",
57
- "build": "ui-scripts build --modules es,cjs",
54
+ "build": "ui-scripts build",
58
55
  "build:watch": "ui-scripts build --watch",
59
56
  "build:types": "tsc -p tsconfig.build.json"
60
57
  }