PluginProbe
Block Animations, Motion & Scroll Effects – Ghost Kit / 3.3.2
Block Animations, Motion & Scroll Effects – Ghost Kit v3.3.2
3.7.2 3.7.1 3.7.0 3.6.1 trunk 1.6.3 2.25.0 3.3.0 3.3.1 3.3.2 3.3.3 3.4.0 3.4.1 3.4.2 3.4.3 3.4.4 3.4.5 3.4.6 3.5.0 3.5.1 3.6.0
ghostkit / gutenberg / components / responsive-toggle / index.js

index.js in Block Animations, Motion & Scroll Effects – Ghost Kit 3.3.2, at gutenberg/components/responsive-toggle/index.js

132 lines 2.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import classnames from 'classnames/dedupe';
2
3 import { Button, Tooltip } from '@wordpress/components';
4 import { useEffect, useState } from '@wordpress/element';
5 import { __, sprintf } from '@wordpress/i18n';
6
7 import useResponsive from '../../hooks/use-responsive';
8 import getIcon from '../../utils/get-icon';
9 import ActiveIndicator from '../active-indicator';
10
11 /**
12 * Component Class
13 *
14 * @param props
15 */
16 export default function ResponsiveToggle(props) {
17 const {
18 checkActive = () => {
19 return false;
20 },
21 } = props;
22
23 const [isOpen, setIsOpen] = useState(false);
24
25 const { allDevices, device, setDevice } = useResponsive();
26
27 useEffect(() => {
28 function handleClickOutside(event) {
29 if (!event.target.closest('.ghostkit-control-responsive-toggle')) {
30 setIsOpen(false);
31 }
32 }
33
34 document.addEventListener('click', handleClickOutside);
35
36 return () => {
37 document.removeEventListener('click', handleClickOutside);
38 };
39 });
40
41 const items = [];
42 const icons = [
43 getIcon('tabs-mobile'),
44 getIcon('tabs-tablet'),
45 getIcon('tabs-laptop'),
46 getIcon('tabs-desktop'),
47 getIcon('tabs-tv'),
48 ];
49
50 let selectedIcon = icons[icons.length - 1];
51 let translateY = '0';
52 let withActiveResponsive = false;
53
54 [...Object.keys(allDevices), ''].forEach((name, i) => {
55 if (name === device) {
56 selectedIcon = icons[i];
57 translateY = `${(100 * (1 + i - icons.length)) / icons.length}%`;
58
59 // Additional transform for gap.
60 translateY = `calc(${translateY} + ${icons.length - i - 1}px)`;
61 }
62
63 const isActive = name && checkActive && checkActive(name);
64
65 withActiveResponsive = withActiveResponsive || isActive;
66
67 items.unshift({
68 name,
69 title: (
70 <Tooltip
71 text={
72 !name
73 ? __('All devices', 'ghostkit')
74 : sprintf(
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