@tempots/beatui 0.2.0 → 0.4.0

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,33 @@
1
+ /**
2
+ * @fileoverview BeatUI Internationalization (i18n) Module
3
+ *
4
+ * Provides reactive internationalization support with:
5
+ * - Locale management with persistent storage
6
+ * - Dynamic translation loading with type safety
7
+ * - Reactive updates throughout the application
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { html, prop, Provide, Use } from '@tempots/dom'
12
+ * import { Locale, makeMessages } from '@tempots/beatui'
13
+ *
14
+ * // Set up locale provider
15
+ * const app = Provide(Locale, {}, () => Use(Locale, ({ locale, setLocale }) => {
16
+ * // Create translation system
17
+ * const { t } = makeMessages({
18
+ * locale,
19
+ * defaultMessages: {
20
+ * welcome: () => 'Welcome!',
21
+ * greeting: (name: string) => `Hello, ${name}!`
22
+ * },
23
+ * localeLoader: locale => import(`./locales/${locale}.js`)
24
+ * })
25
+ * return html.div(
26
+ * html.h1(t.welcome()),
27
+ * html.p(t.userGreeting(prop('User')))
28
+ * )
29
+ * })
30
+ * ```
31
+ */
32
+ export * from './translate';
33
+ export * from './locale';
@@ -0,0 +1,29 @@
1
+ import { Signal, Provider } from '@tempots/dom';
2
+ /**
3
+ * Value provided by the Locale provider containing locale state and setter.
4
+ */
5
+ export type LocaleValue = {
6
+ /** Reactive signal containing the current locale string (e.g., 'en-US', 'es-ES') */
7
+ locale: Signal<string>;
8
+ /** Function to update the current locale */
9
+ setLocale: (locale: string) => void;
10
+ };
11
+ /**
12
+ * Locale provider that manages the current application locale with persistent storage.
13
+ *
14
+ * Features:
15
+ * - Automatically detects browser language as default
16
+ * - Persists locale selection to localStorage with key 'beatui-locale'
17
+ * - Provides reactive updates when locale changes
18
+ * - Includes proper cleanup for memory management
19
+ *
20
+ * @example
21
+ * ```typescript
22
+ * import { Use } from '@tempots/dom'
23
+ * import { Locale } from '@tempots/beatui'
24
+ *
25
+ * // Use in component
26
+ * Use(Locale, ({ locale, setLocale }) => html.div(locale))
27
+ * ```
28
+ */
29
+ export declare const Locale: Provider<LocaleValue, object>;
@@ -0,0 +1,82 @@
1
+ import { Signal, Value } from '@tempots/dom';
2
+ /**
3
+ * Utility type that wraps each element of a tuple in a Value<T>.
4
+ * Used to convert function parameters to reactive values.
5
+ */
6
+ type WrapInValue<T extends unknown[]> = {
7
+ [K in keyof T]: Value<T[K]>;
8
+ };
9
+ /**
10
+ * Transforms a messages object into a computed version where each message function
11
+ * returns a reactive Signal instead of a direct value.
12
+ */
13
+ type MessagesToComputed<M extends object> = {
14
+ [K in keyof M]: M[K] extends (...args: infer Args) => infer R ? (...args: WrapInValue<Args>) => Signal<R> : never;
15
+ };
16
+ /**
17
+ * Creates a reactive translation system that dynamically loads locale-specific messages.
18
+ *
19
+ * Features:
20
+ * - Type-safe translation functions with parameter validation
21
+ * - Dynamic loading of locale-specific message bundles
22
+ * - Reactive translations that update when locale changes
23
+ * - Automatic fallback to default locale on loading errors
24
+ * - Proper cleanup and memory management
25
+ *
26
+ * @param config Configuration object for the translation system
27
+ * @param config.locale Reactive signal containing the current locale
28
+ * @param config.defaultLocale Default locale to fall back to (default: 'en-US')
29
+ * @param config.defaultMessages Default message functions used as fallback
30
+ * @param config.localeLoader Async function to load locale-specific messages
31
+ *
32
+ * @returns Object containing translation functions (`t`) and cleanup (`dispose`)
33
+ *
34
+ * @example
35
+ * ```typescript
36
+ * import { prop, Use } from '@tempots/dom'
37
+ * import { Locale, makeMessages } from '@tempots/beatui'
38
+ *
39
+ * const defaultMessages = {
40
+ * welcome: () => 'Welcome!',
41
+ * userGreeting: (name: string) => `Hello, ${name}!`,
42
+ * itemCount: (count: number) => `${count} item${count !== 1 ? 's' : ''}`
43
+ * }
44
+ *
45
+ * const { locale } = Use(Locale, ({ locale }) => {
46
+ * // Create translation system
47
+ * const { t, dispose } = makeMessages({
48
+ * locale,
49
+ * defaultMessages,
50
+ * localeLoader: locale => import(`./locales/${locale}.js`)
51
+ * })
52
+ *
53
+ * return html.div(
54
+ * html.h1(t.welcome()),
55
+ * html.p(t.userGreeting(prop('User')))
56
+ * )
57
+ * })
58
+ *
59
+ * // Use reactive translations
60
+ * const greeting = t.greeting(prop('John')) // Signal<string>
61
+ * const count = t.itemCount(prop(5)) // Signal<string>
62
+ *
63
+ * // Clean up when done
64
+ * dispose()
65
+ * ```
66
+ */
67
+ export declare function makeMessages<M extends object>({ locale, defaultLocale, defaultMessages, localeLoader, }: {
68
+ /** Reactive signal containing the current locale string */
69
+ locale: Signal<string>;
70
+ /** Default locale to use as fallback (default: 'en-US') */
71
+ defaultLocale: string;
72
+ /** Default message functions used as fallback and type reference */
73
+ defaultMessages: M;
74
+ /** Async function that loads locale-specific message bundles */
75
+ localeLoader: (locale: string) => Promise<M>;
76
+ }): {
77
+ /** Clean up all resources and event listeners */
78
+ dispose: () => void;
79
+ /** Translation functions that return reactive signals */
80
+ t: MessagesToComputed<M>;
81
+ };
82
+ export {};
@@ -8,5 +8,6 @@ export * from './components/navigation';
8
8
  export * from './components/overlay';
9
9
  export * from './components/theme';
10
10
  export * from './components/typography';
11
+ export * from './i18n';
11
12
  export * from './tokens';
12
13
  export * from './utils';
@@ -0,0 +1,32 @@
1
+ import { TNode } from '@tempots/dom';
2
+ /**
3
+ * Focus trap options
4
+ */
5
+ export interface FocusTrapOptions {
6
+ /** Whether the focus trap is active */
7
+ active?: boolean;
8
+ /** Whether to handle escape key */
9
+ escapeDeactivates?: boolean;
10
+ /** Callback when escape key is pressed */
11
+ onEscape?: () => void;
12
+ /** Element to focus initially (if not provided, focuses first focusable element) */
13
+ initialFocus?: HTMLElement | (() => HTMLElement | null);
14
+ /** Element to return focus to when trap is deactivated */
15
+ returnFocus?: HTMLElement | (() => HTMLElement | null);
16
+ /** Whether to prevent outside clicks */
17
+ clickOutsideDeactivates?: boolean;
18
+ /** Callback when clicking outside */
19
+ onClickOutside?: () => void;
20
+ }
21
+ /**
22
+ * Creates a focus trap within an element
23
+ * Returns a TNode that should be added to the container element
24
+ */
25
+ export declare function FocusTrap(options?: FocusTrapOptions): TNode;
26
+ /**
27
+ * Hook for managing focus trap state
28
+ */
29
+ export declare function useFocusTrap(options?: FocusTrapOptions): {
30
+ activate: () => TNode;
31
+ deactivate: () => TNode;
32
+ };
@@ -1,2 +1,4 @@
1
1
  export * from './delay-signal';
2
+ export * from './focus-trap';
3
+ export * from './session-id';
2
4
  export * from './use-animated-toggle';
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Generates a unique session ID for the given prefix
3
+ * @param prefix The prefix to use for the session ID
4
+ * @returns The generated session ID
5
+ */
6
+ export declare function sessionId(prefix: string): string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tempots/beatui",
3
- "version": "0.2.0",
3
+ "version": "0.4.0",
4
4
  "type": "module",
5
5
  "main": "dist/index.umd.js",
6
6
  "module": "dist/index.es.js",
@@ -54,9 +54,9 @@
54
54
  "registry": "https://registry.npmjs.org/"
55
55
  },
56
56
  "peerDependencies": {
57
- "@tempots/dom": "28.2.0",
58
- "@tempots/std": "0.22.0",
59
- "@tempots/ui": "5.2.0"
57
+ "@tempots/dom": "28.2.2",
58
+ "@tempots/std": "0.22.1",
59
+ "@tempots/ui": "6.0.0"
60
60
  },
61
61
  "peerDependenciesMeta": {
62
62
  "@tempots/dom": {