@react-native-rethemed/radix-ui-tokens 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/LICENSE +21 -0
- package/README.md +63 -0
- package/dist/cjs/index.d.ts +2 -0
- package/dist/cjs/index.js +10 -0
- package/dist/cjs/package.json +1 -0
- package/dist/cjs/theme.d.ts +53 -0
- package/dist/cjs/theme.js +161 -0
- package/dist/cjs/tokens.gen.d.ts +3695 -0
- package/dist/cjs/tokens.gen.js +1056 -0
- package/dist/esm/index.d.ts +2 -0
- package/dist/esm/index.js +2 -0
- package/dist/esm/package.json +1 -0
- package/dist/esm/theme.d.ts +53 -0
- package/dist/esm/theme.js +156 -0
- package/dist/esm/tokens.gen.d.ts +3695 -0
- package/dist/esm/tokens.gen.js +1053 -0
- package/package.json +74 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"module"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { type ThemeConfig } from '@react-native-rethemed/core/config';
|
|
2
|
+
import { radiusFactors, scales, scalings } from './tokens.gen.js';
|
|
3
|
+
/** Every Radix color scale. */
|
|
4
|
+
export type ScaleName = keyof typeof scales;
|
|
5
|
+
/** `<Theme accentColor>`: every scale except the five tinted grays. */
|
|
6
|
+
export type AccentColor = Exclude<ScaleName, 'mauve' | 'slate' | 'sage' | 'olive' | 'sand'>;
|
|
7
|
+
/** `<Theme grayColor>`. `auto` picks the gray that matches the accent. */
|
|
8
|
+
export type GrayColor = 'auto' | Extract<ScaleName, 'gray' | 'mauve' | 'slate' | 'sage' | 'olive' | 'sand'>;
|
|
9
|
+
export type Radius = keyof typeof radiusFactors;
|
|
10
|
+
export type Scaling = keyof typeof scalings;
|
|
11
|
+
export type RadixUiThemeOptions = {
|
|
12
|
+
/** Default `indigo`. */
|
|
13
|
+
accentColor?: AccentColor;
|
|
14
|
+
/** Default `auto`. */
|
|
15
|
+
grayColor?: GrayColor;
|
|
16
|
+
/** Default `medium`. */
|
|
17
|
+
radius?: Radius;
|
|
18
|
+
/** Default `100%`. */
|
|
19
|
+
scaling?: Scaling;
|
|
20
|
+
/**
|
|
21
|
+
* More scales to use by name (`'red.9'`, `'green.a3'`), e.g. for status
|
|
22
|
+
* colors. `accent`, `gray` and `color` are always included; every scale
|
|
23
|
+
* would add ~870 colors to the generated types, so pick what you use.
|
|
24
|
+
*/
|
|
25
|
+
colors?: Exclude<ScaleName, 'gray'>[];
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* The gray `grayColor: 'auto'` picks for an accent, as Radix Themes'
|
|
29
|
+
* `getMatchingGrayColor` does.
|
|
30
|
+
*/
|
|
31
|
+
export declare function getMatchingGrayColor(accentColor: AccentColor): Exclude<GrayColor, 'auto'>;
|
|
32
|
+
/**
|
|
33
|
+
* A Radix Themes theme, configured like `<Theme>`:
|
|
34
|
+
*
|
|
35
|
+
* ```ts
|
|
36
|
+
* createRadixUiTheme({ accentColor: 'blue', radius: 'large', colors: ['red'] })
|
|
37
|
+
* ```
|
|
38
|
+
*
|
|
39
|
+
* - `semanticTokens.colors`: `accent` and `gray` (each `1`–`12`, `a1`–`a12`,
|
|
40
|
+
* `contrast`, `surface`, `indicator`, `track`), `color` (`background`,
|
|
41
|
+
* `panel-solid`, `panel-translucent`, `surface`, `overlay`), plus the
|
|
42
|
+
* scales in `colors`. Every one switches with light/dark.
|
|
43
|
+
* - `spacing` (`1`–`9`), `fontSizes` (`1`–`9`) and `radii` (`1`–`6`,
|
|
44
|
+
* `full`) follow `scaling` and `radius`.
|
|
45
|
+
* - Text presets: `themed.text.text[3]()` is `<Text size="3">` and
|
|
46
|
+
* `themed.text.heading[3]()` is `<Heading size="3">` (bold, tighter line
|
|
47
|
+
* height), both with Radix's letter spacing.
|
|
48
|
+
* - `shadows` `2`–`6`: one layer each, light-mode values; `1` is inset and
|
|
49
|
+
* left out.
|
|
50
|
+
*/
|
|
51
|
+
export declare function createRadixUiTheme(options?: RadixUiThemeOptions): ThemeConfig;
|
|
52
|
+
/** Radix Themes' defaults: `indigo` accent, `slate` gray, `medium` radius, `100%` scaling. */
|
|
53
|
+
export declare const radixUiTheme: ThemeConfig;
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { defineTheme, } from '@react-native-rethemed/core/config';
|
|
2
|
+
import { backgroundColors, fontSizes, fontWeights, headingLetterSpacing, headingLineHeights, letterSpacings, lineHeights, radii, radiusFactors, radiusFull, scales, scalings, shadows, space, } from './tokens.gen.js';
|
|
3
|
+
/**
|
|
4
|
+
* The gray `grayColor: 'auto'` picks for an accent, as Radix Themes'
|
|
5
|
+
* `getMatchingGrayColor` does.
|
|
6
|
+
*/
|
|
7
|
+
export function getMatchingGrayColor(accentColor) {
|
|
8
|
+
switch (accentColor) {
|
|
9
|
+
case 'tomato':
|
|
10
|
+
case 'red':
|
|
11
|
+
case 'ruby':
|
|
12
|
+
case 'crimson':
|
|
13
|
+
case 'pink':
|
|
14
|
+
case 'plum':
|
|
15
|
+
case 'purple':
|
|
16
|
+
case 'violet':
|
|
17
|
+
return 'mauve';
|
|
18
|
+
case 'iris':
|
|
19
|
+
case 'indigo':
|
|
20
|
+
case 'blue':
|
|
21
|
+
case 'sky':
|
|
22
|
+
case 'cyan':
|
|
23
|
+
return 'slate';
|
|
24
|
+
case 'teal':
|
|
25
|
+
case 'jade':
|
|
26
|
+
case 'mint':
|
|
27
|
+
case 'green':
|
|
28
|
+
return 'sage';
|
|
29
|
+
case 'grass':
|
|
30
|
+
case 'lime':
|
|
31
|
+
return 'olive';
|
|
32
|
+
case 'yellow':
|
|
33
|
+
case 'amber':
|
|
34
|
+
case 'orange':
|
|
35
|
+
case 'brown':
|
|
36
|
+
case 'gold':
|
|
37
|
+
case 'bronze':
|
|
38
|
+
return 'sand';
|
|
39
|
+
case 'gray':
|
|
40
|
+
return 'gray';
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
const round = (n, digits = 2) => {
|
|
44
|
+
const factor = 10 ** digits;
|
|
45
|
+
return Math.round(n * factor) / factor;
|
|
46
|
+
};
|
|
47
|
+
const scaled = (table, factor) => Object.fromEntries(Object.entries(table).map(([key, value]) => [key, round(value * factor)]));
|
|
48
|
+
/** `'gray.a2'` → that step of the theme's gray in `scheme`; hex as-is. */
|
|
49
|
+
function grayRef(value, gray, scheme) {
|
|
50
|
+
return value.startsWith('gray.') ? gray[value.slice(5)][scheme] : value;
|
|
51
|
+
}
|
|
52
|
+
/** `#rrggbbaa` → `{ color: '#rrggbb', opacity }`. */
|
|
53
|
+
function splitAlpha(hex) {
|
|
54
|
+
if (hex.length !== 9)
|
|
55
|
+
return { color: hex, opacity: 1 };
|
|
56
|
+
return {
|
|
57
|
+
color: hex.slice(0, 7),
|
|
58
|
+
opacity: round(Number.parseInt(hex.slice(7), 16) / 255, 3),
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
/** Android elevation per shadow level; CSS has none, so picked by hand. */
|
|
62
|
+
const SHADOW_ELEVATIONS = {
|
|
63
|
+
2: 2,
|
|
64
|
+
3: 4,
|
|
65
|
+
4: 8,
|
|
66
|
+
5: 12,
|
|
67
|
+
6: 16,
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* A Radix Themes theme, configured like `<Theme>`:
|
|
71
|
+
*
|
|
72
|
+
* ```ts
|
|
73
|
+
* createRadixUiTheme({ accentColor: 'blue', radius: 'large', colors: ['red'] })
|
|
74
|
+
* ```
|
|
75
|
+
*
|
|
76
|
+
* - `semanticTokens.colors`: `accent` and `gray` (each `1`–`12`, `a1`–`a12`,
|
|
77
|
+
* `contrast`, `surface`, `indicator`, `track`), `color` (`background`,
|
|
78
|
+
* `panel-solid`, `panel-translucent`, `surface`, `overlay`), plus the
|
|
79
|
+
* scales in `colors`. Every one switches with light/dark.
|
|
80
|
+
* - `spacing` (`1`–`9`), `fontSizes` (`1`–`9`) and `radii` (`1`–`6`,
|
|
81
|
+
* `full`) follow `scaling` and `radius`.
|
|
82
|
+
* - Text presets: `themed.text.text[3]()` is `<Text size="3">` and
|
|
83
|
+
* `themed.text.heading[3]()` is `<Heading size="3">` (bold, tighter line
|
|
84
|
+
* height), both with Radix's letter spacing.
|
|
85
|
+
* - `shadows` `2`–`6`: one layer each, light-mode values; `1` is inset and
|
|
86
|
+
* left out.
|
|
87
|
+
*/
|
|
88
|
+
export function createRadixUiTheme(options = {}) {
|
|
89
|
+
const { accentColor = 'indigo', grayColor = 'auto', radius = 'medium', scaling = '100%', colors = [], } = options;
|
|
90
|
+
const factor = scalings[scaling];
|
|
91
|
+
const gray = scales[grayColor === 'auto' ? getMatchingGrayColor(accentColor) : grayColor];
|
|
92
|
+
const sizes = fontSizes;
|
|
93
|
+
const spacingsEm = letterSpacings;
|
|
94
|
+
const preset = (size, lineHeight, extraEm = 0) => {
|
|
95
|
+
const fontSize = round(sizes[size] * factor);
|
|
96
|
+
return {
|
|
97
|
+
fontSize,
|
|
98
|
+
lineHeight: round(lineHeight * factor),
|
|
99
|
+
letterSpacing: round((spacingsEm[size] + extraEm) * fontSize, 3),
|
|
100
|
+
};
|
|
101
|
+
};
|
|
102
|
+
return defineTheme({
|
|
103
|
+
tokens: {
|
|
104
|
+
spacing: scaled(space, factor),
|
|
105
|
+
radii: {
|
|
106
|
+
...scaled(radii, factor * radiusFactors[radius]),
|
|
107
|
+
full: radiusFull[radius],
|
|
108
|
+
},
|
|
109
|
+
fontSizes: scaled(fontSizes, factor),
|
|
110
|
+
fontWeights,
|
|
111
|
+
shadows: Object.fromEntries(Object.entries(shadows).map(([key, layer]) => {
|
|
112
|
+
const { color, opacity } = splitAlpha(grayRef(layer.color, gray, 'light'));
|
|
113
|
+
return [
|
|
114
|
+
key,
|
|
115
|
+
{
|
|
116
|
+
shadowColor: color,
|
|
117
|
+
shadowOffset: { width: layer.x, height: layer.y },
|
|
118
|
+
shadowOpacity: opacity,
|
|
119
|
+
shadowRadius: layer.blur / 2,
|
|
120
|
+
elevation: SHADOW_ELEVATIONS[key],
|
|
121
|
+
},
|
|
122
|
+
];
|
|
123
|
+
})),
|
|
124
|
+
},
|
|
125
|
+
semanticTokens: {
|
|
126
|
+
colors: {
|
|
127
|
+
accent: scales[accentColor],
|
|
128
|
+
gray,
|
|
129
|
+
color: Object.fromEntries(Object.entries(backgroundColors).map(([key, value]) => [
|
|
130
|
+
key,
|
|
131
|
+
{
|
|
132
|
+
light: grayRef(value.light, gray, 'light'),
|
|
133
|
+
dark: grayRef(value.dark, gray, 'dark'),
|
|
134
|
+
},
|
|
135
|
+
])),
|
|
136
|
+
...Object.fromEntries(colors.map((name) => [name, scales[name]])),
|
|
137
|
+
},
|
|
138
|
+
text: {
|
|
139
|
+
text: Object.fromEntries(Object.entries(lineHeights).map(([size, lineHeight]) => [
|
|
140
|
+
size,
|
|
141
|
+
preset(size, lineHeight),
|
|
142
|
+
])),
|
|
143
|
+
heading: Object.fromEntries(Object.entries(headingLineHeights).map(([size, lineHeight]) => [
|
|
144
|
+
size,
|
|
145
|
+
{
|
|
146
|
+
...preset(size, lineHeight, headingLetterSpacing),
|
|
147
|
+
fontWeight: 'bold',
|
|
148
|
+
},
|
|
149
|
+
])),
|
|
150
|
+
},
|
|
151
|
+
},
|
|
152
|
+
defaults: { fontSize: '3' },
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/** Radix Themes' defaults: `indigo` accent, `slate` gray, `medium` radius, `100%` scaling. */
|
|
156
|
+
export const radixUiTheme = createRadixUiTheme();
|