From ec42eafc2b768ba06649f628c733e8d3440fdcaf Mon Sep 17 00:00:00 2001 From: Vojtech Simetka Date: Wed, 11 Aug 2021 19:46:09 +0200 Subject: [PATCH] feat: synchronized platform tabs (#165) * feat: synchronized platform tabs * chore: rename enums to pascal case * chore: fixed typo --- src/App.tsx | 19 ++-- src/components/CodeBlockTabs.tsx | 152 ++++--------------------------- src/components/TabsContainer.tsx | 29 +++--- src/providers/Platform.tsx | 69 ++++++++++++++ 4 files changed, 112 insertions(+), 157 deletions(-) create mode 100644 src/providers/Platform.tsx diff --git a/src/App.tsx b/src/App.tsx index 1f7fe87..7016cf4 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -9,6 +9,7 @@ import { SnackbarProvider } from 'notistack' import BaseRouter from './routes/routes' import { lightTheme, darkTheme } from './theme' import { Provider as StampsProvider } from './providers/Stamps' +import { Provider as PlatformProvider } from './providers/Platform' const App = (): ReactElement => { const [themeMode, toggleThemeMode] = useState('light') @@ -36,14 +37,16 @@ const App = (): ReactElement => {
- - <> - - - - - - + + + <> + + + + + + +
diff --git a/src/components/CodeBlockTabs.tsx b/src/components/CodeBlockTabs.tsx index f6010d9..583768d 100644 --- a/src/components/CodeBlockTabs.tsx +++ b/src/components/CodeBlockTabs.tsx @@ -1,13 +1,7 @@ -import React, { ReactElement, useEffect } from 'react' -import { withStyles, Theme, createStyles } from '@material-ui/core/styles' -import { Tabs, Tab, Box, Typography } from '@material-ui/core' +import { ReactElement, useContext } from 'react' +import TabsContainer from './TabsContainer' import CodeBlock from './CodeBlock' - -interface TabPanelProps { - children?: React.ReactNode - index: number - value: number -} +import { Context } from '../providers/Platform' interface Props { linux: string @@ -15,133 +9,23 @@ interface Props { showLineNumbers?: boolean } -function a11yProps(index: number) { - return { - id: `simple-tab-${index}`, - 'aria-controls': `simple-tabpanel-${index}`, - } -} - -function getOS() { - const userAgent = window.navigator.userAgent - const platform = window.navigator.platform - const macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'] - const windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'] - const iosPlatforms = ['iPhone', 'iPad', 'iPod'] - - if (macosPlatforms.includes(platform)) return 'macOS' - - if (iosPlatforms.includes(platform)) return 'iOS' - - if (windowsPlatforms.includes(platform)) return 'windows' - - if (/Android/.test(userAgent)) return 'android' - - if (/Linux/.test(platform)) return 'linux' - - return null -} - export default function CodeBlockTabs(props: Props): ReactElement { - const [value, setValue] = React.useState(0) - - const handleChange = (event: React.ChangeEvent, newValue: number) => { - setValue(newValue) - } - - useEffect(() => { - const os = getOS() - - if (os === 'windows') { - setValue(0) - } else if (os === 'linux') { - setValue(0) - } else if (os === 'macOS') { - setValue(1) - } - }, []) - - function TabPanel(props: TabPanelProps) { - const { children, value, index, ...other } = props - - return ( - - ) - } - - const AntTabs = withStyles({ - root: { - borderBottom: '1px solid #e8e8e8', - }, - indicator: { - backgroundColor: '#3f51b5', - }, - })(Tabs) - - interface StyledTabProps { - label: string - } - - const AntTab = withStyles((theme: Theme) => - createStyles({ - root: { - textTransform: 'none', - minWidth: 72, - backgroundColor: 'transparent', - fontWeight: theme.typography.fontWeightRegular, - marginRight: theme.spacing(4), - fontFamily: [ - '-apple-system', - 'BlinkMacSystemFont', - '"Segoe UI"', - 'Roboto', - '"Helvetica Neue"', - 'Arial', - 'sans-serif', - '"Apple Color Emoji"', - '"Segoe UI Emoji"', - '"Segoe UI Symbol"', - ].join(','), - '&:hover': { - color: '#3f51b5', - opacity: 1, - }, - '&$selected': { - color: '#3f51b5', - fontWeight: theme.typography.fontWeightMedium, - }, - '&:focus': { - color: '#3f51b5', - }, - }, - selected: {}, - }), - )((props: StyledTabProps) => ) + const { platform, setPlatform } = useContext(Context) return ( -
- - - - - - - - - - -
+ , + }, + { + label: 'macOS', + component: , + }, + ]} + /> ) } diff --git a/src/components/TabsContainer.tsx b/src/components/TabsContainer.tsx index 0100f1e..5ed873c 100644 --- a/src/components/TabsContainer.tsx +++ b/src/components/TabsContainer.tsx @@ -15,13 +15,7 @@ function TabPanel(props: TabPanelProps) { const { children, value, index, ...other } = props return ( -