All files / service-web-theme/src header-template.ts

100% Statements 10/10
100% Branches 21/21
100% Functions 6/6
100% Lines 10/10

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108                        68x 68x 68x                                                       78x                         3x                                   110x           78x 31x     47x                                   53x            
import { accountIdentityTemplate } from './account-identity-template.ts';
import { skeletonGroupTemplate, skeletonTemplate } from './skeleton-template.ts';
import { languageMenuTemplate } from './popover-template.ts';
import type { Locale } from './locale.ts';
import { html, type TemplateResult } from 'lit';
import { ifDefined } from 'lit/directives/if-defined.js';
import { repeat } from 'lit/directives/repeat.js';
import { unsafeHTML } from 'lit/directives/unsafe-html.js';
import { resolveServiceHeader, type HeaderAction, type ServiceHeaderInput } from './header-model.ts';
 
/** Header markup for clients. Only the model's fixed icon constants enter through unsafeHTML. */
export function serviceHeaderTemplate(input: ServiceHeaderInput): TemplateResult {
  const view = resolveServiceHeader(input);
  const { account, language } = view;
  return html`<header class="bg-background/90 sticky top-0 z-40 border-b backdrop-blur">
    <div class=${view.rowClass}>
      <div class="flex shrink-0 items-center gap-1">
        <a class="font-semibold tracking-tight whitespace-nowrap no-underline" href=${view.homeHref}>${view.title}</a>
        <span class=${view.badgeClass} data-variant="outline">Beta</span>
      </div>
      <nav class="ms-auto flex items-center gap-1 whitespace-nowrap sm:gap-2" aria-label=${view.navLabel}>
        ${
          account.loading
            ? accountLoadingTemplate(input.locale, account.loadingLabel, account.iconHTML)
            : html`<a
                class="btn"
                data-size="sm"
                data-variant="ghost"
                data-service-header="account"
                href=${account.href}
                target=${ifDefined(account.newTab ? '_blank' : undefined)}
                rel=${ifDefined(account.newTab ? 'noopener' : undefined)}
                title=${ifDefined(account.title)}
                aria-label=${ifDefined(account.ariaLabel)}
              >
                ${
                  input.account !== undefined
                    ? html`${accountIdentityTemplate({ account: input.account, locale: input.locale, variant: 'compact' })}${icon(account.iconHTML, 'text-muted-foreground')}`
                    : account.label
                }
              </a>`
        }
        ${repeat(view.actions, (action) => action.key, actionTemplate)}
        ${languageMenuTemplate({ label: language.label, choices: language.choices, buttonSize: 'icon-sm' })}
      </nav>
    </div>
  </header>`;
}
 
/**
 * The account control while the page reads who is signed in: the signed-in control's box (avatar, then the name from
 * `sm` up at the label's 12ch cap, then the chevron), so the header does not move when the account arrives. It is not
 * a link yet.
 */
function accountLoadingTemplate(locale: Locale, label: string, chevronHTML: string): TemplateResult {
  return html`<span
    class="btn pointer-events-none"
    data-size="sm"
    data-variant="ghost"
    data-service-header="account-loading"
    >${skeletonGroupTemplate({
      locale,
      label,
      content: html`${skeletonTemplate({ shape: 'circle', width: 24 })}${skeletonTemplate({
        shape: 'line',
        width: '12ch',
        className: 'hidden sm:inline-block',
      })}`,
    })}${icon(chevronHTML, 'text-muted-foreground')}</span
  >`;
}
 
function icon(svg: string, className = ''): TemplateResult {
  return html`<span class=${`inline-flex shrink-0 [&>svg]:size-4${className ? ` ${className}` : ''}`} aria-hidden="true"
    >${unsafeHTML(svg)}</span
  >`;
}
 
function actionTemplate(action: HeaderAction): TemplateResult {
  if (action.href !== undefined)
    return html`<a class="btn" data-size="sm" href=${action.href} title=${action.title} aria-label=${action.label}
      >${icon(action.iconHTML)}<span class="hidden md:inline">${action.label}</span></a
    >`;
  return html`<button
    type="button"
    class="btn"
    data-size="icon-sm"
    data-service-header=${action.key}
    aria-haspopup=${ifDefined(action.dialog === undefined ? undefined : 'dialog')}
    aria-controls=${ifDefined(action.dialog)}
    data-tooltip=${action.label}
    data-side="bottom"
    aria-label=${action.label}
    aria-expanded=${ifDefined(action.expanded === undefined ? undefined : String(action.expanded))}
    aria-pressed=${ifDefined(action.pressed === undefined ? undefined : String(action.pressed))}
  >
    ${icon(action.iconHTML)}
  </button>`;
}
 
export function serviceSkipLinkTemplate(locale: Locale): TemplateResult {
  return html`<a
    class="sr-only focus:not-sr-only focus:fixed focus:top-2 focus:left-4 focus:z-50 focus:rounded-md focus:bg-primary focus:px-3 focus:py-2 focus:text-sm focus:font-medium focus:text-primary-foreground"
    href="#main"
    >${locale === 'ja' ? '本文へ移動' : 'Skip to content'}</a
  >`;
}