@va-scheduling/ui 0.1.0-dev.1 → 0.1.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 +129 -0
- package/dist/cjs/index.js +1 -1
- package/dist/cjs/types/index.d.ts +1 -1
- package/dist/esm/index.js +1 -1
- package/dist/esm/types/index.d.ts +1 -1
- package/package.json +1 -1
package/README.md
ADDED
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
# @va-scheduling/ui
|
|
2
|
+
|
|
3
|
+
React component library for VA scheduling, built on [Radix Themes](https://www.radix-ui.com/themes). It provides `SplThemeProvider` (light/dark/system theming) and loads all styling for you.
|
|
4
|
+
|
|
5
|
+
## Use this package or `@va-scheduling/design-tokens`, not both
|
|
6
|
+
|
|
7
|
+
`@va-scheduling/ui` already includes everything in `@va-scheduling/design-tokens`:
|
|
8
|
+
|
|
9
|
+
- Importing `@va-scheduling/ui` loads the Radix Themes styles (`@radix-ui/themes/styles.css`) and then the `--spl-*` design tokens, in that order. The order matters so the `--spl-*` tokens win over Radix's scales.
|
|
10
|
+
- Install **one** of these:
|
|
11
|
+
- **`@va-scheduling/ui`**: React apps that use these components, `SplThemeProvider`, or Radix Themes.
|
|
12
|
+
- **`@va-scheduling/design-tokens`**: apps that only need the CSS tokens and no React components.
|
|
13
|
+
- Don't also install `design-tokens` or import `@radix-ui/themes/styles.css` in your app. Doing so risks duplicated CSS and the wrong import order.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @va-scheduling/ui
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
`react` and `react-dom` (18+) must already be in your app.
|
|
22
|
+
|
|
23
|
+
## Setup
|
|
24
|
+
|
|
25
|
+
Import the package once at your app root, then wrap your app in `SplThemeProvider`:
|
|
26
|
+
|
|
27
|
+
```tsx
|
|
28
|
+
import "@va-scheduling/ui"; // loads Radix Themes styles + --spl-* tokens
|
|
29
|
+
import { SplThemeProvider } from "@va-scheduling/ui";
|
|
30
|
+
|
|
31
|
+
root.render(
|
|
32
|
+
<SplThemeProvider>
|
|
33
|
+
<App />
|
|
34
|
+
</SplThemeProvider>,
|
|
35
|
+
);
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
The `import "@va-scheduling/ui"` line is redundant if you already import a named export from it elsewhere at the root, because the CSS loads as a side effect of the package entry.
|
|
39
|
+
|
|
40
|
+
`SplThemeProvider` does two things:
|
|
41
|
+
|
|
42
|
+
1. Applies the theme to `<html>`: sets `data-theme="light|dark"` and toggles the `dark` class, which the design tokens and Radix dark scales key off of. It does not set `color-scheme`.
|
|
43
|
+
2. Wraps children in a Radix `<Theme appearance="inherit" grayColor="slate" hasBackground={false}>`. Radix tokens such as `--space-*`, `--font-size-*` and `--shadow-*` only resolve inside this wrapper, and Radix components (Dialog, Button, ...) require it.
|
|
44
|
+
|
|
45
|
+
Then use the tokens in CSS:
|
|
46
|
+
|
|
47
|
+
```css
|
|
48
|
+
.card {
|
|
49
|
+
background: var(--spl-colors-bg-default);
|
|
50
|
+
padding: var(--space-3);
|
|
51
|
+
}
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
See the [design-tokens reference](../design-tokens/TOKEN-REFERENCE.md) for all `--spl-*` tokens.
|
|
55
|
+
|
|
56
|
+
## Theme preference
|
|
57
|
+
|
|
58
|
+
The preference is `'light' | 'dark' | 'system'`. `'system'` follows the OS/browser setting and updates live.
|
|
59
|
+
|
|
60
|
+
### Uncontrolled
|
|
61
|
+
|
|
62
|
+
The provider holds the state. Nothing is persisted.
|
|
63
|
+
|
|
64
|
+
```tsx
|
|
65
|
+
<SplThemeProvider defaultPreference="system">
|
|
66
|
+
<App />
|
|
67
|
+
</SplThemeProvider>
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
### Controlled (recommended when you persist the preference)
|
|
71
|
+
|
|
72
|
+
Your app owns storage, for example in a store or `localStorage`:
|
|
73
|
+
|
|
74
|
+
```tsx
|
|
75
|
+
<SplThemeProvider preference={preference} onPreferenceChange={setPreference}>
|
|
76
|
+
<App />
|
|
77
|
+
</SplThemeProvider>
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
### Reading and changing the theme
|
|
81
|
+
|
|
82
|
+
```tsx
|
|
83
|
+
import { useSplTheme } from "@va-scheduling/ui";
|
|
84
|
+
|
|
85
|
+
function ThemeToggle() {
|
|
86
|
+
const { preference, theme, setPreference } = useSplTheme();
|
|
87
|
+
return (
|
|
88
|
+
<button onClick={() => setPreference(theme === "dark" ? "light" : "dark")}>
|
|
89
|
+
Theme: {preference} (showing {theme})
|
|
90
|
+
</button>
|
|
91
|
+
);
|
|
92
|
+
}
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
`useSplTheme` throws if used outside `SplThemeProvider`.
|
|
96
|
+
|
|
97
|
+
| Value | Meaning |
|
|
98
|
+
| ------------ | ------------------------------------------------------------------ |
|
|
99
|
+
| `preference` | Stored choice: `'light'`, `'dark'` or `'system'` |
|
|
100
|
+
| `theme` | Theme currently applied: `'light'` or `'dark'` |
|
|
101
|
+
| `setPreference` | Updates the preference (calls `onPreferenceChange` if provided) |
|
|
102
|
+
|
|
103
|
+
### Avoiding a flash of the wrong theme
|
|
104
|
+
|
|
105
|
+
Call `applyInitialSplTheme` before first render, passing a function that reads your stored preference:
|
|
106
|
+
|
|
107
|
+
```ts
|
|
108
|
+
import { applyInitialSplTheme } from "@va-scheduling/ui";
|
|
109
|
+
|
|
110
|
+
applyInitialSplTheme(() => readMyStoredPreference()); // returns 'light' | 'dark' | 'system'
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
Other exports: `applySplTheme`, `resolveSplTheme`, `getSystemTheme`, `isSplThemePreference`, and the `SplTheme` / `SplThemePreference` types.
|
|
114
|
+
|
|
115
|
+
## Testing (Jest)
|
|
116
|
+
|
|
117
|
+
Jest can't process CSS imports. Map them to a stub:
|
|
118
|
+
|
|
119
|
+
```js
|
|
120
|
+
moduleNameMapper: { "\\.css$": "<rootDir>/test/styleMock.js" },
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
jsdom drops `var()` values, so test token availability in a real browser (for example Playwright) rather than in jsdom.
|
|
124
|
+
|
|
125
|
+
## Troubleshooting
|
|
126
|
+
|
|
127
|
+
- **Tokens or components look unstyled**: the package wasn't imported at the root, or something else loads `@radix-ui/themes/styles.css` after it. Remove the other import.
|
|
128
|
+
- **`--space-*` or `--font-size-*` are empty**: the element is outside `SplThemeProvider`.
|
|
129
|
+
- **Dark mode does nothing**: check that `<html>` gets the `dark` class, and that no other code is resetting it.
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import '@radix-ui/themes/
|
|
1
|
+
import '@radix-ui/themes/styles.css';
|
|
2
2
|
import '@va-scheduling/design-tokens/css';
|
|
3
3
|
export * from './components';
|
|
4
4
|
export { applyInitialSplTheme, applySplTheme, getSystemTheme, isSplThemePreference, resolveSplTheme, } from './theme/themeUtils';
|
package/dist/esm/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import '@radix-ui/themes/
|
|
1
|
+
import '@radix-ui/themes/styles.css';
|
|
2
2
|
import '@va-scheduling/design-tokens/css';
|
|
3
3
|
import React, { createContext, useState, useEffect, useLayoutEffect, useMemo, useContext } from 'react';
|
|
4
4
|
import { Theme } from '@radix-ui/themes';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import '@radix-ui/themes/
|
|
1
|
+
import '@radix-ui/themes/styles.css';
|
|
2
2
|
import '@va-scheduling/design-tokens/css';
|
|
3
3
|
export * from './components';
|
|
4
4
|
export { applyInitialSplTheme, applySplTheme, getSystemTheme, isSplThemePreference, resolveSplTheme, } from './theme/themeUtils';
|