Skip to content

Commit 04f537d

Browse files
authored
Merge pull request #27 from Microsoft/content-class-style-overall
[Core] Content class and contentStyle refactor and enhancements
2 parents c1097d3 + e9eb227 commit 04f537d

File tree

7 files changed

+58
-16
lines changed

7 files changed

+58
-16
lines changed

.vscode/settings.json

+1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"nrwl",
1818
"renderprop",
1919
"scrollable",
20+
"stylenames",
2021
"submenu",
2122
"uifabric",
2223
"unmount",

libs/core/package.json

+9-3
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66
"deleteDestPath": true,
77
"whitelistedNonPeerDependencies": [
88
"tslib",
9-
"css-to-style"
9+
"css-to-style",
10+
"classnames",
11+
"stylenames"
1012
],
1113
"lib": {
1214
"languageLevel": [
@@ -56,9 +58,13 @@
5658
"react": "^16.4.1"
5759
},
5860
"dependencies": {
59-
"css-to-style": "^1.2.0"
61+
"css-to-style": "^1.2.0",
62+
"classnames": "^2.2.6",
63+
"stylenames": "^1.1.6"
6064
},
6165
"bundledDependencies": [
62-
"css-to-style"
66+
"css-to-style",
67+
"classnames",
68+
"stylesnames"
6369
]
6470
}

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

+21-13
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ import {
1515
TemplateRef,
1616
Type,
1717
} from '@angular/core';
18+
import classnames from 'classnames';
1819
import toStyle from 'css-to-style';
20+
import stylenames, { StyleObject } from 'stylenames';
21+
import { Many } from '../declarations/many';
1922
import { ReactContentProps } from '../renderer/react-content';
2023
import { isReactNode } from '../renderer/react-node';
2124
import { isReactRendererData } from '../renderer/renderer';
@@ -42,52 +45,57 @@ export type InputRendererOptions<TContext extends object> =
4245

4346
export type JsxRenderFunc<TContext> = (context: TContext) => JSX.Element;
4447

48+
export type ContentClassValue = string[] | Set<string> | { [klass: string]: any };
49+
export type ContentStyleValue = string | StyleObject;
50+
4551
/**
4652
* Base class for Angular @Components wrapping React Components.
47-
* Simplifies some of the handling around passing down props and setting CSS on the host component.
53+
* Simplifies some of the handling around passing down props and CSS styling on the host component.
4854
*/
4955
// NOTE: TProps is not used at the moment, but a preparation for a potential future change.
5056
export abstract class ReactWrapperComponent<TProps extends {}> implements AfterViewInit, OnChanges {
51-
private _contentClass: string;
52-
private _contentStyle: string;
57+
private _contentClass: Many<ContentClassValue>;
58+
private _contentStyle: ContentStyleValue;
5359

5460
protected abstract reactNodeRef: ElementRef<HTMLElement>;
5561

5662
/**
57-
* Alternative to `class` using the same syntax.
63+
* Alternative to `class` and `[ngClass]` using the same syntax.
5864
*
59-
* @description Since this is a wrapper component, sticking to the virtual DOM concept, this should have any styling of its own.
60-
* Any value passes to `contentClass` will be passed to the root component's class.
65+
* @description Since this is a wrapper component, sticking to the virtual DOM concept, its DOM element shouldn't have any styling of its own.
66+
* Instead, any value passes to `contentClass` will be passed to the root component's class as `className`.
6167
*/
6268
@Input()
63-
set contentClass(value: string) {
69+
set contentClass(value: Many<ContentClassValue>) {
6470
this._contentClass = value;
6571
if (isReactNode(this.reactNodeRef.nativeElement)) {
66-
this.reactNodeRef.nativeElement.setProperty('className', value);
72+
this.reactNodeRef.nativeElement.setProperty('className', classnames(value));
6773
this.changeDetectorRef.detectChanges();
6874
}
6975
}
7076

71-
get contentClass(): string {
77+
get contentClass(): Many<ContentClassValue> {
7278
return this._contentClass;
7379
}
7480

7581
/**
76-
* Alternative to `style` using the same syntax.
82+
* Alternative to `style` and `[ngStyle]` using (almost) the same syntax.
83+
* All syntax supports by `ngStyle` is supported, with the exception of specifying units in the key (`{ 'width.px': 12 }`).
7784
*
7885
* @description Since this is a wrapper component, sticking to the virtual DOM concept, this should have any styling of its own.
7986
* Any value passes to `contentStyle` will be passed to the root component's style.
8087
*/
8188
@Input()
82-
set contentStyle(value: string) {
89+
set contentStyle(value: ContentStyleValue) {
8390
this._contentStyle = value;
8491
if (isReactNode(this.reactNodeRef.nativeElement)) {
85-
this.reactNodeRef.nativeElement.setProperty('style', toStyle(value));
92+
const stringValue = typeof value === 'string' ? value : stylenames(value);
93+
this.reactNodeRef.nativeElement.setProperty('style', toStyle(stringValue));
8694
this.changeDetectorRef.detectChanges();
8795
}
8896
}
8997

90-
get contentStyle(): string {
98+
get contentStyle(): ContentStyleValue {
9199
return this._contentStyle;
92100
}
93101

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type Many<T> = T | T[];

libs/core/src/lib/declarations/public-api.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33

44
export * from './index-signature';
55
export * from './known-keys';
6+
export * from './many';
67
export * from './omit';
78
export * from './string-map';

package-lock.json

+21
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+4
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,15 @@
9292
"@ngrx/schematics": "6.0.1",
9393
"@nrwl/nx": "^6.2.0",
9494
"@nrwl/schematics": "^6.2.0",
95+
"@types/classnames": "^2.2.6",
9596
"@types/css-to-style": "^1.2.0",
9697
"@types/jasmine": "~2.8.6",
9798
"@types/jasminewd2": "~2.0.3",
9899
"@types/node": "~10.5.5",
99100
"@types/react": "^16.4.7",
100101
"@types/react-dom": "^16.0.6",
102+
"@types/stylenames": "^1.1.0",
103+
"classnames": "^2.2.6",
101104
"codelyzer": "~4.4.2",
102105
"copyfiles": "^2.0.0",
103106
"css-to-style": "^1.2.0",
@@ -113,6 +116,7 @@
113116
"prettier": "1.14.0",
114117
"protractor": "~5.4.0",
115118
"rimraf": "^2.6.2",
119+
"stylenames": "^1.1.6",
116120
"ts-node": "~7.0.0",
117121
"tsickle": "^0.32.1",
118122
"tslib": "^1.9.0",

0 commit comments

Comments
 (0)