SPB Git

Repository Public

Tabs.tsx 3.6 KB 85 lines tsx

1/**2 * =============================================================================3 *                  SP BOUCHER | SOFTWARE & RESEARCH4 *             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING5 * =============================================================================6 *7 * Project          : SP Mail8 * File             : web/src/ui/templates/Tabs.tsx9 * Module           : ui10 * Repository       : https://git.spboucher.ai/sp-mail11 * Project Website  : https://mail.spboucher.ai12 * Created          : 2026-11-1913 * Last Modified    : 2026-11-1914 *15 * Author           : Simon-Pierre Boucher16 * Position         : Professor of Finance17 * Institution      : Université du Québec en Outaouais (UQO)18 * Organization     : SP Boucher19 * Website          : https://spboucher.ai20 * Git              : https://git.spboucher.ai21 *22 * Preferred Contact: support@spboucher.ai23 * Development      : dev@spboucher.ai24 * Support          : support@spboucher.ai25 * Security/Admin   : admin@spboucher.ai26 *27 * Copyright (c) 202628 * Simon-Pierre Boucher29 * All rights reserved where applicable. This file is governed by the30 * applicable project license, copyright notices and relevant law.31 * Existing third-party rights and notices remain unaffected.32 *33 * License          : None specified (no license file in this repository)34 *35 * Do not embed credentials, private keys, access tokens,36 * certificates or confidential personal information.37 *38 * =============================================================================39 * END OF SP BOUCHER SOURCE HEADER40 * =============================================================================41 */4243// Onglets accessibles (WAI-ARIA) : flèches, Début/Fin, un seul onglet dans l'ordre de tabulation.44import { useRef } from "react"45import { Icon, type IconName } from "../kit"4647export interface TabDef<K extends string> {48  id: K49  label: string50  icon?: IconName51  badge?: string52  disabled?: boolean53}5455export const tabId = (base: string, id: string) => `${base}-tab-${id}`56export const panelId = (base: string, id: string) => `${base}-panel-${id}`5758export function Tabs<K extends string>({ base, label, tabs, current, onChange, className }: { base: string; label: string; tabs: TabDef<K>[]; current: K; onChange: (id: K) => void; className?: string }) {59  const list = useRef<HTMLDivElement>(null)60  const onKeyDown = (e: React.KeyboardEvent) => {61    const enabled = tabs.filter((x) => !x.disabled)62    const i = enabled.findIndex((x) => x.id === current)63    let next = -164    if (e.key === "ArrowRight" || e.key === "ArrowDown") next = (i + 1) % enabled.length65    else if (e.key === "ArrowLeft" || e.key === "ArrowUp") next = (i - 1 + enabled.length) % enabled.length66    else if (e.key === "Home") next = 067    else if (e.key === "End") next = enabled.length - 168    if (next < 0) return69    e.preventDefault()70    onChange(enabled[next].id)71    requestAnimationFrame(() => list.current?.querySelector<HTMLElement>(`#${CSS.escape(tabId(base, enabled[next].id))}`)?.focus())72  }73  return (74    <div ref={list} className={`tpl-tabs${className ? ` ${className}` : ""}`} role="tablist" aria-label={label} onKeyDown={onKeyDown}>75      {tabs.map((tab) => (76        <button key={tab.id} type="button" role="tab" id={tabId(base, tab.id)} aria-selected={tab.id === current} aria-controls={panelId(base, tab.id)} tabIndex={tab.id === current ? 0 : -1} disabled={tab.disabled} onClick={() => onChange(tab.id)}>77          {tab.icon ? <Icon name={tab.icon} small /> : null}78          <span>{tab.label}</span>79          {tab.badge ? <span className="badge warn">{tab.badge}</span> : null}80        </button>81      ))}82    </div>83  )84}85
Files main
Menu

Theme