-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(style): split to Style and TemplateStyle
- Loading branch information
Showing
2 changed files
with
58 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters