@wordpress/compose 8.1.0 → 8.1.2-next.v.202606191442.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 (148) hide show
  1. package/CHANGELOG.md +20 -1
  2. package/README.md +46 -42
  3. package/build/higher-order/pure/index.cjs +15 -0
  4. package/build/higher-order/pure/index.cjs.map +3 -3
  5. package/build/hooks/use-constrained-tabbing/index.cjs +3 -9
  6. package/build/hooks/use-constrained-tabbing/index.cjs.map +3 -3
  7. package/build/hooks/use-debounce/index.cjs +1 -1
  8. package/build/hooks/use-debounce/index.cjs.map +3 -3
  9. package/build/hooks/use-disabled/index.cjs.map +2 -2
  10. package/build/hooks/use-dragging/index.cjs +27 -22
  11. package/build/hooks/use-dragging/index.cjs.map +4 -4
  12. package/build/hooks/use-dragging/types.cjs +19 -0
  13. package/build/hooks/use-dragging/types.cjs.map +7 -0
  14. package/build/hooks/use-drop-zone/index.cjs +2 -5
  15. package/build/hooks/use-drop-zone/index.cjs.map +3 -3
  16. package/build/hooks/use-drop-zone/types.cjs +19 -0
  17. package/build/hooks/use-drop-zone/types.cjs.map +7 -0
  18. package/build/hooks/use-fixed-window-list/index.cjs +10 -8
  19. package/build/hooks/use-fixed-window-list/index.cjs.map +3 -3
  20. package/build/hooks/use-focus-return/index.cjs +12 -9
  21. package/build/hooks/use-focus-return/index.cjs.map +3 -3
  22. package/build/hooks/use-isomorphic-layout-effect/index.cjs +1 -1
  23. package/build/hooks/use-isomorphic-layout-effect/index.cjs.map +1 -1
  24. package/build/hooks/use-keyboard-shortcut/index.cjs +1 -4
  25. package/build/hooks/use-keyboard-shortcut/index.cjs.map +3 -3
  26. package/build/hooks/use-media-query/index.cjs +2 -2
  27. package/build/hooks/use-media-query/index.cjs.map +2 -2
  28. package/build/hooks/use-merge-refs/index.cjs +28 -8
  29. package/build/hooks/use-merge-refs/index.cjs.map +2 -2
  30. package/build/hooks/use-reduced-motion/index.cjs +1 -1
  31. package/build/hooks/use-reduced-motion/index.cjs.map +2 -2
  32. package/build/hooks/use-throttle/index.cjs +1 -1
  33. package/build/hooks/use-throttle/index.cjs.map +3 -3
  34. package/build/hooks/use-viewport-match/index.cjs +3 -6
  35. package/build/hooks/use-viewport-match/index.cjs.map +3 -3
  36. package/build/hooks/use-warn-on-change/index.cjs +3 -10
  37. package/build/hooks/use-warn-on-change/index.cjs.map +3 -3
  38. package/build/utils/subscribe-delegated-listener/index.cjs.map +1 -1
  39. package/build-module/higher-order/pure/index.mjs +5 -0
  40. package/build-module/higher-order/pure/index.mjs.map +2 -2
  41. package/build-module/hooks/use-constrained-tabbing/index.mjs +3 -9
  42. package/build-module/hooks/use-constrained-tabbing/index.mjs.map +3 -3
  43. package/build-module/hooks/use-debounce/index.mjs +1 -1
  44. package/build-module/hooks/use-debounce/index.mjs.map +3 -3
  45. package/build-module/hooks/use-disabled/index.mjs.map +2 -2
  46. package/build-module/hooks/use-dragging/index.mjs +27 -22
  47. package/build-module/hooks/use-dragging/index.mjs.map +4 -4
  48. package/build-module/hooks/use-dragging/types.mjs +1 -0
  49. package/build-module/hooks/use-dragging/types.mjs.map +7 -0
  50. package/build-module/hooks/use-drop-zone/index.mjs +2 -5
  51. package/build-module/hooks/use-drop-zone/index.mjs.map +3 -3
  52. package/build-module/hooks/use-drop-zone/types.mjs +1 -0
  53. package/build-module/hooks/use-drop-zone/types.mjs.map +7 -0
  54. package/build-module/hooks/use-fixed-window-list/index.mjs +10 -8
  55. package/build-module/hooks/use-fixed-window-list/index.mjs.map +3 -3
  56. package/build-module/hooks/use-focus-return/index.mjs +12 -9
  57. package/build-module/hooks/use-focus-return/index.mjs.map +3 -3
  58. package/build-module/hooks/use-isomorphic-layout-effect/index.mjs +1 -1
  59. package/build-module/hooks/use-isomorphic-layout-effect/index.mjs.map +1 -1
  60. package/build-module/hooks/use-keyboard-shortcut/index.mjs +1 -4
  61. package/build-module/hooks/use-keyboard-shortcut/index.mjs.map +3 -3
  62. package/build-module/hooks/use-media-query/index.mjs +2 -2
  63. package/build-module/hooks/use-media-query/index.mjs.map +2 -2
  64. package/build-module/hooks/use-merge-refs/index.mjs +28 -8
  65. package/build-module/hooks/use-merge-refs/index.mjs.map +2 -2
  66. package/build-module/hooks/use-reduced-motion/index.mjs +1 -1
  67. package/build-module/hooks/use-reduced-motion/index.mjs.map +2 -2
  68. package/build-module/hooks/use-throttle/index.mjs +1 -1
  69. package/build-module/hooks/use-throttle/index.mjs.map +3 -3
  70. package/build-module/hooks/use-viewport-match/index.mjs +3 -6
  71. package/build-module/hooks/use-viewport-match/index.mjs.map +3 -3
  72. package/build-module/hooks/use-warn-on-change/index.mjs +3 -10
  73. package/build-module/hooks/use-warn-on-change/index.mjs.map +3 -3
  74. package/build-module/utils/subscribe-delegated-listener/index.mjs.map +1 -1
  75. package/build-types/higher-order/pure/index.d.ts.map +1 -1
  76. package/build-types/hooks/use-constrained-tabbing/index.d.ts +2 -2
  77. package/build-types/hooks/use-constrained-tabbing/index.d.ts.map +1 -1
  78. package/build-types/hooks/use-debounce/index.d.ts +7 -6
  79. package/build-types/hooks/use-debounce/index.d.ts.map +1 -1
  80. package/build-types/hooks/use-disabled/index.d.ts +1 -1
  81. package/build-types/hooks/use-disabled/index.d.ts.map +1 -1
  82. package/build-types/hooks/use-dragging/index.d.ts +9 -13
  83. package/build-types/hooks/use-dragging/index.d.ts.map +1 -1
  84. package/build-types/hooks/use-dragging/types.d.ts +11 -0
  85. package/build-types/hooks/use-dragging/types.d.ts.map +1 -0
  86. package/build-types/hooks/use-drop-zone/index.d.ts +4 -21
  87. package/build-types/hooks/use-drop-zone/index.d.ts.map +1 -1
  88. package/build-types/hooks/use-drop-zone/types.d.ts +35 -0
  89. package/build-types/hooks/use-drop-zone/types.d.ts.map +1 -0
  90. package/build-types/hooks/use-fixed-window-list/index.d.ts +22 -26
  91. package/build-types/hooks/use-fixed-window-list/index.d.ts.map +1 -1
  92. package/build-types/hooks/use-focus-return/index.d.ts +3 -3
  93. package/build-types/hooks/use-focus-return/index.d.ts.map +1 -1
  94. package/build-types/hooks/use-isomorphic-layout-effect/index.d.ts.map +1 -1
  95. package/build-types/hooks/use-keyboard-shortcut/index.d.ts +29 -23
  96. package/build-types/hooks/use-keyboard-shortcut/index.d.ts.map +1 -1
  97. package/build-types/hooks/use-media-query/index.d.ts +1 -1
  98. package/build-types/hooks/use-media-query/index.d.ts.map +1 -1
  99. package/build-types/hooks/use-merge-refs/index.d.ts +7 -5
  100. package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
  101. package/build-types/hooks/use-reduced-motion/index.d.ts +1 -1
  102. package/build-types/hooks/use-reduced-motion/index.d.ts.map +1 -1
  103. package/build-types/hooks/use-throttle/index.d.ts +8 -6
  104. package/build-types/hooks/use-throttle/index.d.ts.map +1 -1
  105. package/build-types/hooks/use-viewport-match/index.d.ts +3 -3
  106. package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
  107. package/build-types/hooks/use-warn-on-change/index.d.ts +7 -7
  108. package/build-types/hooks/use-warn-on-change/index.d.ts.map +1 -1
  109. package/package.json +21 -12
  110. package/src/higher-order/pure/index.tsx +6 -0
  111. package/src/higher-order/pure/test/index.js +14 -81
  112. package/src/hooks/use-constrained-tabbing/{index.js → index.ts} +7 -11
  113. package/src/hooks/use-debounce/{index.js → index.ts} +11 -6
  114. package/src/hooks/use-disabled/index.ts +3 -1
  115. package/src/hooks/use-dragging/index.ts +78 -0
  116. package/src/hooks/use-dragging/types.ts +11 -0
  117. package/src/hooks/use-drop-zone/README.md +1 -1
  118. package/src/hooks/use-drop-zone/{index.js → index.ts} +17 -31
  119. package/src/hooks/use-drop-zone/types.ts +34 -0
  120. package/src/hooks/use-fixed-window-list/{index.js → index.ts} +46 -37
  121. package/src/hooks/use-focus-return/{index.js → index.ts} +26 -21
  122. package/src/hooks/use-keyboard-shortcut/{index.js → index.ts} +43 -28
  123. package/src/hooks/use-media-query/index.ts +10 -3
  124. package/src/hooks/use-media-query/test/ssr.js +47 -0
  125. package/src/hooks/use-merge-refs/index.ts +49 -18
  126. package/src/hooks/use-merge-refs/test/index.js +288 -0
  127. package/src/hooks/use-reduced-motion/{index.js → index.ts} +2 -2
  128. package/src/hooks/use-throttle/{index.js → index.ts} +12 -6
  129. package/src/hooks/use-viewport-match/{index.js → index.ts} +33 -26
  130. package/src/hooks/use-warn-on-change/index.ts +42 -0
  131. package/src/higher-order/with-network-connectivity/README.md +0 -20
  132. package/src/higher-order/with-network-connectivity/index.native.js +0 -19
  133. package/src/higher-order/with-preferred-color-scheme/index.native.js +0 -40
  134. package/src/hooks/use-constrained-tabbing/index.native.js +0 -14
  135. package/src/hooks/use-dragging/index.js +0 -74
  136. package/src/hooks/use-focus-outside/index.native.js +0 -170
  137. package/src/hooks/use-keyboard-shortcut/index.native.js +0 -2
  138. package/src/hooks/use-network-connectivity/index.native.js +0 -59
  139. package/src/hooks/use-network-connectivity/test/index.native.js +0 -87
  140. package/src/hooks/use-preferred-color-scheme/index.android.js +0 -30
  141. package/src/hooks/use-preferred-color-scheme/index.ios.js +0 -13
  142. package/src/hooks/use-preferred-color-scheme-style/index.native.js +0 -33
  143. package/src/hooks/use-resize-observer/index.native.js +0 -1
  144. package/src/hooks/use-resize-observer/legacy/index.native.js +0 -59
  145. package/src/hooks/use-resize-observer/legacy/test/index.native.js +0 -46
  146. package/src/hooks/use-warn-on-change/index.js +0 -43
  147. package/src/index.native.js +0 -44
  148. /package/src/hooks/use-isomorphic-layout-effect/{index.js → index.ts} +0 -0
@@ -1,19 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import { createHigherOrderComponent } from '../../utils/create-higher-order-component';
5
- import useNetworkConnectivity from '../../hooks/use-network-connectivity';
6
-
7
- const withNetworkConnectivity = createHigherOrderComponent(
8
- ( WrappedComponent ) => {
9
- return ( props ) => {
10
- const { isConnected } = useNetworkConnectivity();
11
- return (
12
- <WrappedComponent { ...props } isConnected={ isConnected } />
13
- );
14
- };
15
- },
16
- 'withNetworkConnectivity'
17
- );
18
-
19
- export default withNetworkConnectivity;
@@ -1,40 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import { createHigherOrderComponent } from '../../utils/create-higher-order-component';
5
- import usePreferredColorScheme from '../../hooks/use-preferred-color-scheme';
6
-
7
- /**
8
- * WordPress dependencies
9
- */
10
- import { useCallback } from '@wordpress/element';
11
-
12
- const withPreferredColorScheme = createHigherOrderComponent(
13
- ( WrappedComponent ) => ( props ) => {
14
- const colorScheme = usePreferredColorScheme();
15
- const isDarkMode = colorScheme === 'dark';
16
-
17
- const getStyles = useCallback(
18
- ( lightStyles, darkStyles ) => {
19
- const finalDarkStyles = {
20
- ...lightStyles,
21
- ...darkStyles,
22
- };
23
-
24
- return isDarkMode ? finalDarkStyles : lightStyles;
25
- },
26
- [ isDarkMode ]
27
- );
28
-
29
- return (
30
- <WrappedComponent
31
- preferredColorScheme={ colorScheme }
32
- getStylesFromColorScheme={ getStyles }
33
- { ...props }
34
- />
35
- );
36
- },
37
- 'withPreferredColorScheme'
38
- );
39
-
40
- export default withPreferredColorScheme;
@@ -1,14 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useRef } from '@wordpress/element';
5
-
6
- function useConstrainedTabbing() {
7
- const ref = useRef();
8
-
9
- // Do nothing on mobile as tabbing is not a mobile behavior.
10
-
11
- return ref;
12
- }
13
-
14
- export default useConstrainedTabbing;
@@ -1,74 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useCallback, useEffect, useRef, useState } from '@wordpress/element';
5
-
6
- /**
7
- * Internal dependencies
8
- */
9
- import useIsomorphicLayoutEffect from '../use-isomorphic-layout-effect';
10
-
11
- // Event handlers that are triggered from `document` listeners accept a MouseEvent,
12
- // while those triggered from React listeners accept a React.MouseEvent.
13
- /**
14
- * @param {Object} props
15
- * @param {(e: React.MouseEvent) => void} props.onDragStart
16
- * @param {(e: MouseEvent) => void} props.onDragMove
17
- * @param {(e?: MouseEvent) => void} props.onDragEnd
18
- */
19
- export default function useDragging( { onDragStart, onDragMove, onDragEnd } ) {
20
- const [ isDragging, setIsDragging ] = useState( false );
21
-
22
- const eventsRef = useRef( {
23
- onDragStart,
24
- onDragMove,
25
- onDragEnd,
26
- } );
27
- useIsomorphicLayoutEffect( () => {
28
- eventsRef.current.onDragStart = onDragStart;
29
- eventsRef.current.onDragMove = onDragMove;
30
- eventsRef.current.onDragEnd = onDragEnd;
31
- }, [ onDragStart, onDragMove, onDragEnd ] );
32
-
33
- /** @type {(e: MouseEvent) => void} */
34
- const onMouseMove = useCallback(
35
- ( event ) =>
36
- eventsRef.current.onDragMove &&
37
- eventsRef.current.onDragMove( event ),
38
- []
39
- );
40
- /** @type {(e?: MouseEvent) => void} */
41
- const endDrag = useCallback( ( event ) => {
42
- if ( eventsRef.current.onDragEnd ) {
43
- eventsRef.current.onDragEnd( event );
44
- }
45
- document.removeEventListener( 'mousemove', onMouseMove );
46
- document.removeEventListener( 'mouseup', endDrag );
47
- setIsDragging( false );
48
- }, [] );
49
- /** @type {(e: React.MouseEvent) => void} */
50
- const startDrag = useCallback( ( event ) => {
51
- if ( eventsRef.current.onDragStart ) {
52
- eventsRef.current.onDragStart( event );
53
- }
54
- document.addEventListener( 'mousemove', onMouseMove );
55
- document.addEventListener( 'mouseup', endDrag );
56
- setIsDragging( true );
57
- }, [] );
58
-
59
- // Remove the global events when unmounting if needed.
60
- useEffect( () => {
61
- return () => {
62
- if ( isDragging ) {
63
- document.removeEventListener( 'mousemove', onMouseMove );
64
- document.removeEventListener( 'mouseup', endDrag );
65
- }
66
- };
67
- }, [ isDragging ] );
68
-
69
- return {
70
- startDrag,
71
- endDrag,
72
- isDragging,
73
- };
74
- }
@@ -1,170 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useCallback, useEffect, useRef } from '@wordpress/element';
5
-
6
- /**
7
- * Input types which are classified as button types, for use in considering
8
- * whether element is a (focus-normalized) button.
9
- *
10
- * @type {string[]}
11
- */
12
- const INPUT_BUTTON_TYPES = [ 'button', 'submit' ];
13
-
14
- /**
15
- * @typedef {HTMLButtonElement | HTMLLinkElement | HTMLInputElement} FocusNormalizedButton
16
- */
17
-
18
- /**
19
- * Returns true if the given element is a button element subject to focus
20
- * normalization, or false otherwise.
21
- *
22
- * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
23
- *
24
- * @param {EventTarget} eventTarget The target from a mouse or touch event.
25
- *
26
- * @return {eventTarget is FocusNormalizedButton} Whether element is a button.
27
- */
28
- function isFocusNormalizedButton( eventTarget ) {
29
- switch ( eventTarget.nodeName ) {
30
- case 'A':
31
- case 'BUTTON':
32
- return true;
33
-
34
- case 'INPUT':
35
- return INPUT_BUTTON_TYPES.includes(
36
- /** @type {HTMLInputElement} */ ( eventTarget ).type
37
- );
38
- }
39
-
40
- return false;
41
- }
42
-
43
- /**
44
- * @typedef {React.SyntheticEvent} SyntheticEvent
45
- */
46
-
47
- /**
48
- * @callback EventCallback
49
- * @param {SyntheticEvent} event input related event.
50
- */
51
-
52
- /**
53
- * @typedef FocusOutsideReactElement
54
- * @property {EventCallback} handleFocusOutside callback for a focus outside event.
55
- */
56
-
57
- /**
58
- * @typedef {React.MutableRefObject<FocusOutsideReactElement | undefined>} FocusOutsideRef
59
- */
60
-
61
- /**
62
- * @typedef {Object} FocusOutsideReturnValue
63
- * @property {EventCallback} onFocus An event handler for focus events.
64
- * @property {EventCallback} onBlur An event handler for blur events.
65
- * @property {EventCallback} onMouseDown An event handler for mouse down events.
66
- * @property {EventCallback} onMouseUp An event handler for mouse up events.
67
- * @property {EventCallback} onTouchStart An event handler for touch start events.
68
- * @property {EventCallback} onTouchEnd An event handler for touch end events.
69
- */
70
-
71
- /**
72
- * A react hook that can be used to check whether focus has moved outside the
73
- * element the event handlers are bound to.
74
- *
75
- * @param {EventCallback} onFocusOutside A callback triggered when focus moves outside
76
- * the element the event handlers are bound to.
77
- *
78
- * @return {FocusOutsideReturnValue} An object containing event handlers. Bind the event handlers
79
- * to a wrapping element element to capture when focus moves
80
- * outside that element.
81
- */
82
- export default function useFocusOutside( onFocusOutside ) {
83
- const currentOnFocusOutside = useRef( onFocusOutside );
84
- useEffect( () => {
85
- currentOnFocusOutside.current = onFocusOutside;
86
- }, [ onFocusOutside ] );
87
-
88
- const preventBlurCheck = useRef( false );
89
-
90
- /**
91
- * @type {React.MutableRefObject<number | undefined>}
92
- */
93
- const blurCheckTimeoutId = useRef();
94
-
95
- /**
96
- * Cancel a blur check timeout.
97
- */
98
- const cancelBlurCheck = useCallback( () => {
99
- clearTimeout( blurCheckTimeoutId.current );
100
- }, [] );
101
-
102
- // Cancel blur checks on unmount.
103
- useEffect( () => {
104
- return () => cancelBlurCheck();
105
- }, [] );
106
-
107
- // Cancel a blur check if the callback or ref is no longer provided.
108
- useEffect( () => {
109
- if ( ! onFocusOutside ) {
110
- cancelBlurCheck();
111
- }
112
- }, [ onFocusOutside, cancelBlurCheck ] );
113
-
114
- /**
115
- * Handles a mousedown or mouseup event to respectively assign and
116
- * unassign a flag for preventing blur check on button elements. Some
117
- * browsers, namely Firefox and Safari, do not emit a focus event on
118
- * button elements when clicked, while others do. The logic here
119
- * intends to normalize this as treating click on buttons as focus.
120
- *
121
- * @see https://developer.mozilla.org/en-US/docs/Web/HTML/Element/button#Clicking_and_focus
122
- *
123
- * @param {SyntheticEvent} event Event for mousedown or mouseup.
124
- */
125
- const normalizeButtonFocus = useCallback( ( event ) => {
126
- const { type, target } = event;
127
- const isInteractionEnd = [ 'mouseup', 'touchend' ].includes( type );
128
-
129
- if ( isInteractionEnd ) {
130
- preventBlurCheck.current = false;
131
- } else if ( isFocusNormalizedButton( target ) ) {
132
- preventBlurCheck.current = true;
133
- }
134
- }, [] );
135
-
136
- /**
137
- * A callback triggered when a blur event occurs on the element the handler
138
- * is bound to.
139
- *
140
- * Calls the `onFocusOutside` callback in an immediate timeout if focus has
141
- * move outside the bound element and is still within the document.
142
- *
143
- * @param {SyntheticEvent} event Blur event.
144
- */
145
- const queueBlurCheck = useCallback( ( event ) => {
146
- // React does not allow using an event reference asynchronously
147
- // due to recycling behavior, except when explicitly persisted.
148
- event.persist();
149
-
150
- // Skip blur check if clicking button. See `normalizeButtonFocus`.
151
- if ( preventBlurCheck.current ) {
152
- return;
153
- }
154
-
155
- blurCheckTimeoutId.current = setTimeout( () => {
156
- if ( 'function' === typeof currentOnFocusOutside.current ) {
157
- currentOnFocusOutside.current( event );
158
- }
159
- }, 0 );
160
- }, [] );
161
-
162
- return {
163
- onFocus: cancelBlurCheck,
164
- onMouseDown: normalizeButtonFocus,
165
- onMouseUp: normalizeButtonFocus,
166
- onTouchStart: normalizeButtonFocus,
167
- onTouchEnd: normalizeButtonFocus,
168
- onBlur: queueBlurCheck,
169
- };
170
- }
@@ -1,2 +0,0 @@
1
- const useKeyboardShortcut = () => null;
2
- export default useKeyboardShortcut;
@@ -1,59 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useEffect, useState } from '@wordpress/element';
5
- import {
6
- requestConnectionStatus,
7
- subscribeConnectionStatus,
8
- } from '@wordpress/react-native-bridge';
9
-
10
- /**
11
- * @typedef {Object} NetworkInformation
12
- *
13
- * @property {boolean} [isConnected] Whether the device is connected to a network.
14
- */
15
-
16
- /**
17
- * Returns the current network connectivity status provided by the native bridge.
18
- *
19
- * @example
20
- *
21
- * ```jsx
22
- * const { isConnected } = useNetworkConnectivity();
23
- * ```
24
- *
25
- * @return {NetworkInformation} Network information.
26
- */
27
- export default function useNetworkConnectivity() {
28
- const [ isConnected, setIsConnected ] = useState( true );
29
-
30
- useEffect( () => {
31
- let isCurrent = true;
32
-
33
- requestConnectionStatus( ( isBridgeConnected ) => {
34
- if ( ! isCurrent ) {
35
- return;
36
- }
37
-
38
- setIsConnected( isBridgeConnected );
39
- } );
40
-
41
- return () => {
42
- isCurrent = false;
43
- };
44
- }, [] );
45
-
46
- useEffect( () => {
47
- const subscription = subscribeConnectionStatus(
48
- ( { isConnected: isBridgeConnected } ) => {
49
- setIsConnected( isBridgeConnected );
50
- }
51
- );
52
-
53
- return () => {
54
- subscription.remove();
55
- };
56
- }, [] );
57
-
58
- return { isConnected };
59
- }
@@ -1,87 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { act, renderHook } from 'test/helpers';
5
-
6
- /**
7
- * WordPress dependencies
8
- */
9
- import {
10
- requestConnectionStatus,
11
- subscribeConnectionStatus,
12
- } from '@wordpress/react-native-bridge';
13
-
14
- /**
15
- * Internal dependencies
16
- */
17
- import useNetworkConnectivity from '../index';
18
-
19
- describe( 'useNetworkConnectivity', () => {
20
- it( 'should optimisitically presume network connectivity', () => {
21
- const { result } = renderHook( () => useNetworkConnectivity() );
22
-
23
- expect( result.current.isConnected ).toBe( true );
24
- } );
25
-
26
- describe( 'when network connectivity is available', () => {
27
- beforeAll( () => {
28
- requestConnectionStatus.mockImplementation( ( callback ) => {
29
- callback( true );
30
- return { remove: jest.fn() };
31
- } );
32
- } );
33
-
34
- it( 'should return true', () => {
35
- const { result } = renderHook( () => useNetworkConnectivity() );
36
-
37
- expect( result.current.isConnected ).toBe( true );
38
- } );
39
-
40
- it( 'should update the status when network connectivity changes', () => {
41
- let subscriptionCallback;
42
- subscribeConnectionStatus.mockImplementation( ( callback ) => {
43
- subscriptionCallback = callback;
44
- return { remove: jest.fn() };
45
- } );
46
-
47
- const { result } = renderHook( () => useNetworkConnectivity() );
48
-
49
- expect( result.current.isConnected ).toBe( true );
50
-
51
- act( () => subscriptionCallback( { isConnected: false } ) );
52
-
53
- expect( result.current.isConnected ).toBe( false );
54
- } );
55
- } );
56
-
57
- describe( 'when network connectivity is unavailable', () => {
58
- beforeAll( () => {
59
- requestConnectionStatus.mockImplementation( ( callback ) => {
60
- callback( false );
61
- return { remove: jest.fn() };
62
- } );
63
- } );
64
-
65
- it( 'should return false', () => {
66
- const { result } = renderHook( () => useNetworkConnectivity() );
67
-
68
- expect( result.current.isConnected ).toBe( false );
69
- } );
70
-
71
- it( 'should update the status when network connectivity changes', () => {
72
- let subscriptionCallback;
73
- subscribeConnectionStatus.mockImplementation( ( callback ) => {
74
- subscriptionCallback = callback;
75
- return { remove: jest.fn() };
76
- } );
77
-
78
- const { result } = renderHook( () => useNetworkConnectivity() );
79
-
80
- expect( result.current.isConnected ).toBe( false );
81
-
82
- act( () => subscriptionCallback( { isConnected: true } ) );
83
-
84
- expect( result.current.isConnected ).toBe( true );
85
- } );
86
- } );
87
- } );
@@ -1,30 +0,0 @@
1
- /**
2
- * WordPress dependencies
3
- */
4
- import { useState, useEffect } from '@wordpress/element';
5
- import {
6
- subscribePreferredColorScheme,
7
- isInitialColorSchemeDark,
8
- } from '@wordpress/react-native-bridge';
9
-
10
- /**
11
- * Returns the color scheme value when it changes. Possible values: [ 'light', 'dark' ]
12
- *
13
- * @return {string} return current color scheme.
14
- */
15
- function usePreferredColorScheme() {
16
- const [ currentColorScheme, setCurrentColorScheme ] = useState(
17
- isInitialColorSchemeDark ? 'dark' : 'light'
18
- );
19
- useEffect( () => {
20
- subscribePreferredColorScheme( ( { isPreferredColorSchemeDark } ) => {
21
- const colorScheme = isPreferredColorSchemeDark ? 'dark' : 'light';
22
- if ( colorScheme !== currentColorScheme ) {
23
- setCurrentColorScheme( colorScheme );
24
- }
25
- } );
26
- } );
27
- return currentColorScheme;
28
- }
29
-
30
- export default usePreferredColorScheme;
@@ -1,13 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { useColorScheme } from 'react-native';
5
-
6
- /**
7
- * Returns the color scheme value when it changes. Possible values: [ 'light', 'dark' ]
8
- *
9
- * @return {string} return current color scheme.
10
- */
11
- const usePreferredColorScheme = useColorScheme;
12
-
13
- export default usePreferredColorScheme;
@@ -1,33 +0,0 @@
1
- /**
2
- * Internal dependencies
3
- */
4
- import usePreferredColorScheme from '../use-preferred-color-scheme';
5
-
6
- /**
7
- * Selects which of the passed style objects should be applied depending on the
8
- * user's preferred color scheme.
9
- *
10
- * The "light" color schemed is assumed to be the default, and its styles are
11
- * always applied. The "dark" styles will always extend those defined for the
12
- * light case.
13
- *
14
- * @example
15
- * const light = { padding: 10, backgroundColor: 'white' };
16
- * const dark = { backgroundColor: 'black' };
17
- * usePreferredColorSchemeStyle( light, dark);
18
- * // On light mode:
19
- * // => { padding: 10, backgroundColor: 'white' }
20
- * // On dark mode:
21
- * // => { padding: 10, backgroundColor: 'black' }
22
- * @param {Object} lightStyle
23
- * @param {Object} darkStyle
24
- * @return {Object} the combined styles depending on the current color scheme
25
- */
26
- const usePreferredColorSchemeStyle = ( lightStyle, darkStyle ) => {
27
- const colorScheme = usePreferredColorScheme();
28
- const isDarkMode = colorScheme === 'dark';
29
-
30
- return isDarkMode ? { ...lightStyle, ...darkStyle } : lightStyle;
31
- };
32
-
33
- export default usePreferredColorSchemeStyle;
@@ -1 +0,0 @@
1
- export { default } from './legacy/index.native';
@@ -1,59 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { View, StyleSheet } from 'react-native';
5
- /**
6
- * WordPress dependencies
7
- */
8
- import { useState, useCallback } from '@wordpress/element';
9
-
10
- /**
11
- * Hook which allows to listen the resize event of any target element when it changes sizes.
12
- *
13
- * @example
14
- *
15
- * ```js
16
- * const App = () => {
17
- * const [ resizeListener, sizes ] = useResizeObserver();
18
- *
19
- * return (
20
- * <View>
21
- * { resizeListener }
22
- * Your content here
23
- * </View>
24
- * );
25
- * };
26
- * ```
27
- */
28
- const useResizeObserver = () => {
29
- const [ measurements, setMeasurements ] = useState( null );
30
-
31
- const onLayout = useCallback( ( { nativeEvent } ) => {
32
- const { width, height } = nativeEvent.layout;
33
- setMeasurements( ( prevState ) => {
34
- if (
35
- ! prevState ||
36
- prevState.width !== width ||
37
- prevState.height !== height
38
- ) {
39
- return {
40
- width: Math.floor( width ),
41
- height: Math.floor( height ),
42
- };
43
- }
44
- return prevState;
45
- } );
46
- }, [] );
47
-
48
- const observer = (
49
- <View
50
- testID="resize-observer"
51
- style={ StyleSheet.absoluteFill }
52
- onLayout={ onLayout }
53
- />
54
- );
55
-
56
- return [ observer, measurements ];
57
- };
58
-
59
- export default useResizeObserver;
@@ -1,46 +0,0 @@
1
- /**
2
- * External dependencies
3
- */
4
- import { render, fireEvent } from 'test/helpers';
5
- import { View } from 'react-native';
6
-
7
- /**
8
- * Internal dependencies
9
- */
10
- import useResizeObserver from '..';
11
-
12
- const TestComponent = ( { onLayout } ) => {
13
- const [ resizeObserver, sizes ] = useResizeObserver();
14
-
15
- return (
16
- <View testID="test-component" sizes={ sizes } onLayout={ onLayout }>
17
- { resizeObserver }
18
- </View>
19
- );
20
- };
21
-
22
- describe( 'useResizeObserver()', () => {
23
- it( 'should return "{ width: 300, height: 500 }"', () => {
24
- const mockNativeEvent = {
25
- nativeEvent: {
26
- layout: {
27
- width: 300,
28
- height: 500,
29
- },
30
- },
31
- };
32
-
33
- const { getByTestId } = render(
34
- <TestComponent onLayout={ mockNativeEvent } />
35
- );
36
-
37
- const resizeObserver = getByTestId( 'resize-observer' );
38
- fireEvent( resizeObserver, 'layout', mockNativeEvent );
39
-
40
- const testComponent = getByTestId( 'test-component' );
41
- expect( testComponent.props.sizes ).toMatchObject( {
42
- width: 300,
43
- height: 500,
44
- } );
45
- } );
46
- } );