-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: [FX-4259] add new spacing values to picasso (#3835)
* feat: add new spacing values to picasso * chore: enhance spacings * chore: update spacings * chore: add unit tests for spacings * chore: update changeset * chore: fix story
- Loading branch information
Showing
13 changed files
with
238 additions
and
29 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,7 @@ | ||
--- | ||
'@toptal/picasso-provider': minor | ||
'@toptal/picasso': minor | ||
'@toptal/picasso-shared': minor | ||
--- | ||
|
||
- add new spacing values to picasso's theme and to `picasso-shared`. See RFC: https://github.com/toptal/picasso/blob/master/docs/decisions/18-spacings.md |
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,75 @@ | ||
// BASE-aligned spacing values in "rem" units | ||
type PicassoSpacingValues = 0 | 0.25 | 0.5 | 0.75 | 1 | 1.5 | 2 | 2.5 | 3 | ||
|
||
export type Sizes = | ||
| 'xxsmall' | ||
| 'xsmall' | ||
| 'small' | ||
| 'medium' | ||
| 'large' | ||
| 'xlarge' | ||
|
||
export type SizeType<T extends Sizes> = T | ||
|
||
/** @deprecated **/ | ||
type DeprecatedSpacingType = | ||
| number | ||
| SizeType<'xsmall' | 'small' | 'medium' | 'large' | 'xlarge'> | ||
|
||
export type SpacingType = PicassoSpacing | DeprecatedSpacingType | ||
|
||
export enum SpacingEnum { | ||
xsmall = 0.5, | ||
small = 1, | ||
medium = 1.5, | ||
large = 2, | ||
xlarge = 2.5, | ||
} | ||
|
||
class PicassoSpacing { | ||
#value: PicassoSpacingValues | ||
|
||
private constructor(value: PicassoSpacingValues) { | ||
this.#value = value | ||
} | ||
|
||
static create(value: PicassoSpacingValues): PicassoSpacing { | ||
return new PicassoSpacing(value) | ||
} | ||
|
||
valueOf(): PicassoSpacingValues { | ||
return this.#value | ||
} | ||
|
||
toString(): string { | ||
return this.#value.toString() | ||
} | ||
} | ||
|
||
export type { PicassoSpacing } | ||
|
||
export const isPicassoSpacing = ( | ||
spacing: SpacingType | ||
): spacing is PicassoSpacing => spacing instanceof PicassoSpacing | ||
|
||
export const SPACING_0 = PicassoSpacing.create(0) | ||
export const SPACING_1 = PicassoSpacing.create(0.25) | ||
export const SPACING_2 = PicassoSpacing.create(0.5) | ||
export const SPACING_3 = PicassoSpacing.create(0.75) | ||
export const SPACING_4 = PicassoSpacing.create(1) | ||
export const SPACING_6 = PicassoSpacing.create(1.5) | ||
export const SPACING_8 = PicassoSpacing.create(2) | ||
export const SPACING_10 = PicassoSpacing.create(2.5) | ||
export const SPACING_12 = PicassoSpacing.create(3) | ||
|
||
export default { | ||
SPACING_0, | ||
SPACING_1, | ||
SPACING_2, | ||
SPACING_3, | ||
SPACING_4, | ||
SPACING_6, | ||
SPACING_8, | ||
SPACING_10, | ||
SPACING_12, | ||
} |
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,2 +1,3 @@ | ||
export * from './generate-random-string' | ||
export * from './get-serverside-stylesheets' | ||
export * from './spacings' |
74 changes: 74 additions & 0 deletions
74
packages/picasso-provider/src/Picasso/utils/spacings.test.ts
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,74 @@ | ||
import { isNumericSpacing, spacingToRem } from './spacings' | ||
import { | ||
SPACING_0, | ||
SPACING_1, | ||
SPACING_2, | ||
SPACING_3, | ||
SPACING_4, | ||
SPACING_6, | ||
SPACING_8, | ||
SPACING_10, | ||
SPACING_12, | ||
} from '../config/spacings' | ||
|
||
describe('spacingUtils', () => { | ||
describe('isNumericSpacing', () => { | ||
describe('when spacing is undefined', () => { | ||
it('returns false', () => { | ||
expect(isNumericSpacing(undefined)).toBe(false) | ||
}) | ||
}) | ||
|
||
describe('when spacing is a number', () => { | ||
it('returns true', () => { | ||
expect(isNumericSpacing(1)).toBe(true) | ||
expect(isNumericSpacing(2.5)).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('when spacing is a new Picasso spacing', () => { | ||
it('returns true', () => { | ||
expect(isNumericSpacing(SPACING_0)).toBe(true) | ||
}) | ||
}) | ||
|
||
describe('when spacing is a string Picasso spacing', () => { | ||
it('returns false', () => { | ||
expect(isNumericSpacing('small')).toBe(false) | ||
}) | ||
}) | ||
}) | ||
|
||
describe('spacingToRem', () => { | ||
describe('when spacing is a number', () => { | ||
it('converts to rem', () => { | ||
expect(spacingToRem(1)).toBe('1rem') | ||
expect(spacingToRem(2.5)).toBe('2.5rem') | ||
}) | ||
}) | ||
|
||
describe('when spacing is a valid Picasso spacing', () => { | ||
it('converts to rem', () => { | ||
expect(spacingToRem(SPACING_0)).toBe('0rem') | ||
expect(spacingToRem(SPACING_1)).toBe('0.25rem') | ||
expect(spacingToRem(SPACING_2)).toBe('0.5rem') | ||
expect(spacingToRem(SPACING_3)).toBe('0.75rem') | ||
expect(spacingToRem(SPACING_4)).toBe('1rem') | ||
expect(spacingToRem(SPACING_6)).toBe('1.5rem') | ||
expect(spacingToRem(SPACING_8)).toBe('2rem') | ||
expect(spacingToRem(SPACING_10)).toBe('2.5rem') | ||
expect(spacingToRem(SPACING_12)).toBe('3rem') | ||
}) | ||
}) | ||
|
||
describe('when spacing is a string Picasso spacing', () => { | ||
it('converts to rem', () => { | ||
expect(spacingToRem('xsmall')).toBe('0.5rem') | ||
expect(spacingToRem('small')).toBe('1rem') | ||
expect(spacingToRem('medium')).toBe('1.5rem') | ||
expect(spacingToRem('large')).toBe('2rem') | ||
expect(spacingToRem('xlarge')).toBe('2.5rem') | ||
}) | ||
}) | ||
}) | ||
}) |
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 type { SizeType, SpacingType } from '../config/spacings' | ||
import { SpacingEnum, isPicassoSpacing } from '../config/spacings' | ||
|
||
const isNumericSpacing = ( | ||
spacing: SpacingType | undefined | ||
): spacing is number | SpacingType => { | ||
const isNotNull = spacing != null | ||
const isNumber = typeof spacing == 'number' | ||
|
||
return isNotNull && (isNumber || isPicassoSpacing(spacing)) | ||
} | ||
|
||
const spacingToRem = (spacing: SpacingType): string => { | ||
return isNumericSpacing(spacing) | ||
? `${spacing}rem` | ||
: `${SpacingEnum[spacing as SizeType<any>]}rem` | ||
} | ||
|
||
export { isNumericSpacing, spacingToRem } |
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