Skip to content

armgit / fider

Public Imported from GitHub
0

Open platform to collect and prioritize feedback

enhancement: introducing a toggle component (#339)

Guilherme Oenningb3a0801c37cb
diff --git a/public/components/common/Toggle.scss b/public/components/common/Toggle.scss
new file mode 100644
index 000000000..75f6c94b8
--- /dev/null
+++ b/public/components/common/Toggle.scss
@@ -0,0 +1,49 @@
+@import '~@fider/assets/styles/variables.scss';
+
+.c-toggle {
+ cursor: pointer;
+
+ input {
+ display: none;
+ }
+
+ .text {
+ margin-left: 5px;
+ vertical-align: middle;
+ }
+
+ label {
+ display: inline-block;
+ vertical-align: middle;
+ width: 2em;
+ height: 1em;
+ border-radius: 0.5em;
+ position: relative;
+ cursor: pointer;
+ background: $gray-darker;
+ }
+
+ .switch {
+ position: absolute;
+ width: 0.9em;
+ height: 0.9em;
+ margin-top: 0.05em;
+ margin-left: 0.05em;
+ border-radius: 1em;
+ background: $white;
+ box-shadow: 5px 0px 28px -9px rgba(0, 0, 0, 0.75);
+ transition: transform .2s ease-in;
+ }
+
+ input:checked ~ label {
+ background: $green;
+ }
+
+ input:checked ~ label .switch {
+ background: $white;
+ transform: translatex(1em);
+ transition: transform .2s ease-in;
+ }
+}
+
+
diff --git a/public/components/common/Toggle.tsx b/public/components/common/Toggle.tsx
new file mode 100644
index 000000000..9fd570bec
--- /dev/null
+++ b/public/components/common/Toggle.tsx
@@ -0,0 +1,46 @@
+import * as React from "react";
+import { classSet } from "@fider/services";
+
+interface ToggleProps {
+ label?: string;
+ active: boolean;
+ onToggle: (active: boolean) => Promise<any>;
+}
+
+interface ToggleState {
+ active: boolean;
+}
+
+import "./Toggle.scss";
+
+export class Toggle extends React.Component<ToggleProps, ToggleState> {
+ public constructor(props: ToggleProps) {
+ super(props);
+ this.state = {
+ active: props.active
+ };
+ }
+
+ public toggle = async () => {
+ this.setState(
+ state => ({
+ active: !state.active
+ }),
+ () => {
+ this.props.onToggle(this.state.active);
+ }
+ );
+ };
+
+ public render() {
+ return (
+ <span className="c-toggle" onClick={this.toggle}>
+ <input type="checkbox" checked={this.state.active} readOnly={true} />
+ <label>
+ <span className="switch" />
+ </label>
+ <span className="text">{!!this.props.label && this.props.label}</span>
+ </span>
+ );
+ }
+}
diff --git a/public/components/common/index.tsx b/public/components/common/index.tsx
index 7bda62085..9facdfac1 100644
--- a/public/components/common/index.tsx
+++ b/public/components/common/index.tsx
@@ -12,6 +12,7 @@ export * from "./Moment";
export * from "./UserName";
export * from "./Modal";
export * from "./Loader";
+export * from "./Toggle";
import Textarea from "react-textarea-autosize";
export { Textarea };
diff --git a/public/pages/MySettings/MySettings.page.scss b/public/pages/MySettings/MySettings.page.scss
index 0910d7e04..7e1e115af 100644
--- a/public/pages/MySettings/MySettings.page.scss
+++ b/public/pages/MySettings/MySettings.page.scss
@@ -4,8 +4,7 @@
}
.notifications-settings {
- .option {
- cursor: pointer;
+ .c-toggle {
margin-right: 20px;
}
.event-title {
diff --git a/public/pages/MySettings/components/NotificationSettings.tsx b/public/pages/MySettings/components/NotificationSettings.tsx
index 23dbdf705..b5833cc14 100644
--- a/public/pages/MySettings/components/NotificationSettings.tsx
+++ b/public/pages/MySettings/components/NotificationSettings.tsx
@@ -1,6 +1,7 @@
import * as React from "react";
import { CurrentUser, UserSettings } from "@fider/models";
+import { Toggle } from "@fider/components";
interface NotificationSettingsProps {
user: CurrentUser;
@@ -41,16 +42,15 @@ export class NotificationSettings extends React.Component<NotificationSettingsPr
}
private icon(settingsKey: string, channel: Channel) {
- const i = this.isEnabled(settingsKey, channel) ? (
- <i className="green large toggle on icon" />
- ) : (
- <i className="large toggle off icon" />
- );
- const text = channel === WebChannel ? "Web *" : "Email";
+ const active = this.isEnabled(settingsKey, channel);
+ const label = channel === WebChannel ? "Web" : "Email";
return (
- <span className="option" onClick={this.toggle.bind(this, settingsKey, channel)}>
- {i} {text}
- </span>
+ <Toggle
+ key={`${settingsKey}_${channel}`}
+ active={active}
+ label={label}
+ onToggle={this.toggle.bind(this, settingsKey, channel)}
+ />
);
}