@compositive/foundation
Version:
Compositive framework foundation package.
164 lines (161 loc) • 6.37 kB
JavaScript
import { __decorate, __classPrivateFieldSet, __classPrivateFieldGet } from 'tslib';
import { Memoized, NamedTupleMap } from '@compositive/commons-memoize';
import { Equals } from '@compositive/commons-predicates';
import { ChainEvaluator } from '@compositive/commons-spec';
import { Enumerable } from '@compositive/commons-util';
import { Theme } from './Theme.js';
var _ComputedTheme_instances, _ComputedTheme_chain, _ComputedTheme_buildEvaluator, _ThemeSnapshot_evaluator, _ThemeSnapshot_origin, _ThemeSnapshot_activeVariants;
// FIXME: The previous type in the definition thunks for non-existing attributes should be "never" instead of "unknown"
// FIXME: Variant options should merge, not be replaced
/**
* A {@link ComputedTheme} is a {@link Theme} implementation that allows for its values to be derived through the use of
* pure functions and derivation rules.
*
* It's possible to create new themes from scratch, or through the extension of other themes,
* by overriding only some of the values.
*/
class ComputedTheme extends Theme {
constructor(chain) {
super();
_ComputedTheme_instances.add(this);
_ComputedTheme_chain.set(this, void 0);
__classPrivateFieldSet(this, _ComputedTheme_chain, chain, "f");
}
static with(definition) {
return new ComputedTheme([
{ definition, variants: {} },
]);
}
using(activeVariants) {
return new ThemeSnapshot(__classPrivateFieldGet(this, _ComputedTheme_instances, "m", _ComputedTheme_buildEvaluator).call(this, activeVariants), this, activeVariants);
}
with(definition) {
return new ComputedTheme([
...__classPrivateFieldGet(this, _ComputedTheme_chain, "f"),
{ definition, variants: {} },
]);
}
when(variantName, options) {
return new ComputedTheme([
...__classPrivateFieldGet(this, _ComputedTheme_chain, "f"),
{ definition: {}, variants: { [variantName]: options } },
]);
}
get surfaces() {
return this.using({}).surfaces;
}
get typography() {
return this.using({}).typography;
}
get metrics() {
return this.using({}).metrics;
}
get palette() {
return this.using({}).palette;
}
get colorPicks() {
return this.using({}).colorPicks;
}
get variants() {
var _a;
const options = {};
for (const { variants } of __classPrivateFieldGet(this, _ComputedTheme_chain, "f")) {
for (const [name, definitions] of Object.entries(variants)) {
options[name] = [
...new Set([...((_a = options[name]) !== null && _a !== void 0 ? _a : []), ...Object.keys(definitions)]),
];
}
}
return options;
}
[(_ComputedTheme_chain = new WeakMap(), _ComputedTheme_instances = new WeakSet(), _ComputedTheme_buildEvaluator = function _ComputedTheme_buildEvaluator(activeVariants = {}) {
var _a;
const sortedActiveVariantKeys = Object.keys(activeVariants).sort();
const definitions = [];
for (const { definition, variants } of __classPrivateFieldGet(this, _ComputedTheme_chain, "f")) {
definitions.push(definition);
for (const variantName of sortedActiveVariantKeys) {
const activeOption = activeVariants[variantName];
if (activeOption == null)
continue;
const variantDefinition = (_a = variants[variantName]) === null || _a === void 0 ? void 0 : _a[activeOption];
if (variantDefinition != null)
definitions.push(variantDefinition);
}
}
return ChainEvaluator.create(1, definitions);
}, Equals)](other) {
return this === other;
}
}
__decorate([
Memoized([NamedTupleMap])
], ComputedTheme.prototype, "using", null);
__decorate([
Enumerable()
], ComputedTheme.prototype, "surfaces", null);
__decorate([
Enumerable()
], ComputedTheme.prototype, "typography", null);
__decorate([
Enumerable()
], ComputedTheme.prototype, "metrics", null);
__decorate([
Enumerable()
], ComputedTheme.prototype, "palette", null);
__decorate([
Enumerable()
], ComputedTheme.prototype, "colorPicks", null);
__decorate([
Memoized()
], ComputedTheme.prototype, "variants", null);
class ThemeSnapshot extends Theme {
constructor(evaluator, origin, activeVariants) {
super();
_ThemeSnapshot_evaluator.set(this, void 0);
_ThemeSnapshot_origin.set(this, void 0);
_ThemeSnapshot_activeVariants.set(this, void 0);
__classPrivateFieldSet(this, _ThemeSnapshot_evaluator, evaluator, "f");
__classPrivateFieldSet(this, _ThemeSnapshot_origin, origin, "f");
__classPrivateFieldSet(this, _ThemeSnapshot_activeVariants, activeVariants, "f");
}
using(activeVariants) {
return __classPrivateFieldGet(this, _ThemeSnapshot_origin, "f").using({ ...__classPrivateFieldGet(this, _ThemeSnapshot_activeVariants, "f"), ...activeVariants });
}
get surfaces() {
return __classPrivateFieldGet(this, _ThemeSnapshot_evaluator, "f").surfaces;
}
get typography() {
return __classPrivateFieldGet(this, _ThemeSnapshot_evaluator, "f").typography;
}
get metrics() {
return __classPrivateFieldGet(this, _ThemeSnapshot_evaluator, "f").metrics;
}
get palette() {
return __classPrivateFieldGet(this, _ThemeSnapshot_evaluator, "f").palette;
}
get colorPicks() {
return __classPrivateFieldGet(this, _ThemeSnapshot_evaluator, "f").colorPicks;
}
get variants() {
return __classPrivateFieldGet(this, _ThemeSnapshot_origin, "f").variants;
}
}
_ThemeSnapshot_evaluator = new WeakMap(), _ThemeSnapshot_origin = new WeakMap(), _ThemeSnapshot_activeVariants = new WeakMap();
__decorate([
Enumerable()
], ThemeSnapshot.prototype, "surfaces", null);
__decorate([
Enumerable()
], ThemeSnapshot.prototype, "typography", null);
__decorate([
Enumerable()
], ThemeSnapshot.prototype, "metrics", null);
__decorate([
Enumerable()
], ThemeSnapshot.prototype, "palette", null);
__decorate([
Enumerable()
], ThemeSnapshot.prototype, "colorPicks", null);
export { ComputedTheme };
//# sourceMappingURL=ComputedTheme.js.map