Skip to content

Commit

Permalink
Show how to import from public/
Browse files Browse the repository at this point in the history
  • Loading branch information
garronej committed Jan 11, 2024
1 parent 0735f23 commit 7a35896
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 0 deletions.
Binary file added public/favicon-32x32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed public/favicon.ico
Binary file not shown.
3 changes: 3 additions & 0 deletions public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
<meta charset="utf-8" />
<link rel="icon" href="/favicon.ico" />
<meta name="viewport" content="width=device-width, initial-scale=1" />

<link rel="icon" type="image/png" sizes="32x32" href="%PUBLIC_URL%/favicon-32x32.png">

<!--
Notice the use of %PUBLIC_URL% in the tags above.
It will be replaced with the URL of the `public` folder during the build.
Expand Down
Binary file added public/keycloakify-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
31 changes: 31 additions & 0 deletions src/PUBLIC_URL.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

import { kcContext as kcLoginThemeContext } from "keycloak-theme/login/kcContext";
import { kcContext as kcAccountThemeContext } from "keycloak-theme/login/kcContext";

/**
* If you need to use process.env.PUBLIC_URL, use this variable instead.
* If you can, import your assets using the import statement.
*
* See: https://docs.keycloakify.dev/importing-assets#importing-custom-assets
*/
export const PUBLIC_URL = (()=>{

const kcContext = (()=>{

if( kcLoginThemeContext !== undefined ){
return kcLoginThemeContext;
}

if( kcAccountThemeContext !== undefined ){
return kcLoginThemeContext
}

return undefined;

})();

return (kcContext === undefined || process.env.NODE_ENV === "development")
? process.env.PUBLIC_URL
: `${kcContext.url.resourcesPath}/build`;

})();
7 changes: 7 additions & 0 deletions src/keycloak-theme/login/Template.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { useGetClassName } from "keycloakify/login/lib/useGetClassName";
import type { KcContext } from "./kcContext";
import type { I18n } from "./i18n";
import keycloakifyLogoPngUrl from "./assets/keycloakify-logo.png";
import { PUBLIC_URL } from "../../PUBLIC_URL";

export default function Template(props: TemplateProps<KcContext, I18n>) {
const {
Expand Down Expand Up @@ -59,7 +60,13 @@ export default function Template(props: TemplateProps<KcContext, I18n>) {
className={getClassName("kcHeaderWrapperClass")}
style={{ "fontFamily": '"Work Sans"' }}
>
{/*
This is just to show you how it can be done but this is not the best option for importing assets.
See: https://docs.keycloakify.dev/importing-assets#importing-custom-assets
*/}
<img src={`${PUBLIC_URL}/keycloakify-logo.png`} alt="Keycloakify logo" width={50} />
{msg("loginTitleHtml", realm.displayNameHtml)}!!!
{/* This is the preferred way to use assets */}
<img src={keycloakifyLogoPngUrl} alt="Keycloakify logo" width={50} />
</div>
</div>
Expand Down

0 comments on commit 7a35896

Please sign in to comment.