| 1 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 2 |
|
| 3 |
import { getMediaSizes } from '../utils/get-media-sizes'; |
| 4 |
|
| 5 |
export default function useResponsive() { |
| 6 |
const { device } = useSelect((select) => { |
| 7 |
const { getDevice } = select('ghostkit/responsive'); |
| 8 |
|
| 9 |
return { |
| 10 |
device: getDevice(), |
| 11 |
}; |
| 12 |
}); |
| 13 |
|
| 14 |
// Check if 'core/editor' store is available. It is not available in the Widgets editor. |
| 15 |
const { setDeviceType } = useDispatch('core/editor') || {}; |
| 16 |
const { setDevice } = useDispatch('ghostkit/responsive'); |
| 17 |
|
| 18 |
const setDeviceWithReset = (...args) => { |
| 19 |
setDevice(...args); |
| 20 |
|
| 21 |
// Reset default preview device. |
| 22 |
if (setDeviceType) { |
| 23 |
setDeviceType('Desktop'); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
return { |
| 28 |
device, |
| 29 |
setDevice: setDeviceWithReset, |
| 30 |
allDevices: getMediaSizes(), |
| 31 |
}; |
| 32 |
} |
| 33 |
|