Skip to content

Commit 045f8f2

Browse files
author
Ben Grynhaus
committed
[Core] Allow more syntax input options in contentStyle input for ReactWrapperComponent
Allows most [ngStyle] options, with the exception of specifying units in the key (`{ 'width.px': 12 }`)
1 parent c0f51c1 commit 045f8f2

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

libs/core/src/lib/components/wrapper-component.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import {
1717
} from '@angular/core';
1818
import classnames from 'classnames';
1919
import toStyle from 'css-to-style';
20+
import stylenames, { StyleObject } from 'stylenames';
2021
import { Many } from '../declarations/many';
2122
import { ReactContentProps } from '../renderer/react-content';
2223
import { isReactNode } from '../renderer/react-node';
@@ -44,10 +45,28 @@ export type InputRendererOptions<TContext extends object> =
4445

4546
export type JsxRenderFunc<TContext> = (context: TContext) => JSX.Element;
4647

48+
/**
49+
* Optional options to pass to `ReactWrapperComponent`.
50+
*/
51+
/**
52+
* Whether the host's `display` should be set to the root child node's`display`.
53+
* @default `false`.
54+
*/
55+
56+
/**
57+
* The zone to use to track changes to inner (Angular) templates & components.
58+
* @default `undefined`.
59+
*/
60+
/* export type _ReactWrapperComponentInternalView<TProps extends {} = {}> = ReactWrapperComponent<TProps> & {
61+
readonly reactNodeRef: ElementRef<HTMLElement>;
62+
}; */
63+
4764
export type ContentClassValue = string[] | Set<string> | { [klass: string]: any };
65+
export type ContentStyleValue = string | StyleObject;
66+
4867
/**
4968
* Base class for Angular @Components wrapping React Components.
50-
* Simplifies some of the handling around passing down props and setting CSS on the host component.
69+
* Simplifies some of the handling around passing down props and CSS styling on the host component.
5170
*/
5271
// NOTE: TProps is not used at the moment, but a preparation for a potential future change.
5372
export abstract class ReactWrapperComponent<TProps extends {}> implements AfterViewInit, OnChanges {
@@ -76,21 +95,23 @@ export abstract class ReactWrapperComponent<TProps extends {}> implements AfterV
7695
}
7796

7897
/**
79-
* Alternative to `style` using the same syntax.
98+
* Alternative to `style` and `[ngStyle]` using (almost) the same syntax.
99+
* All syntax supports by `ngStyle` is supported, with the exception of specifying units in the key (`{ 'width.px': 12 }`).
80100
*
81101
* @description Since this is a wrapper component, sticking to the virtual DOM concept, this should have any styling of its own.
82102
* Any value passes to `contentStyle` will be passed to the root component's style.
83103
*/
84104
@Input()
85-
set contentStyle(value: string) {
105+
set contentStyle(value: ContentStyleValue) {
86106
this._contentStyle = value;
87107
if (isReactNode(this.reactNodeRef.nativeElement)) {
88-
this.reactNodeRef.nativeElement.setProperty('style', toStyle(value));
108+
const stringValue = typeof value === 'string' ? value : stylenames(value);
109+
this.reactNodeRef.nativeElement.setProperty('style', toStyle(stringValue));
89110
this.changeDetectorRef.detectChanges();
90111
}
91112
}
92113

93-
get contentStyle(): string {
114+
get contentStyle(): ContentStyleValue {
94115
return this._contentStyle;
95116
}
96117

0 commit comments

Comments
 (0)