Skip to content

Commit 2934037

Browse files
committed
feat(core): inlineStyle, attributeStyleMap, computedStyleMap and getComputedStyle
view.inlineStyle reads and writes the local values as css strings: the specified expression for var()/calc() values, the serialized typed value otherwise, and '' when there is none. Assigning its cssText replaces every local value of the view. view.attributeStyleMap and view.computedStyleMap() expose the typed local and computed values by css name. getComputedStyle(view) returns a live, read-only declaration of canonical computed strings. ShorthandProperty takes a valueSerializer to compose its longhands; transform has one.
1 parent 6b2debc commit 2934037

9 files changed

Lines changed: 1407 additions & 9 deletions

File tree

‎packages/core/ui/core/properties/index.ts‎

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,15 +46,23 @@ const cssValueSourceKeys: Record<string, symbol> = Object.create(null);
4646
export type _RegisteredStyleProperty = CssProperty<any, any> | CssAnimationProperty<any, any> | ShorthandProperty<any, any>;
4747

4848
const registeredStyleProperties = new Map<string, _RegisteredStyleProperty>();
49-
let registeredStylePropertiesVersion = 0;
49+
const styleRegistrationListeners: ((property: _RegisteredStyleProperty) => void)[] = [];
5050

5151
function registerStyleProperty(property: _RegisteredStyleProperty): void {
5252
// A property registered without a css name has no css surface.
5353
if (!property.cssLocalName) {
5454
return;
5555
}
5656
registeredStyleProperties.set(property.cssLocalName, property);
57-
registeredStylePropertiesVersion++;
57+
for (let i = 0, length = styleRegistrationListeners.length; i < length; i++) {
58+
styleRegistrationListeners[i](property);
59+
}
60+
}
61+
62+
/** Calls the listener for every style property registered so far and every later one. */
63+
export function _onStylePropertyRegistered(listener: (property: _RegisteredStyleProperty) => void): void {
64+
styleRegistrationListeners.push(listener);
65+
registeredStyleProperties.forEach((property) => listener(property));
5866
}
5967

6068
/** The style property registered under a css name, longhand or shorthand. */
@@ -67,11 +75,6 @@ export function _getRegisteredStyleProperties(): IterableIterator<_RegisteredSty
6775
return registeredStyleProperties.values();
6876
}
6977

70-
/** Changes whenever a style property is registered. */
71-
export function _getRegisteredStylePropertiesVersion(): number {
72-
return registeredStylePropertiesVersion;
73-
}
74-
7578
/**
7679
* Whether the style still carries what the cascade last wrote for the property.
7780
* A `CssProperty` keeps one value: a local value both suppresses the css write and
@@ -1724,8 +1727,12 @@ export class ShorthandProperty<T extends Style, P> implements ShorthandProperty<
17241727

17251728
public readonly sourceKey: symbol;
17261729

1730+
/** Composes serialized longhands into the shorthand; see `ShorthandPropertyOptions.valueSerializer`. */
1731+
public readonly valueSerializer?: (longhands: string[]) => string;
1732+
17271733
constructor(options: ShorthandPropertyOptions<P>) {
17281734
this.name = options.name;
1735+
this.valueSerializer = options.valueSerializer;
17291736

17301737
const key = Symbol(this.name + ':propertyKey');
17311738
this.key = key;

‎packages/core/ui/core/properties/property-shared.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ export interface ShorthandPropertyOptions<P> {
3737
readonly cssName: string;
3838
readonly converter: (value: string | P) => [any, any][];
3939
readonly getter: (this: any) => string | P;
40+
/**
41+
* Composes the serialized longhands, in the converter's order, into a shorthand value.
42+
* Defaults to the box notation for four longhands and to joining them with spaces otherwise.
43+
*/
44+
readonly valueSerializer?: (longhands: string[]) => string;
4045
}
4146
// TODO: revisit this after circular dependency issues are resolved to see if types can be retained.
4247
// export interface ShorthandPropertyOptions<P> {

‎packages/core/ui/core/view-base/index.ts‎

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import { profile } from '../../../profiling';
1717

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

2223
export { booleanConverter } from './utils';
@@ -636,6 +637,45 @@ export abstract class ViewBase extends Observable {
636637
}
637638
}
638639

640+
/**
641+
* The view's inline style as css strings, like the web's `element.style`: the local
642+
* values set from code, view properties, XML attributes or inline style strings, as
643+
* specified. A property without a local value reads `''`.
644+
*
645+
* Assigning a string sets its `cssText`, which replaces every local value of the view.
646+
* `view.style` will become this object in a future major version.
647+
*/
648+
get inlineStyle(): InlineStyleDeclaration {
649+
return _getInlineStyle(this);
650+
}
651+
652+
set inlineStyle(cssText: InlineStyleDeclaration | string) {
653+
if (typeof cssText === 'string') {
654+
_getInlineStyle(this).cssText = cssText;
655+
} else {
656+
throw new Error('View.inlineStyle property is read-only.');
657+
}
658+
}
659+
660+
/**
661+
* The view's typed local values by css name, like the web's `element.attributeStyleMap`.
662+
*/
663+
get attributeStyleMap(): InlineStylePropertyMap {
664+
return _getAttributeStyleMap(this);
665+
}
666+
667+
set attributeStyleMap(value: InlineStylePropertyMap) {
668+
throw new Error('View.attributeStyleMap property is read-only.');
669+
}
670+
671+
/**
672+
* The view's typed computed values by css name, like the web's `element.computedStyleMap()`.
673+
* @returns A live, read-only map; the same object for every call.
674+
*/
675+
public computedStyleMap(): ComputedStylePropertyMap {
676+
return _getComputedStyleMap(this);
677+
}
678+
639679
get android(): any {
640680
// this._disableNativeViewRecycling = true;
641681
return this._androidView;

‎packages/core/ui/index.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,9 @@ export { Font, FontStyle, FontWeight, FontVariationSettings } from './styling/fo
8686
export type { FontStyleType, FontWeightType, FontVariationSettingsType } from './styling/font-interfaces';
8787
export { Style } from './styling/style';
8888
export type { CommonLayoutParams } from './styling/style';
89+
export { InlineStyleDeclaration, ComputedStyleDeclaration, InlineStylePropertyMap, ComputedStylePropertyMap, getComputedStyle } from './styling/style-declaration';
90+
export type { CssStyleDeclarationProperties } from './styling/style-declaration';
91+
export { serializeCssValue, serializeColor } from './styling/css-value-serializer';
8992
export * from './styling/style-properties';
9093
export * from './styling/length-shared';
9194
export { LinearGradient } from './styling/linear-gradient';

0 commit comments

Comments
 (0)