Skip to content

Commit 8a3a55c

Browse files
Add utils/css-variables
1 parent 706dd32 commit 8a3a55c

File tree

6 files changed

+86
-1
lines changed

6 files changed

+86
-1
lines changed

packages/minimal-shared/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# minimal-shared
22

3+
## 1.0.9
4+
5+
_May 7, 2025_
6+
7+
- Add `utils/css-variables`.
8+
9+
---
10+
311
## 1.0.8
412

513
_Apr 8, 2025_

packages/minimal-shared/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "minimal-shared",
33
"author": "Minimals",
4-
"version": "1.0.8",
4+
"version": "1.0.9",
55
"description": "Shared hooks and utils used by Minimal UI and Zone UI.",
66
"keywords": [
77
"typescript",
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import { getCssVarName } from './css-variables';
2+
3+
// ----------------------------------------------------------------------
4+
5+
describe('getCssVarName()', () => {
6+
const errorSpy = vi.spyOn(console, 'error').mockImplementation(() => {});
7+
8+
afterEach(() => {
9+
errorSpy.mockClear();
10+
});
11+
12+
afterAll(() => {
13+
errorSpy.mockRestore();
14+
});
15+
16+
it('1. Extracts variable without fallback', () => {
17+
expect(getCssVarName('var(--palette-Tooltip-bg)')).toBe('--palette-Tooltip-bg');
18+
});
19+
20+
it('2. Extracts variable with fallback', () => {
21+
expect(getCssVarName('var(--palette-Tooltip-bg, rgba(69, 79, 91, 0.92))')).toBe(
22+
'--palette-Tooltip-bg'
23+
);
24+
});
25+
26+
it('3. Returns empty string on empty or non-string input', () => {
27+
const badInputs = [null, undefined, '', 123, {}, true];
28+
29+
for (const val of badInputs) {
30+
expect(getCssVarName(val)).toBe('');
31+
}
32+
33+
expect(errorSpy).toHaveBeenCalledTimes(badInputs.length);
34+
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid input'));
35+
});
36+
37+
it('4. Returns empty string on invalid CSS var() format', () => {
38+
const invalidFormats = ['rgba(0,0,0,0.5)', 'var(-bad)', 'var()', 'var(--bad value)'];
39+
40+
for (const val of invalidFormats) {
41+
expect(getCssVarName(val)).toBe('');
42+
}
43+
44+
expect(errorSpy).toHaveBeenCalledTimes(invalidFormats.length);
45+
expect(errorSpy).toHaveBeenCalledWith(expect.stringContaining('Invalid CSS variable format'));
46+
});
47+
});
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Extract the CSS variable name from a `var(--...)` expression.
3+
*
4+
* @param cssValue - A string like `var(--variable-name)` or `var(--variable-name, fallback)`.
5+
* @returns The extracted CSS variable name (e.g., '--palette-Tooltip-bg').
6+
*
7+
* @example
8+
* getCssVarName('var(--palette-Tooltip-bg)'); // → '--palette-Tooltip-bg'
9+
* getCssVarName('var(--palette-Tooltip-bg, rgba(69, 79, 91, 0.92))'); // → '--palette-Tooltip-bg'
10+
* getCssVarName(theme.vars.palette.Tooltip.bg); // → '--palette-Tooltip-bg'
11+
*/
12+
export function getCssVarName(cssValue: unknown): string {
13+
if (typeof cssValue !== 'string' || !cssValue.trim()) {
14+
console.error('Invalid input: CSS value must be a non-empty string');
15+
return '';
16+
}
17+
18+
const match = cssValue.match(/var\(\s*(--[\w-]+)(?:\s*,[^)]*)?\s*\)/);
19+
20+
if (!match) {
21+
console.error(
22+
`Invalid CSS variable format: "${cssValue}". Expected format: var(--variable-name)`
23+
);
24+
return '';
25+
}
26+
27+
return match[1];
28+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './css-variables';

packages/minimal-shared/src/utils/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ export * from './classes';
88
export * from './cookies';
99
export * from './active-link';
1010
export * from './local-storage';
11+
export * from './css-variables';
1112
export * from './transform-number';

0 commit comments

Comments
 (0)