-
Notifications
You must be signed in to change notification settings - Fork 831
/
index.tsx
177 lines (167 loc) · 4.98 KB
/
index.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
/*
Copyright (c) 2018-2019 Uber Technologies, Inc.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
*/
// @flow
import * as React from 'react';
// baseui imports
import {
useStyletron,
createTheme,
lightThemePrimitives,
darkThemePrimitives,
ThemeProvider,
} from 'baseui';
import {Card} from 'baseui/card';
import {Spinner} from 'baseui/spinner';
import {useRouter} from 'next/router';
import {useView, Compiler, Error} from 'react-view';
// yard (baseweb customization of react-view)
import {getProvider, getThemeFromContext, TProviderValue} from './provider';
import {customProps, TCustomPropFields} from './custom-props';
import ThemeEditor from './theme-editor';
import Overrides from './overrides';
import Editor from './editor';
import ActionButtons from './action-buttons';
import Knobs from './knobs';
import {YardTabs, YardTab} from './styled-components';
import {countProps, countOverrides, countThemeValues} from './utils';
import PropsTooltip from './props-tooltip';
import {TYardProps} from './types';
const Yard: React.FC<TYardProps> = ({
componentName,
placeholderHeight,
scope,
props,
theme,
imports,
queryStringName,
mapTokensToProps,
}) => {
const [css, baseTheme] = useStyletron();
const componentTheme = getThemeFromContext(baseTheme, theme);
const themePrimitives =
baseTheme.name && baseTheme.name.startsWith('dark-theme')
? 'darkThemePrimitives'
: 'lightThemePrimitives';
const provider = getProvider(componentTheme, themePrimitives);
const {query, push, pathname} = useRouter();
const initialCode = (typeof queryStringName !== 'undefined'
? query[queryStringName]
: query.code) as string;
const params = useView<TProviderValue, TCustomPropFields>({
componentName,
props,
scope: {
...scope,
ThemeProvider,
lightThemePrimitives,
darkThemePrimitives,
createTheme,
},
imports,
provider,
customProps,
initialCode,
onUpdate: ({code}) => {
const query = queryStringName || 'code';
push({
pathname,
query: {
[query]: code,
},
});
},
});
const activeProps = countProps(params.knobProps.state, props);
const activeOverrides = countOverrides(params.knobProps.state.overrides);
const activeThemeValues = countThemeValues(params.providerValue);
const showOverrides =
props.overrides.custom.names && props.overrides.custom.names.length > 0;
const showTheme = theme.length > 0;
return (
<Card>
<Compiler
{...params.compilerProps}
minHeight={placeholderHeight}
placeholder={() => (
<div
className={css({
height: `${placeholderHeight}px`,
width: '100%',
display: 'flex',
justifyContent: 'center',
alignItems: 'center',
})}
>
<Spinner size={placeholderHeight > 50 ? 50 : placeholderHeight} />
</div>
)}
/>
<Error msg={params.errorProps.msg} isPopup />
{showOverrides || showTheme ? (
<YardTabs>
<YardTab title={`Props${activeProps > 0 ? ` (${activeProps})` : ''}`}>
<Knobs {...params.knobProps} />
</YardTab>
{showOverrides && (
<YardTab
title={`Style Overrides${
activeOverrides > 0 ? ` (${activeOverrides})` : ''
}`}
>
<Overrides
componentName={componentName}
componentConfig={props}
overrides={params.knobProps.state.overrides}
set={(propValue: any) => {
params.knobProps.set(propValue, 'overrides');
}}
/>
</YardTab>
)}
{showTheme && (
<YardTab
title={`Theme ${
activeThemeValues > 0 ? `(${activeThemeValues})` : ''
}`}
>
<ThemeEditor
theme={params.providerValue || {}}
themeInit={componentTheme}
set={params.actions.updateProvider}
componentName={componentName}
/>
</YardTab>
)}
</YardTabs>
) : (
<Knobs {...params.knobProps} />
)}
<Editor
{...params.editorProps}
transformToken={tokenProps => {
const token = tokenProps.children.trim();
if (mapTokensToProps && mapTokensToProps[token]) {
return (
<PropsTooltip
{...tokenProps}
typeDefinition={mapTokensToProps[token]}
/>
);
}
return <span {...tokenProps} />;
}}
/>
<Error {...params.errorProps} />
<ActionButtons
{...params.actions}
code={params.editorProps.code}
componentName={componentName}
importsConfig={imports}
/>
</Card>
);
};
export default Yard;