@choerodon/master
Version:
A package of Master for Choerodon platform.
35 lines (27 loc) • 1.05 kB
JavaScript
import _ from 'lodash';
import { useContext, useEffect } from 'react';
import { setLocalConfig, ThemeContext } from '@hzero-front-ui/core';
function syncBodyThemeAttribute(theme) {
var _localStorage$getItem;
if (!(_.isUndefined(theme) || _.isNull(theme))) {
localStorage.setItem('theme', theme);
}
document.body.setAttribute('data-theme', (_localStorage$getItem = localStorage.getItem('theme')) !== null && _localStorage$getItem !== void 0 ? _localStorage$getItem : '');
}
export default function useTheme() {
var _useContext = useContext(ThemeContext),
changeTheme = _useContext.setTheme,
config = _useContext.config,
schema = _useContext.schema;
useEffect(function () {
if (schema) {
setTheme(schema);
}
}, []);
var setTheme = function setTheme(theme) {
changeTheme(theme === '' ? 'THEME_EMPTY' : theme, config);
setLocalConfig(theme === '' ? 'THEME_EMPTY' : theme, config); // 保存到本地
syncBodyThemeAttribute(theme === '' ? '' : theme);
};
return [schema, setTheme];
}