1
- import * as React from "react" ;
2
- import { glyphBuildingblocks } from "@microsoft/fast-glyphs-msft" ;
3
- import manageJss , { DesignSystemProvider } from "@microsoft/fast-jss-manager-react" ;
4
- import { DesignSystemDefaults , IDesignSystem } from "@microsoft/fast-components-styles-msft" ;
5
- import { IHypertextClassNameContract , IManagedClasses } from "@microsoft/fast-components-class-name-contracts-base" ;
6
1
import Site , {
7
2
componentFactory ,
8
3
formChildFromExamplesFactory ,
9
4
IFormChildOption ,
10
5
ISiteProps ,
11
6
ITheme ,
7
+ ShellSlot ,
12
8
SiteCategory ,
13
9
SiteCategoryIcon ,
14
10
SiteCategoryItem ,
15
11
SiteMenu ,
16
12
SiteMenuItem ,
17
13
SiteTitle ,
18
- SiteTitleBrand ,
14
+ SiteTitleBrand
19
15
} from "@microsoft/fast-development-site-react" ;
16
+ import * as React from "react" ;
17
+ import manageJss , { DesignSystemProvider } from "@microsoft/fast-jss-manager-react" ;
18
+ import { DesignSystemDefaults , IDesignSystem } from "@microsoft/fast-components-styles-msft" ;
19
+ import { IHypertextClassNameContract , IManagedClasses } from "@microsoft/fast-components-class-name-contracts-base" ;
20
+ import { glyphBuildingblocks } from "@microsoft/fast-glyphs-msft" ;
20
21
import { ComponentStyles , ICSSRules } from "@microsoft/fast-jss-manager" ;
21
22
import { Direction } from "@microsoft/fast-application-utilities" ;
22
23
import * as examples from "./examples" ;
23
24
import Hypertext from "../src/hypertext" ;
25
+ import ColorPicker , { IColorConfig } from "./color-picker" ;
24
26
25
27
/* tslint:disable-next-line */
26
28
const sketchDesignKit = require ( "./fast-dna-msft-design-kit.sketch" ) ;
@@ -36,23 +38,33 @@ const hypertextStyles: ComponentStyles<IHypertextClassNameContract, undefined> =
36
38
}
37
39
} ;
38
40
39
- export interface IAppState {
40
- direction : Direction ;
41
- theme : string ;
41
+ enum Theme {
42
+ dark = "dark" ,
43
+ light = "light" ,
44
+ custom = "custom"
42
45
}
43
46
44
- const themes : ITheme [ ] = [
45
- { id : "light" , displayName : "light" , background : DesignSystemDefaults . backgroundColor } ,
46
- { id : "dark" , displayName : "dark" , background : DesignSystemDefaults . foregroundColor }
47
- ] ;
47
+ export interface IAppState extends IColorConfig {
48
+ theme : Theme ;
49
+ direction : Direction ;
50
+ }
48
51
49
52
export default class App extends React . Component < { } , IAppState > {
53
+ private themes : ITheme [ ] = [
54
+ { id : Theme . light , displayName : Theme . light , background : DesignSystemDefaults . backgroundColor } ,
55
+ { id : Theme . dark , displayName : Theme . dark , background : DesignSystemDefaults . foregroundColor } ,
56
+ { id : Theme . custom , displayName : Theme . custom }
57
+ ] ;
58
+
50
59
constructor ( props : { } ) {
51
60
super ( props ) ;
52
61
53
62
this . state = {
54
63
direction : Direction . ltr ,
55
- theme : "light"
64
+ foregroundColor : DesignSystemDefaults . foregroundColor ,
65
+ backgroundColor : DesignSystemDefaults . backgroundColor ,
66
+ accentColor : DesignSystemDefaults . brandColor ,
67
+ theme : Theme . light
56
68
} ;
57
69
}
58
70
@@ -62,7 +74,8 @@ export default class App extends React.Component<{}, IAppState> {
62
74
formChildOptions = { formChildOptions }
63
75
onUpdateDirection = { this . handleUpdateDirection }
64
76
onUpdateTheme = { this . handleUpdateTheme }
65
- themes = { themes }
77
+ themes = { this . themes }
78
+ activeTheme = { this . getThemeById ( this . state . theme ) }
66
79
>
67
80
< SiteMenu slot = { "header" } >
68
81
< SiteMenuItem >
@@ -85,15 +98,30 @@ export default class App extends React.Component<{}, IAppState> {
85
98
< SiteCategory slot = { "category" } name = { "Components" } >
86
99
{ componentFactory ( examples , { ...this . generateDesignSystem ( ) } ) }
87
100
</ SiteCategory >
101
+ < div slot = { ShellSlot . infoBar } >
102
+ < ColorPicker
103
+ foregroundColor = { this . state . foregroundColor }
104
+ backgroundColor = { this . state . backgroundColor }
105
+ accentColor = { this . state . accentColor }
106
+ onColorUpdate = { this . handleColorUpdate }
107
+ />
108
+ </ div >
88
109
</ Site >
89
110
) ;
90
111
}
91
112
113
+ private getThemeById ( id : Theme ) : ITheme {
114
+ return this . themes . find ( ( theme : ITheme ) : boolean => {
115
+ return theme . id === id ;
116
+ } ) ;
117
+ }
118
+
92
119
private generateDesignSystem ( ) : IDesignSystem {
93
120
const designSystem : Partial < IDesignSystem > = {
94
121
direction : this . state . direction ,
95
- foregroundColor : this . state . theme === "dark" ? DesignSystemDefaults . backgroundColor : DesignSystemDefaults . foregroundColor ,
96
- backgroundColor : this . state . theme === "dark" ? DesignSystemDefaults . foregroundColor : DesignSystemDefaults . backgroundColor
122
+ foregroundColor : this . state . foregroundColor ,
123
+ backgroundColor : this . state . backgroundColor ,
124
+ brandColor : this . state . accentColor
97
125
} ;
98
126
99
127
return Object . assign ( { } , DesignSystemDefaults , designSystem ) ;
@@ -109,9 +137,40 @@ export default class App extends React.Component<{}, IAppState> {
109
137
} ) ;
110
138
}
111
139
112
- private handleUpdateTheme = ( theme : string ) : void => {
113
- this . setState ( {
114
- theme
140
+ private handleUpdateTheme = ( theme : Theme ) : void => {
141
+ if ( theme !== Theme . custom ) {
142
+ this . setState ( {
143
+ theme,
144
+ foregroundColor : theme === Theme . dark ? DesignSystemDefaults . backgroundColor : DesignSystemDefaults . foregroundColor ,
145
+ backgroundColor : theme === Theme . dark ? DesignSystemDefaults . foregroundColor : DesignSystemDefaults . backgroundColor
146
+ } ) ;
147
+ } else {
148
+ this . setCustomThemeBackground ( this . state . backgroundColor ) ;
149
+ this . setState ( {
150
+ theme
151
+ } ) ;
152
+ }
153
+ }
154
+
155
+ /**
156
+ * Handles any changes made by the user to the color picker inputs
157
+ */
158
+ private handleColorUpdate = ( config : IColorConfig ) : void => {
159
+ this . setCustomThemeBackground ( config . backgroundColor ) ;
160
+ this . setState (
161
+ config . backgroundColor !== this . state . backgroundColor || config . foregroundColor !== this . state . foregroundColor
162
+ ? { theme : Theme . custom , ...config }
163
+ : config
164
+ ) ;
165
+ }
166
+
167
+ /**
168
+ * Assign a background color to the custom theme so that it can be applied to the background of the examples view
169
+ * @param value The color to assign
170
+ */
171
+ private setCustomThemeBackground ( value : string ) : void {
172
+ this . themes = this . themes . map ( ( theme : ITheme ) : ITheme => {
173
+ return theme . id !== Theme . custom ? theme : Object . assign ( { } , theme , { background : value } ) ;
115
174
} ) ;
116
175
}
117
176
}
0 commit comments