| 1 |
import { Button, Tooltip } from '@wordpress/components'; |
| 2 |
import { __, sprintf } from '@wordpress/i18n'; |
| 3 |
import classnames from 'classnames/dedupe'; |
| 4 |
|
| 5 |
import useResponsive from '../../hooks/use-responsive'; |
| 6 |
import getIcon from '../../utils/get-icon'; |
| 7 |
import ActiveIndicator from '../active-indicator'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Component Class |
| 11 |
* |
| 12 |
* @param props |
| 13 |
*/ |
| 14 |
export default function ResponsiveTabPanel(props) { |
| 15 |
const { children } = props; |
| 16 |
|
| 17 |
// Fallback for deprecated filledTabs prop. |
| 18 |
const active = props?.active || props?.filledTabs || {}; |
| 19 |
|
| 20 |
const { allDevices, device, setDevice } = useResponsive(); |
| 21 |
|
| 22 |
const tabs = []; |
| 23 |
const icons = [ |
| 24 |
getIcon('tabs-mobile'), |
| 25 |
getIcon('tabs-tablet'), |
| 26 |
getIcon('tabs-laptop'), |
| 27 |
getIcon('tabs-desktop'), |
| 28 |
getIcon('tabs-tv'), |
| 29 |
]; |
| 30 |
|
| 31 |
[...Object.keys(allDevices), ''].forEach((name, i) => { |
| 32 |
tabs.unshift({ |
| 33 |
name, |
| 34 |
title: ( |
| 35 |
<Tooltip |
| 36 |
text={ |
| 37 |
!name |
| 38 |
? __('All devices', 'ghostkit') |
| 39 |
: sprintf( |
| 40 |
// translators: %s: breakpoint width in pixels, for example "768px". |
| 41 |
__( |
| 42 |
'Devices with screen width <= %s', |
| 43 |
'ghostkit' |
| 44 |
), |
| 45 |
`${allDevices[name]}px` |
| 46 |
) |
| 47 |
} |
| 48 |
> |
| 49 |
<span className="ghostkit-control-tabs-icon"> |
| 50 |
{icons[i]} |
| 51 |
{active && active[name] ? <ActiveIndicator /> : ''} |
| 52 |
</span> |
| 53 |
</Tooltip> |
| 54 |
), |
| 55 |
}); |
| 56 |
}); |
| 57 |
|
| 58 |
return ( |
| 59 |
<div className="ghostkit-control-tabs ghostkit-control-tabs-wide"> |
| 60 |
<div className="components-tab-panel__tabs"> |
| 61 |
{tabs.map((data) => { |
| 62 |
return ( |
| 63 |
<Button |
| 64 |
key={data.name} |
| 65 |
className={classnames( |
| 66 |
'ghostkit-control-tabs-tab', |
| 67 |
data.name === device && 'is-active' |
| 68 |
)} |
| 69 |
onClick={() => { |
| 70 |
setDevice(data.name); |
| 71 |
}} |
| 72 |
> |
| 73 |
{data.title} |
| 74 |
</Button> |
| 75 |
); |
| 76 |
})} |
| 77 |
</div> |
| 78 |
{children({ name: device })} |
| 79 |
</div> |
| 80 |
); |
| 81 |
} |
| 82 |
|