Skip to content

Commit

Permalink
feat(style): split to Style and TemplateStyle
Browse files Browse the repository at this point in the history
  • Loading branch information
MrTelanie committed Apr 7, 2020
1 parent 438bedf commit e4cf841
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 62 deletions.
72 changes: 38 additions & 34 deletions src/style.js
Original file line number Diff line number Diff line change
@@ -1,42 +1,46 @@
const NAME = Symbol('name');
const TEMPLATE = Symbol('template');
const TARGET = Symbol('target');
const KEYS = Symbol('keys');

function getCSSVars(name, target, keys) {
let prefix = '';
if (name) {
prefix = `-${name}-`;
export class Style {
constructor(name = undefined) {
this.name = name;
}

return keys.reduce((res, key) => {
const t = target[key];
if (t.toCSSVars) {
t.toCSSVars(`${prefix}${key}`, res);
} else {
res[`-${prefix}-${key}`] = t.toString();
get cssVars() {
let prefix = '';
const { name } = this;
if (name) {
prefix = `${name}-`;
}
return res;
}, {});

return Object.entries(this).reduce((res, [key, t]) => {
if (key === 'name' || key === 'cssVars' || key === 'template') {
return res;
}
if (t.toCSSVars) {
t.toCSSVars(`${prefix}${key}`, res);
} else {
res[`--${prefix}${key}`] = t.toString();
}
return res;
}, {});
}
}

const handler = {
get(obj, prop) {
if (prop === 'cssTemplate') {
return obj[TEMPLATE];
}
if (prop === 'cssVars') {
return getCSSVars(obj[NAME], obj, obj[KEYS]);
}
return obj[prop];
export class TemplateStyle extends Style {
constructor(template, name = undefined) {
super(name);
this.template = template;
}
};

export function style({ name, template, ...definition }) {
const proxy = new Proxy(definition, handler);
proxy[NAME] = name;
proxy[TEMPLATE] = template;
proxy[TARGET] = definition;
proxy[KEYS] = Object.keys(definition);
return proxy;
bakeStyle() {
const { cssVars } = this;
const vars = Object.entries(cssVars).map(([key, v]) => `${key}: ${v}`).join((';\n'));
return `\n${this.template}\n${vars}\n`;
}
}

export function style(name = undefined) {
return new Style(name);
}

export function templateStyle(template, name = undefined) {
return new TemplateStyle(template, name);
}
48 changes: 20 additions & 28 deletions test/style.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { assert } from 'chai';
import { style } from '../src/style';
import { style, templateStyle } from '../src/style';
import { victor } from '../src';

describe('style test.', () => {
Expand All @@ -12,20 +11,19 @@ describe('style test.', () => {
rotateZ(calc(var(--rotation-z) * 1deg))
rotateX(calc(var(--rotation-x) * 1deg))
rotateY(calc(var(--rotation-y) * 1deg))
translate3d(calc(--position-x * 1px), calc(--position-y * 1px), calc(--position-z * 1px))
`;
const definition = {
template,
fov: 450,
position: victor(5, 6, 7),
rotation: victor(45, 6, 7),
};
translate3d(calc(--position-x * 1px), calc(--position-y * 1px), calc(--position-z * 1px));
perspective: calc(var(--fov) * 1px);`;

const matrix = style(definition);
const matrix = style();

matrix.position = victor(-5, -6, -7);
matrix.fov = 450;
matrix.position = victor(5, 6, 7);
matrix.rotation = victor(45, 6, 7);

console.log('hallo', matrix.cssTemplate, matrix.cssVars);
console.log('css vars', matrix.cssVars);
console.log('for template', template);

// assert.equal(formatNumber(0.00000001), '0.00000001');

Expand All @@ -34,26 +32,20 @@ describe('style test.', () => {
it('a quaternion should behave like static fromOrientation from real Quaternion clas', () => {

const template = `
transform:
rotateY(calc(var(--alpha-y) * 1deg))
rotateZ(calc(var(--beta-z) * 1deg))
rotateX(calc(var(--gamma-x) * 1deg))
rotateZ(-90deg))
`;
const definition = {
template,
alpha: 450,
beta: 10,
gamma: 12,
orientation: 0
};
transform:
rotateY(calc(var(--gyro-alpha) * 1deg))
rotateZ(calc(var(--gyro-beta) * 1deg))
rotateX(calc(var(--gyro-gamma) * 1deg))
rotateZ(-90deg));
`;

const matrix = style(definition);
const matrix = templateStyle(template, 'gyro');

matrix.beta = 20;
matrix.orientation = 90;
matrix.alpha = 20;
matrix.gamma = 12;

console.log('hallo', matrix.cssTemplate, matrix.cssVars);
console.log('baked style', matrix.bakeStyle());

// assert.equal(formatNumber(0.00000001), '0.00000001');

Expand Down

0 comments on commit e4cf841

Please sign in to comment.