Skip to content

Commit

Permalink
feat: ⚡ add new Switch component to ui-kit
Browse files Browse the repository at this point in the history
  • Loading branch information
VoloshchenkoAl committed Jun 10, 2022
1 parent b17f46a commit 63d59a0
Show file tree
Hide file tree
Showing 8 changed files with 159 additions and 7 deletions.
5 changes: 3 additions & 2 deletions internal/Navigation/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import Link from 'next/link'
import Link from 'next/link';
import styles from './Navigation.module.css';

const pages = [
Expand All @@ -23,12 +23,13 @@ const pages = [
'payment',
'radio',
'select',
'switch',
'tag',
'tag-input',
'table',
'tooltip',
'typography',
]
];

const Navigation = () => (
<div className={styles.navigation}>
Expand Down
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@macpaw/macpaw-ui",
"version": "4.0.0",
"version": "4.1.0",
"main": "lib/ui.js",
"scripts": {
"dev": "next -p 1234",
Expand Down
55 changes: 55 additions & 0 deletions pages/switch.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import { Switch, FormRow } from '../src/ui';

# Switch

<FormRow variety>
<h3>Default</h3>
<Switch />
</FormRow>

<FormRow variety>
<h3>Disabled</h3>
<Switch disabled />
</FormRow>

<FormRow variety>
<h3>Error</h3>
<Switch error>Some label</Switch>
</FormRow>

<FormRow variety>
<h3>With handler</h3>
<Switch onChange={() => alert('Value is change')} />
</FormRow>

<FormRow variety>
<h3>With Label</h3>
<Switch>change settings</Switch>
</FormRow>

```
<FormRow variety>
<h3>Default</h3>
<Switch />
</FormRow>
<FormRow variety>
<h3>Disabled</h3>
<Switch disabled />
</FormRow>
<FormRow variety>
<h3>Error</h3>
<Switch error />
</FormRow>
<FormRow variety>
<h3>With handler</h3>
<Switch onChange={() => alert('Value is change')} />
</FormRow>
<FormRow variety>
<h3>With Label</h3>
<Switch>change settings</Switch>
</FormRow>
```
72 changes: 72 additions & 0 deletions src/Switch/Switch.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
.switch {
display: inline-flex;
position: relative;
user-select: none;
align-items: center;

&.-error {
color: var(--color-error);

.switch-track {
box-shadow: 0 0 0 2px var(--color-error);
}
}

input {
position: absolute;
opacity: 0;
z-index: 1;
cursor: pointer;
width: 106%;
height: 106%;
margin: 0;
left: -3%;
top: -3%;

&:checked + .switch-track {
background-color: var(--color-primary);
}

&:checked + .switch-track .switch-thumb {
transform: translateX(16px);
}

&:focus + .switch-track {
border-color: var(--color-secondary);
box-shadow: 0 0 0 3px var(--color-outline);
}

&[disabled] {
cursor: not-allowed;
}

&[disabled] + .switch-track {
background-color: var(--color-black-16);
}
}

.switch-track {
width: 32px;
height: 16px;
border-radius: 10px;
background-color: var(--color-black-16);
position: relative;
display: inline-flex;
transition: 0.1s background-color ease-in-out;

& + * {
margin-left: 8px;
}
}

.switch-thumb {
width: 12px;
height: 12px;
border-radius: 50%;
background-color: var(--color-white);
display: inline-flex;
margin: 2px;
transform: translateX(0);
transition: 0.1s transform ease-in-out;
}
}
22 changes: 22 additions & 0 deletions src/Switch/Switch.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import React, { FC, InputHTMLAttributes, PropsWithChildren } from 'react';
import cx from 'clsx';

export interface SwitchProps extends InputHTMLAttributes<HTMLInputElement> {
error?: boolean;
}

const Switch: FC<PropsWithChildren<SwitchProps>> = (props) => {
const { className, style, children, error, ...other } = props;

return (
<span className={cx('switch', className, error && '-error')} style={style}>
<input type="checkbox" {...other} />
<span className="switch-track">
<span className="switch-thumb" />
</span>
{!!children && <span>{children}</span>}
</span>
);
};

export default Switch;
1 change: 1 addition & 0 deletions src/ui.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ export { default as Table } from './Table/Table';
export { default as Tooltip } from './Tooltip/Tooltip';
export { default as TableRow } from './TableRow/TableRow';
export { default as TagInput, TagInputListItem } from './TagInput/TagInput';
export { default as Switch } from './Switch/Switch';
1 change: 1 addition & 0 deletions src/ui.scss
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,4 @@
@import './Tooltip/Tooltip.scss';
@import './Typography/Typography.scss';
@import './TagInput/TagInput.scss';
@import './Switch/Switch.scss';

0 comments on commit 63d59a0

Please sign in to comment.