Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(ui): use react-media-hook instead of react-media #1876

Merged
merged 1 commit into from
Jun 22, 2020
Merged
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
27 changes: 4 additions & 23 deletions ui/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 ui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"react-js-pagination": "3.0.3",
"react-json-pretty": "2.2.0",
"react-linkify": "0.2.2",
"react-media": "1.10.0",
"react-media-hook": "0.4.7",
"react-popper": "2.2.3",
"react-resize-detector": "5.0.6",
"react-reveal": "1.2.2",
Expand Down
90 changes: 41 additions & 49 deletions ui/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import React, {

import { useObserver } from "mobx-react-lite";

import Media from "react-media";
// no types, see react-app-env.d.ts
import { useMediaPredicate } from "react-media-hook";

import { AlertStore, DecodeLocationSearch } from "Stores/AlertStore";
import { Settings } from "Stores/Settings";
Expand Down Expand Up @@ -100,59 +101,50 @@ const App: FunctionComponent<AppProps> = ({ defaultFilters, uiDefaults }) => {
};
}, [onPopState]);

const prefersColorScheme = useMediaPredicate("(prefers-color-scheme)");
const prefersDark = useMediaPredicate("(prefers-color-scheme: dark)");

return useObserver(() => (
<ErrorBoundary>
<span data-theme={`${settingsStore.themeConfig.config.theme}`} />
<Media
queries={{
isSupported: "(prefers-color-scheme)",
light: "(prefers-color-scheme: light)",
dark: "(prefers-color-scheme: dark)",
<ThemeContext.Provider
value={{
isDark:
settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.auto.value && prefersColorScheme
? prefersDark
: settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.dark.value,
reactSelectStyles:
settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.auto.value && prefersColorScheme
? prefersDark
? ReactSelectStyles(ReactSelectColors.Dark)
: ReactSelectStyles(ReactSelectColors.Light)
: settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.dark.value
? ReactSelectStyles(ReactSelectColors.Dark)
: ReactSelectStyles(ReactSelectColors.Light),
animations: {
enabled: true,
duration: 1000,
},
}}
>
{(matches) => (
<ThemeContext.Provider
value={{
isDark:
settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.auto.value &&
matches.isSupported
? matches.dark
: settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.dark.value,
reactSelectStyles:
settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.auto.value &&
matches.isSupported
? matches.dark
? ReactSelectStyles(ReactSelectColors.Dark)
: ReactSelectStyles(ReactSelectColors.Light)
: settingsStore.themeConfig.config.theme ===
settingsStore.themeConfig.options.dark.value
? ReactSelectStyles(ReactSelectColors.Dark)
: ReactSelectStyles(ReactSelectColors.Light),
animations: {
enabled: true,
duration: 1000,
},
}}
>
<BodyTheme />
<React.Suspense fallback={null}>
<NavBar
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
/>
<Grid
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
/>
</React.Suspense>
</ThemeContext.Provider>
)}
</Media>
<BodyTheme />
<React.Suspense fallback={null}>
<NavBar
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
/>
<Grid
alertStore={alertStore}
settingsStore={settingsStore}
silenceFormStore={silenceFormStore}
/>
</React.Suspense>
</ThemeContext.Provider>
<React.Suspense fallback={null}>
<FaviconBadge alertStore={alertStore} />
<Fetcher alertStore={alertStore} settingsStore={settingsStore} />
Expand Down
4 changes: 4 additions & 0 deletions ui/src/react-app-env.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
/// <reference types="react-scripts" />

declare module "react-media-hook" {
function useMediaPredicate(query: string): boolean;
}