@wordpress/compose 8.0.0 → 8.1.1
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 +21 -0
- package/README.md +5 -5
- package/build/higher-order/pure/index.cjs +15 -0
- package/build/higher-order/pure/index.cjs.map +3 -3
- package/build/hooks/use-disabled/index.cjs +2 -2
- package/build/hooks/use-disabled/index.cjs.map +2 -2
- package/build/hooks/use-focus-return/index.cjs.map +1 -1
- package/build/hooks/use-media-query/index.cjs +2 -2
- package/build/hooks/use-media-query/index.cjs.map +2 -2
- package/build/hooks/use-merge-refs/index.cjs +28 -8
- package/build/hooks/use-merge-refs/index.cjs.map +2 -2
- package/build/hooks/use-resize-observer/use-resize-observer.cjs.map +2 -2
- package/build/hooks/use-viewport-match/index.cjs +1 -1
- package/build/hooks/use-viewport-match/index.cjs.map +2 -2
- package/build-module/higher-order/pure/index.mjs +5 -0
- package/build-module/higher-order/pure/index.mjs.map +2 -2
- package/build-module/hooks/use-disabled/index.mjs +2 -2
- package/build-module/hooks/use-disabled/index.mjs.map +2 -2
- package/build-module/hooks/use-focus-return/index.mjs.map +1 -1
- package/build-module/hooks/use-media-query/index.mjs +2 -2
- package/build-module/hooks/use-media-query/index.mjs.map +2 -2
- package/build-module/hooks/use-merge-refs/index.mjs +28 -8
- package/build-module/hooks/use-merge-refs/index.mjs.map +2 -2
- package/build-module/hooks/use-resize-observer/use-resize-observer.mjs.map +2 -2
- package/build-module/hooks/use-viewport-match/index.mjs +1 -1
- package/build-module/hooks/use-viewport-match/index.mjs.map +2 -2
- package/build-types/higher-order/pure/index.d.ts.map +1 -1
- package/build-types/higher-order/with-safe-timeout/index.d.ts +3 -1
- package/build-types/higher-order/with-safe-timeout/index.d.ts.map +1 -1
- package/build-types/hooks/use-media-query/index.d.ts +1 -1
- package/build-types/hooks/use-media-query/index.d.ts.map +1 -1
- package/build-types/hooks/use-merge-refs/index.d.ts +7 -5
- package/build-types/hooks/use-merge-refs/index.d.ts.map +1 -1
- package/build-types/hooks/use-viewport-match/index.d.ts +1 -1
- package/build-types/hooks/use-viewport-match/index.d.ts.map +1 -1
- package/package.json +17 -13
- package/src/higher-order/pure/index.tsx +6 -0
- package/src/higher-order/pure/test/index.js +14 -81
- package/src/hooks/use-disabled/index.ts +3 -3
- package/src/hooks/use-disabled/test/index.js +4 -4
- package/src/hooks/use-drop-zone/README.md +1 -1
- package/src/hooks/use-focus-return/index.js +2 -2
- package/src/hooks/use-media-query/index.ts +10 -3
- package/src/hooks/use-media-query/test/ssr.js +47 -0
- package/src/hooks/use-merge-refs/index.ts +51 -20
- package/src/hooks/use-merge-refs/test/index.js +288 -0
- package/src/hooks/use-resize-observer/use-resize-observer.ts +1 -1
- package/src/hooks/use-viewport-match/index.js +8 -2
- package/src/higher-order/with-network-connectivity/README.md +0 -20
- package/src/higher-order/with-network-connectivity/index.native.js +0 -19
- package/src/higher-order/with-preferred-color-scheme/index.native.js +0 -40
- package/src/hooks/use-constrained-tabbing/index.native.js +0 -14
- package/src/hooks/use-focus-outside/index.native.js +0 -170
- package/src/hooks/use-keyboard-shortcut/index.native.js +0 -2
- package/src/hooks/use-network-connectivity/index.native.js +0 -59
- package/src/hooks/use-network-connectivity/test/index.native.js +0 -87
- package/src/hooks/use-preferred-color-scheme/index.android.js +0 -30
- package/src/hooks/use-preferred-color-scheme/index.ios.js +0 -13
- package/src/hooks/use-preferred-color-scheme-style/index.native.js +0 -33
- package/src/hooks/use-resize-observer/index.native.js +0 -1
- package/src/hooks/use-resize-observer/legacy/index.native.js +0 -59
- package/src/hooks/use-resize-observer/legacy/test/index.native.js +0 -46
- package/src/index.native.js +0 -44
|
@@ -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.RefObject<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.RefObject<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,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
|
-
} );
|
package/src/index.native.js
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
// The `createHigherOrderComponent` helper and helper types.
|
|
2
|
-
export * from './utils/create-higher-order-component';
|
|
3
|
-
// The `debounce` helper and its types.
|
|
4
|
-
export * from './utils/debounce';
|
|
5
|
-
// The `throttle` helper and its types.
|
|
6
|
-
export * from './utils/throttle';
|
|
7
|
-
// The `ObservableMap` data structure
|
|
8
|
-
export * from './utils/observable-map';
|
|
9
|
-
|
|
10
|
-
// The `compose` and `pipe` helpers (inspired by `flowRight` and `flow` from Lodash).
|
|
11
|
-
export { default as compose } from './higher-order/compose';
|
|
12
|
-
export { default as pipe } from './higher-order/pipe';
|
|
13
|
-
|
|
14
|
-
// Higher-order components.
|
|
15
|
-
export { default as ifCondition } from './higher-order/if-condition';
|
|
16
|
-
export { default as pure } from './higher-order/pure';
|
|
17
|
-
export { default as withGlobalEvents } from './higher-order/with-global-events';
|
|
18
|
-
export { default as withInstanceId } from './higher-order/with-instance-id';
|
|
19
|
-
export { default as withSafeTimeout } from './higher-order/with-safe-timeout';
|
|
20
|
-
export { default as withState } from './higher-order/with-state';
|
|
21
|
-
export { default as withPreferredColorScheme } from './higher-order/with-preferred-color-scheme';
|
|
22
|
-
export { default as withNetworkConnectivity } from './higher-order/with-network-connectivity';
|
|
23
|
-
|
|
24
|
-
// Hooks.
|
|
25
|
-
export { default as useConstrainedTabbing } from './hooks/use-constrained-tabbing';
|
|
26
|
-
export { default as __experimentalUseDragging } from './hooks/use-dragging';
|
|
27
|
-
export { default as __experimentalUseFocusOutside } from './hooks/use-focus-outside';
|
|
28
|
-
export { default as useInstanceId } from './hooks/use-instance-id';
|
|
29
|
-
export { default as useIsomorphicLayoutEffect } from './hooks/use-isomorphic-layout-effect';
|
|
30
|
-
export { default as useKeyboardShortcut } from './hooks/use-keyboard-shortcut';
|
|
31
|
-
export { default as useMediaQuery } from './hooks/use-media-query';
|
|
32
|
-
export { default as usePrevious } from './hooks/use-previous';
|
|
33
|
-
export { default as useReducedMotion } from './hooks/use-reduced-motion';
|
|
34
|
-
export { default as useViewportMatch } from './hooks/use-viewport-match';
|
|
35
|
-
export { default as usePreferredColorScheme } from './hooks/use-preferred-color-scheme';
|
|
36
|
-
export { default as usePreferredColorSchemeStyle } from './hooks/use-preferred-color-scheme-style';
|
|
37
|
-
export { default as useResizeObserver } from './hooks/use-resize-observer';
|
|
38
|
-
export { default as useDebounce } from './hooks/use-debounce';
|
|
39
|
-
export { default as useDebouncedInput } from './hooks/use-debounced-input';
|
|
40
|
-
export { default as useThrottle } from './hooks/use-throttle';
|
|
41
|
-
export { default as useMergeRefs } from './hooks/use-merge-refs';
|
|
42
|
-
export { default as useRefEffect } from './hooks/use-ref-effect';
|
|
43
|
-
export { default as useNetworkConnectivity } from './hooks/use-network-connectivity';
|
|
44
|
-
export { default as useObservableValue } from './hooks/use-observable-value';
|