index.js
1 year ago
useAuthors.js
3 years ago
useBlockStyles.js
1 year ago
useDebounceState.js
4 years ago
useDeviceAttributes.js
2 years ago
useDeviceType.js
1 year ago
useInnerBlocksCount.js
4 years ago
useQueryReducer.js
1 year ago
useRecordsReducer.js
3 years ago
useSelectedBlockElements.js
2 years ago
useStyleIndicator.js
2 years ago
useTaxonomies.js
1 year ago
useTaxonomyRecords.js
3 years ago
useDeviceType.js
76 lines
| 1 | import { dispatch, useSelect } from '@wordpress/data'; |
| 2 | import { useEffect } from '@wordpress/element'; |
| 3 | import useLocalStorageState from 'use-local-storage-state'; |
| 4 | |
| 5 | export default ( initialDeviceType = 'Desktop' ) => { |
| 6 | const [ localDeviceType, setLocalDeviceType ] = useLocalStorageState( |
| 7 | 'generateblocksDeviceType', { |
| 8 | ssr: true, |
| 9 | defaultValue: initialDeviceType, |
| 10 | } |
| 11 | ); |
| 12 | |
| 13 | const setPreviewDeviceType = ( deviceType ) => { |
| 14 | const { setDeviceType } = dispatch( 'core/editor' ) || {}; |
| 15 | |
| 16 | if ( 'function' === typeof setDeviceType ) { |
| 17 | return setDeviceType( deviceType ); |
| 18 | } |
| 19 | |
| 20 | const { __experimentalSetPreviewDeviceType: experimentalSetPreviewDeviceType } = dispatch( 'core/edit-post' ); |
| 21 | |
| 22 | if ( 'function' === typeof experimentalSetPreviewDeviceType ) { |
| 23 | return experimentalSetPreviewDeviceType( deviceType ); |
| 24 | } |
| 25 | }; |
| 26 | |
| 27 | const previewDeviceType = useSelect( ( select ) => { |
| 28 | const { getDeviceType } = select( 'core/editor' ) || {}; |
| 29 | |
| 30 | if ( 'function' === typeof getDeviceType ) { |
| 31 | return getDeviceType(); |
| 32 | } |
| 33 | |
| 34 | const { |
| 35 | __experimentalGetPreviewDeviceType: experimentalGetPreviewDeviceType, |
| 36 | } = select( 'core/edit-post' ); |
| 37 | |
| 38 | if ( 'function' === typeof experimentalGetPreviewDeviceType ) { |
| 39 | return experimentalGetPreviewDeviceType(); |
| 40 | } |
| 41 | |
| 42 | return undefined; |
| 43 | }, [] ); |
| 44 | |
| 45 | if ( |
| 46 | undefined === previewDeviceType || // core/editor and/or core/edit-post must not be available. |
| 47 | ( generateBlocksInfo && ! generateBlocksInfo.syncResponsivePreviews ) |
| 48 | ) { |
| 49 | const setDeviceType = ( type ) => { |
| 50 | setLocalDeviceType( type ); |
| 51 | }; |
| 52 | |
| 53 | // If we don't have the necessary functions, or sync previews are turned off, return the local states here. |
| 54 | return [ localDeviceType, setDeviceType ]; |
| 55 | } |
| 56 | |
| 57 | useEffect( () => { |
| 58 | if ( previewDeviceType !== localDeviceType ) { |
| 59 | setLocalDeviceType( previewDeviceType ); |
| 60 | } |
| 61 | }, [ previewDeviceType ] ); |
| 62 | |
| 63 | const setDeviceType = ( type ) => { |
| 64 | setPreviewDeviceType( type ); |
| 65 | setLocalDeviceType( type ); |
| 66 | }; |
| 67 | |
| 68 | // Here we are anticipating the return of the correct device value, instead of waiting another update from the useEffect above. |
| 69 | // This avoids attributes with old values when changing devices using the core buttons. |
| 70 | if ( previewDeviceType !== localDeviceType ) { |
| 71 | return [ previewDeviceType, setDeviceType ]; |
| 72 | } |
| 73 | |
| 74 | return [ localDeviceType, setDeviceType ]; |
| 75 | }; |
| 76 |