-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
index.tsx
248 lines (215 loc) · 7.04 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import * as React from 'react';
import {noop} from '@shopify/javascript-utilities/other';
import get from 'lodash/get';
import merge from 'lodash/merge';
import replace from 'lodash/replace';
import hoistStatics from 'hoist-non-react-statics';
import {autobind} from '@shopify/javascript-utilities/decorators';
import createApp, {getShopOrigin} from '@shopify/app-bridge';
import {
polarisAppProviderContextTypes,
TranslationDictionary,
PrimitiveReplacementDictionary,
ComplexReplacementDictionary,
WithAppProviderProps,
CreateAppProviderContext,
} from '../types';
import {PolarisContext} from '../../types';
import Intl from '../Intl';
import Link from '../Link';
import {Context} from '../AppProvider';
import StickyManager from '../StickyManager';
import ScrollLockManager from '../ScrollLockManager';
import {
createThemeContext,
ThemeContext as CreateThemeContext,
} from '../../ThemeProvider';
const REPLACE_REGEX = /{([^}]*)}/g;
export function translate(
id: string,
translations: TranslationDictionary | TranslationDictionary[] | undefined,
replacements?: PrimitiveReplacementDictionary | ComplexReplacementDictionary,
) {
const text = get(translations, id) as string;
if (!text) {
return '';
}
if (replacements) {
return replace(text, REPLACE_REGEX, (match: string) => {
const replacement: string = match.substring(1, match.length - 1);
if (!replacements.hasOwnProperty(replacement)) {
throw new Error(
`No replacement found for key '${replacement}'. The following replacements were passed: ${Object.keys(
replacements,
)
.map((key) => `'${key}'`)
.join(', ')}`,
);
}
return replacements[replacement] as string;
});
}
return text;
}
export function withAppProvider<OwnProps>() {
return function addProvider<C>(
WrappedComponent:
| React.ComponentClass<OwnProps & WithAppProviderProps> & C
| React.SFC<OwnProps & WithAppProviderProps> & C,
): React.ComponentClass<OwnProps> & C {
class WithProvider extends React.Component<OwnProps, never> {
static contextTypes = WrappedComponent.contextTypes
? merge(WrappedComponent.contextTypes, polarisAppProviderContextTypes)
: polarisAppProviderContextTypes;
componentDidMount() {
const {
polaris: {subscribe: subscribeToPolaris},
polarisTheme: {subscribe: subscribeToTheme},
} = this.context;
if (subscribeToPolaris) {
subscribeToPolaris(this.handleContextUpdate);
}
if (subscribeToTheme) {
subscribeToTheme(this.handleContextUpdate);
}
}
componentWillUnmount() {
const {
polaris: {unsubscribe: unsubscribeToPolaris},
polarisTheme: {unsubscribe: unsubscribeToTheme},
} = this.context;
if (unsubscribeToPolaris) {
unsubscribeToPolaris(this.handleContextUpdate);
}
if (unsubscribeToTheme) {
unsubscribeToTheme(this.handleContextUpdate);
}
}
render() {
const {polaris, polarisTheme} = this.context;
const polarisContext = {...polaris, theme: polarisTheme};
if (!polaris) {
throw new Error(
`The <AppProvider> component is required as of v2.0 of Polaris React. See
https://polaris.shopify.com/components/structure/app-provider for implementation
instructions.`,
);
}
return <WrappedComponent {...this.props} polaris={polarisContext} />;
}
@autobind
private handleContextUpdate() {
this.forceUpdate();
}
}
const FinalComponent = hoistStatics(
WithProvider,
WrappedComponent as React.ComponentClass<any>,
);
return FinalComponent as React.ComponentClass<OwnProps> & C;
};
}
export function withSticky() {
return function addStickyManager<OwnProps, C>(
WrappedComponent:
| React.ComponentClass<OwnProps & WithAppProviderProps> & C
| React.SFC<OwnProps & WithAppProviderProps> & C,
): any & C {
// eslint-disable-next-line shopify/react-initialize-state
class WithStickyManager extends React.Component<
{},
OwnProps & WithAppProviderProps
> {
static childContextTypes = polarisAppProviderContextTypes;
static contextTypes = WrappedComponent.contextTypes
? merge(WrappedComponent.contextTypes, polarisAppProviderContextTypes)
: polarisAppProviderContextTypes;
private stickyManager: StickyManager = new StickyManager();
private polarisContext: any;
constructor(props: OwnProps & WithAppProviderProps, context: Context) {
super(props);
const {polaris} = context;
this.polarisContext = {
...polaris,
stickyManager: this.stickyManager,
};
}
getChildContext(): Context {
return {
polaris: this.polarisContext,
};
}
render() {
return (
<WrappedComponent {...this.props} polaris={this.polarisContext} />
);
}
}
const FinalComponent = hoistStatics(
WithStickyManager,
WrappedComponent as React.ComponentClass<any>,
);
return FinalComponent as React.ComponentClass<any> & C;
};
}
export function createAppProviderContext({
i18n,
linkComponent,
apiKey,
shopOrigin,
forceRedirect,
stickyManager,
scrollLockManager,
subscribe = noop,
unsubscribe = noop,
}: CreateAppProviderContext = {}): Context {
const intl = new Intl(i18n);
const link = new Link(linkComponent);
const appBridge = apiKey
? createApp({
apiKey,
shopOrigin: shopOrigin || getShopOrigin(),
forceRedirect,
})
: undefined;
return {
polaris: {
intl,
link,
stickyManager: stickyManager || new StickyManager(),
scrollLockManager: scrollLockManager || new ScrollLockManager(),
subscribe,
unsubscribe,
appBridge,
},
};
}
export function createPolarisContext(): PolarisContext;
export function createPolarisContext(
contextOne: CreateAppProviderContext | CreateThemeContext,
): PolarisContext;
export function createPolarisContext(
contextOne: CreateAppProviderContext | CreateThemeContext,
contextTwo: CreateAppProviderContext | CreateThemeContext,
): PolarisContext;
export function createPolarisContext(
contextOne?: CreateAppProviderContext | CreateThemeContext,
contextTwo?: CreateAppProviderContext | CreateThemeContext,
) {
let appProviderContext: CreateAppProviderContext | undefined;
let themeContext: CreateThemeContext | undefined;
if (contextOne && 'logo' in contextOne) {
themeContext = contextOne as CreateThemeContext;
appProviderContext = contextTwo;
} else {
appProviderContext = contextOne;
themeContext = contextTwo as CreateThemeContext | undefined;
}
const appProvider = appProviderContext
? createAppProviderContext(appProviderContext)
: createAppProviderContext();
const theme = themeContext
? createThemeContext(themeContext)
: createThemeContext();
return {...appProvider, ...theme};
}