esm-styles 0.2.0 → 0.2.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/README.md CHANGED
@@ -133,11 +133,12 @@ export default {
133
133
  },
134
134
  },
135
135
 
136
- // Media query shorthands
136
+ // Media query shorthands (in addition to media.device and media.theme names)
137
137
  mediaQueries: {
138
- mobile: '(max-width: 767px)',
139
- tablet: '(min-width: 768px) and (max-width: 1024px)',
140
- desktop: '(min-width: 1025px)',
138
+ 'min-tablet': '(min-width: 768px)',
139
+ 'max-tablet': '(max-width: 1024px)',
140
+ hover: '(hover: hover)',
141
+ // ...whatever you want
141
142
  },
142
143
  }
143
144
  ```
package/dist/lib/build.js CHANGED
@@ -123,7 +123,10 @@ export async function build(configPath = 'esm-styles.config.js') {
123
123
  if (key === '') {
124
124
  // Only possible at non-root
125
125
  const varName = '--' + path.map((k) => k.replace(/_/g, '-')).join('-');
126
- const leaf = { var: varName };
126
+ const leaf = {
127
+ var: `var(${varName})`,
128
+ name: varName,
129
+ };
127
130
  for (let i = 0; i < sets.length; i++) {
128
131
  const v = path.length === 0
129
132
  ? mergedSets[sets[i]]
package/dist/lib/index.js CHANGED
@@ -1,19 +1,16 @@
1
1
  import * as utils from './utils/index.js';
2
+ import { toCssValue } from './utils/to-css-value.js';
2
3
  function flatWalk(obj, selectorPath = [], result = { rules: [], media: {}, layers: {} }, options = {}, currentMedia = []) {
3
4
  const props = {};
4
5
  for (const key in obj) {
5
6
  const value = obj[key];
6
7
  if (utils.isEndValue(value)) {
7
8
  const cssKey = utils.jsKeyToCssKey(key);
8
- if (typeof value === 'object' &&
9
- value !== null &&
10
- 'var' in value &&
11
- typeof value.var === 'string') {
12
- // Use CSS variable reference
13
- props[cssKey] = `var(${value.var})`;
9
+ if (cssKey === 'content') {
10
+ props[cssKey] = utils.contentValue(toCssValue(value));
14
11
  }
15
12
  else {
16
- props[cssKey] = cssKey === 'content' ? utils.contentValue(value) : value;
13
+ props[cssKey] = toCssValue(value);
17
14
  }
18
15
  }
19
16
  else if (typeof value === 'object' && value !== null) {
@@ -1,13 +1,33 @@
1
+ const keywords = [
2
+ 'normal',
3
+ 'none',
4
+ 'open-quote',
5
+ 'close-quote',
6
+ 'no-open-quote',
7
+ 'no-close-quote',
8
+ 'inherit',
9
+ 'initial',
10
+ 'revert',
11
+ 'revert-layer',
12
+ 'unset',
13
+ ];
1
14
  export const contentValue = (value) => {
2
15
  if (typeof value !== 'string')
3
16
  return value;
4
- // If already quoted, return as is
5
- if (/^'.*'$/.test(value) || /^".*"$/.test(value))
17
+ if (keywords.some((keyword) => {
18
+ const regex = new RegExp(`\\b${keyword}\\b`);
19
+ return regex.test(value);
20
+ }))
6
21
  return value;
7
- // If all characters are printable ASCII, return as quoted string
8
- if (/^[\x20-\x7E]*$/.test(value)) {
22
+ // If already contains quoted values, return as is
23
+ if (/'.*'/.test(value) || /".*"/.test(value))
24
+ return value;
25
+ // If contains functions, return as is
26
+ if (/[a-z]+\(.*\)/i.test(value))
27
+ return value; // url(...), attr(...), etc.
28
+ // If all characters are printable ASCII, wrap in single quotes
29
+ if (/^[\x20-\x7E]*$/.test(value))
9
30
  return `'${value}'`;
10
- }
11
31
  // Otherwise, convert each character to CSS unicode escape: \00xxxx
12
32
  const unicode = value
13
33
  .split('')
@@ -6,3 +6,4 @@ export * from './format.js';
6
6
  export * from './end-value.js';
7
7
  export * from './get-css-variables.js';
8
8
  export * from './media-shorthand.js';
9
+ export * from './to-css-value.js';
@@ -6,3 +6,4 @@ export * from './format.js';
6
6
  export * from './end-value.js';
7
7
  export * from './get-css-variables.js';
8
8
  export * from './media-shorthand.js';
9
+ export * from './to-css-value.js';
@@ -0,0 +1 @@
1
+ export declare function toCssValue(value: any): string;
@@ -0,0 +1,15 @@
1
+ // Utility to convert a value to a CSS string, handling arrays, objects with 'var', and primitives
2
+ export function toCssValue(value) {
3
+ if (Array.isArray(value)) {
4
+ // Join array parts, recursively converting each
5
+ return value.map(toCssValue).join(' ');
6
+ }
7
+ if (typeof value === 'object' &&
8
+ value !== null &&
9
+ 'var' in value &&
10
+ typeof value.var === 'string') {
11
+ return value.var;
12
+ }
13
+ // For primitives (string, number), just return as string
14
+ return String(value);
15
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "esm-styles",
3
- "version": "0.2.0",
3
+ "version": "0.2.2",
4
4
  "description": "A library for working with ESM styles",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",