@vendure-io/ui 2.0.0-beta.1 → 2.0.0-beta.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,36 @@
1
+ import { cn } from '@vendure-io/ui/lib/utils';
2
+ import type * as React from 'react';
3
+
4
+ interface SkipLinkProps extends React.ComponentProps<'a'> {
5
+ /** The main-content element to focus. Include the leading `#`. */
6
+ href?: `#${string}`;
7
+ }
8
+
9
+ /**
10
+ * Keyboard-only escape hatch for repeated application chrome. Place it as the
11
+ * first focusable element in the document and point it at `AppShellMain` (whose
12
+ * default id is `main-content`). Native anchor behavior keeps this server-safe
13
+ * and works before React hydrates.
14
+ */
15
+ function SkipLink({
16
+ className,
17
+ href = '#main-content',
18
+ children = 'Skip to main content',
19
+ ...props
20
+ }: SkipLinkProps) {
21
+ return (
22
+ <a
23
+ data-slot="skip-link"
24
+ href={href}
25
+ className={cn(
26
+ 'bg-primary text-primary-foreground fixed top-3 left-3 z-[100] -translate-y-20 rounded-md px-3 py-2 text-sm font-medium opacity-0 outline-none transition-[transform,opacity] duration-(--transition-duration-fast) ease-(--ease-out) focus-visible:translate-y-0 focus-visible:opacity-100 focus-visible:ring-3 focus-visible:ring-ring/50 motion-reduce:transition-none',
27
+ className,
28
+ )}
29
+ {...props}
30
+ >
31
+ {children}
32
+ </a>
33
+ );
34
+ }
35
+
36
+ export { SkipLink, type SkipLinkProps };
@@ -24,13 +24,16 @@ export interface LoadingStateProps
24
24
  rows?: number;
25
25
  /** Classes applied to each skeleton row (e.g. `h-10` for tighter lists). */
26
26
  rowClassName?: string;
27
- /** Visible label. An sr-only "Loading…" is always rendered for assistive tech. */
27
+ /** Visible label. An sr-only `srLabel` is always rendered for assistive tech. */
28
28
  label?: React.ReactNode;
29
+ /** Sr-only label announced to assistive tech. Override for host i18n. @default "Loading…" */
30
+ srLabel?: string;
29
31
  }
30
32
 
31
33
  /**
32
34
  * Placeholder shell for in-flight list/detail queries. Renders as an
33
- * `aria-live` `<output>` with an always-present sr-only "Loading…" label.
35
+ * `aria-live` `<output>` with an always-present sr-only `srLabel` (defaults to
36
+ * "Loading…", overridable for host i18n).
34
37
  * `skeleton` (default) draws N shimmer rows; `spinner` centers the `Spinner`
35
38
  * atom for compact or unknown-height regions.
36
39
  */
@@ -39,6 +42,7 @@ function LoadingState({
39
42
  rows = 5,
40
43
  rowClassName,
41
44
  label,
45
+ srLabel = 'Loading…',
42
46
  className,
43
47
  ...props
44
48
  }: LoadingStateProps) {
@@ -49,7 +53,7 @@ function LoadingState({
49
53
  className={cn(loadingStateVariants({ variant }), className)}
50
54
  {...props}
51
55
  >
52
- <span className="sr-only">Loading…</span>
56
+ <span className="sr-only">{srLabel}</span>
53
57
  {variant === 'spinner' ? (
54
58
  <>
55
59
  {/* aria-hidden overrides the atom's role="status": the <output> is
@@ -0,0 +1,48 @@
1
+ const CALENDAR_DATE_PATTERN = /^(\d{4})-(\d{2})-(\d{2})$/;
2
+
3
+ /** Parse `YYYY-MM-DD` into a local calendar date without a UTC shift. */
4
+ function parseCalendarDate(value: string | null | undefined): Date | undefined {
5
+ if (!value) return undefined;
6
+ const match = CALENDAR_DATE_PATTERN.exec(value);
7
+ if (!match) return undefined;
8
+ const year = Number(match[1]);
9
+ const month = Number(match[2]);
10
+ const day = Number(match[3]);
11
+ const date = new Date(year, month - 1, day);
12
+ if (date.getFullYear() !== year || date.getMonth() !== month - 1 || date.getDate() !== day) {
13
+ return undefined;
14
+ }
15
+ return date;
16
+ }
17
+
18
+ /** Serialize a local calendar date as `YYYY-MM-DD`. */
19
+ function formatCalendarDate(date: Date): string {
20
+ const year = date.getFullYear();
21
+ const month = String(date.getMonth() + 1).padStart(2, '0');
22
+ const day = String(date.getDate()).padStart(2, '0');
23
+ return `${year}-${month}-${day}`;
24
+ }
25
+
26
+ function parseInstant(value: string | null | undefined): Date | undefined {
27
+ if (!value) return undefined;
28
+ const date = new Date(value);
29
+ return Number.isNaN(date.getTime()) ? undefined : date;
30
+ }
31
+
32
+ function formatDateLabel(date: Date, locale?: string): string {
33
+ return new Intl.DateTimeFormat(locale, { dateStyle: 'medium' }).format(date);
34
+ }
35
+
36
+ function formatDateRangeLabel(from: Date, to: Date | undefined, locale?: string): string {
37
+ if (!to || from.getTime() === to.getTime()) return formatDateLabel(from, locale);
38
+ const formatter = new Intl.DateTimeFormat(locale, { dateStyle: 'medium' });
39
+ return formatter.formatRange(from, to);
40
+ }
41
+
42
+ export {
43
+ formatCalendarDate,
44
+ formatDateLabel,
45
+ formatDateRangeLabel,
46
+ parseCalendarDate,
47
+ parseInstant,
48
+ };
@@ -0,0 +1,141 @@
1
+ import {
2
+ transformerNotationDiff,
3
+ transformerNotationErrorLevel,
4
+ transformerNotationFocus,
5
+ transformerNotationHighlight,
6
+ transformerNotationWordHighlight,
7
+ } from '@shikijs/transformers';
8
+ import { createHighlighterCore } from 'shiki/core';
9
+ import { createJavaScriptRegexEngine } from 'shiki/engine/javascript';
10
+
11
+ /**
12
+ * The design system's Shiki setup: a lazy shared highlighter with the DS themes
13
+ * (github-light / github-dark-default), a fixed grammar set, and the `[!code ...]`
14
+ * notation transformers. Exported so consumers rendering highlighted HTML outside
15
+ * of `CodeBlock` (e.g. custom docs pipelines) reuse the exact same setup instead
16
+ * of duplicating it — and get the lazy per-language chunks instead of the full
17
+ * Shiki bundle.
18
+ */
19
+
20
+ const languageLoaders = {
21
+ bash: () => import('@shikijs/langs/bash').then((module) => module.default),
22
+ css: () => import('@shikijs/langs/css').then((module) => module.default),
23
+ dotenv: () => import('@shikijs/langs/dotenv').then((module) => module.default),
24
+ graphql: () => import('@shikijs/langs/graphql').then((module) => module.default),
25
+ html: () => import('@shikijs/langs/html').then((module) => module.default),
26
+ ini: () => import('@shikijs/langs/ini').then((module) => module.default),
27
+ javascript: () => import('@shikijs/langs/javascript').then((module) => module.default),
28
+ json: () => import('@shikijs/langs/json').then((module) => module.default),
29
+ jsonc: () => import('@shikijs/langs/jsonc').then((module) => module.default),
30
+ jsx: () => import('@shikijs/langs/jsx').then((module) => module.default),
31
+ markdown: () => import('@shikijs/langs/markdown').then((module) => module.default),
32
+ mdx: () => import('@shikijs/langs/mdx').then((module) => module.default),
33
+ python: () => import('@shikijs/langs/python').then((module) => module.default),
34
+ shellscript: () => import('@shikijs/langs/shellscript').then((module) => module.default),
35
+ sql: () => import('@shikijs/langs/sql').then((module) => module.default),
36
+ tsx: () => import('@shikijs/langs/tsx').then((module) => module.default),
37
+ typescript: () => import('@shikijs/langs/typescript').then((module) => module.default),
38
+ yaml: () => import('@shikijs/langs/yaml').then((module) => module.default),
39
+ } as const;
40
+
41
+ type SupportedLanguage = keyof typeof languageLoaders;
42
+
43
+ const themeLoaders = [
44
+ () => import('@shikijs/themes/github-light').then((module) => module.default),
45
+ () => import('@shikijs/themes/github-dark-default').then((module) => module.default),
46
+ ] as const;
47
+
48
+ let highlighterPromise: ReturnType<typeof createHighlighterCore> | null = null;
49
+
50
+ function getHighlighter(): ReturnType<typeof createHighlighterCore> {
51
+ highlighterPromise ??= Promise.all([
52
+ Promise.all(themeLoaders.map((loadTheme) => loadTheme())),
53
+ Promise.all(Object.values(languageLoaders).map((loadLanguage) => loadLanguage())),
54
+ ]).then(([themes, languages]) =>
55
+ createHighlighterCore({
56
+ themes,
57
+ langs: languages.flat(),
58
+ engine: createJavaScriptRegexEngine(),
59
+ }),
60
+ );
61
+
62
+ return highlighterPromise;
63
+ }
64
+
65
+ const highlightedCodeCache = new Map<string, Promise<string>>();
66
+
67
+ /**
68
+ * Highlight code using Shiki with all transformers.
69
+ * Uses Shiki's native notation for highlighting:
70
+ * - // [!code highlight] - highlight a line (use language-appropriate comment)
71
+ * - // [!code ++] / // [!code --] - diff highlighting
72
+ * - // [!code focus] - focus mode (blur other lines)
73
+ * - // [!code word:myVar] - highlight specific word
74
+ * - // [!code error] / // [!code warning] - error levels
75
+ */
76
+ async function highlightCode(code: string, language: SupportedLanguage): Promise<string> {
77
+ const cacheKey = `${language}:${code}`;
78
+ const cached = highlightedCodeCache.get(cacheKey);
79
+ if (cached) return cached;
80
+
81
+ const highlighted = getHighlighter().then((highlighter) =>
82
+ highlighter.codeToHtml(code, {
83
+ lang: language,
84
+ themes: {
85
+ light: 'github-light',
86
+ dark: 'github-dark-default',
87
+ },
88
+ transformers: [
89
+ transformerNotationDiff({ matchAlgorithm: 'v3' }),
90
+ transformerNotationHighlight({ matchAlgorithm: 'v3' }),
91
+ transformerNotationWordHighlight({ matchAlgorithm: 'v3' }),
92
+ transformerNotationFocus({ matchAlgorithm: 'v3' }),
93
+ transformerNotationErrorLevel({ matchAlgorithm: 'v3' }),
94
+ ],
95
+ }),
96
+ );
97
+ highlightedCodeCache.set(cacheKey, highlighted);
98
+ return highlighted;
99
+ }
100
+
101
+ /**
102
+ * Language aliases that map to Shiki's bundled language names
103
+ */
104
+ const LANGUAGE_ALIASES: Record<string, SupportedLanguage> = {
105
+ env: 'dotenv',
106
+ js: 'javascript',
107
+ ts: 'typescript',
108
+ sh: 'bash',
109
+ shell: 'bash',
110
+ yml: 'yaml',
111
+ py: 'python',
112
+ md: 'markdown',
113
+ plaintext: 'ini',
114
+ text: 'ini',
115
+ chroma: 'ini',
116
+ };
117
+
118
+ /**
119
+ * Normalize language identifier for Shiki/BundledLanguage.
120
+ * Falls back to 'ini' for unsupported languages (minimal highlighting).
121
+ */
122
+ function normalizeLanguage(lang?: string): SupportedLanguage {
123
+ const normalized = lang?.toLowerCase() || 'ini';
124
+
125
+ // Check for aliases first
126
+ const alias = LANGUAGE_ALIASES[normalized];
127
+ if (alias) {
128
+ return alias;
129
+ }
130
+
131
+ // Check if the language is one of the explicitly bundled grammars.
132
+ if (normalized in languageLoaders) {
133
+ return normalized as SupportedLanguage;
134
+ }
135
+
136
+ // Fallback to 'ini' for unsupported languages (has minimal highlighting)
137
+ return 'ini';
138
+ }
139
+
140
+ export { getHighlighter, highlightCode, normalizeLanguage };
141
+ export type { SupportedLanguage };