@pablo2410/shared-ui

中文文档
0.46.0发布于 2026/11/02UNLICENSEDTypeScriptES Module

Shared UI component library for all Oplytics.digital subdomains

原始使用文档
文档目录10 节
  1. Installation
  2. Modules
  3. Quick Start
  4. Opi tours
  5. Translations
  6. Theme CSS
  7. Development
  8. Architecture
  9. Versioning
  10. License

@oplytics/shared-ui

Shared UI component library for all Oplytics.digital subdomains. Provides standardised layouts, hierarchy management, RBAC, reporting, hooks, and design tokens as reusable building blocks.

Installation

pnpm add @oplytics/shared-ui

Requires .npmrc with @oplytics:registry=https://npm.pkg.github.com

Modules

ModuleImportDescription
Layout@oplytics/shared-ui/layoutConfig-driven sidebar, page header, footer, layout factory
Hierarchy@oplytics/shared-ui/hierarchyOrg hierarchy types, selection state, enterprise override
RBAC@oplytics/shared-ui/rbac7-role model, permission checks, error constants
Reporting@oplytics/shared-ui/reportingIncident reporting and feedback toolbar types/config
Components@oplytics/shared-ui/componentsSharedFooter, NotFound, ErrorBoundary, ViewOnlyBadge, AcceptanceBadge types
Hooks@oplytics/shared-ui/hooksuseMobile, useComposition, usePersistFn
Theme@oplytics/shared-ui/themeDesign tokens, color palette, typography, CSS variables
Actions@pablo2410/shared-ui/actionsDue-date status (overdue, due soon) and its chip, for actions and charters
I18n@pablo2410/shared-ui/i18nTranslations: useT(), English fallback, language picker, formatDate

Quick Start

// RBAC — check permissions
import { hasMinimumRole, ALL_ROLES } from "@oplytics/shared-ui/rbac";

if (hasMinimumRole(user.role, "enterprise_admin")) {
  // show admin features
}

// Hierarchy — enterprise override
import { applyEnterpriseOverride } from "@oplytics/shared-ui/hierarchy";

const resolvedUser = applyEnterpriseOverride(user, req.headers.cookie);

// Layout — service config
import {
  SERVICE_CONFIGS,
  defineServiceLayout,
} from "@oplytics/shared-ui/layout";

const myConfig = SERVICE_CONFIGS.sqdcp;

// Theme — design tokens
import { OPLYTICS_COLORS, OPLYTICS_FONTS } from "@oplytics/shared-ui/theme";

// Hooks
import { useMobile, usePersistFn } from "@oplytics/shared-ui/hooks";

Opi tours

Every module gets its onboarding tour the same way. A module supplies a definition and marks the elements its spotlight steps point at — nothing else is per-module (no storage, no trigger, no narration code):

import {
  useRegisterTour,
  type TourDefinition,
} from "@pablo2410/shared-ui/tour";

// Module scope (or useMemo) so the object is stable across renders.
const tour: TourDefinition = {
  id: "policy-deployment", // lowercase-kebab; never rename once shipped
  steps: [
    { id: "welcome", kind: "intro", label: "Welcome", fallbackText: "…" },
    {
      id: "plans",
      kind: "service",
      label: "Policy plans",
      fallbackText: "…",
      spotlight: { selector: "plan-card" }, // <div data-tour-target="plan-card">
    },
  ],
};

function PolicyPage() {
  useRegisterTour(tour);
  // …
}

What the shared code then does:

  • Trigger — DashboardLayout's header shows "Take the tour" only while a tour is registered, and replays it from step 1.
  • Progress — read and written through Portal's onboarding.userTourState / setUserTourState, so it follows the user across modules. No module table.
  • Narration — each step calls Portal's ai.askOpi with the reserved onboarding service (Portal's onboarding persona), falling back to the step's fallbackText if Opi is unavailable.
  • Auto-open — off by default; set autoStart: true on the definition to open for users who haven't seen the tour or left it part-way. Modules never mount their own OpiTourHost — DashboardLayout mounts the one.

Per-tour storage needs Portal to accept and echo a tourId on those two endpoints. Until it does, progress for module tours is not saved (reads fail safe, nothing auto-opens, the tour and its header replay still work), and the Service Hub tour's own progress is never overwritten.

Translations

@pablo2410/shared-ui/i18n is the one translation set-up every app uses (i18next with react-i18next). English is the default and the fallback: a string missing in another language shows in English, never as its key or a blank. Keys read <app>.<page>.<thing>; shared-ui's own are the shared. keys in src/i18n/en.json and fr.json.

import { I18nProvider, createI18n, useT } from "@pablo2410/shared-ui/i18n";

// Once, around the app, with the app's own strings beside shared-ui's:
const i18n = createI18n({ language: user.language, resources: { en, fr } });
<I18nProvider i18n={i18n} onLanguageChange={saveLanguage}>
  …
</I18nProvider>;

// In a component:
const t = useT();
t("portal.home.title");
  • With no provider, shared-ui's components still render, in English or the language this browser picked.
  • The language picker sits in the shell's user menu (English, Français). It hands the choice to onLanguageChange (on DashboardLayout or I18nProvider); with none, the browser remembers it. DashboardLayout's language prop switches the shell to a language the app already knows.
  • An app that doesn't have text in every language yet says which it offers: <I18nProvider languages={["en"]}>. The pickers then list only those, a language held back is never the active one (even one picked before), and useLanguage().languages gives the same list. Left unset, every language is offered (shared-ui#211).
  • formatDate and formatNumber (or useFormat()) format through Intl in the current language; English is en-GB.
  • What customers type themselves is shown as typed and never translated.
  • The service catalog stays in English for servers and the marketing site. localizeService(service, t) (from /services) gives a service's tagline, description and onboarding text in t's language.
  • A test fails if a component renders text that isn't in en.json, if display text sits in code (a label, title or description value, a prop default, a *_LABEL map), or if fr.json doesn't match en.json key for key.

Theme CSS

Import the canonical Oplytics theme variables:

@import "@oplytics/shared-ui/theme/oplytics-theme.css";

This provides all --oplytics-* CSS custom properties for backgrounds, text, accents, borders, spacing, radius, and shadows.

Development

pnpm install
pnpm build        # Build with tsup
pnpm test         # Run vitest
pnpm typecheck    # TypeScript check

Architecture

The package is tree-shakeable with separate entry points per module. Each subdomain only imports what it needs:

src/
  layout/       # SharedSidebar, createServiceLayout, serviceConfigs
  hierarchy/    # orgHierarchy types, enterpriseOverride
  rbac/         # Roles, permissions, error constants
  reporting/    # ReportingToolbar types and config
  components/   # SharedFooter, NotFound, ErrorBoundary types
  hooks/        # useMobile, useComposition, usePersistFn
  theme/        # Design tokens, CSS variables
  actions/      # getDueDateStatus, DueDateChip (Action Manager and Policy Deployment)
  i18n/         # i18next set-up, useT, en.json / fr.json, LanguagePicker, formatDate

Versioning

  • v0.x — Types, configs, and pure utilities (no React component rendering)
  • v1.0 — Full React components with shadcn/ui primitives bundled

License

UNLICENSED — Internal Oplytics.digital use only.

版本与发布记录

@pablo2410/shared-ui 共发布 77 个版本,首次发布于 2026/03/17;过去 12 个月发布了 77 个版本。

版本发布日期距今
0.46.0latest2026/11/021 天前
0.45.02026/11/021 天前
0.44.02026/11/011 天前
0.43.32026/10/313 天前
0.43.22026/10/303 天前
0.43.12026/10/304 天前

关于 @pablo2410/shared-ui 的常见问题

@pablo2410/shared-ui 是什么?

共享UI组件库为所有的Oplytics。数字子域

如何安装 @pablo2410/shared-ui?

在项目目录运行 npm install @pablo2410/shared-ui,或使用 pnpm add / yarn add / bun add。它还需要宿主项目提供 peer 依赖:react(^18.0.0 || ^19.0.0)、react-dom(^18.0.0 || ^19.0.0)。npm 7 及以上与 pnpm 8 及以上默认会自动安装必需的 peer 依赖,Yarn 需要手动安装。

@pablo2410/shared-ui 支持 TypeScript 吗?

支持。包内自带 TypeScript 类型声明(package.json 声明了 types 入口),安装后即可获得类型提示,无需再安装 @types 包。

@pablo2410/shared-ui 支持 ESM 还是 CommonJS?

以 ES Module 形式发布,推荐使用 import 引入。在 CommonJS 项目中,Node.js 20.19+ / 22.12+ 可以直接 require() 不含顶层 await 的 ES Module,更早的版本需要改用动态 import()。

@pablo2410/shared-ui 可以商用吗?

@pablo2410/shared-ui 使用 UNLICENSED 许可。作者声明为 UNLICENSED,即不授予公开使用许可(与公共领域的 Unlicense 不同)。使用前需获得作者授权。以上为概括,不构成法律意见,请以许可原文为准。

@pablo2410/shared-ui 还在维护吗?

最新版本 0.46.0 发布于 2026/11/02(1 天前);至今共发布 77 个版本,其中过去 12 个月发布了 77 个;首次发布于 2026/03/17。发布频率只是参考信号:功能稳定的工具可能很少需要更新,建议结合源码仓库的 issue 和提交记录判断。