@wener/ui
Version:
69 lines • 2.28 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.useAntdTheme = exports.loadAntdTheme = void 0;
const react_1 = require("react");
const utils_1 = require("@wener/utils");
function loadTheme(options) {
const { theme, type, url = '' } = options ?? {};
const attr = `data-${type}-theme`;
const cur = document.querySelector(`link[${attr}="${theme}"]`);
if (!cur) {
if (url) {
console.info(`load theme ${type} ${theme} ${url}`);
return utils_1.loadStyles(url, {
attributes: {
[attr]: theme,
},
}).then(() => {
// disable others
const themes = document.querySelectorAll(`link[${attr}]:not([${attr}="${theme}"])`);
themes.forEach((v) => v.setAttribute('disabled', 'true'));
return true;
});
}
return false;
}
const themes = document.querySelectorAll(`link[${attr}]:not([${attr}="${theme}"])`);
themes.forEach((v) => v.setAttribute('disabled', 'true'));
cur.removeAttribute('disabled');
return true;
}
// fixme - should use current version ?
const urls = {
light: 'https://unpkg.com/antd/dist/antd.min.css',
dark: 'https://unpkg.com/antd/dist/antd.dark.min.css',
};
function loadAntdTheme(options) {
const { theme = 'light', src } = options || {};
const url = src || urls[theme];
if (!url) {
console.error(`Theme not found: ${theme}`);
return false;
}
return loadTheme({
theme,
type: 'antd',
url,
});
}
exports.loadAntdTheme = loadAntdTheme;
function useAntdTheme(options) {
const { theme = 'light', src } = options || {};
const [loading, setLoading] = react_1.useState(true);
react_1.useEffect(() => {
const url = src || urls[theme];
if (!url) {
console.error(`Theme not found: ${theme}`);
return;
}
setLoading(true);
Promise.resolve(loadTheme({
theme,
type: 'antd',
url,
})).finally(() => setLoading(false));
}, [theme, src]);
return loading;
}
exports.useAntdTheme = useAntdTheme;
//# sourceMappingURL=useAntdTheme.js.map