@instructure/ui-react-utils 8.17.0 → 8.17.1-snapshot.17

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 (41) hide show
  1. package/LICENSE.md +27 -0
  2. package/es/DeterministicIdContext/DeterministicIdContext.js +27 -0
  3. package/es/DeterministicIdContext/DeterministicIdProvider.js +47 -0
  4. package/es/DeterministicIdContext/generateInstanceCounterMap.js +29 -0
  5. package/es/DeterministicIdContext/index.js +27 -0
  6. package/es/DeterministicIdContext/withDeterministicId.js +74 -0
  7. package/es/index.js +2 -1
  8. package/es/omitProps.js +1 -1
  9. package/es/passthroughProps.js +1 -1
  10. package/lib/DeterministicIdContext/DeterministicIdContext.js +39 -0
  11. package/lib/DeterministicIdContext/DeterministicIdProvider.js +59 -0
  12. package/lib/DeterministicIdContext/generateInstanceCounterMap.js +37 -0
  13. package/lib/DeterministicIdContext/index.js +37 -0
  14. package/lib/DeterministicIdContext/withDeterministicId.js +92 -0
  15. package/lib/index.js +27 -1
  16. package/lib/omitProps.js +1 -1
  17. package/lib/passthroughProps.js +1 -1
  18. package/package.json +11 -9
  19. package/src/DeterministicIdContext/DeterministicIdContext.ts +29 -0
  20. package/src/DeterministicIdContext/DeterministicIdProvider.tsx +58 -0
  21. package/src/DeterministicIdContext/generateInstanceCounterMap.ts +29 -0
  22. package/src/DeterministicIdContext/index.ts +30 -0
  23. package/src/DeterministicIdContext/withDeterministicId.tsx +101 -0
  24. package/src/index.ts +10 -0
  25. package/src/omitProps.ts +2 -1
  26. package/src/passthroughProps.ts +2 -1
  27. package/tsconfig.build.tsbuildinfo +1 -1
  28. package/types/DeterministicIdContext/DeterministicIdContext.d.ts +4 -0
  29. package/types/DeterministicIdContext/DeterministicIdContext.d.ts.map +1 -0
  30. package/types/DeterministicIdContext/DeterministicIdProvider.d.ts +21 -0
  31. package/types/DeterministicIdContext/DeterministicIdProvider.d.ts.map +1 -0
  32. package/types/DeterministicIdContext/generateInstanceCounterMap.d.ts +5 -0
  33. package/types/DeterministicIdContext/generateInstanceCounterMap.d.ts.map +1 -0
  34. package/types/DeterministicIdContext/index.d.ts +7 -0
  35. package/types/DeterministicIdContext/index.d.ts.map +1 -0
  36. package/types/DeterministicIdContext/withDeterministicId.d.ts +17 -0
  37. package/types/DeterministicIdContext/withDeterministicId.d.ts.map +1 -0
  38. package/types/index.d.ts +2 -0
  39. package/types/index.d.ts.map +1 -1
  40. package/types/omitProps.d.ts.map +1 -1
  41. package/types/passthroughProps.d.ts.map +1 -1
@@ -0,0 +1,29 @@
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
+ import React from 'react'
25
+ import { generateInstanceCounterMap } from './generateInstanceCounterMap'
26
+
27
+ const DeterministicIdContext = React.createContext(generateInstanceCounterMap())
28
+
29
+ export { DeterministicIdContext }
@@ -0,0 +1,58 @@
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
+ import React from 'react'
25
+ import { generateInstanceCounterMap } from './generateInstanceCounterMap'
26
+ import { DeterministicIdContext } from './DeterministicIdContext'
27
+ type DeterministicIdProviderValue = Map<string, number>
28
+ type DeterministicIdProviderProps = React.PropsWithChildren<{
29
+ instanceCounterMap?: DeterministicIdProviderValue
30
+ }>
31
+
32
+ const defaultContextValue = generateInstanceCounterMap()
33
+
34
+ /**
35
+ * ---
36
+ * category: components/utilities
37
+ * ---
38
+ * This is utility component for wrapping components with `DeterministicIdContext.Provider`
39
+ * See detailed documentation about how to use it: [InstUISettingsProvider](/#InstUISettingsProvider)
40
+ */
41
+
42
+ const DeterministicIdContextProvider = ({
43
+ children,
44
+ instanceCounterMap
45
+ }: DeterministicIdProviderProps) => {
46
+ return (
47
+ <DeterministicIdContext.Provider value={instanceCounterMap!}>
48
+ {children}
49
+ </DeterministicIdContext.Provider>
50
+ )
51
+ }
52
+ DeterministicIdContextProvider.defaultProps = {
53
+ instanceCounterMap: defaultContextValue
54
+ }
55
+
56
+ export { DeterministicIdContextProvider }
57
+
58
+ export type { DeterministicIdProviderValue }
@@ -0,0 +1,29 @@
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
+ import type { DeterministicIdProviderValue } from './DeterministicIdProvider'
25
+ function generateInstanceCounterMap(): DeterministicIdProviderValue {
26
+ return new Map<string, number>()
27
+ }
28
+ export default generateInstanceCounterMap
29
+ export { generateInstanceCounterMap }
@@ -0,0 +1,30 @@
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
+ export { generateInstanceCounterMap } from './generateInstanceCounterMap'
26
+ export { DeterministicIdContextProvider } from './DeterministicIdProvider'
27
+ export { DeterministicIdContext } from './DeterministicIdContext'
28
+ export { withDeterministicId } from './withDeterministicId'
29
+ export type { DeterministicIdProviderValue } from './DeterministicIdProvider'
30
+ export type { WithDeterministicIdProps } from './withDeterministicId'
@@ -0,0 +1,101 @@
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
+ import React, {
25
+ forwardRef,
26
+ PropsWithoutRef,
27
+ RefAttributes,
28
+ useContext
29
+ } from 'react'
30
+ import hoistNonReactStatics from 'hoist-non-react-statics'
31
+
32
+ import { DeterministicIdContext } from './DeterministicIdContext'
33
+ import { decorator } from '@instructure/ui-decorator'
34
+ import { generateId } from '@instructure/ui-utils'
35
+
36
+ import type { InstUIComponent } from '@instructure/shared-types'
37
+ import { warn } from '@instructure/console'
38
+
39
+ type WithDeterministicIdProps = {
40
+ deterministicId?: (instanceName?: string) => string
41
+ }
42
+ /**
43
+ * This decorator is used to enable the decorated class to use the `DeterministicIdContext` which is needed
44
+ * for deterministic id generation.
45
+ *
46
+ * The context is there for the users to pass an `instanceCounterMap` Map which is then used
47
+ * in the child components to deterministically create ids for them based on the `instanceCounterMap`.
48
+ * Read more about it here: [SSR guide](https://instructure.design/#server-side-rendering)
49
+ */
50
+ const withDeterministicId = decorator((ComposedComponent: InstUIComponent) => {
51
+ type Props = PropsWithoutRef<Record<string, unknown>> & RefAttributes<any>
52
+ const WithDeterministicId = forwardRef(
53
+ (props: Props, ref: React.ForwardedRef<any>) => {
54
+ const componentName =
55
+ ComposedComponent.componentId ||
56
+ ComposedComponent.displayName ||
57
+ ComposedComponent.name
58
+ const instanceCounterMap = useContext(DeterministicIdContext)
59
+ const deterministicId = (instanceName = componentName) =>
60
+ generateId(instanceName, instanceCounterMap)
61
+
62
+ if (props.deterministicId) {
63
+ warn(
64
+ false,
65
+ `Manually passing the "deterministicId" property is not allowed on the ${componentName} component.\n`,
66
+ props.deterministicId
67
+ )
68
+ }
69
+
70
+ return (
71
+ <ComposedComponent
72
+ ref={ref}
73
+ deterministicId={deterministicId}
74
+ {...props}
75
+ />
76
+ )
77
+ }
78
+ )
79
+
80
+ hoistNonReactStatics(WithDeterministicId, ComposedComponent)
81
+
82
+ // we have to pass these on, because sometimes users
83
+ // access propTypes of the component in other components
84
+ // eslint-disable-next-line react/forbid-foreign-prop-types
85
+ WithDeterministicId.propTypes = ComposedComponent.propTypes
86
+ WithDeterministicId.defaultProps = ComposedComponent.defaultProps
87
+
88
+ // These static fields exist on InstUI components
89
+ //@ts-expect-error fix this
90
+ WithDeterministicId.allowedProps = ComposedComponent.allowedProps
91
+
92
+ if (process.env.NODE_ENV !== 'production') {
93
+ WithDeterministicId.displayName = `WithDeterministicId(${ComposedComponent.displayName})`
94
+ }
95
+
96
+ return WithDeterministicId
97
+ })
98
+
99
+ export default withDeterministicId
100
+ export { withDeterministicId }
101
+ export type { WithDeterministicIdProps }
package/src/index.ts CHANGED
@@ -38,5 +38,15 @@ export { passthroughProps } from './passthroughProps'
38
38
  export { pickProps } from './pickProps'
39
39
  export { safeCloneElement } from './safeCloneElement'
40
40
  export { windowMessageListener } from './windowMessageListener'
41
+ export {
42
+ DeterministicIdContext,
43
+ generateInstanceCounterMap,
44
+ DeterministicIdContextProvider,
45
+ withDeterministicId
46
+ } from './DeterministicIdContext'
41
47
  export type { GetInteractionOptions } from './getInteraction'
42
48
  export type { InteractionType } from './getInteraction'
49
+ export type {
50
+ DeterministicIdProviderValue,
51
+ WithDeterministicIdProps
52
+ } from './DeterministicIdContext'
package/src/omitProps.ts CHANGED
@@ -66,7 +66,8 @@ const omit = <T>(originalObject: T, keysToOmit: string[]) => {
66
66
  key === 'style' ||
67
67
  key === 'styles' ||
68
68
  key === 'makeStyles' ||
69
- key === 'themeOverride'
69
+ key === 'themeOverride' ||
70
+ key === 'instanceCounterMap'
70
71
  ) {
71
72
  continue
72
73
  }
@@ -44,7 +44,8 @@ function passthroughProps<P>(props: P) {
44
44
  propName !== 'className' &&
45
45
  propName !== 'children' &&
46
46
  propName !== 'styles' &&
47
- propName !== 'makeStyles'
47
+ propName !== 'makeStyles' &&
48
+ propName !== 'instanceCounterMap'
48
49
  )
49
50
  .forEach((propName) => {
50
51
  validProps[propName as keyof P] = props[propName as keyof P]