>, newValue: number) => {
- setValue(newValue)
+ if (indexChanged) indexChanged(newValue)
+ else setValue(newValue)
}
+ const v = index !== undefined ? index : value
+
return (
-
- {values.map(({ label }, index) => (
-
+
+ {values.map(({ label }, idx) => (
+
))}
- {values.map(({ component }, index) => (
-
+ {values.map(({ component }, idx) => (
+
{component}
))}
diff --git a/src/providers/Platform.tsx b/src/providers/Platform.tsx
new file mode 100644
index 0000000..bad6d39
--- /dev/null
+++ b/src/providers/Platform.tsx
@@ -0,0 +1,69 @@
+import { createContext, ReactChild, ReactElement, useState, useEffect } from 'react'
+
+// These need to be numeric values as they are used as indexes in the TabsContainer
+export enum Platforms {
+ macOS = 0,
+ Linux,
+ Windows,
+ iOS,
+ Android,
+}
+
+export enum SupportedPlatforms {
+ macOS = Platforms.macOS,
+ Linux = Platforms.Windows,
+}
+
+interface ContextInterface {
+ platform: SupportedPlatforms
+ setPlatform: (platform: SupportedPlatforms) => void
+}
+
+const initialValues: ContextInterface = {
+ platform: SupportedPlatforms.macOS,
+ setPlatform: () => {}, // eslint-disable-line
+}
+
+export const Context = createContext(initialValues)
+export const Consumer = Context.Consumer
+
+interface Props {
+ children: ReactChild
+}
+
+function isSupportedPlatform(platform: unknown): platform is SupportedPlatforms {
+ return Object.keys(SupportedPlatforms).includes(platform as string)
+}
+
+function getOS(): Platforms | null {
+ 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 Platforms.macOS
+
+ if (iosPlatforms.includes(platform)) return Platforms.iOS
+
+ if (windowsPlatforms.includes(platform)) return Platforms.Windows
+
+ if (/Android/.test(userAgent)) return Platforms.Android
+
+ if (/Linux/.test(platform)) return Platforms.Linux
+
+ return null
+}
+
+export function Provider({ children }: Props): ReactElement {
+ const [platform, setPlatform] = useState(SupportedPlatforms.Linux)
+
+ // This is in useEffect as it really just needs to run once and not on each re-render
+ useEffect(() => {
+ const os = getOS()
+
+ setPlatform(isSupportedPlatform(os) ? os : SupportedPlatforms.Linux)
+ }, [])
+
+ return {children}
+}