Skip to content
This repository was archived by the owner on Aug 25, 2020. It is now read-only.

Commit 741706a

Browse files
committed
feat: pick/toggle unwrap styles from function
1 parent 22219f2 commit 741706a

3 files changed

Lines changed: 22 additions & 6 deletions

File tree

package/src/meta/def.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@ export interface StyleReactiveDef {
1111
(state: any): StyleDef;
1212
}
1313

14+
export interface WrappedStyleDef {
15+
(): StyleDef;
16+
}
17+
1418
export interface PickStyleDef {
15-
[key: string]: StyleDef;
19+
[key: string]: StyleDef | WrappedStyleDef;
1620
}
1721

1822
export type DirectiveSelector = [string, StateSetter];

package/src/styler-def.service.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Injectable } from '@angular/core';
2-
import { PickStyleDef, StyleDef } from './meta/def';
2+
import { PickStyleDef, StyleDef, WrappedStyleDef } from './meta/def';
3+
import { isFunction } from './utils/is-function';
34
import { mergeDeepAll } from './utils/merge-deep';
45

56
@Injectable()
@@ -13,17 +14,25 @@ export class StylerDefService {
1314

1415
pick(state: string, styles: PickStyleDef, def: string | null = null): StyleDef {
1516
if (state) {
16-
return styles[state];
17+
return this.unwrap(styles[state]);
1718
} else if (def !== null) {
18-
return styles[def];
19+
return this.unwrap(styles[def]);
1920
} else {
2021
return {};
2122
}
2223
}
2324

2425
toggle(state: boolean, styles: StyleDef, falseStyles?: StyleDef): StyleDef {
2526
return state
26-
? styles
27-
: falseStyles || {};
27+
? this.unwrap(styles)
28+
: this.unwrap(falseStyles || {});
29+
}
30+
31+
unwrap(raw: StyleDef | WrappedStyleDef): StyleDef {
32+
if (isFunction(raw)) {
33+
return raw();
34+
} else {
35+
return raw;
36+
}
2837
}
2938
}

package/src/utils/is-function.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export function isFunction(x: any): x is Function {
2+
return x !== null && typeof x === 'function';
3+
}

0 commit comments

Comments
 (0)