-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: consider empty string props in typo/border/shadow
- Loading branch information
1 parent
0255ee2
commit ce69ada
Showing
9 changed files
with
44 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@tokens-studio/sd-transforms': patch | ||
--- | ||
|
||
Consider that properties can also be empty string, which is especially common if the typography/border/shadow tokens are coming from Tokens Studio. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,24 @@ | ||
import { checkAndEvaluateMath } from '../checkAndEvaluateMath.js'; | ||
import { transformDimension } from '../transformDimension.js'; | ||
import { transformHEXRGBaForCSS } from './transformHEXRGBa.js'; | ||
import { isNothing } from '../utils/is-nothing.js'; | ||
|
||
/** | ||
* Helper: Transforms border object to border shorthand | ||
* This currently works fine if every value uses an alias, | ||
* but if any one of these use a raw value, it will not be transformed. | ||
*/ | ||
export function transformBorderForCSS( | ||
border: Record<string, string> | undefined | string, | ||
border: Record<string, string | undefined> | undefined | string, | ||
): string | undefined { | ||
if (typeof border !== 'object') { | ||
return border; | ||
} | ||
const { color, width, style } = border; | ||
return `${transformDimension(checkAndEvaluateMath(width)) ?? ''} ${style ?? ''} ${ | ||
transformHEXRGBaForCSS(color) ?? '' | ||
let { color, width } = border; | ||
const { style } = border; | ||
width = transformDimension(checkAndEvaluateMath(width)); | ||
color = transformHEXRGBaForCSS(color); | ||
return `${isNothing(width) ? '' : width} ${isNothing(style) ? '' : style} ${ | ||
isNothing(color) ? '' : color | ||
}`.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,28 @@ | ||
import { checkAndEvaluateMath } from '../checkAndEvaluateMath.js'; | ||
import { transformDimension } from '../transformDimension.js'; | ||
import { transformHEXRGBaForCSS } from './transformHEXRGBa.js'; | ||
import { isNothing } from '../utils/is-nothing.js'; | ||
|
||
/** | ||
* Helper: Transforms boxShadow object to shadow shorthand | ||
* This currently works fine if every value uses an alias, | ||
* but if any one of these use a raw value, it will not be transformed. | ||
*/ | ||
export function transformShadowForCSS( | ||
shadow: Record<string, string> | undefined | string, | ||
shadow: Record<string, string | undefined> | undefined | string, | ||
): string | undefined { | ||
if (typeof shadow !== 'object') { | ||
return shadow; | ||
} | ||
let { x, y, blur, spread } = shadow; | ||
const { color, type } = shadow; | ||
x = transformDimension(checkAndEvaluateMath(x)) as string; | ||
y = transformDimension(checkAndEvaluateMath(y)) as string; | ||
blur = transformDimension(checkAndEvaluateMath(blur)) as string; | ||
spread = transformDimension(checkAndEvaluateMath(spread)) as string; | ||
return `${type === 'innerShadow' ? 'inset ' : ''}${x ?? 0} ${y ?? 0} ${blur ?? 0}${ | ||
spread == null ? ' ' : ` ${spread} ` | ||
}${transformHEXRGBaForCSS(color) ?? 'rgba(0, 0, 0, 1)'}`.trim(); | ||
x = transformDimension(checkAndEvaluateMath(x)); | ||
y = transformDimension(checkAndEvaluateMath(y)); | ||
blur = transformDimension(checkAndEvaluateMath(blur)); | ||
spread = transformDimension(checkAndEvaluateMath(spread)); | ||
return `${type === 'innerShadow' ? 'inset ' : ''}${isNothing(x) ? 0 : x} ${ | ||
isNothing(y) ? 0 : y | ||
} ${isNothing(blur) ? 0 : blur}${isNothing(spread) ? ' ' : ` ${spread} `}${ | ||
transformHEXRGBaForCSS(color) ?? 'rgba(0, 0, 0, 1)' | ||
}`.trim(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,26 @@ | ||
import { transformDimension } from '../transformDimension.js'; | ||
import { transformFontWeights } from '../transformFontWeights.js'; | ||
import { checkAndEvaluateMath } from '../checkAndEvaluateMath.js'; | ||
import { isNothing } from '../utils/is-nothing.js'; | ||
|
||
/** | ||
* Helper: Transforms typography object to typography shorthand for CSS | ||
* This currently works fine if every value uses an alias, but if any one of these use a raw value, it will not be transformed. | ||
* If you'd like to output all typography values, you'd rather need to return the typography properties itself | ||
*/ | ||
export function transformTypographyForCSS( | ||
value: Record<string, string> | undefined | string, | ||
value: Record<string, string | undefined> | undefined | string, | ||
): string | undefined { | ||
if (typeof value !== 'object') { | ||
return value; | ||
} | ||
const { fontFamily } = value; | ||
let { fontWeight, fontSize, lineHeight } = value; | ||
fontWeight = transformFontWeights(fontWeight) as string; | ||
fontSize = transformDimension(checkAndEvaluateMath(fontSize)) as string; | ||
lineHeight = checkAndEvaluateMath(lineHeight) as string; | ||
fontWeight = transformFontWeights(fontWeight); | ||
fontSize = transformDimension(checkAndEvaluateMath(fontSize)); | ||
lineHeight = checkAndEvaluateMath(lineHeight); | ||
|
||
return `${fontWeight ?? 400} ${fontSize ?? '16px'}/${lineHeight ?? 1} ${ | ||
fontFamily ?? 'sans-serif' | ||
}`; | ||
return `${isNothing(fontWeight) ? 400 : fontWeight} ${isNothing(fontSize) ? '16px' : fontSize}/${ | ||
isNothing(lineHeight) ? 1 : lineHeight | ||
} ${isNothing(fontFamily) ? 'sans-serif' : fontFamily}`; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export function isNothing(value: string | null | undefined): boolean { | ||
if (value == null || value === '') { | ||
return true; | ||
} | ||
return false; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters