@navita/css 0.0.0 → 0.0.4

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.md ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Alexander Liljengård
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/index.d.ts CHANGED
@@ -1,29 +1,33 @@
1
- import { StyleRule, GlobalStyleRule, CSSKeyframes, NullableTokens, ThemeVars, Tokens, Contract, MapLeafNodes, CSSVarFunction } from '@navita/types';
1
+ import { StyleRule, GlobalStyleRule, CSSKeyframes, FontFaceRule, NullableTokens, ThemeVars, Tokens, Contract, MapLeafNodes, CSSVarFunction } from '@navita/types';
2
2
 
3
3
  declare function style(rule: StyleRule): string;
4
4
 
5
5
  declare function globalStyle(selector: string, rule: GlobalStyleRule): void;
6
6
 
7
- declare function merge(...classNames: (string | boolean)[]): string;
7
+ declare function merge(...classNames: (string | false | void | null | 0 | '')[]): string;
8
8
 
9
9
  declare function keyframes(rule: CSSKeyframes): string;
10
10
 
11
+ declare function fontFace(rule: FontFaceRule | FontFaceRule[]): string;
12
+
11
13
  declare function createThemeContract<ThemeTokens extends NullableTokens>(tokens: ThemeTokens): ThemeVars<ThemeTokens>;
12
14
  declare function createGlobalThemeContract<ThemeTokens extends Tokens>(tokens: ThemeTokens): ThemeVars<ThemeTokens>;
13
15
  declare function createGlobalThemeContract<ThemeTokens extends NullableTokens>(tokens: ThemeTokens, mapFn: (value: string | null, path: Array<string>) => string): ThemeVars<ThemeTokens>;
14
16
  declare function createGlobalTheme<ThemeTokens extends Tokens>(selector: string, tokens: ThemeTokens): ThemeVars<ThemeTokens>;
15
17
  declare function createGlobalTheme<ThemeContract extends Contract>(selector: string, themeContract: ThemeContract, tokens: MapLeafNodes<ThemeContract, string>): void;
16
18
  declare function createTheme<ThemeTokens extends Tokens>(tokens: ThemeTokens, debugId?: string): [className: string, vars: ThemeVars<ThemeTokens>];
17
- declare function createTheme<ThemeContract extends Contract>(themeContract: ThemeContract, tokens: MapLeafNodes<ThemeContract, string>, debugId?: string): string;
19
+ declare function createTheme<ThemeContract extends Contract>(themeContract: ThemeContract, tokens: MapLeafNodes<ThemeContract, string>): string;
18
20
 
21
+ declare function createVar(name: string): string;
22
+ declare function fallbackVar(...values: [string, ...Array<string>]): CSSVarFunction;
19
23
  declare function assignVars<VarContract extends Contract>(varContract: VarContract, tokens: MapLeafNodes<VarContract, string>): Record<CSSVarFunction, string>;
20
24
 
21
25
  declare class ClassList extends String {
22
26
  classList: {
23
- [key: string]: any;
27
+ [key: string]: string[];
24
28
  };
25
29
  constructor(str: string, classList: {
26
- [key: string]: any;
30
+ [key: string]: string[];
27
31
  });
28
32
  }
29
33
 
@@ -32,4 +36,4 @@ declare const importMap: {
32
36
  source: string;
33
37
  }[];
34
38
 
35
- export { ClassList, assignVars, createGlobalTheme, createGlobalThemeContract, createTheme, createThemeContract, globalStyle, importMap, keyframes, merge, style };
39
+ export { ClassList, assignVars, createGlobalTheme, createGlobalThemeContract, createTheme, createThemeContract, createVar, fallbackVar, fontFace, globalStyle, importMap, keyframes, merge, style };
package/index.js CHANGED
@@ -1,26 +1,34 @@
1
1
  'use strict';
2
2
 
3
- var core = require('@navita/core');
3
+ var adapter = require('@navita/adapter');
4
4
  var cssesc = require('cssesc');
5
5
  var chalk = require('chalk');
6
6
  var deepObjectDiff = require('deep-object-diff');
7
7
 
8
8
  function style(rule) {
9
- return core.setCss(rule);
9
+ return adapter.addCss(rule);
10
10
  }
11
11
 
12
12
  function globalStyle(selector, rule) {
13
- core.setStaticCss(selector, rule);
13
+ adapter.addStaticCss(selector, rule);
14
14
  }
15
15
 
16
16
  function merge(...classNames) {
17
- return [
18
- ...new Map(classNames.filter(Boolean).reverse().flatMap((className)=>className.split(' ')).map((className)=>className.split('-')).reverse())
19
- ].map((className)=>className.filter(Boolean).join('-')).join(' ');
17
+ const input = classNames.filter(Boolean).join(' ').split(' ');
18
+ const output = {};
19
+ for (const className of input){
20
+ const [property] = className.split(/\d+$/);
21
+ output[property] = className;
22
+ }
23
+ return Object.values(output).join(' ');
20
24
  }
21
25
 
22
26
  function keyframes(rule) {
23
- return core.setKeyFrame(rule);
27
+ return adapter.addKeyframe(rule);
28
+ }
29
+
30
+ function fontFace(rule) {
31
+ return adapter.addFontFace(rule);
24
32
  }
25
33
 
26
34
  function walkObject(objectToWalk, transformFn, currentPath = []) {
@@ -90,10 +98,27 @@ function renderDiff(orig, diff, nesting = 0) {
90
98
  }
91
99
 
92
100
  function createVar(name) {
101
+ if (!name) {
102
+ return `var(--${adapter.generateIdentifier(undefined)})`;
103
+ }
93
104
  return `var(--${cssesc(name, {
94
105
  isIdentifier: true
95
106
  })})`;
96
107
  }
108
+ function fallbackVar(...values) {
109
+ let finalValue = '';
110
+ values.reverse().forEach((value)=>{
111
+ if (finalValue === '') {
112
+ finalValue = String(value);
113
+ } else {
114
+ if (typeof value !== 'string' || !/^var\(--.*\)$/.test(value)) {
115
+ throw new Error(`Invalid variable name: ${value}`);
116
+ }
117
+ finalValue = value.replace(/\)$/, `, ${finalValue})`);
118
+ }
119
+ });
120
+ return finalValue;
121
+ }
97
122
  function assignVars(varContract, tokens) {
98
123
  const varSetters = {};
99
124
  const { valid , diffString } = validateContract(varContract, tokens);
@@ -131,14 +156,13 @@ function createGlobalTheme(selector, arg2, arg3) {
131
156
  const themeVars = shouldCreateVars ? createThemeContract(arg2) : arg2;
132
157
  const tokens = shouldCreateVars ? arg2 : arg3;
133
158
  const temp = assignVars(themeVars, tokens);
134
- core.setStaticCss(selector, temp);
135
- // Todo: setStaticCss
159
+ adapter.addStaticCss(selector, temp);
136
160
  if (shouldCreateVars) {
137
161
  return themeVars;
138
162
  }
139
163
  }
140
- function createTheme(arg1, arg2, arg3) {
141
- const themeClassName = core.generateIdentifier(typeof arg2 === 'object' ? arg3 : arg2);
164
+ function createTheme(arg1, arg2) {
165
+ const themeClassName = `.${adapter.generateIdentifier(typeof arg2 === 'object' ? arg2 : arg1)}`;
142
166
  const vars = typeof arg2 === 'object' ? createGlobalTheme(themeClassName, arg1, arg2) : createGlobalTheme(themeClassName, arg1);
143
167
  return vars ? [
144
168
  themeClassName,
@@ -167,6 +191,10 @@ const importMap = [
167
191
  callee: "keyframes",
168
192
  source
169
193
  },
194
+ {
195
+ callee: "fontFace",
196
+ source
197
+ },
170
198
  {
171
199
  callee: "createThemeContract",
172
200
  source
@@ -182,6 +210,14 @@ const importMap = [
182
210
  {
183
211
  callee: "createTheme",
184
212
  source
213
+ },
214
+ {
215
+ callee: "createVar",
216
+ source
217
+ },
218
+ {
219
+ callee: "fallbackVar",
220
+ source
185
221
  }
186
222
  ];
187
223
 
@@ -191,6 +227,9 @@ exports.createGlobalTheme = createGlobalTheme;
191
227
  exports.createGlobalThemeContract = createGlobalThemeContract;
192
228
  exports.createTheme = createTheme;
193
229
  exports.createThemeContract = createThemeContract;
230
+ exports.createVar = createVar;
231
+ exports.fallbackVar = fallbackVar;
232
+ exports.fontFace = fontFace;
194
233
  exports.globalStyle = globalStyle;
195
234
  exports.importMap = importMap;
196
235
  exports.keyframes = keyframes;
package/index.mjs CHANGED
@@ -1,24 +1,32 @@
1
- import { setCss, setStaticCss, setKeyFrame, generateIdentifier } from '@navita/core';
1
+ import { addCss, addStaticCss, addKeyframe, addFontFace, generateIdentifier } from '@navita/adapter';
2
2
  import cssesc from 'cssesc';
3
3
  import chalk from 'chalk';
4
4
  import { diff } from 'deep-object-diff';
5
5
 
6
6
  function style(rule) {
7
- return setCss(rule);
7
+ return addCss(rule);
8
8
  }
9
9
 
10
10
  function globalStyle(selector, rule) {
11
- setStaticCss(selector, rule);
11
+ addStaticCss(selector, rule);
12
12
  }
13
13
 
14
14
  function merge(...classNames) {
15
- return [
16
- ...new Map(classNames.filter(Boolean).reverse().flatMap((className)=>className.split(' ')).map((className)=>className.split('-')).reverse())
17
- ].map((className)=>className.filter(Boolean).join('-')).join(' ');
15
+ const input = classNames.filter(Boolean).join(' ').split(' ');
16
+ const output = {};
17
+ for (const className of input){
18
+ const [property] = className.split(/\d+$/);
19
+ output[property] = className;
20
+ }
21
+ return Object.values(output).join(' ');
18
22
  }
19
23
 
20
24
  function keyframes(rule) {
21
- return setKeyFrame(rule);
25
+ return addKeyframe(rule);
26
+ }
27
+
28
+ function fontFace(rule) {
29
+ return addFontFace(rule);
22
30
  }
23
31
 
24
32
  function walkObject(objectToWalk, transformFn, currentPath = []) {
@@ -88,10 +96,27 @@ function renderDiff(orig, diff, nesting = 0) {
88
96
  }
89
97
 
90
98
  function createVar(name) {
99
+ if (!name) {
100
+ return `var(--${generateIdentifier(undefined)})`;
101
+ }
91
102
  return `var(--${cssesc(name, {
92
103
  isIdentifier: true
93
104
  })})`;
94
105
  }
106
+ function fallbackVar(...values) {
107
+ let finalValue = '';
108
+ values.reverse().forEach((value)=>{
109
+ if (finalValue === '') {
110
+ finalValue = String(value);
111
+ } else {
112
+ if (typeof value !== 'string' || !/^var\(--.*\)$/.test(value)) {
113
+ throw new Error(`Invalid variable name: ${value}`);
114
+ }
115
+ finalValue = value.replace(/\)$/, `, ${finalValue})`);
116
+ }
117
+ });
118
+ return finalValue;
119
+ }
95
120
  function assignVars(varContract, tokens) {
96
121
  const varSetters = {};
97
122
  const { valid , diffString } = validateContract(varContract, tokens);
@@ -129,14 +154,13 @@ function createGlobalTheme(selector, arg2, arg3) {
129
154
  const themeVars = shouldCreateVars ? createThemeContract(arg2) : arg2;
130
155
  const tokens = shouldCreateVars ? arg2 : arg3;
131
156
  const temp = assignVars(themeVars, tokens);
132
- setStaticCss(selector, temp);
133
- // Todo: setStaticCss
157
+ addStaticCss(selector, temp);
134
158
  if (shouldCreateVars) {
135
159
  return themeVars;
136
160
  }
137
161
  }
138
- function createTheme(arg1, arg2, arg3) {
139
- const themeClassName = generateIdentifier(typeof arg2 === 'object' ? arg3 : arg2);
162
+ function createTheme(arg1, arg2) {
163
+ const themeClassName = `.${generateIdentifier(typeof arg2 === 'object' ? arg2 : arg1)}`;
140
164
  const vars = typeof arg2 === 'object' ? createGlobalTheme(themeClassName, arg1, arg2) : createGlobalTheme(themeClassName, arg1);
141
165
  return vars ? [
142
166
  themeClassName,
@@ -165,6 +189,10 @@ const importMap = [
165
189
  callee: "keyframes",
166
190
  source
167
191
  },
192
+ {
193
+ callee: "fontFace",
194
+ source
195
+ },
168
196
  {
169
197
  callee: "createThemeContract",
170
198
  source
@@ -180,7 +208,15 @@ const importMap = [
180
208
  {
181
209
  callee: "createTheme",
182
210
  source
211
+ },
212
+ {
213
+ callee: "createVar",
214
+ source
215
+ },
216
+ {
217
+ callee: "fallbackVar",
218
+ source
183
219
  }
184
220
  ];
185
221
 
186
- export { ClassList, assignVars, createGlobalTheme, createGlobalThemeContract, createTheme, createThemeContract, globalStyle, importMap, keyframes, merge, style };
222
+ export { ClassList, assignVars, createGlobalTheme, createGlobalThemeContract, createTheme, createThemeContract, createVar, fallbackVar, fontFace, globalStyle, importMap, keyframes, merge, style };
package/package.json CHANGED
@@ -1,7 +1,10 @@
1
1
  {
2
- "sideEffects": false,
3
2
  "name": "@navita/css",
4
- "version": "0.0.0",
3
+ "version": "0.0.4",
4
+ "license": "MIT",
5
+ "private": false,
6
+ "sideEffects": false,
7
+ "types": "src/index.ts",
5
8
  "exports": {
6
9
  ".": {
7
10
  "import": "./index.mjs",
@@ -10,10 +13,10 @@
10
13
  }
11
14
  },
12
15
  "dependencies": {
16
+ "@navita/adapter": "*",
17
+ "@navita/types": "*",
13
18
  "deep-object-diff": "^1.1.9",
14
19
  "cssesc": "^3.0.0",
15
- "chalk": "^4.1.2",
16
- "@navita/core": "*",
17
- "@navita/types": "*"
20
+ "chalk": "^4.1.2"
18
21
  }
19
22
  }