Skip to content

Commit

Permalink
feat: added slices and optimizations to dev tool
Browse files Browse the repository at this point in the history
  • Loading branch information
mauroerta committed Jun 2, 2021
1 parent 0b98dfd commit a53f5e8
Show file tree
Hide file tree
Showing 12 changed files with 186 additions and 80 deletions.
29 changes: 29 additions & 0 deletions apps/web-sandbox/src/theme.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,35 @@ export const lightTheme = {
borderWidths: {
s: '2px',
},
shadows: {
none: {
color: 'transparent',
offset: { width: 0, height: 0 },
opacity: 0,
elevation: 0,
},
soft: {
color: 'accent',
offset: { width: 0, height: 2 },
opacity: 0.2,
radius: 4,
elevation: 2,
},
medium: {
color: 'black',
offset: { width: 0, height: 3 },
opacity: 0.3,
radius: 4,
elevation: 3,
},
strong: {
color: 'black',
offset: { width: 0, height: 4 },
opacity: 0.4,
radius: 4,
elevation: 4,
},
},
breakpoints: {
md: '900px',
lg: '1300px',
Expand Down
27 changes: 5 additions & 22 deletions packages/dev-tools/src/devtools/builders/colors.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,9 @@
import { theme } from '@morfeo/web';
import { injectStyle } from './injectStyle';
import { makeRow } from './makeRow';

export function colors() {
const { colors } = theme.get();
const colorKeys = Object.keys(colors);
const colorSlice = document.getElementById('colors');
colorKeys.forEach(key => {
const colorBlock = document.createElement('div');
colorBlock.classList.add('item', 'column', 'centered');
const colorBall = document.createElement('div');
colorBall.classList.add('circle');
const colorName = document.createElement('span');
const colorValue = document.createElement('span');

injectStyle(colorBall, { bg: key });

colorName.innerHTML = key;
colorValue.innerHTML = theme.getValue('colors', key);

colorBlock.appendChild(colorName);
colorBlock.appendChild(colorBall);
colorBlock.appendChild(colorValue);
colorSlice.appendChild(colorBlock);
makeRow({
slice: 'colors',
property: 'bg',
kind: 'circle',
});
}
28 changes: 9 additions & 19 deletions packages/dev-tools/src/devtools/builders/gradients.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
import { theme } from '@morfeo/web';
import { injectStyle } from './injectStyle';
import { parsers } from '@morfeo/web';
import { makeRow } from './makeRow';

export function gradients() {
const gradients = theme.getSlice('gradients');
const gradientKeys = Object.keys(gradients);
const gradientSlice = document.getElementById('gradients');
gradientKeys.forEach(key => {
const gradientBlock = document.createElement('div');
gradientBlock.classList.add('item', 'column', 'centered');
const circle = document.createElement('div');
circle.classList.add('circle');
const gradientName = document.createElement('span');

injectStyle(circle, { gradient: key });

gradientName.innerHTML = key;

gradientBlock.appendChild(gradientName);
gradientBlock.appendChild(circle);
gradientSlice.appendChild(gradientBlock);
makeRow({
slice: 'gradients',
property: 'gradient',
kind: 'circle',
getValue(_, value) {
return parsers.resolve({ gradient: value })['background'];
},
});
}
18 changes: 18 additions & 0 deletions packages/dev-tools/src/devtools/builders/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
import { radii } from './radii';
import { colors } from './colors';
import { gradients } from './gradients';
import { sizes, spaces } from './sizes';
import { components } from './components';
import { shadows } from './shadows';

export * from './sizes';
export * from './radii';
export * from './colors';
export * from './gradients';
export * from './components';

export const all = [
colors,
sizes,
radii,
spaces,
shadows,
gradients,
components,
];
39 changes: 39 additions & 0 deletions packages/dev-tools/src/devtools/builders/makeRow.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { theme } from '@morfeo/web';
import { injectStyle } from './injectStyle';

export function makeRow({
slice,
kind = 'square',
property,
showValue = true,
getValue,
style = {},
}) {
const themeSlice = theme.getSlice(slice);
const sliceKeys = Object.keys(themeSlice);
const sliceElement = document.getElementById(slice);
sliceKeys.forEach(key => {
const block = document.createElement('div');
block.classList.add('item', 'column', 'centered');
const element = document.createElement('div');
element.classList.add(kind);
const elementName = document.createElement('span');

injectStyle(element, { ...style, [property]: key });

elementName.innerHTML = key;

block.appendChild(elementName);
block.appendChild(element);

if (showValue) {
const elementValue = document.createElement('span');
elementValue.innerHTML = getValue
? getValue(slice, key)
: theme.getValue(slice, key);
block.appendChild(elementValue);
}

sliceElement.appendChild(block);
});
}
8 changes: 8 additions & 0 deletions packages/dev-tools/src/devtools/builders/radii.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { makeRow } from './makeRow';

export function radii() {
makeRow({
slice: 'radii',
property: 'borderRadius',
});
}
12 changes: 12 additions & 0 deletions packages/dev-tools/src/devtools/builders/shadows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { parsers } from '@morfeo/web';
import { makeRow } from './makeRow';

export function shadows() {
makeRow({
slice: 'shadows',
property: 'shadow',
getValue(_, value) {
return parsers.resolve({ shadow: value })['boxShadow'];
},
});
}
34 changes: 3 additions & 31 deletions packages/dev-tools/src/devtools/builders/sizes.js
Original file line number Diff line number Diff line change
@@ -1,37 +1,9 @@
import { theme } from '@morfeo/web';
import { injectStyle } from './injectStyle';

function makeRow(themeKey, pageClass) {
const values = theme.getSlice(themeKey);
const keys = Object.keys(values);
const slice = document.getElementById(pageClass);
keys.forEach(key => {
const block = document.createElement('div');
block.classList.add('item', 'column', 'centered');
const square = document.createElement('div');
square.classList.add('square');
const name = document.createElement('span');
const value = document.createElement('span');

injectStyle(square, {
bg: '#2c3e50',
width: theme.getValue(themeKey, key),
});

name.innerHTML = key;
value.innerHTML = theme.getValue(themeKey, key);

block.appendChild(name);
block.appendChild(square);
block.appendChild(value);
slice.appendChild(block);
});
}
import { makeRow } from './makeRow';

export function spaces() {
makeRow('space', 'spaces');
makeRow({ slice: 'space', property: 'width' });
}

export function sizes() {
makeRow('sizes', 'sizes');
makeRow({ slice: 'sizes', property: 'width' });
}
20 changes: 20 additions & 0 deletions packages/dev-tools/src/devtools/events.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const collapseButtons = document.querySelectorAll('.collapse');

collapseButtons.forEach(button => {
button.addEventListener('click', e => {
const target = button.getAttribute('data-target');

const collapsible = document.getElementById(target);
if (!collapsible) {
return;
}

if (collapsible.classList.contains('collapsed')) {
collapsible.classList.remove('collapsed');
button.innerHTML = `▲`;
} else {
collapsible.classList.add('collapsed');
button.innerHTML = `▼`;
}
});
});
7 changes: 3 additions & 4 deletions packages/dev-tools/src/devtools/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { theme } from '@morfeo/web';
import { GET_THEME_ACTION, MORFEO_DEVTOOLS } from '../constants';
import { colors, components, spaces, sizes, gradients } from './builders';

const builders = [colors, spaces, sizes, gradients, components];
import { all } from './builders';
import './events';

function reset() {
const dynamicContents = document.querySelectorAll('.dynamic');
Expand All @@ -12,7 +11,7 @@ function reset() {
}

function build() {
builders.forEach(builder => builder());
all.forEach(builder => builder());
}

theme.subscribe(build);
Expand Down
23 changes: 22 additions & 1 deletion packages/dev-tools/src/views/index.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ h1.title {

.row {
display: flex;
flex-wrap: wrap;
flex-direction: row;
}

Expand All @@ -22,6 +23,7 @@ h1.title {
}

.slice {
position: relative;
color: #2c3e50;
padding: 16px;
padding-top: 8px;
Expand All @@ -33,19 +35,38 @@ h1.title {
-moz-box-shadow: 1px 9px 32px 2px rgba(0, 0, 0, 0.24);
}

button.collapse {
position: absolute;
top: 10px;
right: 20px;
border: none;
}

.collapsed {
display: none;
}

.item {
flex: 1 1 auto;
display: flex;
flex-basis: calc(25%);
margin-bottom: 10px;
}

.circle {
background-color: #2c3e50;
width: 50px;
height: 50px;
border-radius: 50%;
margin-top: 5px;
margin-bottom: 5px;
}

.square {
background-color: #2c3e50;
width: 50px;
height: 50px;
margin-top: 5px;
margin-bottom: 5px;
}

.centered {
Expand Down
21 changes: 18 additions & 3 deletions packages/dev-tools/src/views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,37 @@ <h1 class="title">@morfeo</h1>
<div id="content">
<div class="slice">
<h2>colors</h2>
<button class="collapse" data-target="colors">&#x25B2;</button>
<div id="colors" class="row dynamic"></div>
</div>
<div class="slice">
<h2>gradients</h2>
<button class="collapse" data-target="gradients">&#x25B2;</button>
<div id="gradients" class="row dynamic"></div>
</div>
<div class="slice">
<h2>shadows</h2>
<button class="collapse" data-target="shadows">&#x25B2;</button>
<div id="shadows" class="row dynamic"></div>
</div>
<div class="slice">
<h2>space</h2>
<div id="spaces" class="row dynamic"></div>
<button class="collapse" data-target="space">&#x25B2;</button>
<div id="space" class="row dynamic"></div>
</div>
<div class="slice">
<h2>sizes</h2>
<button class="collapse" data-target="sizes">&#x25B2;</button>
<div id="sizes" class="row dynamic"></div>
</div>
<div class="slice">
<h2>gradients</h2>
<div id="gradients" class="row dynamic"></div>
<h2>radii</h2>
<button class="collapse" data-target="radii">&#x25B2;</button>
<div id="radii" class="row dynamic"></div>
</div>
<div class="slice">
<h2>components</h2>
<button class="collapse" data-target="components">&#x25B2;</button>
<div id="components" class="column dynamic"></div>
</div>
</div>
Expand Down

0 comments on commit a53f5e8

Please sign in to comment.