Skip to content

Commit

Permalink
feat: devtool config
Browse files Browse the repository at this point in the history
it coloses the feat morfeojs#101
  • Loading branch information
andreaSimonePorceddu committed Nov 2, 2021
1 parent cdce5df commit 842b8ff
Show file tree
Hide file tree
Showing 17 changed files with 257 additions and 50 deletions.
1 change: 1 addition & 0 deletions devtool/src/_shared/components/Grid/Item/style.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
min-width: 25%;
box-sizing: border-box;
position: relative;
flex-grow: 1;
}

@media screen and (min-width: 375px) {
Expand Down
50 changes: 42 additions & 8 deletions devtool/src/_shared/components/Slices/Components/Detail/index.tsx
Original file line number Diff line number Diff line change
@@ -1,30 +1,64 @@
import { Component } from '@morfeo/react';
import { Color, Component, component, morfeo } from '@morfeo/react';
import clsx from 'clsx';
import { useMemo, useState, useCallback } from 'react';
import { useRouter } from '../../../../hooks';
import { MorfeoComponent } from '../MorfeoComponent';

import styles from './style.module.css';
import { Card } from '../../../Card';
import { Icon } from '../../../Icon';

export const Detail = () => {
const { route } = useRouter();
const { state } = route;
const { detailKey: componentName = '', componentVariant } = state || {};
const { meta } =
component(componentName as Component, componentVariant).get() || {};
const { devtoolConfig } = meta || {};
const [defaultStyleActive, setDefaultStyleActive] = useState(true);

const currentThemeName = morfeo.getCurrent();

const computedBackground = useMemo(() => {
if (typeof devtoolConfig?.background === 'object') {
return devtoolConfig?.background[currentThemeName];
}
return (devtoolConfig?.background as Color) || ('#fff' as Color);
}, [currentThemeName, devtoolConfig?.background]);

const onDefaultStyleCheckboxChange = useCallback(() => {
setDefaultStyleActive(state => !state);
}, [])

return (
<div className={styles.container}>
<div
className={clsx(
'morfeo-card-primary-clickable',
styles.previewContainer,
)}
{devtoolConfig?.style && (
<div className={styles.defaultStyleAlertContainer}>
<input type="checkbox" checked={defaultStyleActive} onChange={onDefaultStyleCheckboxChange} />
<div className="ml-xxs">
<Icon name="warning" />
</div>
<h4 className="morfeo-typography-h4">
Web extension default style
</h4>
<div>
<Icon name="warning" />
</div>
</div>
)}
<Card
className={clsx('morfeo-card-primary', styles.previewContainer)}
style={{
bg: computedBackground,
}}
>
<MorfeoComponent
applyDefaultStyle={defaultStyleActive}
name={componentName as Component}
variant={componentVariant}
>
{componentName}
</MorfeoComponent>
</div>
</Card>
</div>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
padding-right: var(--spacings-xs);
}

.defaultStyleAlertContainer {
display: flex;
align-items: center;
margin-bottom: var(--spacings-xxs);
}

.icon {
margin-right: var(--spacings-xxs);
}

.previewContainer {
width: 100%;
padding-top: var(--spacings-xxl);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import clsx from 'clsx';
type Props = {
name: Component;
variant?: string;
applyDefaultStyle?: boolean;
};

type TagName = keyof HTMLElementTagNameMap;
Expand All @@ -13,12 +14,18 @@ export const MorfeoComponent: React.FC<Props> = ({
name,
variant,
children,
applyDefaultStyle,
...props
}) => {
const { tag = 'div', props: componentProps = {} } =
component(name, variant).get() || {};
const {
tag = 'div',
props: componentProps = {},
meta,
} = component(name, variant).get() || {};
const { devtoolConfig } = meta || {};
const { classes } = getStyles({
[name]: {
...(applyDefaultStyle && devtoolConfig?.style),
componentName: name as Component,
variant,
},
Expand All @@ -37,6 +44,6 @@ export const MorfeoComponent: React.FC<Props> = ({
...(props as any).style,
...(componentProps as any).style,
},
children,
children: devtoolConfig?.label || children,
});
};
21 changes: 17 additions & 4 deletions devtool/src/_shared/components/Slices/Components/Preview.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useCallback } from 'react';
import { Component, component } from '@morfeo/react';
import React, { useCallback, useMemo} from 'react';
import { Color, Component, component, morfeo } from '@morfeo/react';
import { useRouter } from '../../../hooks/useRouter';
import { RouteName } from '../../../contexts';
import { SliceName } from '../../../contexts/Routing/types';
Expand Down Expand Up @@ -41,7 +41,10 @@ const Info: React.FC<Props> = ({ name, variant }) => {

export const Preview: React.FC<Props> = ({ name, variant }) => {
const { navigate } = useRouter();

const { meta } =
component(name, variant).get() || {};
const currentThemeName = morfeo.getCurrent();
const { devtoolConfig } = meta || {};
const onClick = useCallback(() => {
navigate(RouteName.COMPONENT, {
slice: SliceName.COMPONENTS,
Expand All @@ -50,6 +53,13 @@ export const Preview: React.FC<Props> = ({ name, variant }) => {
});
}, [name, variant, navigate]);

const computedBackground = useMemo(() => {
if (typeof devtoolConfig?.background === 'object') {
return devtoolConfig?.background[currentThemeName]
}
return (devtoolConfig?.background as Color) || '#fff' as Color
}, [currentThemeName, devtoolConfig?.background])

return (
<Card
className={clsx(
Expand All @@ -62,8 +72,11 @@ export const Preview: React.FC<Props> = ({ name, variant }) => {
kind="squared"
className={styles.previewContainer}
copyText={variant || name}
style={{
bg: computedBackground,
}}
>
<MorfeoComponent name={name} variant={variant}>
<MorfeoComponent applyDefaultStyle name={name} variant={variant}>
{name}
</MorfeoComponent>
</Card>
Expand Down
9 changes: 2 additions & 7 deletions devtool/src/_shared/css/dark-components.css
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,18 @@
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="dark"] .morfeo-typography-p1 {
font-size: var(--font-sizes-m);
font-size: var(--font-sizes-s);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p2 {
font-size: var(--font-sizes-s);
font-size: var(--font-sizes-xs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p3 {
font-size: var(--font-sizes-xs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
}
[data-morfeo-theme="dark"] .morfeo-typography-p4 {
font-size: var(--font-sizes-xxs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
Expand Down
9 changes: 2 additions & 7 deletions devtool/src/_shared/css/light-components.css
Original file line number Diff line number Diff line change
Expand Up @@ -159,23 +159,18 @@
line-height: var(--line-heights-heading);
}
[data-morfeo-theme="light"] .morfeo-typography-p1 {
font-size: var(--font-sizes-m);
font-size: var(--font-sizes-s);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="light"] .morfeo-typography-p2 {
font-size: var(--font-sizes-s);
font-size: var(--font-sizes-xs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
letter-spacing: var(--letter-spacings-body);
}
[data-morfeo-theme="light"] .morfeo-typography-p3 {
font-size: var(--font-sizes-xs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
}
[data-morfeo-theme="light"] .morfeo-typography-p4 {
font-size: var(--font-sizes-xxs);
font-family: var(--fonts-regular);
line-height: var(--line-heights-body);
Expand Down
11 changes: 2 additions & 9 deletions devtool/src/_shared/styles/components/typography.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,20 @@ export const Typography: ComponentConfig = {
p1: {
tag: 'p',
style: {
fontSize: 'm',
fontSize: 's',
letterSpacing: 'body',
lineHeight: 'body',
},
},
p2: {
tag: 'p',
style: {
fontSize: 's',
fontSize: 'xs',
letterSpacing: 'body',
lineHeight: 'body',
},
},
p3: {
tag: 'p',
style: {
fontSize: 'xs',
lineHeight: 'body',
},
},
p4: {
tag: 'p',
style: {
fontSize: 'xxs',
Expand Down
6 changes: 3 additions & 3 deletions devtool/src/devtool/pages/Home/SliceCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ export const SliceCard: React.FC<Props> = ({ name, values, status }) => {
return undefined;
}
return (
<p className={clsx('morfeo-typography-p1', styles.value)}>{values}</p>
<p className={clsx('morfeo-typography-p2', styles.value)}>{values}</p>
);
}, [isComingSoon, values]);

Expand All @@ -56,12 +56,12 @@ export const SliceCard: React.FC<Props> = ({ name, values, status }) => {
return (
<p
className={clsx(
'morfeo-typography-p1',
'morfeo-typography-p2',
styles.value,
styles.valueLabel,
)}
>
{labelText}
{labelText.toUpperCase()}
</p>
);
}, [isEmpty, labelText]);
Expand Down
4 changes: 3 additions & 1 deletion devtool/src/devtool/pages/Home/style.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -56,15 +56,17 @@
position: absolute;
margin-left: var(--spacings-xxs);
margin-right: var(--spacings-xxs);
transition: var(--transitions-medium);
transition: var(--transitions-none);
}

.valuesContainer:hover, .valuesContainer.comingSoon {
width: fit-content;
transition: var(--transitions-medium);
}

.sliceContainer:hover .valueLabel, .valuesContainer.comingSoon .valueLabel {
max-width: 10rem;
opacity: 1;
position: relative;
transition: var(--transitions-medium);
}
48 changes: 48 additions & 0 deletions docs/docs/Features/web-extension.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,54 @@ enableMorfeoDevTool();
morfeo.setTheme('light', myTheme);
```

## Components customization

:::caution Use it with caution!
This feature is very helpful to present your components in a nice way but it will change the real component style.
So, use it consciously.
:::

Sometimes the style of a component couldn't be enough consistent to present it in a nice way inside the web extension.

```typescript
const Flex = {
tag: 'div',
style: {
display: 'flex'
}
}
```

In this case it could be needed to add an additional style, in order to present the component in a better way. You can use the `devtoolConfig` property inside `meta` to define these additional behaviors.

```typescript
const Flex = {
tag: 'div',
style: {
display: 'flex'
},
meta: {
devtoolConfig: {
label: 'Flex Div',
style: {
size: '100px',
bg: 'gray.light'
},
background: 'white',
}
}
}
```

### devtoolConfig Props

| prop | description | type |
|-------|-------------|--------|
| label | the text used as component children in the web extension | `string` |
| style | the default style of the component in the web extension | `Style` |
| background | the default background for the card that contains the component | `Color` `Record<string, Color>` (where the string is the theme name) |


## Contribute

We want to improve the web extension as much as possible and add functionalities, If you want to help us, check our [GitHub](https://github.com/VLK-STUDIO/morfeo)!
Expand Down
35 changes: 35 additions & 0 deletions docs/src/styles/components/box.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { ComponentConfig } from '@morfeo/react';

export const Box: ComponentConfig = {
tag: 'div',
style: {
fontFamily: 'regular'
},
variants: {
flexRow: {
style: {
display: 'flex',
flexDirection: 'row'
}
},
flexColumn: {
style: {
flexDirection: 'column'
}
}
},
meta: {
description: 'a simple div',
devtoolConfig: {
background: 'background',
style: {
width: '80px',
height: '80px',
backgroundColor: 'gray.lighter',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}
}
}
} as ComponentConfig;
Loading

0 comments on commit 842b8ff

Please sign in to comment.