mirror of
https://github.com/strapi/strapi.git
synced 2025-12-28 07:33:17 +00:00
load and persist application theme
This commit is contained in:
parent
3a74150d3a
commit
d50bf67bf2
@ -8,11 +8,21 @@ import React, { useState } from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
import { ThemeToggleContext } from '../../contexts';
|
||||
|
||||
const THEME_KEY = 'STRAPI_THEME';
|
||||
|
||||
const getDefaultTheme = () => {
|
||||
const browserTheme = window.matchMedia('(prefers-color-scheme: dark)').matches ? 'dark' : 'light';
|
||||
const persistedTheme = localStorage.getItem(THEME_KEY);
|
||||
|
||||
return persistedTheme || browserTheme;
|
||||
};
|
||||
|
||||
const ThemeToggleProvider = ({ children, themes }) => {
|
||||
const [currentTheme, setCurrentTheme] = useState('light');
|
||||
const [currentTheme, setCurrentTheme] = useState(getDefaultTheme());
|
||||
|
||||
const handleChangeTheme = nextTheme => {
|
||||
setCurrentTheme(nextTheme);
|
||||
localStorage.setItem(THEME_KEY, nextTheme);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
@ -1,9 +1,12 @@
|
||||
import { isEmpty } from 'lodash';
|
||||
// TODO @soupette we need to refactor this file
|
||||
|
||||
import isEmpty from 'lodash/isEmpty';
|
||||
|
||||
const TOKEN_KEY = 'jwtToken';
|
||||
const USER_INFO = 'userInfo';
|
||||
const CURRENT_STEP = 'GUIDED_TOUR_CURRENT_STEP';
|
||||
const COMPLETED_STEPS = 'GUIDED_TOUR_COMPLETED_STEPS';
|
||||
const THEME_KEY = 'STRAPI_THEME'; // Also used in packages/core/admin/admin/src/components/ThemeToggleProvider/index.js
|
||||
|
||||
const parse = JSON.parse;
|
||||
const stringify = JSON.stringify;
|
||||
@ -29,6 +32,7 @@ const auth = {
|
||||
const localeLang = localStorage.getItem('strapi-admin-language');
|
||||
const guidedTourCurrentStep = auth.get(CURRENT_STEP);
|
||||
const guidedTourState = auth.get(COMPLETED_STEPS);
|
||||
const applicationTheme = localStorage.getItem(THEME_KEY);
|
||||
|
||||
localStorage.clear();
|
||||
|
||||
@ -38,6 +42,7 @@ const auth = {
|
||||
localStorage.setItem('strapi-admin-language', localeLang);
|
||||
localStorage.setItem(CURRENT_STEP, stringify(guidedTourCurrentStep));
|
||||
localStorage.setItem(COMPLETED_STEPS, stringify(guidedTourState));
|
||||
localStorage.setItem(THEME_KEY, applicationTheme);
|
||||
}
|
||||
|
||||
if (sessionStorage) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user