This repository has been archived by the owner on Dec 8, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCounterGadget.tsx
73 lines (66 loc) · 1.88 KB
/
CounterGadget.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import React, { useRef, useState } from "react";
import Draggable, { ControlPosition } from "react-draggable";
import { draggableGadget, GadgetProps } from "../lib/gadget";
import Switch from 'react-switch';
interface CounterGadgetSettings {
enabled: boolean;
offsets: ControlPosition;
count: number;
}
export const CounterGadgetDefaults: CounterGadgetSettings = {
enabled: false,
offsets: {x: 100, y: 100},
count: 0,
}
const CounterGadget = (props: GadgetProps<CounterGadgetSettings>) => {
const counterGadgetRef = useRef<HTMLDivElement>(null);
return (
<Draggable
nodeRef={counterGadgetRef}
onStop={draggableGadget(counterGadgetRef, props.setSettings)}
scale={1}
bounds="parent"
defaultPosition={props.settings.offsets}
>
<div className="Gadget StickyGadget" ref={counterGadgetRef}>
<p>Count: {props.settings.count}</p>
<p><button onClick={() => {
props.setSettings((current) => ({
...current,
count: ++current.count,
}))
}}>Increment</button></p>
<p><button onClick={() => {
props.setSettings((current) => ({
...current,
count: 0,
}))
}}>Clear</button></p>
</div>
</Draggable>
);
}
export const CounterGadgetSettingsNode = (props: GadgetProps<CounterGadgetSettings>) => {
const [gadgetToggle, setGadgetToggle] = useState(props.settings.enabled);
return (
<div className="SettingsNode">
<p>Enable Counter Gadget</p>
<label>
<Switch
checked={gadgetToggle}
onChange={() => {
setGadgetToggle(cur => !cur);
props.setSettings(cur => ({
...cur,
enabled: !cur.enabled,
}))
}}
className="StickyGadgetSwitch"
height={25}
width={50}
/>
</label>
</div>
);
}
export default CounterGadget;