@weser/theming 0.0.18 → 1.0.1

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,3 +1,3 @@
1
1
  # @weser/theming
2
2
 
3
- [Documentation](https://packages.weser.io/theming)
3
+ [Documentation](https://stack.weser.io/theming)
@@ -0,0 +1 @@
1
+ export default function alpha(reference: string, alpha: number): string;
@@ -0,0 +1,3 @@
1
+ export default function alpha(reference, alpha) {
2
+ return `hsl(from ${reference} h s l / max(0, min(1, ${alpha})))`;
3
+ }
@@ -0,0 +1,7 @@
1
+ import { Tokens } from './types.js';
2
+ export type SingleConfig = {
3
+ selector?: string;
4
+ shouldTransformValue?: (path: string) => boolean;
5
+ };
6
+ export declare const defaultShouldTransformValue: () => boolean;
7
+ export default function createTheme<T extends Tokens>(tokens: T, config?: SingleConfig): [T, string];
@@ -0,0 +1,27 @@
1
+ import isObject from 'isobject';
2
+ import createVariable from './createVariable.js';
3
+ export const defaultShouldTransformValue = () => true;
4
+ export default function createTheme(tokens, config = {}) {
5
+ const { selector = ':root', shouldTransformValue = defaultShouldTransformValue, } = config;
6
+ const [theme, declarations] = extractVariables(structuredClone(tokens), shouldTransformValue);
7
+ const css = `${selector}{${declarations.join(';')}}`;
8
+ return [theme, css];
9
+ }
10
+ function extractVariables(tokens, convert, path = '', variables = []) {
11
+ for (const property in tokens) {
12
+ const value = tokens[property];
13
+ const name = path + (path ? '.' : '') + property;
14
+ if (isObject(value)) {
15
+ extractVariables(value, convert, name, variables);
16
+ }
17
+ else {
18
+ if (convert(path)) {
19
+ const [reference, declaration] = createVariable(name.replace(/[.]/g, '-'), value);
20
+ variables.push(declaration);
21
+ // @ts-ignore
22
+ tokens[property] = reference;
23
+ }
24
+ }
25
+ }
26
+ return [tokens, variables];
27
+ }
@@ -0,0 +1,11 @@
1
+ import { SingleConfig } from './createTheme.js';
2
+ import { Tokens } from './types.js';
3
+ type ThemeMap<T> = {
4
+ [name: string]: T;
5
+ };
6
+ type MutliConfig = {
7
+ shouldTransformValue?: SingleConfig['shouldTransformValue'];
8
+ getSelector?: (name: string) => string;
9
+ };
10
+ export default function createThemes<T extends Tokens>(themes: ThemeMap<T>, config?: MutliConfig): [T, string];
11
+ export {};
@@ -0,0 +1,16 @@
1
+ import createTheme, { defaultShouldTransformValue, } from './createTheme.js';
2
+ const defaultGetSelector = (name) => '.' + name;
3
+ export default function createThemes(themes, config = {}) {
4
+ const { shouldTransformValue = defaultShouldTransformValue, getSelector = defaultGetSelector, } = config;
5
+ let css = '';
6
+ let theme;
7
+ for (const name in themes) {
8
+ const [_theme, _css] = createTheme(themes[name], {
9
+ shouldTransformValue,
10
+ selector: getSelector(name),
11
+ });
12
+ css += _css;
13
+ theme = _theme;
14
+ }
15
+ return [theme, css];
16
+ }
@@ -0,0 +1 @@
1
+ export default function createVariable(name: string, value: string): [string, string];
@@ -0,0 +1,6 @@
1
+ export default function createVariable(name, value) {
2
+ const variable = `--${name}`;
3
+ const reference = `var(${variable})`;
4
+ const declaration = `${variable}:${value}`;
5
+ return [reference, declaration];
6
+ }
@@ -0,0 +1 @@
1
+ export default function createVariableReference(name: string, fallbackValue?: string): [`--${string}`, string];
@@ -0,0 +1,7 @@
1
+ export default function createVariableReference(name, fallbackValue) {
2
+ const variable = `--${name}`;
3
+ if (fallbackValue) {
4
+ return [variable, `var(${variable}, ${fallbackValue})`];
5
+ }
6
+ return [variable, `var(${variable})`];
7
+ }
@@ -0,0 +1 @@
1
+ export default function darken(color: string, amount: number): string;
@@ -0,0 +1,4 @@
1
+ export default function darken(color, amount) {
2
+ const percentage = amount * 100;
3
+ return `hsl(from ${color} h s calc(max(0, min(100, l - ${percentage}))))`;
4
+ }
@@ -0,0 +1,8 @@
1
+ export { default as createVariable } from './createVariable.js';
2
+ export { default as createVariableReference } from './createVariableReference.js';
3
+ export { default as createTheme } from './createTheme.js';
4
+ export { default as createThemes } from './createThemes.js';
5
+ export { default as alpha } from './alpha.js';
6
+ export { default as a } from './alpha.js';
7
+ export { default as lighten } from './lighten.js';
8
+ export { default as darken } from './darken.js';
@@ -0,0 +1,8 @@
1
+ export { default as createVariable } from './createVariable.js';
2
+ export { default as createVariableReference } from './createVariableReference.js';
3
+ export { default as createTheme } from './createTheme.js';
4
+ export { default as createThemes } from './createThemes.js';
5
+ export { default as alpha } from './alpha.js';
6
+ export { default as a } from './alpha.js';
7
+ export { default as lighten } from './lighten.js';
8
+ export { default as darken } from './darken.js';
@@ -0,0 +1 @@
1
+ export default function lighten(color: string, amount: number): string;
@@ -0,0 +1,4 @@
1
+ export default function lighten(color, amount) {
2
+ const percentage = amount * 100;
3
+ return `hsl(from ${color} h s calc(max(0, min(100, l + ${percentage}))))`;
4
+ }
@@ -0,0 +1 @@
1
+ export type Tokens = Record<string, any>;
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ {"root":["../vitest.config.ts","../src/alpha.ts","../src/createtheme.ts","../src/createthemes.ts","../src/createvariable.ts","../src/createvariablereference.ts","../src/darken.ts","../src/index.ts","../src/lighten.ts","../src/types.ts"],"version":"5.9.3"}
@@ -0,0 +1,2 @@
1
+ declare const _default: import("vite").UserConfig;
2
+ export default _default;
@@ -0,0 +1,10 @@
1
+ import { defineConfig } from 'vitest/config';
2
+ export default defineConfig({
3
+ test: {
4
+ include: ['**/__tests__/**/*.{js,ts}'],
5
+ globals: false,
6
+ },
7
+ esbuild: {
8
+ target: 'esnext',
9
+ },
10
+ });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@weser/theming",
3
- "version": "0.0.18",
3
+ "version": "1.0.1",
4
4
  "description": "Utils for theming with CSS variables",
5
5
  "author": "Robin Weser <robin@weser.io>",
6
6
  "license": "MIT",
@@ -33,7 +33,7 @@
33
33
  "clean": "rimraf dist",
34
34
  "build": "tsc -b",
35
35
  "dev": "pnpm build -w",
36
- "test": "ava"
36
+ "test": "vitest run"
37
37
  },
38
38
  "keywords": [
39
39
  "theming",
@@ -44,9 +44,9 @@
44
44
  "isobject": "^4.0.0"
45
45
  },
46
46
  "devDependencies": {
47
- "ava": "^6.1.3",
48
47
  "rimraf": "^3.0.2",
49
- "typescript": "^5.4.5"
48
+ "typescript": "^5.4.5",
49
+ "vitest": "^2.1.8"
50
50
  },
51
- "gitHead": "4dd312c8cf505d0c06d25ca489b893a6c6c0d9fe"
51
+ "gitHead": "490b5b37e8da7f0cac2880543874914c6f6be5d6"
52
52
  }