import React, {useContext, useEffect, useState} from 'react'; import {ThemeContext} from "../contexts/contexts"; import { Accordion as ACCORDION, Button as BUTTON, Card as CARD, Form as FORM, FormField as FORM_FIELD, FormGroup as FORM_GROUP, FormInput as FORM_INPUT, Header as HEADER, Icon as ICON, Loader as LOADER, Menu as MENU, Placeholder as PLACEHOLDER, Popup as POPUP, Progress as PROGRESS, Segment as SEGMENT, Statistic as STATISTIC, StatisticGroup as STATISTIC_GROUP, Tab as TAB, Table as TABLE, TabPane as TAB_PANE, TextArea as TEXTAREA } from "semantic-ui-react"; import {ColorToSemanticHexColor} from "./Common"; import _ from "lodash"; export const darkTheme = 'dark'; export const lightTheme = 'light'; export const defaultTheme = lightTheme; export const systemTheme = 'system'; export const themeSessionKey = 'color-scheme'; export function ThemeWrapper({children, ...props}) { if (!_.isEmpty(props)) { console.log(props); console.error('ThemeWrapper does not support props!'); } // Properties to manipulate elements with theme. // Invert when Semantic supports it. Example: const [i, setI] = useState({}); // Invert style when Semantic does not support it. Example:

This paragraph changes style

const [s, setS] = useState({}); // Invert text when Semantic does not support it. const [t, setT] = useState({}); const [inverted, setInverted] = useState(''); // theme can be one of [darkTheme, lightTheme] const [theme, setTheme] = useState(defaultTheme); // savedTheme can be one of [null, darkTheme, lightTheme, systemTheme] const [savedTheme, setSavedTheme] = useState(localStorage.getItem('color-scheme')); const setDarkTheme = (save = false) => { console.debug('setDarkTheme'); setI({inverted: true}); setS({style: {backgroundColor: '#1B1C1D', color: '#dddddd'}}); setT({style: {color: '#dddddd'}}); setInverted('inverted'); setTheme(darkTheme); document.body.style.background = '#171616'; if (save) { saveTheme(darkTheme); } } const setLightTheme = (save = false) => { console.debug('setLightTheme'); setI({inverted: undefined}); setS({}); setT({}); setInverted(''); setTheme(lightTheme); document.body.style.background = '#FFFFFF'; if (save) { saveTheme(lightTheme); } } const setSystemTheme = () => { console.debug('setSystemTheme'); saveTheme(systemTheme); } const saveTheme = (value) => { setSavedTheme(value); localStorage.setItem(themeSessionKey, value); } const cycleSavedTheme = (e) => { // Cycle: System -> Dark -> Light if (e) { e.preventDefault(); } if (savedTheme === systemTheme || savedTheme == null) { saveTheme(darkTheme); } else if (savedTheme === darkTheme) { saveTheme(lightTheme); } else if (savedTheme === lightTheme) { saveTheme(systemTheme); } else { console.error(`Unknown theme! savedTheme=${savedTheme}`); } applyTheme(); } const matchTheme = () => { // Returns darkTheme if saved theme is dark, lightTheme if saved theme is dark, // darkTheme if system prefers dark, otherwise lightTheme. const colorScheme = localStorage.getItem(themeSessionKey); setSavedTheme(colorScheme); if (colorScheme === darkTheme) { return darkTheme; } else if (colorScheme === lightTheme) { return lightTheme } return window.matchMedia('(prefers-color-scheme: dark)').matches ? darkTheme : lightTheme; } const applyTheme = () => { const match = matchTheme(); if (match === darkTheme) { setDarkTheme(); } else { setLightTheme(); } }; useEffect(() => { applyTheme(); window.matchMedia('(prefers-color-scheme: dark)').addEventListener('change', applyTheme); }, []); const themeValue = { i, // Used for Semantic elements which support "inverted". s, // Used to invert the style some elements. t, // Used to invert text. inverted, // Used to add "invert" to className. theme, savedTheme, setDarkTheme, setLightTheme, setSystemTheme, cycleSavedTheme, }; return {children} } // Simple wrappers for Semantic UI elements to use the current theme. const invertedNull = (props) => { if (props['inverted'] === true) { props['inverted'] = null; } return props; } const defaultGrey = (props, inverted) => { // Some elements look softer when grey, use grey only if another color is not provided. if (inverted) { return {color: 'grey', ...props}; } return props; } export function Button(props) { const {i, inverted} = useContext(ThemeContext); props = defaultGrey({...i, ...props}, inverted); return