Skip to content
88 changes: 86 additions & 2 deletions packages/core/ui/core/properties/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,39 @@ const enum ValueSource {
/** `sourceKey` of every registered `CssProperty`, by css name. */
const cssValueSourceKeys: Record<string, symbol> = Object.create(null);

/** A style property registered on `Style`. */
export type _RegisteredStyleProperty = CssProperty<any, any> | CssAnimationProperty<any, any> | ShorthandProperty<any, any>;

const registeredStyleProperties = new Map<string, _RegisteredStyleProperty>();
const styleRegistrationListeners: ((property: _RegisteredStyleProperty) => void)[] = [];

function registerStyleProperty(property: _RegisteredStyleProperty): void {
// A property registered without a css name has no css surface.
if (!property.cssLocalName) {
return;
}
registeredStyleProperties.set(property.cssLocalName, property);
for (let i = 0, length = styleRegistrationListeners.length; i < length; i++) {
styleRegistrationListeners[i](property);
}
}

/** Calls the listener for every style property registered so far and every later one. */
export function _onStylePropertyRegistered(listener: (property: _RegisteredStyleProperty) => void): void {
styleRegistrationListeners.push(listener);
registeredStyleProperties.forEach((property) => listener(property));
}

/** The style property registered under a css name, longhand or shorthand. */
export function _getRegisteredStyleProperty(cssName: string): _RegisteredStyleProperty | undefined {
return registeredStyleProperties.get(cssName);
}

/** Every registered style property, in registration order. */
export function _getRegisteredStyleProperties(): IterableIterator<_RegisteredStyleProperty> {
return registeredStyleProperties.values();
}

/**
* Whether the style still carries what the cascade last wrote for the property.
* A `CssProperty` keeps one value: a local value both suppresses the css write and
Expand Down Expand Up @@ -150,6 +183,11 @@ function getCssShorthandLonghands(cssName: string): string[] | undefined {
}
}

/** The longhand css names of a registered shorthand, or `undefined`. */
export function _getCssShorthandLonghands(cssName: string): readonly string[] | undefined {
return getCssShorthandLonghands(cssName);
}

/**
* One pending-substitution value per longhand of the shorthand, or `undefined`
* when the longhands cannot be determined.
Expand Down Expand Up @@ -958,11 +996,15 @@ export class CssProperty<T extends Style, U> {
public readonly defaultValueKey: symbol;
public readonly defaultValue: U;

/** Serializes a typed value to css; `serializeCssValue` is used when unset. */
public valueSerializer?: (value: U) => string;

public overrideHandlers: (options: CssPropertyOptions<T, U>) => void;

constructor(options: CssPropertyOptions<T, U>) {
const propertyName = options.name;
this.name = propertyName;
this.valueSerializer = options.valueSerializer;

// Guard against undefined cssName
if (options.cssName) {
Expand Down Expand Up @@ -997,6 +1039,8 @@ export class CssProperty<T extends Style, U> {
let valueConverter = options.valueConverter;
let overrideConverter = false;

const property = this;

this.overrideHandlers = function (options: CssPropertyOptions<T, U>) {
if (typeof options.equalityComparer !== 'undefined') {
equalityComparer = options.equalityComparer;
Expand All @@ -1011,10 +1055,11 @@ export class CssProperty<T extends Style, U> {
valueConverter = options.valueConverter;
overrideConverter = true;
}
if (typeof options.valueSerializer !== 'undefined') {
property.valueSerializer = options.valueSerializer;
}
};

const property = this;

function setLocalValue(this: T, newValue: U | string): void {
const view = this.viewRef.get();
if (!view) {
Expand Down Expand Up @@ -1223,11 +1268,22 @@ export class CssProperty<T extends Style, U> {
}

cssValueSourceKeys[this.cssLocalName] = this.sourceKey;
registerStyleProperty(this);
}

public isSet(instance: T): boolean {
return this.key in instance;
}

/** Whether the style holds a local value for the property. */
public _isLocalValueSet(instance: T): boolean {
return instance[this.sourceKey] === ValueSource.Local;
}

/** The local value, meaningful only while `_isLocalValueSet` holds. */
public _getLocalValue(instance: T): U {
return instance[this.name];
}
}
CssProperty.prototype.isStyleProperty = true;

Expand All @@ -1245,6 +1301,7 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro
public readonly defaultValueKey: symbol;
public readonly key: symbol;
private readonly source: symbol;
private readonly localValueKey: symbol;

public readonly defaultValue: U;

Expand All @@ -1256,9 +1313,13 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro

public _valueConverter?: (value: string) => any;

/** Serializes a typed value to css; `serializeCssValue` is used when unset. */
public valueSerializer?: (value: U) => string;

constructor(options: CssAnimationPropertyOptions<T, U>) {
const propertyName = options.name;
this.name = propertyName;
this.valueSerializer = options.valueSerializer;

if (options.cssName) {
cssPropertyNames.push(options.cssName);
Expand Down Expand Up @@ -1287,6 +1348,7 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro

const cssValue = Symbol(cssName);
const styleValue = Symbol(`local:${propertyName}`);
this.localValueKey = styleValue;
const keyframeValue = Symbol(keyframeName);
const computedValue = Symbol('computed-value:' + propertyName);
this.key = computedValue;
Expand Down Expand Up @@ -1421,6 +1483,7 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro
Object.defineProperty(cls.prototype, options.cssName, stylePropertyDescriptor);
}
Object.defineProperty(cls.prototype, keyframeName, keyframePropertyDescriptor);
registerStyleProperty(this);
};
}

Expand Down Expand Up @@ -1451,6 +1514,19 @@ export class CssAnimationProperty<T extends Style, U> implements CssAnimationPro
public isSet(instance: T): boolean {
return instance[this.source] !== ValueSource.Default;
}

/** Whether the style holds a local value for the property, even while a keyframe overrides it. */
public _isLocalValueSet(instance: T): boolean {
const value = instance[this.localValueKey];

// A reset keeps the written keyword in the local slot.
return value !== '' && !isResetValue(value);
}

/** The local value, meaningful only while `_isLocalValueSet` holds. */
public _getLocalValue(instance: T): U {
return instance[this.localValueKey];
}
}
CssAnimationProperty.prototype.isStyleProperty = true;

Expand Down Expand Up @@ -1492,6 +1568,9 @@ export class InheritedCssProperty<T extends Style, U> extends CssProperty<T, U>
valueConverter = options.valueConverter;
overrideConverter = true;
}
if (typeof options.valueSerializer !== 'undefined') {
property.valueSerializer = options.valueSerializer;
}
};

const setFunc = (valueSource: ValueSource) => {
Expand Down Expand Up @@ -1648,8 +1727,12 @@ export class ShorthandProperty<T extends Style, P> implements ShorthandProperty<

public readonly sourceKey: symbol;

/** Composes serialized longhands into the shorthand; see `ShorthandPropertyOptions.valueSerializer`. */
public readonly valueSerializer?: (longhands: string[]) => string;

constructor(options: ShorthandPropertyOptions<P>) {
this.name = options.name;
this.valueSerializer = options.valueSerializer;

const key = Symbol(this.name + ':propertyKey');
this.key = key;
Expand Down Expand Up @@ -1745,6 +1828,7 @@ export class ShorthandProperty<T extends Style, P> implements ShorthandProperty<
if (this.cssLocalName !== this.cssName) {
Object.defineProperty(cls.prototype, this.cssLocalName, this.localValueDescriptor);
}
registerStyleProperty(this);

// Nothing is defined on `PropertyBag`: shorthands are expanded while parsing,
// and the var()/calc() ones that do reach the bag must stay unconverted.
Expand Down
15 changes: 15 additions & 0 deletions packages/core/ui/core/properties/property-shared.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface CoerciblePropertyOptions<T, U> extends PropertyOptions<T, U> {

export interface CssPropertyOptions<T, U> extends PropertyOptions<T, U> {
readonly cssName: string;
/**
* Serializes a typed value to the css string the property's converter parses back.
* Defaults to `serializeCssValue`.
*/
readonly valueSerializer?: (value: U) => string;
}
// TODO: revisit this after circular dependency issues are resolved to see if types can be retained.
// export interface CssPropertyOptions<T extends Style, U> extends PropertyOptions<T, U> {
Expand All @@ -32,6 +37,11 @@ export interface ShorthandPropertyOptions<P> {
readonly cssName: string;
readonly converter: (value: string | P) => [any, any][];
readonly getter: (this: any) => string | P;
/**
* Composes the serialized longhands, in the converter's order, into a shorthand value.
* Defaults to the box notation for four longhands and to joining them with spaces otherwise.
*/
readonly valueSerializer?: (longhands: string[]) => string;
}
// TODO: revisit this after circular dependency issues are resolved to see if types can be retained.
// export interface ShorthandPropertyOptions<P> {
Expand All @@ -48,6 +58,11 @@ export interface CssAnimationPropertyOptions<T, U> {
readonly equalityComparer?: (x: U, y: U) => boolean;
readonly valueChanged?: (target: T, oldValue: U, newValue: U) => void;
readonly valueConverter?: (value: string) => U;
/**
* Serializes a typed value to the css string the property's converter parses back.
* Defaults to `serializeCssValue`.
*/
readonly valueSerializer?: (value: U) => string;
}

export function isCssUnsetValue(value: any): boolean {
Expand Down
40 changes: 40 additions & 0 deletions packages/core/ui/core/view-base/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { profile } from '../../../profiling';

import { DOMNode } from '../../../debugger/dom-types';
import { applyInlineStyle, CssState, StyleScope } from '../../styling/style-scope';
import { _getAttributeStyleMap, _getComputedStyleMap, _getInlineStyle, ComputedStylePropertyMap, InlineStyleDeclaration, InlineStylePropertyMap } from '../../styling/style-declaration';
import { booleanConverter } from './utils';

export { booleanConverter } from './utils';
Expand Down Expand Up @@ -636,6 +637,45 @@ export abstract class ViewBase extends Observable {
}
}

/**
* The view's inline style as css strings, like the web's `element.style`: the local
* values set from code, view properties, XML attributes or inline style strings, as
* specified. A property without a local value reads `''`.
*
* Assigning a string sets its `cssText`, which replaces every local value of the view.
* `view.style` will become this object in a future major version.
*/
get inlineStyle(): InlineStyleDeclaration {
return _getInlineStyle(this);
}

set inlineStyle(cssText: InlineStyleDeclaration | string) {
if (typeof cssText === 'string') {
_getInlineStyle(this).cssText = cssText;
} else {
throw new Error('View.inlineStyle property is read-only.');
}
}

/**
* The view's typed local values by css name, like the web's `element.attributeStyleMap`.
*/
get attributeStyleMap(): InlineStylePropertyMap {
return _getAttributeStyleMap(this);
}

set attributeStyleMap(value: InlineStylePropertyMap) {
throw new Error('View.attributeStyleMap property is read-only.');
}

/**
* The view's typed computed values by css name, like the web's `element.computedStyleMap()`.
* @returns A live, read-only map; the same object for every call.
*/
public computedStyleMap(): ComputedStylePropertyMap {
return _getComputedStyleMap(this);
}

get android(): any {
// this._disableNativeViewRecycling = true;
return this._androidView;
Expand Down
3 changes: 3 additions & 0 deletions packages/core/ui/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,9 @@ export { Font, FontStyle, FontWeight, FontVariationSettings } from './styling/fo
export type { FontStyleType, FontWeightType, FontVariationSettingsType } from './styling/font-interfaces';
export { Style } from './styling/style';
export type { CommonLayoutParams } from './styling/style';
export { InlineStyleDeclaration, ComputedStyleDeclaration, InlineStylePropertyMap, ComputedStylePropertyMap, getComputedStyle } from './styling/style-declaration';
export type { CssStyleDeclarationProperties } from './styling/style-declaration';
export { serializeCssValue, serializeColor } from './styling/css-value-serializer';
export * from './styling/style-properties';
export * from './styling/length-shared';
export { LinearGradient } from './styling/linear-gradient';
Expand Down
Loading
Loading