Skip to content

Commit

Permalink
feat: web styled-components package introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed May 10, 2021
1 parent 356cbd6 commit a7677c3
Show file tree
Hide file tree
Showing 23 changed files with 343 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# See https://help.github.com/ignore-files/ for more about ignoring files.
build
__snapshots__

# dependencies
node_modules
Expand Down
1 change: 1 addition & 0 deletions apps/web-sandbox/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
"dependencies": {
"@morfeo/hooks": "^0.0.3",
"@morfeo/web": "^0.0.3",
"@morfeo/styled-components-web": "^0.0.3",
"@testing-library/jest-dom": "^5.12.0",
"@testing-library/react": "^11.2.6",
"@testing-library/user-event": "^12.8.3",
Expand Down
5 changes: 2 additions & 3 deletions apps/web-sandbox/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import { useState, useCallback, FC } from 'react';
import { ThemeProvider } from 'styled-components';
import styled, { ThemeProvider } from '@morfeo/styled-components-web';
import { theme, Component } from '@morfeo/web';
import { useTheme, useStyles } from '@morfeo/hooks';
import { customStyled } from './styled';
import { darkTheme, lightTheme } from './theme';

theme.set(lightTheme);

const Button = customStyled('Button');
const Button = styled.Button({});

const StyledProvider: FC = ({ children }) => {
const currentTheme = useTheme();
Expand Down
30 changes: 0 additions & 30 deletions apps/web-sandbox/src/styled.tsx

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
"prettify": "npm run prettify:js && npm run prettify:ts",
"prettify:js": "npx prettier --write \"**/*.js\"",
"prettify:ts": "npx prettier --write \"**/*.ts\"",
"test": "jest",
"test:watch": "jest --watch",
"test:coverage": "jest --coverage",
"test": "jest --updateSnapshot",
"test:watch": "jest --watch --updateSnapshot",
"test:coverage": "jest --coverage --updateSnapshot",
"test:badges": "npm run test:coverage && jest-coverage-badges",
"publish": "npx lerna publish from-package --yes",
"version": "npx lerna version patch --no-push --no-git-tag-version --conventional-commits"
Expand Down
4 changes: 2 additions & 2 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"main": "build/index.js",
"module": "build/index.js",
"types": "build/index",
"typings": "build/index",
"types": "src/index",
"typings": "src/index",
"keywords": [
"design",
"system"
Expand Down
1 change: 0 additions & 1 deletion packages/core/src/parsers/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ const ADDITIONAL_PARSERS = {
const INITIAL_PARSERS = {
...DEFAULT_PARSERS,
...ADDITIONAL_PARSERS,
children: () => ({}),
};

type ParsersContext = {
Expand Down
2 changes: 1 addition & 1 deletion packages/core/tests/parsers/components.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe('components', () => {
property: 'componentName',
value: 'Button',
});
expect(result.componentTag).not.toBeDefined();
expect((result as any).componentTag).not.toBeDefined();
});

test('should return the default components style', () => {
Expand Down
9 changes: 8 additions & 1 deletion packages/core/tests/parsers/shadows.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Theme } from '@morfeo/spec';
import { shadows } from '../../src/parsers/shadows';
import { theme } from '../../src';
import { parsers, theme } from '../../src';

const THEME: Theme = {
colors: {
Expand Down Expand Up @@ -55,6 +55,13 @@ describe('shadows', () => {
});
});

test('should generate the property `boxShadow` if the prop `shadow` is passed', () => {
const result = parsers.resolve({ style: { shadow: 'strong' } });
expect(result).toEqual({
boxShadow: '10px 10px 30px white',
});
});

test('should generate the property `boxShadow` based on the light shadow', () => {
const result = shadows({ property: 'boxShadow', value: 'light' });
expect(result).toEqual({
Expand Down
6 changes: 3 additions & 3 deletions packages/hooks/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"main": "build/index.js",
"module": "build/index.js",
"types": "build/index",
"typings": "build/index",
"types": "src/index",
"typings": "src/index",
"keywords": [
"design",
"system"
Expand All @@ -20,7 +20,7 @@
"watch": "tsc -w"
},
"peerDependencies": {
"@morfeo/core": "^0.0.1",
"@morfeo/core": "^0.0.3",
"react": "17.0.2",
"react-dom": "^17.0.2"
},
Expand Down
34 changes: 0 additions & 34 deletions packages/native/CHANGELOG.md

This file was deleted.

4 changes: 2 additions & 2 deletions packages/native/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"main": "build/index.js",
"module": "build/index.js",
"types": "build/index",
"typings": "build/index",
"types": "src/index",
"typings": "src/index",
"keywords": [
"design",
"system",
Expand Down
18 changes: 5 additions & 13 deletions packages/native/src/parsers/shadows.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
parsers,
theme,
parsers,
Style,
Shadows,
ParserParams,
ShadowProperty,
Expand Down Expand Up @@ -43,17 +44,8 @@ export function shadowOffset({ value }) {
}

export function shadows({ value }: ParserParams<ShadowProperty>) {
if (
!theme ||
!theme.getSlice('shadows') ||
!theme.getValue('shadows', value as keyof Shadows)
) {
return {};
}
const { color, offset, opacity, radius, elevation } = theme.getValue(
'shadows',
value as keyof Shadows,
);
const { color, offset, opacity, radius, elevation } =
theme.getValue('shadows', value as keyof Shadows) || {};

return parsers.resolve({
style: {
Expand All @@ -62,6 +54,6 @@ export function shadows({ value }: ParserParams<ShadowProperty>) {
...(offset ? { shadowOffset: offset } : {}),
...(radius ? { shadowRadius: radius } : {}),
...(opacity ? { shadowOpacity: opacity } : {}),
} as any,
} as Style,
});
}
17 changes: 12 additions & 5 deletions packages/native/src/types/core.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,27 @@
import { Color, Opacity, Space } from '@morfeo/core';
import { ViewStyle, ImageStyle, TextStyle } from 'react-native';

type CustomNativeProperties = {
type CustomNativeStyle = {
shadowColor?: Color;
shadowOffset?: Space;
shadowRadius?: number;
shadowRadius?: Space;
shadowOpacity?: Opacity;
};

type CustomNativeProperties = {
shadowColor: 'colors';
shadowOffset: 'space';
shadowRadius: 'space';
shadowOpacity: 'opacities';
};

type NativeStyle = ViewStyle & ImageStyle & TextStyle;

type NativeCustomStyle = Omit<NativeStyle, keyof CustomNativeProperties> &
CustomNativeProperties;
type NativeCustomStyle = Omit<NativeStyle, keyof CustomNativeStyle> &
CustomNativeStyle;

interface NativeShadowConfig {
elevation?: number;
elevation?: Space;
}

declare module '@morfeo/core' {
Expand Down
115 changes: 115 additions & 0 deletions packages/native/tests/shadows.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import { parsers, theme } from '../src';
import { shadowOffset } from '../src/parsers';

const THEME = {
colors: {
primary: 'black',
secondary: 'white',
},
space: {
s: 10,
m: 20,
},
sizes: {
l: 30,
xl: 40,
},
shadows: {
strong: {
color: 'primary',
offset: { height: 's', width: 's' },
opacity: 0.4,
radius: 30,
},
light: {
color: 'secondary',
offset: { width: 'xl' },
opacity: 0.4,
radius: 30,
},
medium: {
elevation: 2,
color: 'secondary',
offset: { height: 30 },
opacity: 0.4,
},
custom: {
color: 'not inside theme',
offset: undefined,
},
noOffset: {
offset: {},
},
},
} as any;

afterAll(() => {
theme.set(THEME);
});

describe('shadows', () => {
beforeAll(() => {
theme.set(THEME);
});
afterAll(() => {
theme.reset();
});

test('should generate the property `boxShadow` based on the strong shadow', () => {
const result = parsers.resolve({ style: { shadow: 'strong' } });
expect(result).toEqual({
shadowColor: 'black',
shadowOffset: { height: 10, width: 10 },
shadowOpacity: 0.4,
shadowRadius: 30,
});
});

test('should generate the property `shadow` based on the light shadow', () => {
const result = parsers.resolve({ style: { shadow: 'light' } });
expect(result).toEqual({
shadowColor: 'white',
shadowOffset: { height: 0, width: 40 },
shadowOpacity: 0.4,
shadowRadius: 30,
});
});

test('should generate the property `shadow` based on the medium shadow', () => {
const result = parsers.resolve({ style: { shadow: 'medium' } });
expect(result).toEqual({
elevation: 2,
shadowColor: 'white',
shadowOffset: { height: 30, width: 0 },
shadowOpacity: 0.4,
});
});

test('should return an empty object if the shadow is not found', () => {
const result = parsers.resolve({ style: { shadow: 'none' } });
expect(result).toEqual({});
});

test('should use custom colors if they are not inside the theme', () => {
const result = parsers.resolve({
style: { shadow: 'custom' as any },
});
expect(result).toEqual({
shadowColor: 'not inside theme',
});
});

test('should not put any value with any empty configuration', () => {
const result = parsers.resolve({
style: { shadow: 'noOffset' as any },
});
expect(result).toEqual({});
});

test('should return an empty object if `shadowOffset` is undefined', () => {
const result = shadowOffset({
value: undefined,
});
expect(result).toEqual({});
});
});
4 changes: 2 additions & 2 deletions packages/spec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
"license": "MIT",
"main": "build/index.js",
"module": "build/index.js",
"types": "build/index",
"typings": "build/index",
"types": "src/index",
"typings": "src/index",
"keywords": [
"design",
"system"
Expand Down
Loading

0 comments on commit a7677c3

Please sign in to comment.