forked from aws-amplify/amplify-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.ts
50 lines (43 loc) · 1.26 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import kebabCase from 'lodash/kebabCase';
// internal style dictionary function
import usesReference from 'style-dictionary/lib/utils/references/usesReference';
import { DesignToken } from './tokens/types/designToken';
export const CSS_VARIABLE_PREFIX = 'amplify';
function referenceValue(value: string) {
if (usesReference(value)) {
const path = value.replace(/\{|\}/g, '').replace('.value', '').split('.');
return `var(--${cssNameTransform({ path })})`;
}
return value;
}
export function cssValue(token: DesignToken) {
const { value } = token;
if (typeof value === 'string') {
return referenceValue(value);
}
if (typeof value === 'object') {
if ('offsetX' in value) {
const {
offsetX = '',
offsetY = '',
blurRadius = '',
spreadRadius = '',
color = '',
} = value;
return [
referenceValue(offsetX),
referenceValue(offsetY),
referenceValue(blurRadius),
referenceValue(spreadRadius),
referenceValue(color),
].join(' ');
}
}
return value;
}
interface NameTransformProps {
path?: Array<string>;
}
export function cssNameTransform({ path = [] }: NameTransformProps): string {
return `${kebabCase([CSS_VARIABLE_PREFIX, ...path].join(' '))}`;
}