Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/clerk-js/src/ui/elements/ApplicationLogo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ import React from 'react';

import { useEnvironment } from '../contexts';
import { descriptors, Flex, Image, useAppearance } from '../customizables';
import type { InternalTheme } from '../foundations';
import type { PropsOfComponent } from '../styledSystem';
import { RouterLink } from './RouterLink';

const logoLoadCache: { [url: string]: boolean } = {};

const getContainerHeightForImageRatio = (imageRef: React.RefObject<HTMLImageElement>, width: string) => {
if (!imageRef.current) {
return `calc(${width} * 2)`;
Expand All @@ -23,13 +26,20 @@ const getContainerHeightForImageRatio = (imageRef: React.RefObject<HTMLImageElem

type ApplicationLogoProps = PropsOfComponent<typeof Flex>;

export const ApplicationLogo = (props: ApplicationLogoProps) => {
export const ApplicationLogo = React.memo((props: ApplicationLogoProps) => {
const imageRef = React.useRef<HTMLImageElement>(null);
const [loaded, setLoaded] = React.useState(false);
const { logoImageUrl, applicationName, homeUrl } = useEnvironment().displayConfig;
const { parsedLayout } = useAppearance();
const imageSrc = parsedLayout.logoImageUrl || logoImageUrl;
const logoUrl = parsedLayout.logoLinkUrl || homeUrl;
const [loaded, setLoaded] = React.useState(() => (imageSrc ? logoLoadCache[imageSrc] || false : false));

const handleImageLoad = React.useCallback(() => {
if (imageSrc) {
logoLoadCache[imageSrc] = true;
setLoaded(true);
}
}, [imageSrc]);

if (!imageSrc) {
return null;
Expand All @@ -42,7 +52,7 @@ export const ApplicationLogo = (props: ApplicationLogoProps) => {
alt={applicationName}
src={imageSrc}
size={200}
onLoad={() => setLoaded(true)}
onLoad={handleImageLoad}
sx={{
display: loaded ? 'inline-block' : 'none',
height: '100%',
Expand All @@ -57,7 +67,7 @@ export const ApplicationLogo = (props: ApplicationLogoProps) => {
elementDescriptor={descriptors.logoBox}
{...props}
sx={[
theme => ({
(theme: InternalTheme) => ({
height: getContainerHeightForImageRatio(imageRef, theme.sizes.$6),
justifyContent: 'center',
}),
Expand All @@ -78,4 +88,4 @@ export const ApplicationLogo = (props: ApplicationLogoProps) => {
)}
</Flex>
);
};
});