@coralreefventures/theme 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 +60 -0
- package/package.json +42 -0
- package/src/css.ts +52 -0
- package/src/index.ts +9 -0
- package/src/theme.ts +74 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Coral Reef Ventures, LLC
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
# @coralreefventures/theme
|
|
2
|
+
|
|
3
|
+
Builds a product's Mantine theme from its brand tokens, and generates the stylesheet of CSS variables that
|
|
4
|
+
`MantineProvider` would inject for it, so a page that renders as server components only still gets the theme.
|
|
5
|
+
|
|
6
|
+
Both Coral Reef sites carried the same `theme.ts` and `generate-css.ts`, differing only in their scales, primary
|
|
7
|
+
color and font variable. Each product's `packages/theme` is now its tokens file plus a call into this package, and
|
|
8
|
+
the stylesheet it generates is byte-identical to what it generated before: a test here runs the builder over both
|
|
9
|
+
products' token files and compares the output with their committed `theme.css`.
|
|
10
|
+
|
|
11
|
+
```ts
|
|
12
|
+
// packages/theme/src/theme.ts in a product
|
|
13
|
+
import { createBrandTheme } from "@coralreefventures/theme";
|
|
14
|
+
import tokens from "@streamlane/brand/tokens";
|
|
15
|
+
|
|
16
|
+
export const brand = tokens.color;
|
|
17
|
+
|
|
18
|
+
export const theme = createBrandTheme(tokens, {
|
|
19
|
+
colors: {
|
|
20
|
+
// Ten shades, each a hex or the name of a brand color: ink is shade 8, ink-soft shade 7.
|
|
21
|
+
ink: ["#EEF4F4", "#D8E5E5", "#B3CBCC", "#8BAFB0", "#669496", "#4A7C7E", "#336567", "ink-soft", "ink", "#0B2728"],
|
|
22
|
+
signal: ["#FEF6EA", "#FCEBD0", "#FADAA5", "signal-soft", "#F4B658", "signal", "#DB9425", "#B97A17", "#94610F", "#704908"],
|
|
23
|
+
},
|
|
24
|
+
black: "text",
|
|
25
|
+
primaryColor: "ink",
|
|
26
|
+
primaryShade: { light: 8, dark: 5 },
|
|
27
|
+
fontVariable: "--streamlane-font-sans",
|
|
28
|
+
});
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
```ts
|
|
32
|
+
// packages/theme/scripts/generate-css.ts in a product
|
|
33
|
+
import { themeCss, writeThemeCss } from "@coralreefventures/theme";
|
|
34
|
+
import { cssVariablesResolver, theme } from "../src/theme";
|
|
35
|
+
|
|
36
|
+
await writeThemeCss(
|
|
37
|
+
new URL("../theme.css", import.meta.url),
|
|
38
|
+
themeCss(theme, { generatedBy: "packages/theme (pnpm --filter @streamlane/theme generate)", resolver: cssVariablesResolver }),
|
|
39
|
+
);
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
`resolver` is a product's own `CSSVariablesResolver` (Streamlane's tones and ink surface) and stays in the product;
|
|
43
|
+
Driftline passes none. `generatedBy` is the first line of the stylesheet, which tells a reader what to run.
|
|
44
|
+
|
|
45
|
+
## What it exports
|
|
46
|
+
|
|
47
|
+
- `createBrandTheme(tokens, config)`: the `MantineThemeOverride`. `config.colors` are the scales, `primaryColor` and
|
|
48
|
+
`primaryShade` as Mantine takes them, `fontVariable` the CSS variable an app sets to its web font (Mantine's system
|
|
49
|
+
stack is the fallback), and `black` the light-scheme text color, each color a hex or a brand color's name.
|
|
50
|
+
- `themeCss(theme, { generatedBy?, resolver? })`: the stylesheet.
|
|
51
|
+
- `writeThemeCss(file, css)`: writes it.
|
|
52
|
+
- `brandColor(tokens, nameOrHex)`, `brandFontFamily(variable)`, and the types `BrandTokens`, `BrandThemeConfig`,
|
|
53
|
+
`Scale`, `ThemeCssOptions`.
|
|
54
|
+
|
|
55
|
+
## Installing
|
|
56
|
+
|
|
57
|
+
This package ships TypeScript source and `@mantine/core` 9.6.3 is a peer. The code that imports it has to be
|
|
58
|
+
transpiled: a product's theme package runs under `tsx` and Vitest and is bundled by Next through
|
|
59
|
+
`transpilePackages`, all of which handle it. Add `@coralreefventures/theme` to `transpilePackages` where the product
|
|
60
|
+
theme already is.
|
package/package.json
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@coralreefventures/theme",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Builds a product's Mantine theme from its brand tokens, and the stylesheet of CSS variables MantineProvider would inject for it.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"type": "module",
|
|
7
|
+
"engines": {
|
|
8
|
+
"node": ">=24"
|
|
9
|
+
},
|
|
10
|
+
"repository": {
|
|
11
|
+
"type": "git",
|
|
12
|
+
"url": "git+https://github.com/Coral-Reef-Ventures/reef.git",
|
|
13
|
+
"directory": "packages/theme"
|
|
14
|
+
},
|
|
15
|
+
"homepage": "https://coralreefventures.com/",
|
|
16
|
+
"publishConfig": {
|
|
17
|
+
"access": "public"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"mantine",
|
|
21
|
+
"theme",
|
|
22
|
+
"design-tokens",
|
|
23
|
+
"coral-reef"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"src",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"exports": {
|
|
30
|
+
".": "./src/index.ts"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"@mantine/core": "9.6.3"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@mantine/core": "9.6.3",
|
|
37
|
+
"@mantine/hooks": "9.6.3",
|
|
38
|
+
"@types/react": "19.3.0",
|
|
39
|
+
"react": "19.3.0",
|
|
40
|
+
"react-dom": "19.3.0"
|
|
41
|
+
}
|
|
42
|
+
}
|
package/src/css.ts
ADDED
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { writeFile } from "node:fs/promises";
|
|
2
|
+
|
|
3
|
+
import {
|
|
4
|
+
type CSSVariablesResolver,
|
|
5
|
+
convertCssVariables,
|
|
6
|
+
DEFAULT_THEME,
|
|
7
|
+
defaultCssVariablesResolver,
|
|
8
|
+
type MantineThemeOverride,
|
|
9
|
+
mergeMantineTheme,
|
|
10
|
+
} from "@mantine/core";
|
|
11
|
+
|
|
12
|
+
export type ThemeCssOptions = {
|
|
13
|
+
/**
|
|
14
|
+
* Named in the stylesheet's first line so a reader knows what to run instead of editing it, such as
|
|
15
|
+
* `packages/theme (pnpm --filter <product>/theme generate)`; this package's name when nothing is given.
|
|
16
|
+
*/
|
|
17
|
+
generatedBy?: string;
|
|
18
|
+
/**
|
|
19
|
+
* The theme's own CSS variables and its corrections to Mantine's, per scheme, merged as MantineProvider merges a
|
|
20
|
+
* resolver it is given: the theme's variables over Mantine's own.
|
|
21
|
+
*/
|
|
22
|
+
resolver?: CSSVariablesResolver;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The CSS variables MantineProvider would inject for a theme, as a stylesheet. A page that renders without the
|
|
27
|
+
* provider (a server component with no client code under it) still gets the theme's colors, type, spacing and radii,
|
|
28
|
+
* and a page that renders with it loads nothing twice (`withCssVariables={false}`).
|
|
29
|
+
*/
|
|
30
|
+
export const themeCss = (theme: MantineThemeOverride, options: ThemeCssOptions = {}): string => {
|
|
31
|
+
const merged = mergeMantineTheme(DEFAULT_THEME, theme);
|
|
32
|
+
const base = defaultCssVariablesResolver(merged);
|
|
33
|
+
const own = options.resolver?.(merged) ?? { variables: {}, light: {}, dark: {} };
|
|
34
|
+
const css = convertCssVariables(
|
|
35
|
+
{
|
|
36
|
+
variables: { ...base.variables, ...own.variables },
|
|
37
|
+
light: { ...base.light, ...own.light },
|
|
38
|
+
dark: { ...base.dark, ...own.dark },
|
|
39
|
+
},
|
|
40
|
+
":root",
|
|
41
|
+
);
|
|
42
|
+
const generatedBy = options.generatedBy ?? "@coralreefventures/theme";
|
|
43
|
+
return `/* Generated by ${generatedBy}; do not edit. */\n${css.trim()}\n`;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/**
|
|
47
|
+
* Writes the stylesheet, for a product's `generate` script; its test compares the committed file with `themeCss()`
|
|
48
|
+
* and fails when the two differ, which is how a stale file is caught.
|
|
49
|
+
*/
|
|
50
|
+
export const writeThemeCss = async (file: URL | string, css: string): Promise<void> => {
|
|
51
|
+
await writeFile(file, css);
|
|
52
|
+
};
|
package/src/index.ts
ADDED
package/src/theme.ts
ADDED
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
import { createTheme, DEFAULT_THEME, type MantineColorsTuple, type MantineThemeOverride } from "@mantine/core";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* A product's brand tokens (its `packages/brand/brand.tokens.json`): the name and the colors are all this package
|
|
5
|
+
* reads. The file carries more (the mark's geometry, the lockup's proportions), which the product's own components
|
|
6
|
+
* read and this package leaves alone.
|
|
7
|
+
*/
|
|
8
|
+
export type BrandTokens = {
|
|
9
|
+
name: string;
|
|
10
|
+
color: Record<string, string>;
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/** Ten shades, each a hex color or the name of a brand color (`"ink"`, `"signal-soft"`), the brand's fixed points. */
|
|
14
|
+
export type Scale = readonly [string, string, string, string, string, string, string, string, string, string];
|
|
15
|
+
|
|
16
|
+
export type BrandThemeConfig = {
|
|
17
|
+
/** Mantine's ten-shade scales, by the name a component asks for (`c="ink"`). */
|
|
18
|
+
colors: Record<string, Scale>;
|
|
19
|
+
/** The scale primary actions are filled with. */
|
|
20
|
+
primaryColor: string;
|
|
21
|
+
/** The shade of the primary scale a filled control takes, per scheme. */
|
|
22
|
+
primaryShade: { light: MantineShade; dark: MantineShade };
|
|
23
|
+
/**
|
|
24
|
+
* The CSS variable an app sets when it loads a web font (`--streamlane-font-sans`); Mantine's system stack is the
|
|
25
|
+
* fallback, so an app that sets nothing keeps the stack it had.
|
|
26
|
+
*/
|
|
27
|
+
fontVariable: `--${string}`;
|
|
28
|
+
/** Mantine's black, the text color in the light scheme: a brand color's name or a hex. Mantine's own when unset. */
|
|
29
|
+
black?: string;
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
type MantineShade = 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9;
|
|
33
|
+
|
|
34
|
+
/** A brand color by name, or a hex as it is; an unknown name is a mistake in the config, not a color. */
|
|
35
|
+
export const brandColor = (tokens: BrandTokens, value: string): string => {
|
|
36
|
+
if (value.startsWith("#")) {
|
|
37
|
+
return value;
|
|
38
|
+
}
|
|
39
|
+
const color = tokens.color[value];
|
|
40
|
+
if (color === undefined) {
|
|
41
|
+
throw new Error(`${tokens.name}'s brand has no color named "${value}"`);
|
|
42
|
+
}
|
|
43
|
+
return color;
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
/** Mantine's system font stack behind the app's variable. */
|
|
47
|
+
export const brandFontFamily = (variable: `--${string}`) => `var(${variable}, ${DEFAULT_THEME.fontFamily})`;
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* A product's theme: its scales with the brand colors at their fixed points, the primary color and shade, and the
|
|
51
|
+
* font behind the app's variable, everything that becomes a CSS variable. It imports no stylesheet, so `themeCss`
|
|
52
|
+
* can generate the stylesheet outside a bundler, and a product adds its components' class names on top (as
|
|
53
|
+
* Streamlane's product theme does) without changing a variable.
|
|
54
|
+
*/
|
|
55
|
+
export const createBrandTheme = (tokens: BrandTokens, config: BrandThemeConfig): MantineThemeOverride => {
|
|
56
|
+
const colors = Object.fromEntries(
|
|
57
|
+
Object.entries(config.colors).map(([name, scale]) => [
|
|
58
|
+
name,
|
|
59
|
+
scale.map((shade) => brandColor(tokens, shade)) as unknown as MantineColorsTuple,
|
|
60
|
+
]),
|
|
61
|
+
);
|
|
62
|
+
if (!(config.primaryColor in colors)) {
|
|
63
|
+
throw new Error(`primaryColor "${config.primaryColor}" is not one of the theme's scales`);
|
|
64
|
+
}
|
|
65
|
+
const fontFamily = brandFontFamily(config.fontVariable);
|
|
66
|
+
return createTheme({
|
|
67
|
+
colors,
|
|
68
|
+
...(config.black !== undefined && { black: brandColor(tokens, config.black) }),
|
|
69
|
+
primaryColor: config.primaryColor,
|
|
70
|
+
primaryShade: config.primaryShade,
|
|
71
|
+
fontFamily,
|
|
72
|
+
headings: { fontFamily, fontWeight: "700" },
|
|
73
|
+
});
|
|
74
|
+
};
|