@instructure/ui-react-utils 11.7.10 → 11.7.11-snapshot-0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (38) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/es/HydrationContext/HydrationContext.js +33 -0
  3. package/es/HydrationContext/HydrationGate.js +49 -0
  4. package/es/HydrationContext/HydrationProvider.js +49 -0
  5. package/es/HydrationContext/index.js +28 -0
  6. package/es/HydrationContext/useIsHydratedContext.js +46 -0
  7. package/es/index.js +3 -1
  8. package/es/useIsHydrated.js +45 -0
  9. package/lib/HydrationContext/HydrationContext.js +37 -0
  10. package/lib/HydrationContext/HydrationGate.js +55 -0
  11. package/lib/HydrationContext/HydrationProvider.js +55 -0
  12. package/lib/HydrationContext/index.js +33 -0
  13. package/lib/HydrationContext/useIsHydratedContext.js +51 -0
  14. package/lib/index.js +33 -1
  15. package/lib/useIsHydrated.js +50 -0
  16. package/package.json +7 -7
  17. package/src/HydrationContext/HydrationContext.ts +34 -0
  18. package/src/HydrationContext/HydrationGate.tsx +54 -0
  19. package/src/HydrationContext/HydrationProvider.tsx +57 -0
  20. package/src/HydrationContext/index.ts +30 -0
  21. package/src/HydrationContext/useIsHydratedContext.ts +48 -0
  22. package/src/index.ts +12 -0
  23. package/src/useIsHydrated.ts +47 -0
  24. package/tsconfig.build.tsbuildinfo +1 -1
  25. package/types/HydrationContext/HydrationContext.d.ts +8 -0
  26. package/types/HydrationContext/HydrationContext.d.ts.map +1 -0
  27. package/types/HydrationContext/HydrationGate.d.ts +25 -0
  28. package/types/HydrationContext/HydrationGate.d.ts.map +1 -0
  29. package/types/HydrationContext/HydrationProvider.d.ts +20 -0
  30. package/types/HydrationContext/HydrationProvider.d.ts.map +1 -0
  31. package/types/HydrationContext/index.d.ts +7 -0
  32. package/types/HydrationContext/index.d.ts.map +1 -0
  33. package/types/HydrationContext/useIsHydratedContext.d.ts +14 -0
  34. package/types/HydrationContext/useIsHydratedContext.d.ts.map +1 -0
  35. package/types/index.d.ts +3 -0
  36. package/types/index.d.ts.map +1 -1
  37. package/types/useIsHydrated.d.ts +15 -0
  38. package/types/useIsHydrated.d.ts.map +1 -0
@@ -0,0 +1,34 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { createContext } from 'react'
26
+
27
+ /**
28
+ * Whether the page has hydrated. `undefined` means there's no
29
+ * `HydrationProvider` above, and `false` means hydration hasn't finished yet.
30
+ */
31
+ const HydrationContext = createContext<boolean | undefined>(undefined)
32
+
33
+ export { HydrationContext }
34
+ export default HydrationContext
@@ -0,0 +1,54 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { ReactNode } from 'react'
26
+
27
+ import { useIsHydratedContext } from './useIsHydratedContext.js'
28
+
29
+ type HydrationGateProps = {
30
+ children: (isHydrated: boolean) => ReactNode
31
+ }
32
+
33
+ /**
34
+ * ---
35
+ * category: utilities/SSR
36
+ * ---
37
+ * Lets class components know whether the page has hydrated. It works with or
38
+ * without a `HydrationProvider` above it.
39
+ *
40
+ * ```jsx
41
+ * <HydrationGate>
42
+ * {(isHydrated) => (isHydrated ? <Content /> : <Placeholder />)}
43
+ * </HydrationGate>
44
+ * ```
45
+ */
46
+ function HydrationGate({ children }: HydrationGateProps) {
47
+ return <>{children(useIsHydratedContext())}</>
48
+ }
49
+
50
+ HydrationGate.displayName = 'HydrationGate'
51
+
52
+ export { HydrationGate }
53
+ export type { HydrationGateProps }
54
+ export default HydrationGate
@@ -0,0 +1,57 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import type { ReactNode } from 'react'
26
+
27
+ import { HydrationContext } from './HydrationContext.js'
28
+ import { useIsHydrated } from '../useIsHydrated.js'
29
+
30
+ type HydrationProviderProps = {
31
+ children?: ReactNode
32
+ }
33
+
34
+ /**
35
+ * ---
36
+ * category: utilities/SSR
37
+ * ---
38
+ * Shares whether the page has hydrated with every component below it, so they
39
+ * all switch from their server output at the same time. Class components read
40
+ * it with `HydrationGate`.
41
+ *
42
+ * `InstUISettingsProvider` already includes it, so you usually don't need to
43
+ * add it yourself.
44
+ */
45
+ function HydrationProvider({ children }: HydrationProviderProps) {
46
+ const isHydrated = useIsHydrated()
47
+
48
+ return (
49
+ <HydrationContext.Provider value={isHydrated}>
50
+ {children}
51
+ </HydrationContext.Provider>
52
+ )
53
+ }
54
+
55
+ export { HydrationProvider }
56
+ export type { HydrationProviderProps }
57
+ export default HydrationProvider
@@ -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 { HydrationContext } from './HydrationContext.js'
26
+ export { HydrationProvider } from './HydrationProvider.js'
27
+ export { useIsHydratedContext } from './useIsHydratedContext.js'
28
+ export { HydrationGate } from './HydrationGate.js'
29
+ export type { HydrationProviderProps } from './HydrationProvider'
30
+ export type { HydrationGateProps } from './HydrationGate'
@@ -0,0 +1,48 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { useContext } from 'react'
26
+
27
+ import { HydrationContext } from './HydrationContext.js'
28
+ import { useIsHydrated } from '../useIsHydrated.js'
29
+
30
+ /**
31
+ * ---
32
+ * category: utilities/SSR
33
+ * ---
34
+ * Like `useIsHydrated`, but reads the value from the nearest
35
+ * `HydrationProvider` when there is one.
36
+ *
37
+ * @module useIsHydratedContext
38
+ * @returns {boolean} `true` once the page has hydrated
39
+ */
40
+ const useIsHydratedContext = (): boolean => {
41
+ const fromProvider = useContext(HydrationContext)
42
+ // Hooks can't be called conditionally, so this always runs.
43
+ const standalone = useIsHydrated()
44
+ return fromProvider ?? standalone
45
+ }
46
+
47
+ export { useIsHydratedContext }
48
+ export default useIsHydratedContext
package/src/index.ts CHANGED
@@ -33,6 +33,7 @@ export { omitProps } from './omitProps.js'
33
33
  export { passthroughProps } from './passthroughProps.js'
34
34
  export { pickProps } from './pickProps.js'
35
35
  export { safeCloneElement } from './safeCloneElement.js'
36
+ export { useIsHydrated } from './useIsHydrated.js'
36
37
  export { windowMessageListener } from './windowMessageListener.js'
37
38
  export {
38
39
  DeterministicIdContext,
@@ -41,9 +42,20 @@ export {
41
42
  useDeterministicId
42
43
  } from './DeterministicIdContext/index.js'
43
44
 
45
+ export {
46
+ HydrationContext,
47
+ HydrationProvider,
48
+ useIsHydratedContext,
49
+ HydrationGate
50
+ } from './HydrationContext/index.js'
51
+
44
52
  export type { GetInteractionOptions } from './getInteraction'
45
53
  export type { InteractionType } from './getInteraction'
46
54
  export type {
47
55
  DeterministicIdProviderValue,
48
56
  WithDeterministicIdProps
49
57
  } from './DeterministicIdContext'
58
+ export type {
59
+ HydrationProviderProps,
60
+ HydrationGateProps
61
+ } from './HydrationContext'
@@ -0,0 +1,47 @@
1
+ /*
2
+ * The MIT License (MIT)
3
+ *
4
+ * Copyright (c) 2015 - present Instructure, Inc.
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ * SOFTWARE.
23
+ */
24
+
25
+ import { useSyncExternalStore } from 'react'
26
+
27
+ // The value never changes, so there's nothing to subscribe to.
28
+ const noopSubscribe = () => () => {}
29
+ const getClientSnapshot = () => true
30
+ const getServerSnapshot = () => false
31
+
32
+ /**
33
+ * ---
34
+ * category: utilities/SSR
35
+ * ---
36
+ * Returns `false` on the server and while React is hydrating, then `true`.
37
+ * Use it to render something different on the client without causing a
38
+ * hydration mismatch. In class components, use `HydrationGate` instead.
39
+ *
40
+ * @module useIsHydrated
41
+ * @returns {boolean} `true` once the page has hydrated
42
+ */
43
+ const useIsHydrated = (): boolean =>
44
+ useSyncExternalStore(noopSubscribe, getClientSnapshot, getServerSnapshot)
45
+
46
+ export default useIsHydrated
47
+ export { useIsHydrated }