analytica-frontend-lib
Version:
Repositório público dos componentes utilizados nas plataformas da Analytica Ensino
231 lines (220 loc) • 7.51 kB
JavaScript
;Object.defineProperty(exports, "__esModule", {value: true}); function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } } function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }
var _chunkZE5JXJ4Yjs = require('./chunk-ZE5JXJ4Y.js');
// src/store/themeStore.ts
var _zustand = require('zustand');
var _middleware = require('zustand/middleware');
// src/utils/cookieUtils.ts
var getCookie = (name) => {
if (typeof document === "undefined") {
return null;
}
const prefix = `${encodeURIComponent(name)}=`;
const match = document.cookie.split("; ").find((entry) => entry.startsWith(prefix));
if (!match) {
return null;
}
try {
return decodeURIComponent(match.slice(prefix.length));
} catch (e) {
return null;
}
};
var setCookie = (name, value, options = {}) => {
if (typeof document === "undefined") {
return;
}
const parts = [`${encodeURIComponent(name)}=${encodeURIComponent(value)}`];
parts.push(`Path=${_nullishCoalesce(options.path, () => ( "/"))}`);
if (options.maxAge !== void 0) {
parts.push(`Max-Age=${options.maxAge}`);
}
if (options.domain) {
parts.push(`Domain=${options.domain}`);
}
parts.push(`SameSite=${_nullishCoalesce(options.sameSite, () => ( "Lax"))}`);
if (options.secure) {
parts.push("Secure");
}
document.cookie = parts.join("; ");
};
var removeCookie = (name, options = {}) => {
setCookie(name, "", { ...options, maxAge: 0 });
};
// src/store/themeStorage.ts
var ONE_YEAR_IN_SECONDS = 60 * 60 * 24 * 365;
var cookieDomain = () => _nullishCoalesce(_chunkZE5JXJ4Yjs.resolveRootHostname.call(void 0, globalThis.location.hostname), () => ( void 0));
var readLocalStorage = (name) => {
try {
return globalThis.localStorage.getItem(name);
} catch (e2) {
return null;
}
};
var validJsonOrNull = (raw) => {
if (raw === null) {
return null;
}
try {
JSON.parse(raw);
return raw;
} catch (e3) {
return null;
}
};
var themeCookieStorage = {
getItem: (name) => {
if (typeof document === "undefined") {
return null;
}
return _nullishCoalesce(validJsonOrNull(getCookie(name)), () => ( validJsonOrNull(readLocalStorage(name))));
},
setItem: (name, value) => {
if (typeof document === "undefined") {
return;
}
setCookie(name, value, {
domain: cookieDomain(),
path: "/",
maxAge: ONE_YEAR_IN_SECONDS,
sameSite: "Lax",
secure: globalThis.location.protocol === "https:"
});
try {
globalThis.localStorage.setItem(name, value);
} catch (e4) {
}
},
removeItem: (name) => {
if (typeof document === "undefined") {
return;
}
removeCookie(name, { domain: cookieDomain(), path: "/" });
try {
globalThis.localStorage.removeItem(name);
} catch (e5) {
}
}
};
// src/store/themeStore.ts
var DARK_THEME_MAP = {
"base-light": "base-dark",
"enem-parana-light": "enem-parana-dark",
"enem-paraiba-light": "enem-paraiba-dark",
"analytica-light": "analytica-dark",
"papole-light": "papole-dark"
};
var resolveDarkTheme = (originalTheme) => {
if (!originalTheme) return "base-dark";
return _nullishCoalesce(DARK_THEME_MAP[originalTheme], () => ( "base-dark"));
};
var applyThemeToDOM = (mode) => {
const htmlElement = document.documentElement;
const originalTheme = htmlElement.dataset.originalTheme;
if (mode === "dark") {
htmlElement.dataset.theme = resolveDarkTheme(originalTheme);
return true;
} else if (mode === "light") {
if (originalTheme) {
htmlElement.dataset.theme = originalTheme;
}
return false;
} else if (mode === "system") {
const isSystemDark = window.matchMedia(
"(prefers-color-scheme: dark)"
).matches;
if (isSystemDark) {
htmlElement.dataset.theme = resolveDarkTheme(originalTheme);
return true;
} else if (originalTheme) {
htmlElement.dataset.theme = originalTheme;
return false;
}
}
return false;
};
var saveOriginalTheme = () => {
const htmlElement = document.documentElement;
const currentTheme = htmlElement.dataset.theme || _optionalChain([document, 'access', _ => _.querySelector, 'call', _2 => _2('meta[name="theme"]'), 'optionalAccess', _3 => _3.getAttribute, 'call', _4 => _4("content")]);
if (currentTheme && !htmlElement.dataset.originalTheme) {
htmlElement.dataset.originalTheme = currentTheme;
}
};
var useThemeStore = _zustand.create.call(void 0, )(
_middleware.devtools.call(void 0,
_middleware.persist.call(void 0,
(set, get) => ({
// Initial state
themeMode: "system",
isDark: false,
// Actions
applyTheme: (mode) => {
const isDark = applyThemeToDOM(mode);
set({ isDark });
},
toggleTheme: () => {
const { themeMode, applyTheme } = get();
let newMode;
if (themeMode === "light") {
newMode = "dark";
} else if (themeMode === "dark") {
newMode = "light";
} else {
newMode = "dark";
}
set({ themeMode: newMode });
applyTheme(newMode);
},
setTheme: (mode) => {
const { applyTheme } = get();
set({ themeMode: mode });
applyTheme(mode);
},
setWhiteLabelTheme: (theme) => {
const htmlElement = document.documentElement;
if (theme) {
htmlElement.dataset.originalTheme = theme;
const { themeMode, applyTheme } = get();
if (themeMode === "light" || themeMode === "system") {
htmlElement.dataset.theme = theme;
}
applyTheme(themeMode);
}
},
clearWhiteLabelTheme: () => {
const htmlElement = document.documentElement;
delete htmlElement.dataset.originalTheme;
delete htmlElement.dataset.theme;
saveOriginalTheme();
const { themeMode, applyTheme } = get();
applyTheme(themeMode);
},
initializeTheme: () => {
const { themeMode, applyTheme } = get();
saveOriginalTheme();
applyTheme(themeMode);
},
handleSystemThemeChange: () => {
const { themeMode, applyTheme } = get();
if (themeMode === "system") {
applyTheme("system");
}
}
}),
{
name: "theme-store",
// Storage key (cookie + localStorage)
// Root-domain cookie so login and app subdomains share the theme
storage: _middleware.createJSONStorage.call(void 0, () => themeCookieStorage),
partialize: (state) => ({
themeMode: state.themeMode
})
// Só persiste o themeMode, não o isDark
}
),
{
name: "theme-store"
}
)
);
exports.getCookie = getCookie; exports.setCookie = setCookie; exports.removeCookie = removeCookie; exports.useThemeStore = useThemeStore;
//# sourceMappingURL=chunk-WDPJM5OF.js.map