Skip to content

Commit

Permalink
feat: switch button (#229)
Browse files Browse the repository at this point in the history
* fix: font weight added

* fix: change color hex

* fix: change color again

* fix: text-ut-burntorange

* fix: importance (tailwind wise)

* feat: switch button initial test

* feat: look at how this switch goes back and forth very mindful very demure

* fix: story

* feat: using type now

* chore: fix lint

* feat: button custom function prop

* fix: styling

* chore: fix lint error and add JSDoc

---------

Co-authored-by: doprz <52579214+doprz@users.noreply.github.com>
  • Loading branch information
DereC4 and doprz authored Oct 1, 2024
1 parent 149fda3 commit abae7a5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/stories/components/ToggleSwitch.stories.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import type { Meta, StoryObj } from '@storybook/react';
import SwitchButton from '@views/components/common/SwitchButton';

const meta = {
title: 'Components/Common/SwitchButton',
component: SwitchButton,
tags: ['autodocs'],
parameters: {
layout: 'centered',
},
} satisfies Meta<typeof SwitchButton>;

export default meta;
type Story = StoryObj<typeof meta>;

export const Default: Story = {
args: {
isChecked: true,
},
};
47 changes: 47 additions & 0 deletions src/views/components/common/SwitchButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Switch } from '@headlessui/react';
import React, { useEffect, useState } from 'react';

type ToggleSwitchProps = {
isChecked?: boolean;
onChange?: (checked: boolean) => void;
};

/**
* A custom switch button component.
*
* @component
* @param {Object} props - The component props.
* @param {boolean} [props.isChecked=true] - The initial checked state of the switch button.
* @param {Function} props.onChange - The callback function to be called when the switch button is toggled.
* @returns {JSX.Element} The rendered SwitchButton component.
*/
const SwitchButton = ({ isChecked = true, onChange }: ToggleSwitchProps): JSX.Element => {
const [enabled, setEnabled] = useState(isChecked);

useEffect(() => {
setEnabled(isChecked);
}, [isChecked]);

const handleChange = (checked: boolean) => {
setEnabled(checked);
if (onChange) {
onChange(checked);
}
};

return (
<Switch
checked={enabled}
onChange={handleChange}
className={`${enabled ? 'bg-[#579D42]' : 'bg-gray-400'}
relative inline-flex items-center h-8 w-13 rounded-full transition-colors ease-in-out duration-200`}
>
<span
className={`${enabled ? 'translate-x-6' : 'translate-x-1'}
inline-block w-6 h-6 transform bg-white rounded-full transition-transform ease-in-out duration-200`}
/>
</Switch>
);
};

export default SwitchButton;

0 comments on commit abae7a5

Please sign in to comment.