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 ability to style states of disabled toggle #196

Closed
wants to merge 2 commits into from
Closed
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
13 changes: 8 additions & 5 deletions src/form/Toggle/Toggle.story.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Toggle } from './Toggle';
import React, { useState } from 'react';
import { Toggle } from './Toggle';

export default {
title: 'Components/Form/Toggle',
Expand All @@ -11,10 +11,13 @@ export const Simple = () => {
return <Toggle checked={state} onChange={setState} />;
};

export const Disabled = () => {
const [state, setState] = useState(true);
return <Toggle disabled checked={state} onChange={setState} />;
};
export const Disabled = () => (
<>
<Toggle disabled checked={false} />
<br />
<Toggle disabled checked />
</>
);

export const Sizes = () => {
const [stateSmall, setStateSmall] = useState(true);
Expand Down
7 changes: 6 additions & 1 deletion src/form/Toggle/Toggle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,12 @@ export const Toggle: FC<ToggleProps & ToggleRef> = forwardRef<
}}
>
<motion.div
className={twMerge(theme.handle.base, theme.handle.sizes[size])}
className={twMerge(
theme.handle.base,
theme.handle.sizes[size],
disabled && theme.handle.disabled,
checked && theme.handle.checked
)}
layout
transition={{
type: 'spring',
Expand Down
8 changes: 7 additions & 1 deletion src/form/Toggle/ToggleTheme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ export interface ToggleTheme {
checked: string;
handle: {
base: string;
disabled: string;
checked: string;
sizes: {
small: string;
medium: string;
Expand All @@ -20,9 +22,11 @@ export interface ToggleTheme {
const baseTheme: ToggleTheme = {
base: 'flex items-center justify-start cursor-pointer bg-surface box-border border border-panel-accent rounded-full',
disabled: 'cursor-not-allowed opacity-60',
checked: 'justify-end bg-primary',
checked: 'checked justify-end bg-primary',
handle: {
base: 'rounded-full bg-panel',
disabled: '',
checked: 'checked',
sizes: {
small: 'w-3 h-full',
medium: 'w-5 h-full',
Expand Down Expand Up @@ -66,6 +70,8 @@ export const legacyToggleTheme: ToggleTheme = {
baseTheme.handle.base,
'bg-[var(--toggle-handle-background)] rounded-[var(--toggle-handle-border-radius)]'
].join(' '),
disabled: '',
checked: '',
sizes: {
small:
'h-[calc(var(--toggle-handle-size,25px)_/_2)] w-[calc(var(--toggle-handle-size,25px)_/_2)]',
Expand Down