| 1 |
import { Button, Tooltip } from '@wordpress/components'; |
| 2 |
import { useEffect, useState } from '@wordpress/element'; |
| 3 |
import { __, sprintf } from '@wordpress/i18n'; |
| 4 |
import classnames from 'classnames/dedupe'; |
| 5 |
|
| 6 |
import useResponsive from '../../hooks/use-responsive'; |
| 7 |
import getIcon from '../../utils/get-icon'; |
| 8 |
import ActiveIndicator from '../active-indicator'; |
| 9 |
|
| 10 |
/** |
| 11 |
* Component Class |
| 12 |
* |
| 13 |
* @param props |
| 14 |
*/ |
| 15 |
export default function ResponsiveToggle(props) { |
| 16 |
const { |
| 17 |
checkActive = () => { |
| 18 |
return false; |
| 19 |
}, |
| 20 |
} = props; |
| 21 |
|
| 22 |
const [isOpen, setIsOpen] = useState(false); |
| 23 |
|
| 24 |
const { allDevices, device, setDevice } = useResponsive(); |
| 25 |
|
| 26 |
useEffect(() => { |
| 27 |
function handleClickOutside(event) { |
| 28 |
if (!event.target.closest('.ghostkit-control-responsive-toggle')) { |
| 29 |
setIsOpen(false); |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
document.addEventListener('click', handleClickOutside); |
| 34 |
|
| 35 |
return () => { |
| 36 |
document.removeEventListener('click', handleClickOutside); |
| 37 |
}; |
| 38 |
}); |
| 39 |
|
| 40 |
const items = []; |
| 41 |
const icons = [ |
| 42 |
getIcon('tabs-mobile'), |
| 43 |
getIcon('tabs-tablet'), |
| 44 |
getIcon('tabs-laptop'), |
| 45 |
getIcon('tabs-desktop'), |
| 46 |
getIcon('tabs-tv'), |
| 47 |
]; |
| 48 |
|
| 49 |
let selectedIcon = icons[icons.length - 1]; |
| 50 |
let translateY = '0'; |
| 51 |
let withActiveResponsive = false; |
| 52 |
|
| 53 |
[...Object.keys(allDevices), ''].forEach((name, i) => { |
| 54 |
if (name === device) { |
| 55 |
selectedIcon = icons[i]; |
| 56 |
translateY = `${(100 * (1 + i - icons.length)) / icons.length}%`; |
| 57 |
|
| 58 |
// Additional transform for gap. |
| 59 |
translateY = `calc(${translateY} + ${icons.length - i - 1}px)`; |
| 60 |
} |
| 61 |
|
| 62 |
const isActive = name && checkActive && checkActive(name); |
| 63 |
|
| 64 |
withActiveResponsive = withActiveResponsive || isActive; |
| 65 |
|
| 66 |
items.unshift({ |
| 67 |
name, |
| 68 |
title: ( |
| 69 |
<Tooltip |
| 70 |
text={ |
| 71 |
!name |
| 72 |
? __('All devices', 'ghostkit') |
| 73 |
: sprintf( |
| 74 |
// translators: %s: breakpoint width in pixels, for example "768px". |
| 75 |
__( |
| 76 |
'Devices with screen width <= %s', |
| 77 |
'ghostkit' |
| 78 |
), |
| 79 |
`${allDevices[name]}px` |
| 80 |
) |
| 81 |
} |
| 82 |
> |
| 83 |
<span className="ghostkit-control-responsive-toggle-icon"> |
| 84 |
{icons[i]} |
| 85 |
{isActive && <ActiveIndicator />} |
| 86 |
</span> |
| 87 |
</Tooltip> |
| 88 |
), |
| 89 |
}); |
| 90 |
}); |
| 91 |
|
| 92 |
return ( |
| 93 |
<div className="ghostkit-control-responsive-toggle"> |
| 94 |
<Button |
| 95 |
className="ghostkit-control-responsive-toggle-button" |
| 96 |
onClick={() => { |
| 97 |
setIsOpen(true); |
| 98 |
}} |
| 99 |
> |
| 100 |
{selectedIcon} |
| 101 |
{withActiveResponsive && <ActiveIndicator />} |
| 102 |
</Button> |
| 103 |
<div |
| 104 |
className={classnames( |
| 105 |
'ghostkit-control-responsive-toggle-dropdown', |
| 106 |
isOpen && 'is-open' |
| 107 |
)} |
| 108 |
style={{ |
| 109 |
transform: `translateY(${translateY})`, |
| 110 |
}} |
| 111 |
> |
| 112 |
{items.map((data) => { |
| 113 |
return ( |
| 114 |
<Button |
| 115 |
key={data.name} |
| 116 |
className={classnames( |
| 117 |
data.name === device && 'is-active' |
| 118 |
)} |
| 119 |
onClick={() => { |
| 120 |
setDevice(data.name); |
| 121 |
setIsOpen(false); |
| 122 |
}} |
| 123 |
> |
| 124 |
{data.title} |
| 125 |
</Button> |
| 126 |
); |
| 127 |
})} |
| 128 |
</div> |
| 129 |
</div> |
| 130 |
); |
| 131 |
} |
| 132 |
|