PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.4
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.4
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 4.0.2 4.0.1 2.3.5 2.3.6 2.4.0 2.4.1 2.4.10 2.4.11 2.4.12 2.4.13 2.4.14 2.4.15 2.4.16 2.4.17 2.4.18 2.4.19 2.4.2 2.4.20 2.4.21 All 88 releases
post-carousel / src / components / rangeControl / units.js

units.js in Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News 4.0.4, at src/components/rangeControl/units.js

81 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 import { memo } from "@wordpress/element";
2 import { Button } from "@wordpress/components";
3 import { useDeviceType } from "../../controls/controls";
4
5 const Units = ({ attributes, setAttributes, attributesKey, units, onUnitChange = false }) => {
6 const deviceType = useDeviceType();
7 // Set unit function.
8 const setUnit = (newValue) => {
9 if (onUnitChange) {
10 return onUnitChange({ unit: newValue, deviceType: deviceType });
11 }
12 if ("undefined" === typeof attributes.unit && attributes?.device?.[deviceType]) {
13 // if (onUnitChange) {
14 // return onUnitChange({ unit: newValue, deviceType: deviceType });
15 // }
16 setAttributes({
17 [attributesKey]: {
18 ...attributes,
19 unit: {
20 ...attributes?.unit,
21 [deviceType]: newValue.toLowerCase(),
22 },
23 },
24 });
25 } else if (attributes.unit?.[deviceType]) {
26 // if (onUnitChange) {
27 // return onUnitChange({ unit: newValue, deviceType: deviceType });
28 // }
29 setAttributes({
30 [attributesKey]: {
31 ...attributes,
32 unit: {
33 ...attributes.unit,
34 [deviceType]: newValue.toLowerCase(),
35 },
36 },
37 });
38 } else {
39 // if (onUnitChange) {
40 // return onUnitChange({ unit: newValue, deviceType: deviceType || "Desktop" });
41 // }
42 setAttributes({
43 [attributesKey]: {
44 ...attributes,
45 unit: newValue.toLowerCase(),
46 },
47 });
48 }
49 };
50
51 const activeUnitValue = () => {
52 if (typeof attributes.unit === "object" && attributes.unit?.[deviceType]) {
53 return attributes.unit?.[deviceType];
54 } else if (typeof attributes.unit === "string") {
55 return attributes.unit.toLowerCase();
56 }
57 return "";
58 };
59
60 return (
61 <div className="sp-smart-post-units">
62 <span>{"object" !== typeof attributes?.unit ? attributes?.unit : attributes.unit?.[deviceType]}</span>
63 <div className="sp-smart-post-units-btn">
64 {units?.map((item, i) => (
65 <Button
66 className={activeUnitValue() === item.toLowerCase() ? "active" : ""}
67 key={i}
68 value={item}
69 onClick={(e) => setUnit(e.target.value)}
70 >
71 {" "}
72 {item.toLowerCase()}{" "}
73 </Button>
74 ))}
75 </div>
76 </div>
77 );
78 };
79
80 export default memo(Units);
81