PluginProbe
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News / 4.0.3
Smart Post – Post Grid, Post Carousel, Post Slider Gutenberg Blocks for Blog & News v4.0.3
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 / resetBtn.js

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

85 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 { useDeviceType } from "../../controls/controls";
3 import { Button } from "@wordpress/components";
4 import { ResetIcon } from "../../icons/icons";
5
6 const ResetButton = ({
7 attributes,
8 setAttributes,
9 attributesKey,
10 defaultValue,
11 value,
12 setCurrentValue = () => {},
13 setCustomReset = false,
14 }) => {
15 const deviceType = useDeviceType();
16 // Ranger single value and multiple device value.
17 const defaultValueOrDevice = defaultValue?.device ? defaultValue?.device?.[deviceType] : defaultValue.value;
18
19 const activeResetButton = () => {
20 // If unit is not present, treat it as not changed
21 const hasUnit = typeof attributes?.unit === "object" && typeof defaultValue?.unit === "object";
22 const isUnitDifferent = hasUnit && defaultValue.unit?.[deviceType] !== attributes.unit?.[deviceType];
23
24 const isValueDifferent = defaultValue?.device?.[deviceType] !== parseInt(value);
25
26 if (isUnitDifferent || isValueDifferent) {
27 return "active";
28 }
29
30 return "";
31 };
32
33 // Set default value function and reset.
34 const setDefault = () => {
35 if (setCustomReset) {
36 // return { value: defaultValue?.value, unit: defaultValue?.unit };
37 return setCurrentValue(defaultValueOrDevice);
38 }
39 // It's multiple device (desktop/tablet/mobile).
40 if ( !setCustomReset && attributes.device) {
41 setAttributes({
42 [attributesKey]: {
43 ...attributes,
44 device: {
45 ...attributes.device,
46 [deviceType]: defaultValue.value,
47 },
48 unit: {
49 ...attributes.unit,
50 [deviceType]: defaultValue.unit,
51 },
52 },
53 });
54 }
55
56 // It's single device (desktop)
57 if ("object" === typeof attributes) {
58 if ("number" === typeof attributes.value) {
59 setAttributes({
60 [attributesKey]: {
61 ...attributes,
62 value: defaultValue.value,
63 },
64 });
65 }
66 } else {
67 setAttributes({ [attributesKey]: defaultValue });
68 }
69
70 setCurrentValue(defaultValueOrDevice);
71 };
72
73 return (
74 <Button
75 className={`sp-smart-post-header-control-reset ${activeResetButton()}`}
76 onClick={() => setDefault()}
77 aria-label="Reset to default value"
78 >
79 <ResetIcon />
80 </Button>
81 );
82 };
83
84 export default memo(ResetButton);
85