@diskette/palette 0.21.0 → 0.21.2

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/dist/cli/cli.js CHANGED
@@ -1,10 +1,10 @@
1
1
  import { defineCommand, runMain } from 'citty';
2
2
  import { existsSync, mkdirSync, writeFileSync } from 'node:fs';
3
- import { dirname, relative, resolve } from 'node:path';
3
+ import { resolve } from 'node:path';
4
4
  import * as palette from "../colors/index.js";
5
5
  import { css } from "../css.js";
6
6
  import { accentColors, grayColors } from "../types.js";
7
- import { getPackageJson } from "../utils.js";
7
+ import { getPackageJson } from "./utils.js";
8
8
  const alphaColors = ['blackAlpha', 'whiteAlpha'];
9
9
  // Combined list of all colors (deduplicated since 'gray' appears in both)
10
10
  const allColors = [
@@ -34,6 +34,11 @@ const main = defineCommand({
34
34
  alias: 'a',
35
35
  description: 'Generate accents.css with [data-accent-color] selectors',
36
36
  },
37
+ tailwind: {
38
+ type: 'boolean',
39
+ alias: 't',
40
+ description: 'Generate index.css with Tailwind v4 @theme mappings',
41
+ },
37
42
  },
38
43
  run({ args }) {
39
44
  const selected = args._.filter((c) => allColors.includes(c));
@@ -56,7 +61,10 @@ const main = defineCommand({
56
61
  const accentColorNames = selected.filter((c) => !alphaColors.includes(c));
57
62
  writeIfNotExists(`${outputPath}/accents.css`, css.accents(accentColorNames));
58
63
  }
59
- const path = relative(dirname(packageJsonPath), resolve(outputPath));
64
+ if (args.tailwind) {
65
+ const tailwindColorNames = selected.map((c) => c === 'blackAlpha' ? 'black' : c === 'whiteAlpha' ? 'white' : c);
66
+ writeIfNotExists(`${outputPath}/index.css`, css.tailwind(tailwindColorNames, { accents: args.accents }));
67
+ }
60
68
  console.log(`\nCSS saved to ${outputPath}`);
61
69
  },
62
70
  });
@@ -0,0 +1,9 @@
1
+ /**
2
+ * get package.json content from startDir
3
+ * @param cwd
4
+ * @returns package.json content
5
+ */
6
+ export declare const getPackageJson: (cwd: string) => {
7
+ pkg: any;
8
+ packageJsonPath: string;
9
+ } | undefined;
@@ -0,0 +1,44 @@
1
+ import fs from 'node:fs';
2
+ import { createRequire } from 'node:module';
3
+ import path from 'node:path';
4
+ import * as url from 'node:url';
5
+ const require = createRequire(import.meta.url);
6
+ /**
7
+ * get package.json content from startDir
8
+ * @param cwd
9
+ * @returns package.json content
10
+ */
11
+ export const getPackageJson = (cwd) => {
12
+ try {
13
+ const startDir = path.dirname(url.fileURLToPath(cwd));
14
+ const packageJsonPath = findPackageJson(startDir);
15
+ if (packageJsonPath) {
16
+ const pkg = require(packageJsonPath);
17
+ return { pkg, packageJsonPath };
18
+ }
19
+ return undefined;
20
+ }
21
+ catch (error) {
22
+ // ignore error
23
+ return undefined;
24
+ }
25
+ };
26
+ /**
27
+ * search package.json from startDir
28
+ * @param startDir
29
+ * @returns
30
+ */
31
+ const findPackageJson = (startDir) => {
32
+ let currentDir = startDir;
33
+ while (true) {
34
+ const packageJsonPath = path.join(currentDir, 'package.json');
35
+ if (fs.existsSync(packageJsonPath)) {
36
+ return packageJsonPath;
37
+ }
38
+ const parentDir = path.resolve(currentDir, '..');
39
+ if (parentDir === currentDir) {
40
+ return '';
41
+ }
42
+ currentDir = parentDir;
43
+ }
44
+ };
package/dist/css.d.ts CHANGED
@@ -13,7 +13,11 @@ declare function palette<C extends Color>(config: PaletteColor<C>, options?: Pal
13
13
  declare function alpha(config: AlphaConfig): string;
14
14
  declare function accents(colorNames: string[]): string;
15
15
  declare function grays(names: readonly string[], className: string): string;
16
- declare function tailwind(colorNames: string[]): string;
16
+ type TailwindOptions = {
17
+ /** Include @import for accents.css. Defaults to false */
18
+ accents?: boolean;
19
+ };
20
+ declare function tailwind(colorNames: string[], options?: TailwindOptions): string;
17
21
  export declare const css: {
18
22
  /**
19
23
  * Generate combined CSS for scale variables and semantic tokens
@@ -36,5 +40,5 @@ export declare const css: {
36
40
  */
37
41
  tailwind: typeof tailwind;
38
42
  };
39
- export declare function applyColorVars(prefix: 'accent' | 'gray', color: PaletteColor<Color>, theme: 'light' | 'dark', space: 'srgb' | 'p3', set: (name: string, value: string) => void): void;
43
+ export declare function applyColorVars(prefix: 'accent' | 'gray', color: PaletteColor<Color>, theme: 'light' | 'dark', space: 'srgb' | 'p3', set: (name: `--${string}`, value: string) => void): void;
40
44
  export {};
package/dist/css.js CHANGED
@@ -103,11 +103,23 @@ function grays(names, className) {
103
103
  });
104
104
  return `.${className} {\n ${blocks.join('\n\n ')}\n}\n`;
105
105
  }
106
- function tailwind(colorNames) {
106
+ function tailwind(colorNames, options = {}) {
107
+ const { accents: includeAccents = false } = options;
108
+ const imports = [];
107
109
  const blocks = [];
110
+ if (includeAccents) {
111
+ imports.push(`@import './accents.css';`);
112
+ }
108
113
  for (const name of colorNames) {
109
114
  const declarations = [];
110
115
  const isAlphaOnly = name === 'white' || name === 'black';
116
+ if (isAlphaOnly) {
117
+ imports.push(`@import './${name}-alpha.css';`);
118
+ }
119
+ else {
120
+ imports.push(`@import './${name}.css';`);
121
+ imports.push(`@import './${name}-dark.css';`);
122
+ }
111
123
  if (!isAlphaOnly) {
112
124
  for (const n of steps) {
113
125
  declarations.push(`--color-${name}-${n}: var(--${name}-${n})`);
@@ -127,8 +139,9 @@ function tailwind(colorNames) {
127
139
  focus.push(`--color-focus-${n}: var(--accent-${n})`);
128
140
  focusAlpha.push(`--color-focus-a${n}: var(--accent-a${n})`);
129
141
  }
130
- blocks.push(focus.join(';\n'), focusAlpha.join(';\n'));
131
- return `@theme inline {\n ${blocks.join(';\n\n ')};\n}\n`;
142
+ blocks.push(focus.join(';\n '), focusAlpha.join(';\n '));
143
+ const importsBlock = imports.length > 0 ? imports.join('\n') + '\n\n' : '';
144
+ return `${importsBlock}@theme inline {\n ${blocks.join(';\n\n ')};\n}\n`;
132
145
  }
133
146
  export const css = {
134
147
  /**
package/dist/utils.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { AccentColor, GrayColor } from './types.ts';
2
- export declare function getMatchingGrayColor(accentColor: AccentColor): GrayColor;
3
2
  export declare const LIGHT_SELECTOR = ":root, .light, .light-theme";
4
3
  export declare const DARK_SELECTOR = ".dark, .dark-theme";
5
4
  export declare const schemeSelector: (scheme: "light" | "dark") => string;
@@ -8,12 +7,4 @@ export declare const toVarName: (key: string) => string;
8
7
  export declare const formatRule: (selector: string, vars: string[]) => string;
9
8
  export declare const formatP3: (selector: string, vars: string[]) => string;
10
9
  export declare const formatNestedBlock: (selector: string, vars: string[]) => string;
11
- /**
12
- * get package.json content from startDir
13
- * @param cwd
14
- * @returns package.json content
15
- */
16
- export declare const getPackageJson: (cwd: string) => {
17
- pkg: any;
18
- packageJsonPath: string;
19
- } | undefined;
10
+ export declare function getMatchingGrayColor(accentColor: AccentColor): GrayColor;
package/dist/utils.js CHANGED
@@ -1,7 +1,11 @@
1
- import fs from 'node:fs';
2
- import { createRequire } from 'node:module';
3
- import path from 'node:path';
4
- import * as url from 'node:url';
1
+ export const LIGHT_SELECTOR = ':root, .light, .light-theme';
2
+ export const DARK_SELECTOR = '.dark, .dark-theme';
3
+ export const schemeSelector = (scheme) => scheme === 'light' ? LIGHT_SELECTOR : DARK_SELECTOR;
4
+ /** Convert "amber9" to "--amber-9" or "amberA9" to "--amber-a9" */
5
+ export const toVarName = (key) => `--${key.replace(/A?(\d+)/, (m, n) => (m.startsWith('A') ? `-a${n}` : `-${n}`))}`;
6
+ export const formatRule = (selector, vars) => `${selector} {\n ${vars.join(';\n ')};\n}`;
7
+ export const formatP3 = (selector, vars) => `@supports (color: color(display-p3 1 1 1)) {\n @media (color-gamut: p3) {\n ${selector} {\n ${vars.join(';\n ')};\n }\n }\n}`;
8
+ export const formatNestedBlock = (selector, vars) => `&:where(${selector}) {\n ${vars.join(';\n ')};\n }`;
5
9
  export function getMatchingGrayColor(accentColor) {
6
10
  switch (accentColor) {
7
11
  case 'tomato':
@@ -39,51 +43,3 @@ export function getMatchingGrayColor(accentColor) {
39
43
  return 'gray';
40
44
  }
41
45
  }
42
- export const LIGHT_SELECTOR = ':root, .light, .light-theme';
43
- export const DARK_SELECTOR = '.dark, .dark-theme';
44
- export const schemeSelector = (scheme) => scheme === 'light' ? LIGHT_SELECTOR : DARK_SELECTOR;
45
- /** Convert "amber9" to "--amber-9" or "amberA9" to "--amber-a9" */
46
- export const toVarName = (key) => `--${key.replace(/A?(\d+)/, (m, n) => (m.startsWith('A') ? `-a${n}` : `-${n}`))}`;
47
- export const formatRule = (selector, vars) => `${selector} {\n ${vars.join(';\n ')};\n}`;
48
- export const formatP3 = (selector, vars) => `@supports (color: color(display-p3 1 1 1)) {\n @media (color-gamut: p3) {\n ${selector} {\n ${vars.join(';\n ')};\n }\n }\n}`;
49
- export const formatNestedBlock = (selector, vars) => `&:where(${selector}) {\n ${vars.join(';\n ')};\n }`;
50
- const require = createRequire(import.meta.url);
51
- /**
52
- * get package.json content from startDir
53
- * @param cwd
54
- * @returns package.json content
55
- */
56
- export const getPackageJson = (cwd) => {
57
- try {
58
- const startDir = path.dirname(url.fileURLToPath(cwd));
59
- const packageJsonPath = findPackageJson(startDir);
60
- if (packageJsonPath) {
61
- const pkg = require(packageJsonPath);
62
- return { pkg, packageJsonPath };
63
- }
64
- return undefined;
65
- }
66
- catch (error) {
67
- // ignore error
68
- return undefined;
69
- }
70
- };
71
- /**
72
- * search package.json from startDir
73
- * @param startDir
74
- * @returns
75
- */
76
- const findPackageJson = (startDir) => {
77
- let currentDir = startDir;
78
- while (true) {
79
- const packageJsonPath = path.join(currentDir, 'package.json');
80
- if (fs.existsSync(packageJsonPath)) {
81
- return packageJsonPath;
82
- }
83
- const parentDir = path.resolve(currentDir, '..');
84
- if (parentDir === currentDir) {
85
- return '';
86
- }
87
- currentDir = parentDir;
88
- }
89
- };
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@diskette/palette",
3
3
  "type": "module",
4
- "version": "0.21.0",
4
+ "version": "0.21.2",
5
5
  "bin": {
6
6
  "palette": "./dist/cli/cli.js"
7
7
  },