Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom checkbox with correct styling #41

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/assets/images/checkbox.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 41 additions & 0 deletions src/components/checkbox.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
.custom-checkbox {
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;

label {
pointer-events: none;
}

&:hover {
svg {
.outline {
display: block;
}
}
}

&.disabled {
pointer-events: none;
opacity: 0.4;
}

svg {
margin-right: 6px;

.outline {
display: none;
}

.checkmark {
display: none;
}

&.checked {
.checkmark {
display: block;
}
}
}
}
27 changes: 27 additions & 0 deletions src/components/checkbox.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import React from "react";
import { clsx } from "clsx";

import CheckboxSVG from "../assets/images/checkbox.svg";

import "./checkbox.scss";

interface ICheckboxProps {
checked: boolean;
onChange: (checked: boolean) => void;
label: string;
disabled?: boolean;
}

// Checkbox component that uses custom CheckoboxSVG instead of the default input style.
export const Checkbox = ({ checked, onChange, label, disabled }: ICheckboxProps) => {
const handleClick = () => {
onChange(!checked);
};

return (
<div className={clsx("custom-checkbox", { disabled })} onClick={handleClick}>
<CheckboxSVG className={clsx("checkbox-icon", { checked })} />
<label>{label}</label>
</div>
);
}
21 changes: 7 additions & 14 deletions src/grasp-seasons/components/seasons.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import PauseIcon from "../../assets/images/pause-icon.svg";

import "./seasons.scss";
import { Checkbox } from "../../components/checkbox";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move up on next commit?


const ANIM_SPEED = 0.02;
const DAILY_ROTATION_ANIM_SPEED = 0.0003;
Expand Down Expand Up @@ -133,7 +134,7 @@
const playStopLabel = mainAnimationStarted ? t("~STOP", simLang) : t("~ORBIT_BUTTON", simLang);

// Log helpers
const logCheckboxChange = (event: ChangeEvent<HTMLInputElement>) => {

Check warning on line 137 in src/grasp-seasons/components/seasons.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'logCheckboxChange' is assigned a value but never used

Check warning on line 137 in src/grasp-seasons/components/seasons.tsx

View workflow job for this annotation

GitHub Actions / Build and Run Jest Tests

'logCheckboxChange' is assigned a value but never used

Check warning on line 137 in src/grasp-seasons/components/seasons.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'logCheckboxChange' is assigned a value but never used

Check warning on line 137 in src/grasp-seasons/components/seasons.tsx

View workflow job for this annotation

GitHub Actions / S3 Deploy

'logCheckboxChange' is assigned a value but never used
log(capitalize(event.target.name) + "CheckboxChanged", {
value: event.target.checked
});
Expand Down Expand Up @@ -195,11 +196,6 @@
});
};

const handleSimCheckboxChange = (event: ChangeEvent<HTMLInputElement>) => {
setSimState(prevState => ({ ...prevState, [event.target.name as any as keyof ISimState]: event.target.checked }));
logCheckboxChange(event);
};

const handleLatSliderChange = (event: any, ui: any) => {
setSimState(prevState => ({ ...prevState, lat: ui.value }));
setLatitude(formatLatLongNumber(ui.value));
Expand Down Expand Up @@ -284,15 +280,12 @@
{ playStopLabel }
</button>
</div>
<label>
<input
type="checkbox"
name="dailyRotation"
checked={simState.dailyRotation}
onChange={handleSimCheckboxChange}
/>
{ t("~DAILY_ROTATION", simLang) }
</label>
<Checkbox
checked={simState.dailyRotation}
onChange={checked => setSimState(prevState => ({ ...prevState, dailyRotation: checked }))}
label={ t("~DAILY_ROTATION", simLang) }
disabled={!mainAnimationStarted}
/>
</div>
<div className="tilt-slider">
<label>{ t("~TILT_VIEW", simLang) }</label>
Expand Down
Loading