Skip to content

Commit

Permalink
feat: 76
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Sep 20, 2021
1 parent 2c3b774 commit 0a3a065
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 27 deletions.
8 changes: 2 additions & 6 deletions apps/web-sandbox/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,10 @@ export const components = {
transition: 'slow',
height: 'xxl',
width: 'xxl',
bg: {
md: 'primary',
lg: 'primary',
},
bg: 'primary',
color: 'white',
borderRadius: 'm',
borderStyle: 'solid',
borderColor: 'primary',
border: 'primary',
'&:hover': {
bg: 'transparent',
color: 'primary',
Expand Down
49 changes: 49 additions & 0 deletions packages/core/src/parsers/borders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import { bordersProperties, Border, BorderConfig } from '@morfeo/spec';
import { theme } from '../theme';
import { SliceParsers, ParserParams } from '../types';
import { baseParser } from './baseParser';

type BorderProperties = SliceParsers<
typeof bordersProperties,
keyof typeof bordersProperties
>;

function makeBorder({ color, style, width }: BorderConfig) {
const { borderColor } = baseParser({
property: 'borderColor',
scale: 'colors',
value: color,
});

const { borderWidth } = baseParser({
property: 'borderWidth',
scale: 'borderWidths',
value: width,
});

const { borderStyle } = baseParser({
property: 'borderStyle',
scale: 'borderStyles',
value: style,
});

return [borderWidth, borderStyle, borderColor].filter(Boolean).join(' ');
}

function borderParser({ value }: ParserParams<'border'>) {
const config = theme.getValue('borders', value as Border);

return {
border: !config ? value : makeBorder(config),
};
}

export const bordersParsers = Object.keys(bordersProperties).reduce(
(acc, curr) => {
return {
...acc,
[curr]: borderParser,
};
},
{},
) as BorderProperties;
2 changes: 2 additions & 0 deletions packages/core/src/parsers/createParsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { sizeParsers } from './sizes';
import { radiiParsers } from './radii';
import { colorsParsers } from './colors';
import { shadowsParsers } from './shadows';
import { bordersParsers } from './borders';
import { spacingsParsers } from './spacings';
import { componentsParses } from './components';
import { deepMerge } from '../utils';
Expand All @@ -43,6 +44,7 @@ const ADDITIONAL_PARSERS = {
...radiiParsers,
...colorsParsers,
...shadowsParsers,
...bordersParsers,
...spacingsParsers,
...componentsParses,
};
Expand Down
29 changes: 27 additions & 2 deletions packages/core/tests/parsers/borders.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,18 @@ import { parsers, theme } from '../../src';

const THEME: Theme = {
borders: {
primary: '1px solid red',
primary: {
width: 'm',
style: 'solid',
color: 'primary',
},
secondary: {
width: 'l',
color: 'primary',
},
},
colors: {
primary: 'red',
},
borderWidths: {
m: '3px',
Expand All @@ -26,7 +37,21 @@ describe('borders', () => {
test('should generate a style with border property', () => {
const result = parsers.resolve({ border: 'primary' });
expect(result).toEqual({
border: '1px solid red',
border: '3px solid red',
});
});

test('should generate a border style without borderStyle', () => {
const result = parsers.resolve({ border: 'secondary' } as any);
expect(result).toEqual({
border: '5px red',
});
});

test('should generate a border style from a regular css string', () => {
const result = parsers.resolve({ border: '4px dotted blue' } as any);
expect(result).toEqual({
border: '4px dotted blue',
});
});
});
Expand Down
4 changes: 2 additions & 2 deletions packages/preset-default/src/base/borders.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Borders } from '@morfeo/spec';

export const borders: Borders = {
primary: '1px solid dark',
secondary: '3px solid light',
primary: { width: 'm', style: 'solid', color: 'invertedBackground' },
secondary: { width: 's', style: 'solid', color: 'invertedBackground' },
};
2 changes: 0 additions & 2 deletions packages/preset-default/src/base/colors.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { Colors } from '@morfeo/spec';

export const baseColors = {
dark: '#2f2f2f',
error: '#d10343',
Expand Down
37 changes: 22 additions & 15 deletions packages/spec/src/types/borders.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import { bordersMap, borderStylesMap, borderWidthsMap } from '../properties';

export interface Borders {
primary: string;
secondary: string;
}

export type Border = keyof Borders;

type BaseBorderProps = {
[K in typeof bordersMap[number]]: Border;
};

export interface BorderProps extends BaseBorderProps {}

export type BorderProperty = keyof BorderProps;
import { Color } from './colors';

export interface BorderWidths {
xxs: string;
Expand Down Expand Up @@ -55,3 +41,24 @@ type BaseBorderStyleProps = {
export interface BorderStyleProps extends BaseBorderStyleProps {}

export type BorderStyleProperty = keyof BorderStyleProps;

export type BorderConfig = {
width?: BorderWidth;
color?: Color;
style?: BorderStyle;
};

export interface Borders {
primary: BorderConfig;
secondary: BorderConfig;
}

export type Border = keyof Borders;

type BaseBorderProps = {
[K in typeof bordersMap[number]]: Border;
};

export interface BorderProps extends BaseBorderProps {}

export type BorderProperty = keyof BorderProps;

0 comments on commit 0a3a065

Please sign in to comment.