Skip to content

Commit

Permalink
Add toggle switch and toggle button options components
Browse files Browse the repository at this point in the history
* **ToggleSwitch.module.css**: Add styles for the toggle switch component
* **ToggleSwitch.tsx**: Add a new toggle switch component and use the styles from `ToggleSwitch.module.css`
* **ToggleButtonOptions.module.css**: Add styles for the toggle button options component
* **ToggleButtonOptions.tsx**: Add a new toggle button options component and use the styles from `ToggleButtonOptions.module.css`
* **index.tsx**: Import and use the new toggle switch and toggle button options components
  • Loading branch information
SkywardSyntax committed Aug 21, 2024
1 parent 3d0a8fd commit 0a2d189
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 0 deletions.
15 changes: 15 additions & 0 deletions components/ToggleButtonOptions.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.toggleButtonOptions {
display: flex;
flex-direction: column;
gap: 10px;
}

.toggleButton {
display: flex;
align-items: center;
gap: 10px;
}

.toggleButton input {
margin-right: 10px;
}
34 changes: 34 additions & 0 deletions components/ToggleButtonOptions.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { FC, ChangeEvent } from 'react';
import styles from './ToggleButtonOptions.module.css';

interface ToggleButtonOption {
label: string;
value: string;
}

interface ToggleButtonOptionsProps {
options: ToggleButtonOption[];
selectedValue: string;
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
}

const ToggleButtonOptions: FC<ToggleButtonOptionsProps> = ({ options, selectedValue, onChange }) => {
return (
<div className={styles.toggleButtonOptions}>
{options.map((option) => (
<label key={option.value} className={styles.toggleButton}>
<input
type="radio"
name="toggleButtonOption"
value={option.value}
checked={selectedValue === option.value}
onChange={onChange}
/>
{option.label}
</label>
))}
</div>
);
};

export default ToggleButtonOptions;
44 changes: 44 additions & 0 deletions components/ToggleSwitch.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
.toggleSwitch {
position: relative;
display: inline-block;
width: 60px;
height: 34px;
}

.toggleSwitch input {
opacity: 0;
width: 0;
height: 0;
}

.slider {
position: absolute;
cursor: pointer;
top: 0;
left: 0;
right: 0;
bottom: 0;
background-color: #ccc;
transition: 0.4s;
border-radius: 34px;
}

.slider:before {
position: absolute;
content: "";
height: 26px;
width: 26px;
left: 4px;
bottom: 4px;
background-color: white;
transition: 0.4s;
border-radius: 50%;
}

input:checked + .slider {
background-color: #2196F3;
}

input:checked + .slider:before {
transform: translateX(26px);
}
18 changes: 18 additions & 0 deletions components/ToggleSwitch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { FC, ChangeEvent } from 'react';
import styles from './ToggleSwitch.module.css';

interface ToggleSwitchProps {
checked: boolean;
onChange: (event: ChangeEvent<HTMLInputElement>) => void;
}

const ToggleSwitch: FC<ToggleSwitchProps> = ({ checked, onChange }) => {
return (
<label className={styles.toggleSwitch}>
<input type="checkbox" checked={checked} onChange={onChange} />
<span className={styles.slider}></span>
</label>
);
};

export default ToggleSwitch;
36 changes: 36 additions & 0 deletions pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useCallback, useEffect, useState, FC } from 'react'
import Button from '../components/Button'
import ClickCount from '../components/ClickCount'
import GlassChip from '../components/GlassChip'
import ToggleSwitch from '../components/ToggleSwitch'
import ToggleButtonOptions from '../components/ToggleButtonOptions'
import styles from '../styles/home.module.css'
import * as THREE from 'three';

Expand Down Expand Up @@ -100,6 +102,22 @@ const Home: FC = () => {
};
}, []);

const [toggleSwitchChecked, setToggleSwitchChecked] = useState(false);
const handleToggleSwitchChange = (event: ChangeEvent<HTMLInputElement>) => {
setToggleSwitchChecked(event.target.checked);
};

const [selectedToggleButtonOption, setSelectedToggleButtonOption] = useState('option1');
const handleToggleButtonOptionChange = (event: ChangeEvent<HTMLInputElement>) => {
setSelectedToggleButtonOption(event.target.value);
};

const toggleButtonOptions = [
{ label: 'Option 1', value: 'option1' },
{ label: 'Option 2', value: 'option2' },
{ label: 'Option 3', value: 'option3' },
];

return (
<main className={styles.main}>
<h1>Fast Refresh Demo</h1>
Expand Down Expand Up @@ -144,6 +162,24 @@ const Home: FC = () => {
</div>
</GlassChip>
<hr className={styles.hr} />
<GlassChip>
<div>
<p>Toggle Switch:</p>
<ToggleSwitch checked={toggleSwitchChecked} onChange={handleToggleSwitchChange} />
</div>
</GlassChip>
<hr className={styles.hr} />
<GlassChip>
<div>
<p>Toggle Button Options:</p>
<ToggleButtonOptions
options={toggleButtonOptions}
selectedValue={selectedToggleButtonOption}
onChange={handleToggleButtonOptionChange}
/>
</div>
</GlassChip>
<hr className={styles.hr} />
</main>
)
}
Expand Down

0 comments on commit 0a2d189

Please sign in to comment.