@vvedantb/ui 0.7.2 → 0.8.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.
- package/README.md +1 -1
- package/app/src/index.ts +31 -0
- package/app/src/page-motion/PageMotionProvider.tsx +44 -0
- package/app/src/page-title/createPageTitleContext.tsx +99 -0
- package/app/src/theme/createThemeProvider.tsx +275 -0
- package/app/src/time/compactRelativeTime.ts +89 -0
- package/package.json +12 -11
- package/app/README.md +0 -100
- package/tokens/README.md +0 -98
package/README.md
CHANGED
|
@@ -4,7 +4,7 @@ Shared React 19 web UI. Source-exported: no build step.
|
|
|
4
4
|
|
|
5
5
|
| Import | What it is |
|
|
6
6
|
| --- | --- |
|
|
7
|
-
| `@vvedantb/ui` | The only JavaScript entry. Primitives (Radix-based), AI elements, audio (`playChime`, `useReplyChime`, `playTone`), `toast` and `SonnerToaster`, the composer (`InputArea`, deprecated 0.3 `Composer`), shell (`SidebarHeader`, `PillTabs`, `SessionsHoverCard`, and the app shell: `AppShell`, `AppSidebar`, `SidebarRail`, `RailTile`, `SidebarPanel`, `SidebarAccountMenu`, `SignOutDialog` and related helpers), app helpers (confirm skip, update watcher, settings chrome, `useStableAuth`), `cn`, and the native tokens `tokens` and `Tokens`. See `app/README.md` in the repository for the app helpers. |
|
|
7
|
+
| `@vvedantb/ui` | The only JavaScript entry. Primitives (Radix-based), AI elements, audio (`playChime`, `useReplyChime`, `playTone`), `toast` and `SonnerToaster`, the composer (`InputArea`, deprecated 0.3 `Composer`), shell (`SidebarHeader`, `PillTabs`, `SessionsHoverCard`, and the app shell: `AppShell`, `AppSidebar`, `SidebarRail`, `RailTile`, `SidebarPanel`, `SidebarAccountMenu`, `SignOutDialog` and related helpers), app helpers (confirm skip, update watcher, settings chrome, `useStableAuth`, and from 0.8.0 `createThemeProvider`, `createPageTitleContext`, `PageMotionProvider` and `formatCompactRelativeTime`), `cn`, and the native tokens `tokens` and `Tokens`. See `app/README.md` in the repository for the app helpers. |
|
|
8
8
|
| `@vvedantb/ui/tokens` | `tokens.css`: colour, radius, shadow, ease and font tokens for Tailwind v4. CSS. |
|
|
9
9
|
| `@vvedantb/ui/tokens/tokens.css` | Same file as `@vvedantb/ui/tokens`. CSS. |
|
|
10
10
|
| `@vvedantb/ui/tokens/css/*.css` | `index`, `base`, `dialog`, `modal`, `tabs`, `tabs-pills`, `utilities`, `scrollbar-thin`, `animations`, `compat-shadcn-rgb`. CSS. |
|
package/app/src/index.ts
CHANGED
|
@@ -41,3 +41,34 @@ export {
|
|
|
41
41
|
type ClerkAuthSlice,
|
|
42
42
|
type StableAuthOptions,
|
|
43
43
|
} from "./auth/useStableAuth.js";
|
|
44
|
+
|
|
45
|
+
export {
|
|
46
|
+
applyThemeClass,
|
|
47
|
+
createThemeProvider,
|
|
48
|
+
getSystemTheme,
|
|
49
|
+
type ThemeAttribute,
|
|
50
|
+
type ThemeProviderKit,
|
|
51
|
+
type ThemeProviderOptions,
|
|
52
|
+
type ThemeProviderProps,
|
|
53
|
+
type ThemeState,
|
|
54
|
+
} from "./theme/createThemeProvider.js";
|
|
55
|
+
|
|
56
|
+
export {
|
|
57
|
+
createPageTitleContext,
|
|
58
|
+
type DocumentTitleOptions,
|
|
59
|
+
type PageTitleContextKit,
|
|
60
|
+
type PageTitleContextOptions,
|
|
61
|
+
type PageTitleValue,
|
|
62
|
+
} from "./page-title/createPageTitleContext.js";
|
|
63
|
+
|
|
64
|
+
export {
|
|
65
|
+
PageMotionProvider,
|
|
66
|
+
useDisablePageMotion,
|
|
67
|
+
type PageMotionConfigProps,
|
|
68
|
+
type PageMotionProviderProps,
|
|
69
|
+
} from "./page-motion/PageMotionProvider.js";
|
|
70
|
+
|
|
71
|
+
export {
|
|
72
|
+
formatCompactRelativeTime,
|
|
73
|
+
type CompactRelativeTimeOptions,
|
|
74
|
+
} from "./time/compactRelativeTime.js";
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { createContext, use, useLayoutEffect, type ComponentType, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/** The props `MotionConfig` from `motion/react` takes here. */
|
|
4
|
+
export type PageMotionConfigProps = {
|
|
5
|
+
reducedMotion: "always" | "never";
|
|
6
|
+
children: ReactNode;
|
|
7
|
+
};
|
|
8
|
+
|
|
9
|
+
export type PageMotionProviderProps = {
|
|
10
|
+
/** Page motion is off, for example from a user setting. */
|
|
11
|
+
disabled: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Pass `MotionConfig` from `motion/react`, or a component of the app that
|
|
14
|
+
* wraps it (for example in `LazyMotion`). Injected so this package does not
|
|
15
|
+
* import `motion`. Define it at module level, not inline.
|
|
16
|
+
*/
|
|
17
|
+
motionConfig: ComponentType<PageMotionConfigProps>;
|
|
18
|
+
children: ReactNode;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const PageMotionContext = createContext(false);
|
|
22
|
+
|
|
23
|
+
/** Whether page motion is off (`false` outside `PageMotionProvider`). */
|
|
24
|
+
export function useDisablePageMotion(): boolean {
|
|
25
|
+
return use(PageMotionContext);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Stamps `html[data-page-motion]` (`"off"` or `"on"`) in a layout effect so CSS
|
|
30
|
+
* page and route transitions can skip, and sets Motion's `reducedMotion` to
|
|
31
|
+
* `"always"` when disabled and `"never"` otherwise. The attribute stays after
|
|
32
|
+
* unmount.
|
|
33
|
+
*/
|
|
34
|
+
export function PageMotionProvider({ disabled, motionConfig: MotionConfig, children }: PageMotionProviderProps) {
|
|
35
|
+
useLayoutEffect(() => {
|
|
36
|
+
document.documentElement.dataset.pageMotion = disabled ? "off" : "on";
|
|
37
|
+
}, [disabled]);
|
|
38
|
+
|
|
39
|
+
return (
|
|
40
|
+
<PageMotionContext value={disabled}>
|
|
41
|
+
<MotionConfig reducedMotion={disabled ? "always" : "never"}>{children}</MotionConfig>
|
|
42
|
+
</PageMotionContext>
|
|
43
|
+
);
|
|
44
|
+
}
|
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createContext, use, useEffect, useState, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
export type PageTitleValue = {
|
|
4
|
+
pageTitle: string;
|
|
5
|
+
setPageTitle: (title: string) => void;
|
|
6
|
+
/** Element a mobile top bar registers (as a ref) so pages can portal actions into it. */
|
|
7
|
+
mobileActionsSlot: HTMLElement | null;
|
|
8
|
+
setMobileActionsSlot: (slot: HTMLElement | null) => void;
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
export type DocumentTitleOptions = {
|
|
12
|
+
/** Appended after the page title, for example the app name. */
|
|
13
|
+
suffix?: string;
|
|
14
|
+
/** Between page title and suffix. Default `" · "`. */
|
|
15
|
+
separator?: string;
|
|
16
|
+
/** Title while no page title is set. Default: `document.title` when the provider mounted. */
|
|
17
|
+
fallback?: string;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type PageTitleContextOptions = {
|
|
21
|
+
/**
|
|
22
|
+
* `usePageTitle()` outside the provider throws this message. When unset it
|
|
23
|
+
* returns an empty title and no-op setters instead.
|
|
24
|
+
*/
|
|
25
|
+
missingProviderMessage?: string;
|
|
26
|
+
/**
|
|
27
|
+
* Also write `document.title` as `pageTitle + separator + suffix`, or
|
|
28
|
+
* `fallback` while the page title is empty. Off by default: the provider
|
|
29
|
+
* does not touch `document.title`. The mount-time title comes back on unmount.
|
|
30
|
+
*/
|
|
31
|
+
documentTitle?: DocumentTitleOptions;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
export type PageTitleContextKit = {
|
|
35
|
+
PageTitleProvider: (props: { children: ReactNode }) => ReactNode;
|
|
36
|
+
usePageTitle: () => PageTitleValue;
|
|
37
|
+
/** Publishes `title` while the caller is mounted; non-string titles publish `""`. Clears on unmount. */
|
|
38
|
+
usePageTitleSync: (title: ReactNode) => void;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
const noop = () => {};
|
|
42
|
+
|
|
43
|
+
function formatDocumentTitle(pageTitle: string, suffix: string | undefined, separator: string): string {
|
|
44
|
+
return suffix ? `${pageTitle}${separator}${suffix}` : pageTitle;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/** One page title context per app: the shell reads `pageTitle`, pages publish theirs. */
|
|
48
|
+
export function createPageTitleContext(options: PageTitleContextOptions = {}): PageTitleContextKit {
|
|
49
|
+
const { missingProviderMessage, documentTitle } = options;
|
|
50
|
+
const Context = createContext<PageTitleValue | null>(null);
|
|
51
|
+
const fallbackValue: PageTitleValue = {
|
|
52
|
+
pageTitle: "",
|
|
53
|
+
setPageTitle: noop,
|
|
54
|
+
mobileActionsSlot: null,
|
|
55
|
+
setMobileActionsSlot: noop,
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
function useDocumentTitle(pageTitle: string): void {
|
|
59
|
+
const [mountTitle] = useState(() => (documentTitle && typeof document !== "undefined" ? document.title : ""));
|
|
60
|
+
useEffect(() => {
|
|
61
|
+
if (!documentTitle) return;
|
|
62
|
+
const { suffix, separator = " · ", fallback = mountTitle } = documentTitle;
|
|
63
|
+
document.title = pageTitle ? formatDocumentTitle(pageTitle, suffix, separator) : fallback;
|
|
64
|
+
}, [pageTitle, mountTitle]);
|
|
65
|
+
useEffect(() => {
|
|
66
|
+
if (!documentTitle) return;
|
|
67
|
+
return () => {
|
|
68
|
+
document.title = mountTitle;
|
|
69
|
+
};
|
|
70
|
+
}, [mountTitle]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function PageTitleProvider({ children }: { children: ReactNode }) {
|
|
74
|
+
const [pageTitle, setPageTitle] = useState("");
|
|
75
|
+
const [mobileActionsSlot, setMobileActionsSlot] = useState<HTMLElement | null>(null);
|
|
76
|
+
useDocumentTitle(pageTitle);
|
|
77
|
+
return (
|
|
78
|
+
<Context value={{ pageTitle, setPageTitle, mobileActionsSlot, setMobileActionsSlot }}>{children}</Context>
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
function usePageTitle(): PageTitleValue {
|
|
83
|
+
const value = use(Context);
|
|
84
|
+
if (value !== null) return value;
|
|
85
|
+
if (missingProviderMessage !== undefined) throw new Error(missingProviderMessage);
|
|
86
|
+
return fallbackValue;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
function usePageTitleSync(title: ReactNode): void {
|
|
90
|
+
const { setPageTitle } = usePageTitle();
|
|
91
|
+
const isStringTitle = typeof title === "string";
|
|
92
|
+
useEffect(() => {
|
|
93
|
+
setPageTitle(isStringTitle ? title : "");
|
|
94
|
+
return () => setPageTitle("");
|
|
95
|
+
}, [title, isStringTitle, setPageTitle]);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return { PageTitleProvider, usePageTitle, usePageTitleSync };
|
|
99
|
+
}
|
|
@@ -0,0 +1,275 @@
|
|
|
1
|
+
import { createContext, use, useEffect, useState, type ReactNode } from "react";
|
|
2
|
+
|
|
3
|
+
/** `"class"` or a `data-*` attribute on `<html>`. */
|
|
4
|
+
export type ThemeAttribute = "class" | `data-${string}`;
|
|
5
|
+
|
|
6
|
+
export type ThemeProviderProps = {
|
|
7
|
+
children?: ReactNode;
|
|
8
|
+
/** localStorage key. Default `"theme"`. */
|
|
9
|
+
storageKey?: string;
|
|
10
|
+
/** Used when nothing is stored. Default `"system"` with `enableSystem`, else `"light"`. */
|
|
11
|
+
defaultTheme?: string;
|
|
12
|
+
/** Resolve `"system"` from `prefers-color-scheme`. Default `true`. */
|
|
13
|
+
enableSystem?: boolean;
|
|
14
|
+
/** Set `style.colorScheme` on `<html>` for light and dark. Default `true`. */
|
|
15
|
+
enableColorScheme?: boolean;
|
|
16
|
+
/** Where the theme goes on `<html>`. Default `"data-theme"`. */
|
|
17
|
+
attribute?: ThemeAttribute | ThemeAttribute[];
|
|
18
|
+
/** Theme names. Default `["light", "dark"]`. */
|
|
19
|
+
themes?: string[];
|
|
20
|
+
/** Theme name to the class or attribute value written. */
|
|
21
|
+
value?: Record<string, string>;
|
|
22
|
+
/** Shown on `<html>` instead of the stored theme, which is left unchanged. */
|
|
23
|
+
forcedTheme?: string;
|
|
24
|
+
/** Turn CSS transitions off for one frame while the theme changes. Default `false`. */
|
|
25
|
+
disableTransitionOnChange?: boolean;
|
|
26
|
+
/** Nonce for the style element `disableTransitionOnChange` adds. */
|
|
27
|
+
nonce?: string;
|
|
28
|
+
};
|
|
29
|
+
|
|
30
|
+
export type ThemeState = {
|
|
31
|
+
/** Stored theme, `"system"` included. `undefined` only without `window`. */
|
|
32
|
+
theme: string | undefined;
|
|
33
|
+
/** Stores the theme. A function gets the theme of the render it came from. */
|
|
34
|
+
setTheme: (theme: string | ((theme: string | undefined) => string)) => void;
|
|
35
|
+
forcedTheme: string | undefined;
|
|
36
|
+
/** `theme`, with `"system"` resolved to `systemTheme`. */
|
|
37
|
+
resolvedTheme: string | undefined;
|
|
38
|
+
/** `themes` plus `"system"` with `enableSystem`. */
|
|
39
|
+
themes: string[];
|
|
40
|
+
/** `prefers-color-scheme` with `enableSystem`. Until the first effect runs it is `theme` unless that is `"system"` (as in next-themes). */
|
|
41
|
+
systemTheme: string | undefined;
|
|
42
|
+
/** `false` until the provider's first effect has run. */
|
|
43
|
+
mounted: boolean;
|
|
44
|
+
/** `mounted && resolvedTheme === "dark"`. */
|
|
45
|
+
isDark: boolean;
|
|
46
|
+
/** `setTheme(resolvedTheme === "dark" ? "light" : "dark")`. */
|
|
47
|
+
toggleTheme: () => void;
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
export type ThemeProviderOptions = {
|
|
51
|
+
/** `useTheme()` outside the provider throws this message. When unset it returns an empty state with no-op setters. */
|
|
52
|
+
missingProviderMessage?: string;
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
export type ThemeProviderKit = {
|
|
56
|
+
ThemeProvider: (props: ThemeProviderProps) => ReactNode;
|
|
57
|
+
useTheme: () => ThemeState;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
const COLOR_SCHEMES = ["light", "dark"];
|
|
61
|
+
const MEDIA = "(prefers-color-scheme: dark)";
|
|
62
|
+
const DISABLE_TRANSITIONS_CSS =
|
|
63
|
+
"*,*::before,*::after{-webkit-transition:none!important;-moz-transition:none!important;-o-transition:none!important;-ms-transition:none!important;transition:none!important}";
|
|
64
|
+
|
|
65
|
+
const noop = () => {};
|
|
66
|
+
|
|
67
|
+
const emptyState: ThemeState = {
|
|
68
|
+
theme: undefined,
|
|
69
|
+
setTheme: noop,
|
|
70
|
+
forcedTheme: undefined,
|
|
71
|
+
resolvedTheme: undefined,
|
|
72
|
+
themes: [],
|
|
73
|
+
systemTheme: undefined,
|
|
74
|
+
mounted: false,
|
|
75
|
+
isDark: false,
|
|
76
|
+
toggleTheme: noop,
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
/** `prefers-color-scheme`, or `"light"` without `window`. */
|
|
80
|
+
export function getSystemTheme(): "light" | "dark" {
|
|
81
|
+
if (typeof window === "undefined") return "light";
|
|
82
|
+
return window.matchMedia(MEDIA).matches ? "dark" : "light";
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
/** Replaces the `light` / `dark` class on `<html>` with `theme`. No-op without `document`. */
|
|
86
|
+
export function applyThemeClass(theme: string): void {
|
|
87
|
+
if (typeof document === "undefined") return;
|
|
88
|
+
document.documentElement.classList.remove("light", "dark");
|
|
89
|
+
document.documentElement.classList.add(theme);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
function systemThemeOf(media: { matches: boolean }): "light" | "dark" {
|
|
93
|
+
return media.matches ? "dark" : "light";
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
function readTheme(storageKey: string, fallback: string): string | undefined {
|
|
97
|
+
if (typeof window === "undefined") return undefined;
|
|
98
|
+
let stored: string | undefined;
|
|
99
|
+
try {
|
|
100
|
+
stored = localStorage.getItem(storageKey) || undefined;
|
|
101
|
+
} catch {
|
|
102
|
+
// Storage can be unavailable (privacy modes); fall back.
|
|
103
|
+
}
|
|
104
|
+
return stored || fallback;
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
function disableTransitions(nonce: string | undefined): () => void {
|
|
108
|
+
const style = document.createElement("style");
|
|
109
|
+
if (nonce) style.setAttribute("nonce", nonce);
|
|
110
|
+
style.appendChild(document.createTextNode(DISABLE_TRANSITIONS_CSS));
|
|
111
|
+
document.head.appendChild(style);
|
|
112
|
+
return () => {
|
|
113
|
+
// Force a style recalculation before the transitions come back.
|
|
114
|
+
window.getComputedStyle(document.body);
|
|
115
|
+
setTimeout(() => {
|
|
116
|
+
document.head.removeChild(style);
|
|
117
|
+
}, 1);
|
|
118
|
+
};
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
type DomOptions = Required<
|
|
122
|
+
Pick<ThemeProviderProps, "attribute" | "themes" | "enableSystem" | "enableColorScheme" | "defaultTheme" | "disableTransitionOnChange">
|
|
123
|
+
> &
|
|
124
|
+
Pick<ThemeProviderProps, "value" | "nonce">;
|
|
125
|
+
|
|
126
|
+
function applyTheme(theme: string | undefined, options: DomOptions): void {
|
|
127
|
+
if (!theme) return;
|
|
128
|
+
const resolved = theme === "system" && options.enableSystem ? systemThemeOf(window.matchMedia(MEDIA)) : theme;
|
|
129
|
+
const written = options.value ? options.value[resolved] : resolved;
|
|
130
|
+
const restoreTransitions = options.disableTransitionOnChange ? disableTransitions(options.nonce) : null;
|
|
131
|
+
const root = document.documentElement;
|
|
132
|
+
const removable = options.value ? Object.values(options.value) : options.themes;
|
|
133
|
+
for (const attribute of Array.isArray(options.attribute) ? options.attribute : [options.attribute]) {
|
|
134
|
+
if (attribute === "class") {
|
|
135
|
+
root.classList.remove(...removable);
|
|
136
|
+
if (written) root.classList.add(written);
|
|
137
|
+
} else if (attribute.startsWith("data-")) {
|
|
138
|
+
if (written) root.setAttribute(attribute, written);
|
|
139
|
+
else root.removeAttribute(attribute);
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
if (options.enableColorScheme) {
|
|
143
|
+
const fallback = COLOR_SCHEMES.includes(options.defaultTheme) ? options.defaultTheme : null;
|
|
144
|
+
// `null` clears the property in browsers; assigned as-is, as next-themes does.
|
|
145
|
+
Reflect.set(root.style, "colorScheme", COLOR_SCHEMES.includes(resolved) ? resolved : fallback);
|
|
146
|
+
}
|
|
147
|
+
restoreTransitions?.();
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A theme provider with next-themes 0.4.6 semantics, plus `mounted`, `isDark`
|
|
152
|
+
* and `toggleTheme`. Each call makes its own context, so an app keeps one kit.
|
|
153
|
+
*
|
|
154
|
+
* Parity notes: the options that shape DOM writes (`attribute`, `themes`,
|
|
155
|
+
* `value`, `enableSystem`, `enableColorScheme`, `defaultTheme`,
|
|
156
|
+
* `disableTransitionOnChange`, `nonce`) are read once, on the first render, as
|
|
157
|
+
* in next-themes; `forcedTheme`, and `enableSystem` for the system listener and
|
|
158
|
+
* the returned state, follow props. A provider nested in another one from the
|
|
159
|
+
* same kit renders only its children. No inline script is rendered: in a
|
|
160
|
+
* client-rendered app React never runs it.
|
|
161
|
+
*/
|
|
162
|
+
export function createThemeProvider(options: ThemeProviderOptions = {}): ThemeProviderKit {
|
|
163
|
+
const { missingProviderMessage } = options;
|
|
164
|
+
const Context = createContext<ThemeState | undefined>(undefined);
|
|
165
|
+
|
|
166
|
+
/** next-themes hands out a new `setTheme` exactly when `theme` changes; apps list it in effect deps. */
|
|
167
|
+
function useSetTheme(
|
|
168
|
+
theme: string | undefined,
|
|
169
|
+
storageKey: string,
|
|
170
|
+
setThemeState: (theme: string) => void,
|
|
171
|
+
): ThemeState["setTheme"] {
|
|
172
|
+
const make = (): ThemeState["setTheme"] => (next) => {
|
|
173
|
+
const resolved = typeof next === "function" ? next(theme) : next;
|
|
174
|
+
setThemeState(resolved);
|
|
175
|
+
try {
|
|
176
|
+
localStorage.setItem(storageKey, resolved);
|
|
177
|
+
} catch {
|
|
178
|
+
// Storage can be unavailable (privacy modes).
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
const [entry, setEntry] = useState(() => ({ theme, setTheme: make() }));
|
|
182
|
+
if (entry.theme === theme) return entry.setTheme;
|
|
183
|
+
const next = { theme, setTheme: make() };
|
|
184
|
+
setEntry(next);
|
|
185
|
+
return next.setTheme;
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
function Provider({
|
|
189
|
+
forcedTheme,
|
|
190
|
+
disableTransitionOnChange = false,
|
|
191
|
+
enableSystem = true,
|
|
192
|
+
enableColorScheme = true,
|
|
193
|
+
storageKey = "theme",
|
|
194
|
+
themes = COLOR_SCHEMES,
|
|
195
|
+
defaultTheme = enableSystem ? "system" : "light",
|
|
196
|
+
attribute = "data-theme",
|
|
197
|
+
value,
|
|
198
|
+
nonce,
|
|
199
|
+
children,
|
|
200
|
+
}: ThemeProviderProps) {
|
|
201
|
+
const [theme, setThemeState] = useState(() => readTheme(storageKey, defaultTheme));
|
|
202
|
+
const [systemTheme, setSystemTheme] = useState<string | undefined>(() =>
|
|
203
|
+
theme === "system" ? systemThemeOf(window.matchMedia(MEDIA)) : theme,
|
|
204
|
+
);
|
|
205
|
+
const [mounted, setMounted] = useState(false);
|
|
206
|
+
const [domOptions] = useState<DomOptions>(() => ({
|
|
207
|
+
attribute,
|
|
208
|
+
themes,
|
|
209
|
+
value,
|
|
210
|
+
enableSystem,
|
|
211
|
+
enableColorScheme,
|
|
212
|
+
defaultTheme,
|
|
213
|
+
disableTransitionOnChange,
|
|
214
|
+
nonce,
|
|
215
|
+
}));
|
|
216
|
+
const setTheme = useSetTheme(theme, storageKey, setThemeState);
|
|
217
|
+
|
|
218
|
+
useEffect(() => setMounted(true), []);
|
|
219
|
+
|
|
220
|
+
// Listener and handler are rebuilt whenever `theme` or `forcedTheme` changes.
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
const handleMedia = (media: { matches: boolean }) => {
|
|
223
|
+
setSystemTheme(systemThemeOf(media));
|
|
224
|
+
if (theme === "system" && enableSystem && !forcedTheme) applyTheme("system", domOptions);
|
|
225
|
+
};
|
|
226
|
+
const media = window.matchMedia(MEDIA);
|
|
227
|
+
media.addListener(handleMedia);
|
|
228
|
+
handleMedia(media);
|
|
229
|
+
return () => media.removeListener(handleMedia);
|
|
230
|
+
}, [theme, forcedTheme]);
|
|
231
|
+
|
|
232
|
+
// Other tabs: follow a new value, or store the default when the key is removed.
|
|
233
|
+
useEffect(() => {
|
|
234
|
+
const handleStorage = (event: StorageEvent) => {
|
|
235
|
+
if (event.key !== storageKey) return;
|
|
236
|
+
if (event.newValue) setThemeState(event.newValue);
|
|
237
|
+
else setTheme(defaultTheme);
|
|
238
|
+
};
|
|
239
|
+
window.addEventListener("storage", handleStorage);
|
|
240
|
+
return () => window.removeEventListener("storage", handleStorage);
|
|
241
|
+
}, [setTheme]);
|
|
242
|
+
|
|
243
|
+
useEffect(() => {
|
|
244
|
+
applyTheme(forcedTheme ?? theme, domOptions);
|
|
245
|
+
}, [forcedTheme, theme]);
|
|
246
|
+
|
|
247
|
+
const resolvedTheme = theme === "system" ? systemTheme : theme;
|
|
248
|
+
const state: ThemeState = {
|
|
249
|
+
theme,
|
|
250
|
+
setTheme,
|
|
251
|
+
forcedTheme,
|
|
252
|
+
resolvedTheme,
|
|
253
|
+
themes: enableSystem ? [...themes, "system"] : themes,
|
|
254
|
+
systemTheme: enableSystem ? systemTheme : undefined,
|
|
255
|
+
mounted,
|
|
256
|
+
isDark: mounted && resolvedTheme === "dark",
|
|
257
|
+
toggleTheme: () => setTheme(resolvedTheme === "dark" ? "light" : "dark"),
|
|
258
|
+
};
|
|
259
|
+
return <Context value={state}>{children}</Context>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
function ThemeProvider(props: ThemeProviderProps) {
|
|
263
|
+
if (use(Context)) return <>{props.children}</>;
|
|
264
|
+
return <Provider {...props} />;
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
function useTheme(): ThemeState {
|
|
268
|
+
const state = use(Context);
|
|
269
|
+
if (state) return state;
|
|
270
|
+
if (missingProviderMessage !== undefined) throw new Error(missingProviderMessage);
|
|
271
|
+
return emptyState;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
return { ThemeProvider, useTheme };
|
|
275
|
+
}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
export type CompactRelativeTimeOptions = {
|
|
2
|
+
/** Clock in ms. Defaults to `Date.now()`. */
|
|
3
|
+
now?: number;
|
|
4
|
+
/**
|
|
5
|
+
* Returned for anything under a minute old, including future times. When
|
|
6
|
+
* unset the result is whole seconds (`"42s"`), and a future time gives zero
|
|
7
|
+
* or a negative count (`"0s"`, `"-300s"`).
|
|
8
|
+
*/
|
|
9
|
+
justNow?: string;
|
|
10
|
+
/**
|
|
11
|
+
* `false` (default): elapsed time only. A day is 24 hours, a month 30 days,
|
|
12
|
+
* a year 12 of those months.
|
|
13
|
+
* `true`: days count local calendar days across a DST change, and months and
|
|
14
|
+
* years are calendar months, as `dayjs().diff(date, unit)` counts them.
|
|
15
|
+
*/
|
|
16
|
+
calendar?: boolean;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const MS_PER_MINUTE = 60_000;
|
|
20
|
+
const MS_PER_DAY = 86_400_000;
|
|
21
|
+
|
|
22
|
+
/** Rounds toward zero; `-0` becomes `0`. */
|
|
23
|
+
function truncate(n: number): number {
|
|
24
|
+
return n < 0 ? Math.ceil(n) || 0 : Math.floor(n);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Local UTC offset in minutes, rounded to 15 minutes. */
|
|
28
|
+
function utcOffset(ms: number): number {
|
|
29
|
+
return -Math.round(new Date(ms).getTimezoneOffset() / 15) * 15;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** Adds calendar months in local time and clamps the day to the target month. */
|
|
33
|
+
function addMonths(ms: number, months: number): number {
|
|
34
|
+
const date = new Date(ms);
|
|
35
|
+
const day = date.getDate();
|
|
36
|
+
date.setDate(1);
|
|
37
|
+
date.setMonth(date.getMonth() + months);
|
|
38
|
+
const daysInMonth = new Date(date.getFullYear(), date.getMonth() + 1, 0).getDate();
|
|
39
|
+
date.setDate(Math.min(day, daysInMonth));
|
|
40
|
+
return date.getTime();
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
/** Fractional calendar months from `a` to `b`, negated (the moment.js formula). */
|
|
44
|
+
function monthDiff(a: number, b: number): number {
|
|
45
|
+
if (new Date(a).getDate() < new Date(b).getDate()) return -monthDiff(b, a);
|
|
46
|
+
const from = new Date(a);
|
|
47
|
+
const to = new Date(b);
|
|
48
|
+
const whole = (to.getFullYear() - from.getFullYear()) * 12 + (to.getMonth() - from.getMonth());
|
|
49
|
+
const anchor = addMonths(a, whole);
|
|
50
|
+
const before = b - anchor < 0;
|
|
51
|
+
const anchor2 = addMonths(a, whole + (before ? -1 : 1));
|
|
52
|
+
return +(-(whole + (b - anchor) / (before ? anchor - anchor2 : anchor2 - anchor)) || 0);
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Time since `date` as one compact unit: `"now"` or `"42s"`, then `"5m"`,
|
|
57
|
+
* `"3h"`, `"2d"`, `"4mo"`, `"1y"`. Strings are parsed with `new Date(string)`.
|
|
58
|
+
* An invalid date gives `"NaNy"`, or `"0mo"` with `calendar`.
|
|
59
|
+
*/
|
|
60
|
+
export function formatCompactRelativeTime(
|
|
61
|
+
date: string | number | Date,
|
|
62
|
+
options: CompactRelativeTimeOptions = {},
|
|
63
|
+
): string {
|
|
64
|
+
const now = options.now ?? Date.now();
|
|
65
|
+
const then = new Date(date).getTime();
|
|
66
|
+
const diff = now - then;
|
|
67
|
+
|
|
68
|
+
const seconds = truncate(diff / 1000);
|
|
69
|
+
if (seconds < 60) return options.justNow ?? `${seconds}s`;
|
|
70
|
+
const minutes = truncate(diff / MS_PER_MINUTE);
|
|
71
|
+
if (minutes < 60) return `${minutes}m`;
|
|
72
|
+
const hours = truncate(diff / (60 * MS_PER_MINUTE));
|
|
73
|
+
if (hours < 24) return `${hours}h`;
|
|
74
|
+
|
|
75
|
+
if (!options.calendar) {
|
|
76
|
+
const days = Math.floor(hours / 24);
|
|
77
|
+
if (days < 30) return `${days}d`;
|
|
78
|
+
const months = Math.floor(days / 30);
|
|
79
|
+
if (months < 12) return `${months}mo`;
|
|
80
|
+
return `${Math.floor(months / 12)}y`;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
const zoneDelta = (utcOffset(then) - utcOffset(now)) * MS_PER_MINUTE;
|
|
84
|
+
const days = truncate((diff - zoneDelta) / MS_PER_DAY);
|
|
85
|
+
if (days < 30) return `${days}d`;
|
|
86
|
+
const months = monthDiff(now, then);
|
|
87
|
+
if (truncate(months) < 12) return `${truncate(months)}mo`;
|
|
88
|
+
return `${truncate(months / 12)}y`;
|
|
89
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@vvedantb/ui",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"description": "Shared React 19 UI for web apps: one JavaScript entry (primitives, shell pieces, app helpers, native tokens), design-token CSS, shared Tailwind v4 CSS and a Tailwind preset.",
|
|
6
6
|
"type": "module",
|
|
@@ -52,15 +52,6 @@
|
|
|
52
52
|
"sideEffects": [
|
|
53
53
|
"./src/ui/sonner.css"
|
|
54
54
|
],
|
|
55
|
-
"scripts": {
|
|
56
|
-
"check:tokens": "node tokens/scripts/check-component-tokens.mjs",
|
|
57
|
-
"build": "node tokens/scripts/check-exports.mjs && node tokens/scripts/check-component-tokens.mjs && tsc -p tsconfig.json",
|
|
58
|
-
"typecheck": "tsc -p tsconfig.json && tsc -p tokens/tsconfig.json",
|
|
59
|
-
"test": "node tokens/scripts/check-component-tokens.mjs && vitest run && node --test tokens/test/*.test.mjs",
|
|
60
|
-
"verify": "node tokens/scripts/check-component-tokens.mjs && node tokens/verify/run.mjs",
|
|
61
|
-
"analyze": "node tokens/verify/analyze.mjs",
|
|
62
|
-
"prepack": "node tokens/scripts/check-exports.mjs"
|
|
63
|
-
},
|
|
64
55
|
"peerDependencies": {
|
|
65
56
|
"@clerk/clerk-react": ">=5",
|
|
66
57
|
"@radix-ui/react-accordion": "^1.2.12",
|
|
@@ -140,10 +131,12 @@
|
|
|
140
131
|
"class-variance-authority": "^0.7.1",
|
|
141
132
|
"clsx": "^2.1.1",
|
|
142
133
|
"cmdk": "^1.1.1",
|
|
134
|
+
"dayjs": "1.11.19",
|
|
143
135
|
"embla-carousel-react": "^8.6.0",
|
|
144
136
|
"happy-dom": "^18.0.0",
|
|
145
137
|
"motion": "^12.0.0",
|
|
146
138
|
"nanoid": "^5.1.0",
|
|
139
|
+
"next-themes": "0.4.6",
|
|
147
140
|
"radix-ui": "^1.4.0",
|
|
148
141
|
"react": "^19.1.0",
|
|
149
142
|
"react-dom": "^19.1.0",
|
|
@@ -171,5 +164,13 @@
|
|
|
171
164
|
"zod": {
|
|
172
165
|
"optional": true
|
|
173
166
|
}
|
|
167
|
+
},
|
|
168
|
+
"scripts": {
|
|
169
|
+
"check:tokens": "node tokens/scripts/check-component-tokens.mjs",
|
|
170
|
+
"build": "node tokens/scripts/check-exports.mjs && node tokens/scripts/check-component-tokens.mjs && tsc -p tsconfig.json",
|
|
171
|
+
"typecheck": "tsc -p tsconfig.json && tsc -p tokens/tsconfig.json",
|
|
172
|
+
"test": "node tokens/scripts/check-component-tokens.mjs && vitest run && node --test tokens/test/*.test.mjs app/tests-node/*.test.mjs",
|
|
173
|
+
"verify": "node tokens/scripts/check-component-tokens.mjs && node tokens/verify/run.mjs",
|
|
174
|
+
"analyze": "node tokens/verify/analyze.mjs"
|
|
174
175
|
}
|
|
175
|
-
}
|
|
176
|
+
}
|
package/app/README.md
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
# App helpers (`@vvedantb/ui`)
|
|
2
|
-
|
|
3
|
-
Shared web React helpers that were duplicated across apps. Source-exported as the `./app` subpath of `@vvedantb/ui`.
|
|
4
|
-
|
|
5
|
-
## Install
|
|
6
|
-
|
|
7
|
-
```sh
|
|
8
|
-
npm install @vvedantb/ui
|
|
9
|
-
```
|
|
10
|
-
|
|
11
|
-
## Peers
|
|
12
|
-
|
|
13
|
-
Required: `react` and `react-dom` (>=19). Optional peers of `@vvedantb/ui`, needed only for the app helpers: `@clerk/clerk-react` (`useStableAuth`), `@tanstack/react-hotkeys` and `zod`.
|
|
14
|
-
|
|
15
|
-
`sonner` is not a dependency. The update watcher takes the toast API as an option. The app source uses a local class join, which the root does not export (the root `cn` is the clsx and tailwind-merge one). Tailwind must scan `node_modules/@vvedantb/ui/app/src/**/*.{ts,tsx}` so the class names are generated, and the app needs the `@vvedantb/ui/tokens` names (`bg-surface-card`, `divide-separator`, `text-muted-foreground` and so on).
|
|
16
|
-
|
|
17
|
-
```css
|
|
18
|
-
@source "../node_modules/@vvedantb/ui/app/src/**/*.{ts,tsx}";
|
|
19
|
-
```
|
|
20
|
-
|
|
21
|
-
## Confirm skip (Alt-click)
|
|
22
|
-
|
|
23
|
-
```tsx
|
|
24
|
-
import { ConfirmSkipHint, skipConfirmTitle, useRequestConfirm } from "@vvedantb/ui";
|
|
25
|
-
|
|
26
|
-
const { request } = useRequestConfirm();
|
|
27
|
-
|
|
28
|
-
<DropdownMenuItem
|
|
29
|
-
title={skipConfirmTitle("Delete")}
|
|
30
|
-
onSelect={(event) => request(() => setOpen(true), () => remove(), event)}
|
|
31
|
-
>
|
|
32
|
-
Delete <ConfirmSkipHint />
|
|
33
|
-
</DropdownMenuItem>
|
|
34
|
-
```
|
|
35
|
-
|
|
36
|
-
- `useRequestConfirm` does not subscribe to Alt. It reads `getAltHeld()` when the request happens, so the menu owner does not re-render when Alt goes down. It returns `{ request }` only.
|
|
37
|
-
- `ConfirmSkipHint` stays mounted and gains a `hidden` class when Alt is not held, so an open Radix menu keeps a stable child tree.
|
|
38
|
-
- The Alt store resets when the last subscriber leaves, clears on blur and tab hide, and cancels a bare Alt keydown only inside `[role='menu']` or `[data-radix-menu-content]`.
|
|
39
|
-
- Also exported: `useAltHeld`, `getAltHeld`, `createAltHeldStore`, `bindToWindow`, `shouldSkipConfirm`, `requestConfirm`.
|
|
40
|
-
|
|
41
|
-
## Update available watcher
|
|
42
|
-
|
|
43
|
-
```ts
|
|
44
|
-
import { toast } from "sonner";
|
|
45
|
-
import { startUpdateAvailableWatcher } from "@vvedantb/ui";
|
|
46
|
-
|
|
47
|
-
useEffect(
|
|
48
|
-
() =>
|
|
49
|
-
startUpdateAvailableWatcher({
|
|
50
|
-
origin: window.location.origin,
|
|
51
|
-
toast,
|
|
52
|
-
closeConvex: () => void convex.close(),
|
|
53
|
-
enabled: !import.meta.env.DEV,
|
|
54
|
-
}),
|
|
55
|
-
[],
|
|
56
|
-
);
|
|
57
|
-
```
|
|
58
|
-
|
|
59
|
-
Options: `origin`, `toast`, `closeConvex?`, `pollMs?` (default 60000), `toastId?` (default `app-update-available`), `enabled?` (default true), `reload?` (default `window.location.reload`). On a changed asset fingerprint it shows a sticky "Update available" toast. Refresh calls `closeConvex` (errors ignored), then reloads. `fingerprintFromHtml` and `fetchDeployFingerprint` are exported too. The fingerprint looks for Vite `/assets/*.js` URLs in the index HTML.
|
|
60
|
-
|
|
61
|
-
## Settings chrome
|
|
62
|
-
|
|
63
|
-
```tsx
|
|
64
|
-
import { SettingsPage, SettingsSection, SettingsToggleRow } from "@vvedantb/ui";
|
|
65
|
-
|
|
66
|
-
<SettingsPage
|
|
67
|
-
title="Notifications"
|
|
68
|
-
layout={({ title, headerRight, toolbar, tabs, fillHeight, children }) => (
|
|
69
|
-
<PageContainer title={title} rightSection={headerRight} toolbar={toolbar} tabs={tabs} noScroll={fillHeight}>
|
|
70
|
-
{children}
|
|
71
|
-
</PageContainer>
|
|
72
|
-
)}
|
|
73
|
-
>
|
|
74
|
-
<SettingsSection title="Email" bodyVariant="list">
|
|
75
|
-
<SettingsToggleRow title="Weekly digest" action={<Switch />} />
|
|
76
|
-
</SettingsSection>
|
|
77
|
-
</SettingsPage>
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
Exports: `SettingsSection`, `SettingsField`, `SettingsStack`, `SettingsToggleRow`, `SettingsEmptyState` (icon is `ComponentType<{ size?: number; className?: string }>`), `SettingsPage`. `SettingsField` `layout`: `"stacked"` (default) puts the label above the control. `"horizontal"` renders one row with the label and description on the left and the control on the right. Without `layout`, `SettingsPage` renders a plain `div` with a heading and the optional slots.
|
|
81
|
-
|
|
82
|
-
## useStableAuth
|
|
83
|
-
|
|
84
|
-
```ts
|
|
85
|
-
import { useStableAuth } from "@vvedantb/ui"; // 2000 ms debounce, wraps Clerk useAuth
|
|
86
|
-
import { createUseStableAuth } from "@vvedantb/ui";
|
|
87
|
-
|
|
88
|
-
const useAuth = createUseStableAuth(useClerkAuth, { debounceMs: 3000 });
|
|
89
|
-
```
|
|
90
|
-
|
|
91
|
-
After a signed-in session, a drop to signed-out reports `isLoaded: false` for the debounce window so Convex does not cascade. State is module-level. `resetStableAuthSession()` clears it and is for tests only.
|
|
92
|
-
|
|
93
|
-
## Develop
|
|
94
|
-
|
|
95
|
-
```sh
|
|
96
|
-
pnpm --filter @vvedantb/ui typecheck
|
|
97
|
-
pnpm --filter @vvedantb/ui test
|
|
98
|
-
```
|
|
99
|
-
|
|
100
|
-
Source is in `app/src` and tests in `app/tests`. Publish steps are in the root `README.md`.
|
package/tokens/README.md
DELETED
|
@@ -1,98 +0,0 @@
|
|
|
1
|
-
# `@vvedantb/ui/tokens`
|
|
2
|
-
|
|
3
|
-
Design tokens and shared Tailwind v4 CSS for the vvedantb web apps. The CSS and Tailwind preset are subpaths of `@vvedantb/ui`; the native JS tokens are named exports of the root (`tokens`, `Tokens`). Source-exported: no build step. Paths in the `File` column are relative to `packages/ui/tokens/`.
|
|
4
|
-
|
|
5
|
-
## Exports
|
|
6
|
-
|
|
7
|
-
| Export | File | What it is |
|
|
8
|
-
| --- | --- | --- |
|
|
9
|
-
| `@vvedantb/ui/tokens`, `@vvedantb/ui/tokens/tokens.css` | `src/tokens.css` | Colour, radius, shadow, ease and font tokens (`@theme`), light/dark variables, `dark` variant, the radius utilities `rounded-surface` (`clamp(0.75rem, var(--radius), 1rem)`), `rounded-menu-item` (`min(var(--radius), 0.5rem)`) and `rounded-control` (`min(var(--radius), 0.625rem)`) (0.7.1; `rounded-surface` was in `utilities.css` before), `*` border and `body` defaults. 0.7.2 adds `--sidebar-accent` / `sidebar-accent` (sidebar nav row fill: light `var(--surface-tertiary)`, dark `oklch(22.64% 0 0)`). In 0.2.1 it also defines shadcn-style aliases (`primary`, `muted-foreground`, `secondary`, `destructive`, `card`, `popover`, `input`, `ring`) that point at the same tokens. |
|
|
10
|
-
| `tokens` and `Tokens` from `@vvedantb/ui` | `src/native.ts` | JS colours, radii and spacing for React Native. `@vvedantb/native-ui` keeps its own copy in `src/tokens.ts`; a test keeps the two equal. |
|
|
11
|
-
| `@vvedantb/ui/tokens/css/base.css` | `src/css/base.css` | `html` touch/accent, `::selection`, control transitions, pointer cursor, range accent (`@layer base`). |
|
|
12
|
-
| `@vvedantb/ui/tokens/css/dialog.css` | `src/css/dialog.css` | Unlayered `max-width: none` for `@vvedantb/ui` compact (bottom-sheet) dialogs. |
|
|
13
|
-
| `@vvedantb/ui/tokens/css/modal.css` | `src/css/modal.css` | `.t-modal`, `.is-open`, `.is-closing` and `t-modal-presence-wait` (`@layer components`). |
|
|
14
|
-
| `@vvedantb/ui/tokens/css/tabs.css` | `src/css/tabs.css` | `.t-tab`, active colour, `.t-tabs-pill`, small-screen `.t-tabs` scrolling. |
|
|
15
|
-
| `@vvedantb/ui/tokens/css/tabs-pills.css` | `src/css/tabs-pills.css` | `--tabs-*` colour variables, `.t-tabs--segmented`, `.t-tabs--pills` and pill hover / active fills. |
|
|
16
|
-
| `@vvedantb/ui/tokens/css/utilities.css` | `src/css/utilities.css` | `@utility hit-target` and `@utility reveal-on-hover`. `rounded-surface` moved to `tokens.css` in 0.7.1. |
|
|
17
|
-
| `@vvedantb/ui/tokens/css/scrollbar-thin.css` | `src/css/scrollbar-thin.css` | `.scrollbar-thin` (`@layer utilities`, always emitted). |
|
|
18
|
-
| `@vvedantb/ui/tokens/css/animations.css` | `src/css/animations.css` | `animate-aurora`, `animate-fade-in-up`, `animate-indeterminate` as `@theme` (CSS-first apps). |
|
|
19
|
-
| `@vvedantb/ui/tokens/css/compat-shadcn-rgb.css`, `@vvedantb/ui/tokens/compat/shadcn-rgb.css` | `src/css/compat-shadcn-rgb.css` | Replaces `tokens.css` for apps with shadcn-style `R G B` channel variables. Defines only the UI-only `--color-*` names as `rgb(var(--x) / alpha)` references, `--radius-field`, `--scrollbar`, `--field-radius`, the same three radius utilities as `tokens.css` and a `glass-panel-strong` fallback. Import it **or** `tokens.css`, never both. |
|
|
20
|
-
| `@vvedantb/ui/tokens/css/index.css` | `src/css/index.css` | base + dialog + utilities + scrollbar-thin + animations, for new CSS-first apps. |
|
|
21
|
-
| `@vvedantb/ui/tokens/tailwind-preset` | `src/tailwind-preset.mjs` (+ `.d.mts`) | Tailwind preset for apps that load a JS config with `@config`. |
|
|
22
|
-
|
|
23
|
-
Every file was extracted because the same rule (same at-rule context, selector and body) sits in 3 or more of the five apps. The measured duplication is in `verify/ANALYSIS.md`, and the proof that each app compiles to the same CSS after migrating is in `verify/RESULTS.md`. Blocks that only 2 apps share, or that drift in a way that would change an app's CSS, stay in the apps; `verify/ANALYSIS.md` lists them.
|
|
24
|
-
|
|
25
|
-
## Use
|
|
26
|
-
|
|
27
|
-
CSS-first app (no `tailwind.config`):
|
|
28
|
-
|
|
29
|
-
```css
|
|
30
|
-
@import "tailwindcss";
|
|
31
|
-
@import "@vvedantb/ui/tokens";
|
|
32
|
-
@import "@vvedantb/ui/tokens/css/animations.css";
|
|
33
|
-
@import "@vvedantb/ui/tokens/css/base.css";
|
|
34
|
-
/* ...app @layer base rules... */
|
|
35
|
-
@import "@vvedantb/ui/tokens/css/modal.css";
|
|
36
|
-
@import "@vvedantb/ui/tokens/css/tabs.css";
|
|
37
|
-
/* ...app @layer components rules... */
|
|
38
|
-
```
|
|
39
|
-
|
|
40
|
-
App with a JS config (`@config "../tailwind.config.ts"`):
|
|
41
|
-
|
|
42
|
-
```ts
|
|
43
|
-
import { tokensPreset } from "@vvedantb/ui/tokens/tailwind-preset";
|
|
44
|
-
|
|
45
|
-
export default {
|
|
46
|
-
presets: [tokensPreset()],
|
|
47
|
-
// or tokensPreset({ color: (name) => `color-mix(in oklch, var(--${name}) calc(<alpha-value> * 100%), transparent)` })
|
|
48
|
-
content: ["./src/**/*.{ts,tsx}"],
|
|
49
|
-
theme: { extend: { /* app-only colours, fonts, animations */ } },
|
|
50
|
-
plugins: [tailwindcssAnimate],
|
|
51
|
-
};
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
The preset holds `darkMode: "class"`, the shared colour map (`border`, `surface.*`, `danger.*`, `field.*`, shadcn aliases `primary`/`secondary`/`destructive`/`card`/`popover`/`input` ...), `borderRadius` lg/md/sm/field, `boxShadow` soft/panel/insetSoft, `ease-smooth`, `screens` xs to 2xl in px, and the aurora / fade-in-up / indeterminate / accordion keyframes. It does not hold plugins, `content`, fonts, `accent`/`muted`/`ring`/`sidebar` (they differ per app) or accordion timings.
|
|
55
|
-
|
|
56
|
-
Do not import `css/animations.css` in an app that uses the preset: the preset already defines the same keyframes and animations.
|
|
57
|
-
|
|
58
|
-
### Where to put the imports
|
|
59
|
-
|
|
60
|
-
Tailwind inlines an `@import` where it sits, even in the middle of a file (it must be at top level, not inside an `@layer` block). Put each import where the app's own copy of the rules was. Within one layer, later rules win at equal specificity, so moving shared rules above or below app rules can flip the result for elements matched by both. `examples/migration.md` gives the exact position for each app, and `pnpm --filter @vvedantb/ui verify` checks it.
|
|
61
|
-
|
|
62
|
-
### Variables each file needs
|
|
63
|
-
|
|
64
|
-
| File | Needs | Provided by |
|
|
65
|
-
| --- | --- | --- |
|
|
66
|
-
| `base.css` | `--accent`, `--foreground` | tokens.css or the app theme |
|
|
67
|
-
| `dialog.css` | none | |
|
|
68
|
-
| `modal.css` | `--modal-open-dur`, `--modal-close-dur`, `--modal-scale`, `--modal-scale-close`, `--modal-ease` | app `:root` |
|
|
69
|
-
| `tabs.css` | `--tabs-dur`, `--tabs-ease` (app `:root`); `--tabs-text-muted`, `--tabs-text-active`, `--tabs-pill-bg` | colours: tabs-pills.css or the app theme |
|
|
70
|
-
| `tabs-pills.css` | `--muted`, `--segment`, `--segment-foreground`, `--default` | tokens.css or the app theme |
|
|
71
|
-
| `utilities.css` | `--breakpoint-sm` (Tailwind default or app theme) | Tailwind |
|
|
72
|
-
| `scrollbar-thin.css` | `--scrollbar` | tokens.css or the app theme |
|
|
73
|
-
| `animations.css` | none | |
|
|
74
|
-
| `tailwind-preset` | the CSS variables named in its colour map, `--radius`, `--field-radius` | tokens.css or the app theme |
|
|
75
|
-
|
|
76
|
-
### Using an existing shadcn RGB theme
|
|
77
|
-
|
|
78
|
-
See the package `README.md`. With the compat file, `base.css`, `scrollbar-thin.css`, `utilities.css`, `dialog.css`, `modal.css` and `tabs.css` need no `tokens.css`. `base.css` reads `--accent` and `--foreground` and `tabs-pills.css` reads `--muted`, `--segment` and `--default`. Those are not channel-compatible, so skip `tabs-pills.css` and `index.css` (which includes `base.css`) unless the app defines those variables as full colours.
|
|
79
|
-
|
|
80
|
-
### Parameters
|
|
81
|
-
|
|
82
|
-
Where apps share a rule but drifted in one value, the value is a custom property whose fallback is the most common value. Set it on `:root` to keep a different value.
|
|
83
|
-
|
|
84
|
-
| File | Property | Default | Set by |
|
|
85
|
-
| --- | --- | --- | --- |
|
|
86
|
-
| `base.css` | `--tokens-control-transition-property` | `background-color, border-color, color, box-shadow, transform, opacity` | app-c: `background-color, color, transform, opacity` |
|
|
87
|
-
| `base.css` | `--tokens-control-transition-duration` | `240ms` | app-c: `100ms` |
|
|
88
|
-
|
|
89
|
-
## Develop
|
|
90
|
-
|
|
91
|
-
```sh
|
|
92
|
-
pnpm --filter @vvedantb/ui build # export targets exist + tsc (primitives, shell, app)
|
|
93
|
-
pnpm --filter @vvedantb/ui test # vitest (app helpers) + offline node --test: exports, CSS structure, Tailwind compile, preset
|
|
94
|
-
pnpm --filter @vvedantb/ui analyze # rule-level duplication across the app clones -> verify/ANALYSIS.tables.md
|
|
95
|
-
pnpm --filter @vvedantb/ui verify # before/after compile of every app -> verify/RESULTS.md, examples/migration.md
|
|
96
|
-
```
|
|
97
|
-
|
|
98
|
-
`analyze` and `verify` need the app clones (`APPS_DIR`, default `/workspace/cvx`; name or symlink the clones `app-a` to `app-e` inside it) and network access the first time: they install Tailwind 4.3.3, the plugins, postcss and jiti into a scratch directory (`VERIFY_DIR`, default `/workspace/pk-tokens-verify`, outside the repo). The migration for each app is data in `verify/migrations/<app>.json`. Each removed block must equal the shared file's block with the same context and selector, or the run fails.
|