-
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.
Merge pull request #96 from tokens-studio/opacity
fix: add opacity percentage handling
- Loading branch information
Showing
15 changed files
with
104 additions
and
19 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': minor | ||
--- | ||
|
||
BREAKING: Register ts/opacity transform, to transform opacity to number between 0 and 1, which is more multi-platform than percentages%. Breaking because people might not expect to have their opacity tokens suddenly as decimal numbers. |
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 |
---|---|---|
@@ -1,13 +1,14 @@ | ||
import { percentageToDecimal } from '../utils/percentageToDecimal.js'; | ||
|
||
/** | ||
* Helper: Transforms letter spacing % to em | ||
*/ | ||
export function transformLetterSpacingForCSS(value: string | undefined): string | undefined { | ||
export function transformLetterSpacingForCSS( | ||
value: string | number | undefined, | ||
): string | undefined { | ||
if (value === undefined) { | ||
return value; | ||
} | ||
if (`${value}`.endsWith('%')) { | ||
const percentValue = value.slice(0, -1); | ||
return `${parseFloat(percentValue) / 100}em`; | ||
} | ||
return value; | ||
const decimal = percentageToDecimal(value); | ||
return typeof decimal === 'string' || isNaN(decimal) ? `${value}` : `${decimal}em`; | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,14 @@ | ||
import { percentageToDecimal } from './utils/percentageToDecimal.js'; | ||
|
||
/** | ||
* Helper: Transforms line-height % to unit-less decimal value | ||
* @example | ||
* 150% -> 1.5 | ||
*/ | ||
export function transformLineHeight( | ||
value: string | number | undefined, | ||
): string | number | undefined { | ||
export function transformLineHeight(value: string | number | undefined): string | undefined { | ||
if (value === undefined) { | ||
return value; | ||
} | ||
if (`${value}`.endsWith('%')) { | ||
const percentValue = `${value}`.slice(0, -1); | ||
return parseFloat(percentValue) / 100; | ||
} | ||
return value; | ||
const decimal = percentageToDecimal(value); | ||
return typeof decimal === 'string' || isNaN(decimal) ? `${value}` : `${decimal}`; | ||
} |
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,14 @@ | ||
import { percentageToDecimal } from './utils/percentageToDecimal.js'; | ||
|
||
/** | ||
* Helper: Transforms opacity % to a decimal point number | ||
* @example | ||
* 50% -> 0.5 | ||
*/ | ||
export function transformOpacity(value: string | number | undefined): string | number | undefined { | ||
if (value === undefined) { | ||
return value; | ||
} | ||
const decimal = percentageToDecimal(value); | ||
return typeof decimal === 'string' || isNaN(decimal) ? `${value}` : `${decimal}`; | ||
} |
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,8 @@ | ||
export function percentageToDecimal(value: string | number): string | number { | ||
if (!`${value}`.endsWith('%')) { | ||
return `${value}`; | ||
} | ||
const percentValue = `${value}`.slice(0, -1); | ||
const numberValue = parseFloat(percentValue); | ||
return numberValue / 100; | ||
} |
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { transformOpacity } from '../../src/transformOpacity.js'; | ||
import { runTransformSuite } from '../suites/transform-suite.spec.js'; | ||
|
||
runTransformSuite(transformOpacity as (value: unknown) => unknown); | ||
|
||
describe('transform opacity', () => { | ||
it('transforms opacity % to unit-less decimal value', () => { | ||
expect(transformOpacity('50%')).to.equal('0.5'); | ||
}); | ||
|
||
it("does not transform opacity if it doesn't end with %", () => { | ||
expect(transformOpacity('100')).to.equal('100'); | ||
}); | ||
|
||
it('does not transform opacity if it cannot be parsed as float', () => { | ||
expect(transformOpacity('not-a-float%')).to.equal('not-a-float%'); | ||
}); | ||
}); |
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,18 @@ | ||
import { expect } from '@esm-bundle/chai'; | ||
import { percentageToDecimal } from '../../../src/utils/percentageToDecimal.js'; | ||
|
||
describe('percentage to decimal', () => { | ||
it('converts percentage strings to decimal numbers', () => { | ||
expect(percentageToDecimal('100%')).to.equal(1); | ||
expect(percentageToDecimal('50%')).to.equal(0.5); | ||
}); | ||
|
||
it('ignores values that do not end with a percentage character', () => { | ||
expect(percentageToDecimal('100')).to.equal('100'); | ||
expect(percentageToDecimal('foo')).to.equal('foo'); | ||
}); | ||
|
||
it('returns NaN if percentage cannot be parsed as a float', () => { | ||
expect(percentageToDecimal('foo%')).to.be.NaN; | ||
}); | ||
}); |