kcHeaderClass can be updated after initial mount

This commit is contained in:
Joseph Garrone 2021-03-21 22:10:33 +01:00
parent 12f69b593f
commit 1606c2884d
2 changed files with 29 additions and 16 deletions

View File

@ -1,6 +1,6 @@
{
"name": "keycloakify",
"version": "0.3.0",
"version": "0.3.1",
"description": "Keycloak theme generator for Reacts app",
"repository": {
"type": "git",

View File

@ -25,7 +25,7 @@ export type TemplateProps = {
showUsernameNode?: ReactNode;
formNode: ReactNode;
infoNode?: ReactNode;
} & { kcContext: KcContext.Template; } & KcTemplateProps;
} & { kcContext: KcContext.Template; } & KcTemplateProps;
export const Template = memo((props: TemplateProps) => {
@ -62,30 +62,31 @@ export const Template = memo((props: TemplateProps) => {
const {
realm, locale, auth,
url, message, isAppInitiatedAction
}= kcContext;
} = kcContext;
useEffect(()=>{
useEffect(() => {
if( !realm.internationalizationEnabled ){
if (!realm.internationalizationEnabled) {
return;
}
assert( locale !== undefined );
assert(locale !== undefined);
if( kcLanguageTag === getBestMatchAmongKcLanguageTag(locale.current) ){
if (kcLanguageTag === getBestMatchAmongKcLanguageTag(locale.current)) {
return;
}
window.location.href =
locale.supported.find(({ languageTag }) => languageTag === kcLanguageTag)!.url;
},[kcLanguageTag]);
}, [kcLanguageTag]);
const [isExtraCssLoaded, setExtraCssLoaded] = useReducer(() => true, false);
useEffect(() => {
let isUnmounted = false;
const cleanups: (() => void)[] = [];
const toArr = (x: string | readonly string[] | undefined) =>
typeof x === "string" ? x.split(" ") : x ?? [];
@ -116,15 +117,27 @@ export const Template = memo((props: TemplateProps) => {
if (props.kcHtmlClass !== undefined) {
document.getElementsByTagName("html")[0]
.classList
.add(...cx(props.kcHtmlClass).split(" "));
const htmlClassList =
document.getElementsByTagName("html")[0]
.classList;
const tokens = cx(props.kcHtmlClass).split(" ")
htmlClassList.add(...tokens);
cleanups.push(() => htmlClassList.remove(...tokens));
}
return () => { isUnmounted = true; };
return () => {
}, []);
isUnmounted = true;
cleanups.forEach(f => f());
};
}, [props.kcHeaderClass]);
if (!isExtraCssLoaded) {
return null;