Skip to content

Commit

Permalink
💄 front: adding slider like checkbox (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericlinagora committed Apr 21, 2024
1 parent 106b916 commit 339d4d7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tdrive/frontend/src/app/atoms/input/input-checkbox-slider.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
interface CheckboxSliderProps extends React.InputHTMLAttributes<HTMLInputElement> {
checked?: boolean;
disabled?: boolean;
}
/** Just classes up a checkbox to look like a slider. Deviates from onChange etc for
* compatibility with `InputHTMLAttributes<HTMLInputElement>`, to expose eg. id to use with htmlFor.
*/
export const CheckboxSlider = (props: CheckboxSliderProps) =>
<input
{...props}
type="checkbox"
role="switch"
// @author https://tw-elements.com/docs/react/forms/switch/
className="!bg-none mr-2 mt-[0.3rem] h-7 w-14 appearance-none rounded-[0.9rem] bg-neutral-300 before:pointer-events-none before:absolute before:h-7 before:w-5 before:rounded-full before:bg-transparent before:content-[''] after:absolute after:z-[2] after:mt-[0.2rem] after:ml-[0.2rem] after:h-5 after:w-5 after:rounded-full after:border-none after:bg-neutral-100 after:shadow-[0_0px_3px_0_rgb(0_0_0_/_7%),_0_2px_2px_0_rgb(0_0_0_/_4%)] after:transition-[background-color_0.2s,transform_0.2s] after:content-[''] checked:bg-primary checked:after:absolute checked:after:z-[2] checked:after:ml-[1.9625rem] checked:after:w-5 checked:after:rounded-full checked:after:border-none checked:after:bg-primary checked:after:shadow-[0_3px_1px_-2px_rgba(0,0,0,0.2),_0_2px_2px_0_rgba(0,0,0,0.14),_0_1px_5px_0_rgba(0,0,0,0.12)] checked:after:transition-[background-color_0.2s,transform_0.2s] checked:after:content-[''] hover:cursor-pointer dark:bg-neutral-600 dark:after:bg-white dark:checked:bg-primary dark:checked:after:bg-primary disabled:cursor-default disabled:opacity-25 !focus:shadow-none"
checked={!!props.checked}
disabled={props.disabled}
/>;
10 changes: 10 additions & 0 deletions tdrive/frontend/src/app/atoms/input/stories/checkbox.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import { ComponentStory } from '@storybook/react';
import { useState } from 'react';
import { Checkbox } from '../input-checkbox';
import { CheckboxSlider } from '../input-checkbox-slider';
import { Title } from 'app/atoms/text';

export default {
title: '@atoms/checkbox',
Expand Down Expand Up @@ -30,6 +32,14 @@ const Template: ComponentStory<any> = (props: { label: string; disabled: boolean
disabled={props.disabled}
label={props.label}
/>

<Title className='my-5'>CheckboxSlider</Title>

<CheckboxSlider
onClick={e => setChecked(!checked)}
checked={checked}
disabled={props.disabled}
/>
</div>
);
};
Expand Down

0 comments on commit 339d4d7

Please sign in to comment.