Skip to content
This repository was archived by the owner on May 18, 2024. It is now read-only.
Open
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
Original file line number Diff line number Diff line change
@@ -1,48 +1,61 @@
import React from 'react';

import '@groww-tech/mint-css/dist/index.css';

import './toggleSwitch.css';


const ToggleSwitch = (props: Props) => {
const {
switchCircleColor, activeBackgroundColor, width,
inactiveBackgroundColor, onChange, height,
isActive, leftText, rightText, dataTestId
size,
onChange,
isDisabled,
isActive,
leftText,
rightText,
dataTestId
} = props;

const getFontClass = () => {
if(size === "XLarge") return "bodyXLarge";
if(size === "Large") return "bodyLarge";
if(size === "Base") return "bodyBase";
if(size === "Small") return "bodySmallHeavy";
if(size === "XSmall") return "bodySmall";

}
// circleDiameter is the diameter of the circular slider which should be smaller than the size of the parent component so as to provide offset between the slider and it's parent
const circleDiameter = height - 4;
const circleDiameter = 20;

const switchLabelStyle = {
background: isActive ? activeBackgroundColor : inactiveBackgroundColor,
width,
height,
borderRadius: height,
borderRadius: '24px',
top: '0px', // required here, as somewhere in the project(globally) these values are altered
left: '0px' // required here
};
const switchDivStyle = {
width,
height
};

const inputStyle = {
margin: '0px' // required here, as somewhere in the project(globally) these values are altered
};


const switchDivisonStyle = {
pointerEvents: isDisabled ? 'none' : 'unset'
};

const switchButtonStyle = {
background: switchCircleColor,
width: `${circleDiameter}px`,
height: `${circleDiameter}px`,
transform: isActive ? `translateX(${width - circleDiameter - 4}px)` : 'none'
transform: isActive ? 'translateX(16px)' : 'none'
};

return (
<div className="valign-wrapper">
{leftText}
<div style={switchDivStyle}
<div className={getFontClass()}>
{leftText}
</div>
<div
className='sw348reactSwitchDivision'
data-test-id={dataTestId.length ? dataTestId : null}
onClick={(e) => onChange(e)}
style={switchDivisonStyle}
>
<input
style={inputStyle}
Expand All @@ -52,31 +65,30 @@ const ToggleSwitch = (props: Props) => {
id="reactSwitchId"
type="checkbox"
/>
<div
style={switchLabelStyle}
className="sw348reactSwitchLabel"
<div style={switchLabelStyle}
className={isDisabled ? 'backgroundTertiary sw348reactSwitchLabel' : isActive ? 'backgroundPositive sw348reactSwitchLabel' : 'backgroundTertiary sw348reactSwitchLabel'}
>
<div style={switchButtonStyle}
className={'sw348reactSwitchButton borderPrimary'}
className={'sw348reactSwitchButton backgroundAlwaysLight'} //check the 'backgrpundPrimary' property, according to design language
/>
</div>
</div>
{rightText}
<div className={getFontClass()}>
{rightText}
</div>
</div>
);

};


const defaultProps: DefaultProps = {
width: 52,
height: 24,
leftText: '',
rightText: '',
inactiveBackgroundColor: 'var(--gray700)',
switchCircleColor: 'var(--white)',
activeBackgroundColor: 'var(--green500)',
dataTestId: ''
isActive: true,
isDisabled: false,
dataTestId: '',
size: 'Base'
};


Expand All @@ -87,14 +99,12 @@ type RequiredProps = {


type DefaultProps = {
width: number;
height: number;
switchCircleColor: string;
activeBackgroundColor: string;
leftText: React.ReactNode;
rightText: React.ReactNode;
inactiveBackgroundColor: string;
dataTestId: string;
isActive: boolean;
isDisabled: boolean;
size: 'XLarge'| 'Large' | 'Base' | 'Small' | 'XSmall';
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,16 @@
align-items: center;
justify-content: space-between;
cursor: pointer;
height: 1.5rem;
width: 2.5rem;
position: relative;
transition: background-color 0.3s;
transition: background-color 0.3s;
}
.sw348reactSwitchButton{
height: 1.25rem;
width: 1.25rem;
filter: 'drop-shadow(0px 1px 2px rgba(0, 0, 0, 0.15))';
}

.sw348reactSwitchLabel .sw348reactSwitchButton {
content: '';
position: absolute;
Expand All @@ -31,3 +37,6 @@
/*same as width, height*/
transition: all 0.3s;
}


/* 'backgroundTertiary sw348reactSwitchLabel' */
32 changes: 14 additions & 18 deletions packages/ui-toolkit/stories/ToggleSwitch.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,27 +12,31 @@ export default {
}
};


const Template: Story<ToggleSwitchProps> = (args) => {
const [checked, setChecked] = React.useState(false);
const [ isChecked, setIsChecked ] = React.useState(true);


const onChange = () => {
setChecked(!checked);
}
setIsChecked(!isChecked);
};

return (
<div className="valign-wrapper">
<ToggleSwitch {...args} isActive={checked} onChange={onChange} />
<ToggleSwitch {...args}
isActive={isChecked}
onChange={onChange}
/>
</div>
);
}

export const Default = Template.bind({});
Default.args = {
width: 52,
height: 24,
leftText: '',
rightText: '',
inactiveBackgroundColor: 'var(--gray700)',
switchCircleColor: 'var(--white)',
activeBackgroundColor: 'var(--green500)'
isActive: false,
isDisabled: 'true'
}

export const WithText = Template.bind({});
Expand All @@ -44,13 +48,5 @@ WithText.args = {

export const SwitchCircle = Template.bind({});
SwitchCircle.args = {
...Default.args,
switchCircleColor: 'var(--red500)'
}

export const Custom = Template.bind({});
Custom.args = {
...Default.args,
inactiveBackgroundColor: 'var(--red500)',
activeBackgroundColor: 'var(--purple500)'
...Default.args
}
Loading