From e35201882342d6808ad29b58e8aff3fd7332b4f1 Mon Sep 17 00:00:00 2001 From: tomaszantas Date: Fri, 22 Apr 2022 15:40:18 +0200 Subject: [PATCH] Improve Switch component --- .../src/components/Inputs/Switch/types.ts | 7 ---- .../{Inputs => }/Switch/Switch.test.tsx | 4 +- .../components/{Inputs => }/Switch/index.tsx | 38 ++++++++++++++++--- .../components/{Inputs => }/Switch/styles.ts | 10 +++-- .../src/components/Switch/types.ts | 10 +++++ .../src/components/TitleBar/index.tsx | 6 +-- .../src/containers/MiningContainer/index.tsx | 19 +++++++++- applications/launchpad_v2/yarn.lock | 5 +++ 8 files changed, 77 insertions(+), 22 deletions(-) delete mode 100644 applications/launchpad_v2/src/components/Inputs/Switch/types.ts rename applications/launchpad_v2/src/components/{Inputs => }/Switch/Switch.test.tsx (84%) rename applications/launchpad_v2/src/components/{Inputs => }/Switch/index.tsx (62%) rename applications/launchpad_v2/src/components/{Inputs => }/Switch/styles.ts (85%) create mode 100644 applications/launchpad_v2/src/components/Switch/types.ts diff --git a/applications/launchpad_v2/src/components/Inputs/Switch/types.ts b/applications/launchpad_v2/src/components/Inputs/Switch/types.ts deleted file mode 100644 index d7ca7aeb355..00000000000 --- a/applications/launchpad_v2/src/components/Inputs/Switch/types.ts +++ /dev/null @@ -1,7 +0,0 @@ -export interface SwitchProps { - value: boolean - label?: string - onClick: (val: boolean) => void - invertedStyle?: boolean - testId?: string -} diff --git a/applications/launchpad_v2/src/components/Inputs/Switch/Switch.test.tsx b/applications/launchpad_v2/src/components/Switch/Switch.test.tsx similarity index 84% rename from applications/launchpad_v2/src/components/Inputs/Switch/Switch.test.tsx rename to applications/launchpad_v2/src/components/Switch/Switch.test.tsx index 07066c6d232..1a6ae879071 100644 --- a/applications/launchpad_v2/src/components/Inputs/Switch/Switch.test.tsx +++ b/applications/launchpad_v2/src/components/Switch/Switch.test.tsx @@ -2,7 +2,7 @@ import { fireEvent, render, screen } from '@testing-library/react' import { ThemeProvider } from 'styled-components' import Switch from '.' -import themes from '../../../styles/themes' +import themes from '../../styles/themes' describe('Switch', () => { it('should render without crash', () => { @@ -11,7 +11,7 @@ describe('Switch', () => { const val = false render( - + , ) diff --git a/applications/launchpad_v2/src/components/Inputs/Switch/index.tsx b/applications/launchpad_v2/src/components/Switch/index.tsx similarity index 62% rename from applications/launchpad_v2/src/components/Inputs/Switch/index.tsx rename to applications/launchpad_v2/src/components/Switch/index.tsx index c9e336b1e49..06d01a1b93a 100644 --- a/applications/launchpad_v2/src/components/Inputs/Switch/index.tsx +++ b/applications/launchpad_v2/src/components/Switch/index.tsx @@ -2,6 +2,8 @@ import { useContext } from 'react' import { useSpring } from 'react-spring' import { ThemeContext } from 'styled-components' +import Text from '../Text' + import { LabelText, SwitchContainer, @@ -16,14 +18,15 @@ import { SwitchProps } from './types' */ const Switch = ({ value, - label, + leftLabel, + rightLabel, onClick, - invertedStyle, + inverted, testId, }: SwitchProps) => { const theme = useContext(ThemeContext) - const themeStyle = invertedStyle ? theme.inverted : theme + const themeStyle = inverted ? theme.inverted : theme const circleAnim = useSpring({ left: value ? 10 : -1, @@ -37,22 +40,47 @@ const Switch = ({ background: value ? themeStyle.accent : 'transparent', }) + const leftLabelEl = + leftLabel && typeof leftLabel === 'string' ? ( + {leftLabel} + ) : ( + leftLabel + ) + const rightLabelEl = + rightLabel && typeof rightLabel === 'string' ? ( + {rightLabel} + ) : ( + rightLabel + ) + return ( onClick && onClick(!value)} data-testid={testId || 'switch-input-cmp'} > + {leftLabelEl ? ( + + {leftLabelEl} + + ) : null} + - {label ? ( + {rightLabelEl ? ( - {label} + {rightLabelEl} ) : null} diff --git a/applications/launchpad_v2/src/components/Inputs/Switch/styles.ts b/applications/launchpad_v2/src/components/Switch/styles.ts similarity index 85% rename from applications/launchpad_v2/src/components/Inputs/Switch/styles.ts rename to applications/launchpad_v2/src/components/Switch/styles.ts index 5d845fc0277..e2ebe8f3025 100644 --- a/applications/launchpad_v2/src/components/Inputs/Switch/styles.ts +++ b/applications/launchpad_v2/src/components/Switch/styles.ts @@ -1,6 +1,6 @@ import { animated } from 'react-spring' import styled from 'styled-components' -import colors from '../../../styles/styles/colors' +import colors from '../../styles/styles/colors' export const SwitchContainer = styled.label` display: flex; @@ -12,7 +12,6 @@ export const SwitchController = styled(animated.div)` width: 24px; border: 1.5px solid ${colors.dark.primary}; border-radius: 6px; - margin-right: 12px; position: relative; box-sizing: border-box; box-shadow: 0px 0px 2px rgba(0, 0, 0, 0.08); @@ -22,8 +21,8 @@ export const SwitchCircle = styled(animated.div)` position: absolute; width: 14px; height: 14px; - top: -1.5px; - bottom: 0; + top: 0; + margin-top: -1px; border-radius: 6px; box-sizing: border-box; background: #fff; @@ -34,4 +33,7 @@ export const LabelText = styled(animated.span)` font-weight: 500; font-size: 14px; line-height: 160%; + display: flex; + align-items: center; + margin: 0; ` diff --git a/applications/launchpad_v2/src/components/Switch/types.ts b/applications/launchpad_v2/src/components/Switch/types.ts new file mode 100644 index 00000000000..babed3dfc08 --- /dev/null +++ b/applications/launchpad_v2/src/components/Switch/types.ts @@ -0,0 +1,10 @@ +import { ReactNode } from 'react' + +export interface SwitchProps { + value: boolean + leftLabel?: string | ReactNode + rightLabel?: string | ReactNode + onClick: (val: boolean) => void + inverted?: boolean + testId?: string +} diff --git a/applications/launchpad_v2/src/components/TitleBar/index.tsx b/applications/launchpad_v2/src/components/TitleBar/index.tsx index 2820fbeaf44..fcc1e1a5d7c 100644 --- a/applications/launchpad_v2/src/components/TitleBar/index.tsx +++ b/applications/launchpad_v2/src/components/TitleBar/index.tsx @@ -6,7 +6,7 @@ import { appWindow } from '@tauri-apps/api/window' import Button from '../Button' import Logo from '../Logo' -import Switch from '../Inputs/Switch' +import Switch from '../Switch' import { selectExpertView } from '../../store/app/selectors' import { setExpertView } from '../../store/app' @@ -196,10 +196,10 @@ const TitleBar = ({ drawerViewWidth = '50%' }: TitleBarProps) => { diff --git a/applications/launchpad_v2/src/containers/MiningContainer/index.tsx b/applications/launchpad_v2/src/containers/MiningContainer/index.tsx index d325bb38a3d..080a092e22f 100644 --- a/applications/launchpad_v2/src/containers/MiningContainer/index.tsx +++ b/applications/launchpad_v2/src/containers/MiningContainer/index.tsx @@ -1,6 +1,12 @@ -import { useDispatch } from 'react-redux' +import { useDispatch, useSelector } from 'react-redux' +import Switch from '../../components/Switch' + +import Text from '../../components/Text' +import SvgSun from '../../styles/Icons/Sun' +import SvgMoon from '../../styles/Icons/Moon' import { setTheme } from '../../store/app' +import { selectTheme } from '../../store/app/selectors' /** * @TODO move user-facing text to i18n file when implementing @@ -8,6 +14,8 @@ import { setTheme } from '../../store/app' const MiningContainer = () => { const dispatch = useDispatch() + const currentTheme = useSelector(selectTheme) + return (

Mining

@@ -15,6 +23,15 @@ const MiningContainer = () => { Set light theme +
+ Select theme + } + rightLabel={'Some text asd'} + value={currentTheme === 'dark'} + onClick={v => dispatch(setTheme(v ? 'dark' : 'light'))} + /> +
) } diff --git a/applications/launchpad_v2/yarn.lock b/applications/launchpad_v2/yarn.lock index 632493c0a20..3e7fa94661a 100644 --- a/applications/launchpad_v2/yarn.lock +++ b/applications/launchpad_v2/yarn.lock @@ -1163,6 +1163,11 @@ minimatch "^3.0.4" strip-json-comments "^3.1.1" +"@headlessui/react@^1.5.0": + version "1.5.0" + resolved "https://registry.yarnpkg.com/@headlessui/react/-/react-1.5.0.tgz#483b44ba2c8b8d4391e1d2c863898d7dd0cc0296" + integrity sha512-aaRnYxBb3MU2FNJf3Ut9RMTUqqU3as0aI1lQhgo2n9Fa67wRu14iOGqx93xB+uMNVfNwZ5B3y/Ndm7qZGuFeMQ== + "@humanwhocodes/config-array@^0.9.2": version "0.9.5" resolved "https://registry.yarnpkg.com/@humanwhocodes/config-array/-/config-array-0.9.5.tgz#2cbaf9a89460da24b5ca6531b8bbfc23e1df50c7"