| 1 |
import { Button } from "@wordpress/components"; |
| 2 |
import { dispatch } from "@wordpress/data"; |
| 3 |
import "./editor.scss"; |
| 4 |
import { DesktopIcon, MobileIcon, TabletIcon } from "./icon"; |
| 5 |
import { useDeviceType } from "../../controls/controls"; |
| 6 |
|
| 7 |
const Responsive = () => { |
| 8 |
const Device = (e) => { |
| 9 |
const canvas = document.getElementsByClassName("edit-site-visual-editor__editor-canvas"); |
| 10 |
if (canvas.length > 0) { |
| 11 |
dispatch("core/edit-site").__experimentalSetPreviewDeviceType(e.target.closest("button").value); |
| 12 |
} else { |
| 13 |
dispatch("core/edit-post").__experimentalSetPreviewDeviceType(e.target.closest("button").value); |
| 14 |
} |
| 15 |
}; |
| 16 |
|
| 17 |
const deviceType = useDeviceType(); |
| 18 |
|
| 19 |
const DeviceIcon = () => { |
| 20 |
if ("Desktop" === deviceType) { |
| 21 |
return <DesktopIcon />; |
| 22 |
} |
| 23 |
if ("Tablet" === deviceType) { |
| 24 |
return <TabletIcon />; |
| 25 |
} |
| 26 |
if ("Mobile" === deviceType) { |
| 27 |
return <MobileIcon />; |
| 28 |
} |
| 29 |
}; |
| 30 |
|
| 31 |
return ( |
| 32 |
<> |
| 33 |
<div className="sp-smart-post-responsive"> |
| 34 |
<div className="sp-smart-post-units"> |
| 35 |
<span aria-label="Change device preview"> |
| 36 |
<DeviceIcon /> |
| 37 |
</span> |
| 38 |
<div className="sp-smart-post-units-btn"> |
| 39 |
<Button |
| 40 |
aria-label="Switch to desktop view" |
| 41 |
className={deviceType === "Desktop" ? "active" : ""} |
| 42 |
value={"Desktop"} |
| 43 |
onClick={Device} |
| 44 |
> |
| 45 |
<DesktopIcon /> |
| 46 |
</Button> |
| 47 |
<Button |
| 48 |
aria-label="Switch to tablet view" |
| 49 |
className={deviceType === "Tablet" ? "active" : ""} |
| 50 |
value={"Tablet"} |
| 51 |
onClick={Device} |
| 52 |
> |
| 53 |
<TabletIcon /> |
| 54 |
</Button> |
| 55 |
<Button |
| 56 |
aria-label="Switch to mobile view" |
| 57 |
className={deviceType === "Mobile" ? "active" : ""} |
| 58 |
value={"Mobile"} |
| 59 |
onClick={Device} |
| 60 |
> |
| 61 |
<MobileIcon /> |
| 62 |
</Button> |
| 63 |
</div> |
| 64 |
</div> |
| 65 |
</div> |
| 66 |
</> |
| 67 |
); |
| 68 |
}; |
| 69 |
|
| 70 |
export default Responsive; |
| 71 |
|