| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203 |
- import { cn } from '@/lib/utils';
- import type { LogoDesignKey, StoreLogoData } from './types';
-
- /** Keep in step with StoreLogos::DESIGNS. */
- export const LOGO_DESIGNS: LogoDesignKey[] = [
- 'wordmark',
- 'spaced',
- 'monogram-circle',
- 'monogram-square',
- 'two-tone',
- 'underline',
- 'badge',
- 'dot',
- ];
-
- /** Up to two letters for a badge: the first letters of the first two words. */
- export function initials(name: string): string {
- const words = name
- .replace(/[^\p{L}\p{N}\s]/gu, ' ')
- .split(/\s+/)
- .filter(Boolean);
-
- return (
- words.length > 1
- ? words[0][0] + words[1][0]
- : (words[0] ?? '?').slice(0, 2)
- ).toUpperCase();
- }
-
- /**
- * A store's logo. Reads the store's colours and fonts from the storefront's CSS variables,
- * so ready designs follow the store's look when it changes.
- */
- export function StoreLogo({
- logo,
- name,
- className,
- }: {
- logo: StoreLogoData;
- name: string;
- className?: string;
- }) {
- if (logo.type === 'upload') {
- return (
- <img
- src={logo.url}
- width={logo.width}
- height={logo.height}
- alt={name}
- className={cn(
- 'h-10 w-auto max-w-[200px] object-contain md:h-12',
- className,
- )}
- data-test="store-logo"
- />
- );
- }
-
- if (logo.type === 'drawn') {
- return (
- <span
- role="img"
- aria-label={name}
- className={cn(
- 'block h-10 md:h-12 [&>svg]:h-full [&>svg]:w-auto [&>svg]:max-w-[260px]',
- className,
- )}
- // Cleaned on the server by LogoSvg: simple shapes and text only.
- dangerouslySetInnerHTML={{ __html: logo.svg }}
- data-test="store-logo"
- />
- );
- }
-
- return <LogoDesign design={logo.key} name={name} className={className} />;
- }
-
- /** One of the ready designs, drawn from the store's name, fonts and colours. */
- export function LogoDesign({
- design,
- name,
- className,
- }: {
- design: LogoDesignKey;
- name: string;
- className?: string;
- }) {
- const heading = 'font-(family-name:--sf-font-heading) text-(--sf-fg)';
- const mark = (shape: 'circle' | 'square') => (
- <span
- aria-hidden="true"
- className={cn(
- 'flex size-9 shrink-0 items-center justify-center bg-(--sf-primary) text-sm font-bold tracking-wide text-(--sf-primary-fg)',
- shape === 'circle' ? 'rounded-full' : 'rounded-(--sf-radius)',
- )}
- >
- {initials(name)}
- </span>
- );
- const [first, ...rest] = name.split(' ');
-
- const body = (() => {
- switch (design) {
- case 'spaced':
- return (
- <span
- className={cn(
- heading,
- 'text-lg font-semibold tracking-[0.22em] uppercase',
- )}
- >
- {name}
- </span>
- );
- case 'monogram-circle':
- case 'monogram-square':
- return (
- <span className="inline-flex items-center gap-2.5">
- {mark(
- design === 'monogram-circle' ? 'circle' : 'square',
- )}
- <span className={cn(heading, 'text-xl font-semibold')}>
- {name}
- </span>
- </span>
- );
- case 'two-tone':
- return (
- <span
- className={cn(
- heading,
- 'text-xl font-bold tracking-tight',
- )}
- >
- <span className="text-(--sf-primary)">{first}</span>
- {rest.length > 0 && ` ${rest.join(' ')}`}
- </span>
- );
- case 'underline':
- return (
- <span className="inline-flex flex-col items-start gap-1">
- <span
- className={cn(
- heading,
- 'text-xl leading-none font-bold',
- )}
- >
- {name}
- </span>
- <span
- aria-hidden="true"
- className="h-[3px] w-2/5 rounded-full bg-(--sf-accent)"
- />
- </span>
- );
- case 'badge':
- return (
- <span
- className={cn(
- heading,
- 'rounded-(--sf-radius) border-2 border-(--sf-fg) px-3 py-1 text-sm font-bold tracking-[0.16em] uppercase',
- )}
- >
- {name}
- </span>
- );
- case 'dot':
- return (
- <span
- className={cn(
- heading,
- 'text-xl font-bold tracking-tight',
- )}
- >
- {name}
- <span aria-hidden="true" className="text-(--sf-accent)">
- .
- </span>
- </span>
- );
- default:
- return (
- <span className={cn(heading, 'text-xl font-semibold')}>
- {name}
- </span>
- );
- }
- })();
-
- return (
- <span
- className={cn(
- 'inline-flex items-center whitespace-nowrap',
- className,
- )}
- aria-label={name}
- role="img"
- data-test="store-logo"
- >
- {body}
- </span>
- );
- }
|