@sanity/themer 0.1.0 → 0.2.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 CHANGED
@@ -1,15 +1,70 @@
1
1
  # @sanity/themer
2
2
 
3
- Recreate [Sanity Studio](https://www.sanity.io/studio) themes from the hosted Themer service locally.
4
-
5
- This package is the npm migration path off the hosted Themer service ([themer.sanity.build](https://themer.sanity.build)) — the generator runs locally, so Studio configs no longer need to import modules from a hosted URL. The `@sanity/themer/legacy` subpath generates the exact same colors the service serves. There is no root export yet.
3
+ Generate [Sanity Studio](https://www.sanity.io/studio) themes from a handful of colors.
6
4
 
7
5
  ```sh
8
6
  npm install @sanity/themer
9
7
  ```
10
8
 
9
+ ## Usage
10
+
11
+ `buildTheme` builds the same type of theme as `buildTheme` from `@sanity/ui/theme` — ready for the `theme` property of a Studio config — but takes colors instead of design tokens. Under the hood it only swaps out the color palette that `@sanity/ui/theme` otherwise fills with [`@sanity/color`](https://www.sanity.io/docs/color):
12
+
13
+ ```ts
14
+ import {buildTheme} from '@sanity/themer'
15
+ import {defineConfig} from 'sanity'
16
+
17
+ export const theme = buildTheme({
18
+ accent: '#f00', // required
19
+ text: '#727892', // optional
20
+ background: {dark: '#0d0e12', light: '#ffffff'}, // optional
21
+ contrast: 85, // optional, 15–100
22
+ })
23
+
24
+ export default defineConfig({
25
+ theme,
26
+ // ...rest of the config
27
+ })
28
+ ```
29
+
30
+ - `accent` replaces the `blue` scale of the palette, which Sanity UI uses for primary buttons, focus rings and links.
31
+ - `text` replaces the `gray` scale — text, icons, borders and neutral surfaces. When omitted it is derived from `accent`: a mostly desaturated version of it, the way the stock gray carries a hint of the stock blue.
32
+ - `background.dark` replaces `black` and `background.light` replaces `white` — the backgrounds of the two color schemes that every other color blends onto.
33
+ - `contrast` controls how strongly text and borders separate from the accent. The default `85` uses the text color as-is; `100` removes its tint entirely (a high contrast scheme with no mixing of text and accent), and lower values blend more and more of the accent into the text scale, giving text and borders more color.
34
+
35
+ A few ground rules keep the generated palette usable: the accent and text colors cannot be too dark or too light (they would mess with the rest of their scales), `background.dark` is made darker until it has enough contrast with both of them (it can never be lighter than either), and `background.light` can never be darker than either.
36
+
37
+ Called with the stock colors — like `text` and `background` in the example — the generated colors match `buildTheme()` from `@sanity/ui/theme` with no options exactly.
38
+
39
+ `buildPalette` returns the generated `@sanity/color`-shaped palette without building a theme from it, and `presets` ships the hosted Themer service presets translated to `buildTheme` options:
40
+
41
+ ```ts
42
+ import {buildTheme, presets} from '@sanity/themer'
43
+
44
+ const verdant = presets.find((preset) => preset.slug === 'verdant')
45
+ const theme = buildTheme(verdant.options)
46
+ ```
47
+
48
+ ## Studio tool
49
+
50
+ `@sanity/themer/tool` adds a themer sidebar to the Studio for these themes: presets, accent/text/background pickers and a contrast slider preview a `buildTheme` theme live on the whole Studio, plus the snippet to make it permanent:
51
+
52
+ ```ts
53
+ import {themerTool} from '@sanity/themer/tool'
54
+ import {defineConfig} from 'sanity'
55
+
56
+ export default defineConfig({
57
+ plugins: [themerTool()],
58
+ // ...rest of the config
59
+ })
60
+ ```
61
+
62
+ If the Studio already uses a `buildTheme` theme, pass the same options so the tool starts editing from them: `themerTool({config: {accent: '#1cb485'}})`.
63
+
11
64
  ## Migrating from themer.sanity.build
12
65
 
66
+ This package is also the npm migration path off the hosted Themer service ([themer.sanity.build](https://themer.sanity.build)) — the generator runs locally, so Studio configs no longer need to import modules from a hosted URL.
67
+
13
68
  `@sanity/themer/legacy` generates the exact same colors as the hosted service, with the same `createTheme`, `hues` and `theme` exports that `https://themer.sanity.build/api/hues` served. Replace the URL import with `buildThemeFromUrl` and the URL as a string:
14
69
 
15
70
  ```ts
@@ -0,0 +1,91 @@
1
+ import { t as BuildThemeOptions } from "./options-BaqJJgRs.js";
2
+ import { ColorHueKey, ColorTintKey } from "@sanity/color";
3
+ import { RootTheme } from "@sanity/ui/theme";
4
+ /**
5
+ * The color palette generated by {@link buildPalette}: the `palette` config
6
+ * of `buildTheme` from `@sanity/ui/theme`, with every color as a hex string.
7
+ *
8
+ * @public
9
+ */
10
+ type GeneratedColorPalette = {
11
+ black: string;
12
+ white: string;
13
+ } & Record<ColorHueKey, Record<ColorTintKey, string>>;
14
+ /**
15
+ * Generates a Sanity UI color palette from the theme options: the accent
16
+ * color replaces the `blue` scale, the text color the `gray` scale, and the
17
+ * backgrounds replace `black` and `white`. All other hues keep their stock
18
+ * `@sanity/color` values. Called with the stock colors, it returns the stock
19
+ * palette byte-for-byte.
20
+ *
21
+ * ```ts
22
+ * import {buildPalette} from '@sanity/themer'
23
+ *
24
+ * const palette = buildPalette({accent: '#556bfc'})
25
+ * ```
26
+ *
27
+ * @public
28
+ */
29
+ declare function buildPalette(options: BuildThemeOptions): GeneratedColorPalette;
30
+ /**
31
+ * Generates a Studio theme from a handful of colors, by replacing the color
32
+ * palette that the `buildTheme` from `@sanity/ui/theme` otherwise fills with
33
+ * `@sanity/color`: the accent color replaces the `blue` scale (primary
34
+ * buttons, focus rings, links), the optional text color the `gray` scale
35
+ * (text, icons, borders), and the optional backgrounds replace `black` and
36
+ * `white`. The result is the same type of theme that `@sanity/ui/theme`
37
+ * builds, ready for the `theme` property of a Studio config.
38
+ *
39
+ * ```ts
40
+ * import {buildTheme} from '@sanity/themer'
41
+ * import {defineConfig} from 'sanity'
42
+ *
43
+ * export const theme = buildTheme({
44
+ * accent: '#556bfc',
45
+ * text: '#727892', // optional — derived from `accent` when omitted
46
+ * background: {dark: '#0d0e12', light: '#ffffff'}, // optional
47
+ * contrast: 85, // optional, 15–100
48
+ * })
49
+ *
50
+ * export default defineConfig({
51
+ * theme,
52
+ * // ...rest of the config
53
+ * })
54
+ * ```
55
+ *
56
+ * Called with the stock colors — like the example — it returns the exact
57
+ * same colors as `buildTheme()` from `@sanity/ui/theme` with no options.
58
+ *
59
+ * @public
60
+ */
61
+ declare function buildTheme(options: BuildThemeOptions): RootTheme;
62
+ /**
63
+ * A preset theme: a named set of {@link BuildThemeOptions} ready to pass to
64
+ * `buildTheme`.
65
+ *
66
+ * @public
67
+ */
68
+ interface ThemePreset {
69
+ slug: string;
70
+ title: string;
71
+ /** The preset's theme options, ready to pass to `buildTheme` */
72
+ options: BuildThemeOptions;
73
+ }
74
+ /**
75
+ * Preset themes for `buildTheme`, carried over from the hosted Themer service
76
+ * (themer.sanity.build) presets: each one maps the legacy preset's primary
77
+ * hue to `accent`, its default hue to `text` and its lightest/darkest colors
78
+ * to the backgrounds.
79
+ *
80
+ * ```ts
81
+ * import {buildTheme, presets} from '@sanity/themer'
82
+ *
83
+ * const verdant = presets.find((preset) => preset.slug === 'verdant')
84
+ * const theme = buildTheme(verdant.options)
85
+ * ```
86
+ *
87
+ * @public
88
+ */
89
+ declare const presets: ThemePreset[];
90
+ export { type BuildThemeOptions, type GeneratedColorPalette, type ThemePreset, buildPalette, buildTheme, presets };
91
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","names":[],"sources":["../src/theme/buildPalette.ts","../src/theme/buildTheme.ts","../src/theme/presets.ts"],"mappings":";;;;;;;;;KAqCY;EAAyB;EAAe;IAAiB,OACnE,aACA,OAAO;;;;;;;;;;;;;;;;iBAkBO,aAAa,SAAS,oBAAoB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCrB1C,WAAW,SAAS,oBAAoB;;;;;;;UC5BvC;EACf;EACA;;EAEA,SAAS;;;;;;;;;;;;;;;;;cAkBE,SAAS"}
package/dist/index.js ADDED
@@ -0,0 +1,2 @@
1
+ import { n as buildTheme, r as buildPalette, t as presets } from "./presets-DWzYcsLg.js";
2
+ export { buildPalette, buildTheme, presets };
package/dist/legacy.d.ts CHANGED
@@ -1,4 +1,75 @@
1
- import { a as PartialHues, i as LegacyTheme, n as HueMidPoint, o as ThemePreset, r as Hues, t as Hue } from "./types-DfPLuLv0.js";
1
+ import { RootTheme } from "@sanity/ui/theme";
2
+ /**
3
+ * The tint (between 50 and 950) that a `Hue`'s `mid` color is placed at.
4
+ *
5
+ * @public
6
+ */
7
+ type HueMidPoint = 50 | 100 | 200 | 300 | 400 | 500 | 600 | 700 | 800 | 900 | 950;
8
+ /**
9
+ * A single hue configuration, matching the hosted Themer service
10
+ * (themer.sanity.build) hue format.
11
+ *
12
+ * @public
13
+ */
14
+ interface Hue {
15
+ /** The mid color of the hue's tint ramp, as a hex color */
16
+ mid: string;
17
+ /** The tint that `mid` is placed at — the rest of the ramp is interpolated */
18
+ midPoint: HueMidPoint;
19
+ /** The hex color that the ramp's lightest end interpolates towards */
20
+ lightest: string;
21
+ /** The hex color that the ramp's darkest end interpolates towards */
22
+ darkest: string;
23
+ }
24
+ /**
25
+ * The six hues a legacy Themer theme is generated from.
26
+ *
27
+ * @public
28
+ */
29
+ interface Hues {
30
+ default: Hue;
31
+ transparent: Hue;
32
+ primary: Hue;
33
+ positive: Hue;
34
+ caution: Hue;
35
+ critical: Hue;
36
+ }
37
+ /**
38
+ * `Hues` where every hue, and every property of each hue, is optional.
39
+ * Omitted properties fall back to the default Studio theme hues.
40
+ *
41
+ * @public
42
+ */
43
+ type PartialHues = { [TKey in keyof Hues]?: Partial<Hues[TKey]>; };
44
+ /**
45
+ * A theme generated by `createTheme`, compatible with the `theme` property in
46
+ * a Sanity Studio `defineConfig`.
47
+ *
48
+ * @public
49
+ */
50
+ type LegacyTheme = RootTheme & {
51
+ /**
52
+ * Set to `undefined` so that `@sanity/ui` derives the v2 theme internally
53
+ * from the generated colors.
54
+ */
55
+ v2: undefined;
56
+ };
57
+ /**
58
+ * A preset theme, carried over from the hosted Themer service.
59
+ *
60
+ * @public
61
+ */
62
+ interface ThemePreset {
63
+ slug: string;
64
+ title: string;
65
+ /**
66
+ * The canonical search params for this preset — the same query that
67
+ * `https://themer.sanity.build/api/hues` served the preset for.
68
+ */
69
+ searchParams: string;
70
+ /** The preset's resolved hues, ready to pass to `createTheme` */
71
+ hues: Hues;
72
+ }
2
73
  /**
3
74
  * Generates a Studio theme from hues, producing the same colors as importing
4
75
  * the theme from the hosted Themer service (themer.sanity.build):
@@ -1 +1 @@
1
- {"version":3,"file":"legacy.d.ts","names":[],"sources":["../src/legacy/createTheme.ts","../src/legacy/defaults.ts","../src/legacy/presets.ts"],"mappings":";;;;;;;;;;;;;;;;;iBAsBgB,YAAY,OAAM,cAAmB;;;;;;;;;;;;;;;;;iBAoBrC,iBAAiB,cAAc,MAAM;;;;;;;;;;;;;;;;;;;iBAyBrC,kBAAkB,cAAc,MAAM;;;;;;;cCzDzC,MAAM;;;;;;;cAQN,OAAO;;;;;;;;;;;;;;cCcP,SAAS"}
1
+ {"version":3,"file":"legacy.d.ts","names":[],"sources":["../src/legacy/types.ts","../src/legacy/createTheme.ts","../src/legacy/defaults.ts","../src/legacy/presets.ts"],"mappings":";;;;;;KAOY;;;;;;;UAQK;;EAEf;;EAEA,UAAU;;EAEV;;EAEA;;;;;;;UAQe;EACf,SAAS;EACT,aAAa;EACb,SAAS;EACT,UAAU;EACV,SAAS;EACT,UAAU;;;;;;;;KASA,iBAAgB,cAAc,QAAQ,QAAQ,KAAK;;;;;;;KAQnD,cAAc;;;;;EAKxB;;;;;;;UAQe;EACf;EACA;;;;;EAKA;;EAEA,MAAM;;;;;;;;;;;;;;;;;;iBCtDQ,YAAY,OAAM,cAAmB;;;;;;;;;;;;;;;;;iBAoBrC,iBAAiB,cAAc,MAAM;;;;;;;;;;;;;;;;;;;iBAyBrC,kBAAkB,cAAc,MAAM;;;;;;;cCzDzC,MAAM;;;;;;;cAQN,OAAO;;;;;;;;;;;;;;cCcP,SAAS"}