Skip to content

Commit d6ec3f8

Browse files
committed
use React.createContext to propagate component settings
1 parent eafcb12 commit d6ec3f8

File tree

3 files changed

+66
-65
lines changed

3 files changed

+66
-65
lines changed

src/scripts/AutoAlign.tsx

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React, { ComponentType } from 'react';
2-
import PropTypes from 'prop-types';
32
import classnames from 'classnames';
43
import RelativePortal from 'react-relative-portal';
54
import { ComponentSettingsContext } from './ComponentSettings';
@@ -237,16 +236,6 @@ export function autoAlign(options: AutoAlignOptions) {
237236
content: any;
238237
/* eslint-enable react/sort-comp */
239238

240-
context!: Pick<
241-
ComponentSettingsContext,
242-
'portalClassName' | 'portalStyle'
243-
>;
244-
245-
static contextTypes = {
246-
portalClassName: PropTypes.string,
247-
portalStyle: PropTypes.object, // eslint-disable-line react/forbid-prop-types
248-
};
249-
250239
constructor(props: ResultProps) {
251240
super(props);
252241
this.state = {
@@ -380,10 +369,6 @@ export function autoAlign(options: AutoAlignOptions) {
380369
children,
381370
...pprops
382371
} = this.props;
383-
const {
384-
portalClassName = 'slds-scope',
385-
portalStyle = { position: 'absolute', top: 0, left: 0, right: 0 },
386-
} = this.context;
387372
// eslint-disable-next-line prefer-const
388373
let { top, left } = calcAlignmentRect(
389374
triggerNodeRect,
@@ -411,22 +396,37 @@ export function autoAlign(options: AutoAlignOptions) {
411396
content
412397
) : (
413398
<div ref={(node) => (this.node = node)}>
414-
<RelativePortal
415-
fullWidth
416-
left={offsetLeft}
417-
right={-offsetLeft}
418-
top={offsetTop}
419-
onScroll={ignoreFirstCall(this.requestRecalcAlignment)}
420-
component='div'
421-
className={classnames(portalClassName, additionalPortalClassName)}
422-
style={{ ...portalStyle, ...additionalPortalStyle }}
423-
>
424-
{this.state.triggerNodeRect ? (
425-
content
426-
) : (
427-
<div className='slds-hidden'>{content}</div>
399+
<ComponentSettingsContext.Consumer>
400+
{({
401+
portalClassName = 'slds-scope',
402+
portalStyle = {
403+
position: 'absolute',
404+
top: 0,
405+
left: 0,
406+
right: 0,
407+
},
408+
}) => (
409+
<RelativePortal
410+
fullWidth
411+
left={offsetLeft}
412+
right={-offsetLeft}
413+
top={offsetTop}
414+
onScroll={ignoreFirstCall(this.requestRecalcAlignment)}
415+
component='div'
416+
className={classnames(
417+
portalClassName,
418+
additionalPortalClassName
419+
)}
420+
style={{ ...portalStyle, ...additionalPortalStyle }}
421+
>
422+
{this.state.triggerNodeRect ? (
423+
content
424+
) : (
425+
<div className='slds-hidden'>{content}</div>
426+
)}
427+
</RelativePortal>
428428
)}
429-
</RelativePortal>
429+
</ComponentSettingsContext.Consumer>
430430
</div>
431431
);
432432
}

src/scripts/ComponentSettings.tsx

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import React from 'react';
2-
import PropTypes from 'prop-types';
1+
import React, { createContext } from 'react';
32

43
export type ComponentSettingsProps = {
54
assetRoot?: string;
@@ -16,22 +15,17 @@ export type ComponentSettingsContext = {
1615
/**
1716
*
1817
*/
19-
export class ComponentSettings extends React.Component<
20-
ComponentSettingsProps,
21-
{}
18+
export class ComponentSettings extends React.PureComponent<
19+
ComponentSettingsProps
2220
> {
23-
static childContextTypes = {
24-
assetRoot: PropTypes.string,
25-
portalClassName: PropTypes.string,
26-
portalStyle: PropTypes.object, // eslint-disable-line react/forbid-prop-types
27-
};
28-
29-
getChildContext(): ComponentSettingsContext {
30-
const { assetRoot, portalClassName, portalStyle } = this.props;
31-
return { assetRoot, portalClassName, portalStyle };
32-
}
33-
3421
render() {
35-
return this.props.children;
22+
const { assetRoot, portalClassName, portalStyle, children } = this.props;
23+
return (
24+
<ComponentSettingsContext.Provider
25+
value={{ assetRoot, portalClassName, portalStyle }}
26+
>
27+
{children}
28+
</ComponentSettingsContext.Provider>
29+
);
3630
}
3731
}

src/scripts/Icon.tsx

Lines changed: 25 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,6 @@ export class Icon extends Component<IconProps, IconState> {
129129
};
130130

131131
// eslint-disable-next-line react/sort-comp
132-
context!: Pick<ComponentSettingsContext, 'assetRoot'>;
133-
134132
iconContainer: HTMLSpanElement | null;
135133

136134
svgIcon: SVGElement | null;
@@ -258,7 +256,6 @@ export class Icon extends Component<IconProps, IconState> {
258256

259257
render() {
260258
const { container, size, ...props } = this.props;
261-
const { assetRoot = getAssetRoot() } = this.context;
262259
let { category, icon } = props;
263260

264261
if (icon.indexOf(':') > 0) {
@@ -274,23 +271,33 @@ export class Icon extends Component<IconProps, IconState> {
274271
iconColor ? `slds-icon-${iconColor}` : null
275272
);
276273
return (
277-
<span
278-
className={ccontainerClassName}
279-
ref={(node) => (this.iconContainer = node)}
280-
>
281-
{this.renderSVG({
282-
...pprops,
283-
size,
284-
category,
285-
icon,
286-
fillColor: iconColor,
287-
container,
288-
assetRoot,
289-
})}
290-
</span>
274+
<ComponentSettingsContext.Consumer>
275+
{({ assetRoot = getAssetRoot() }) => (
276+
<span
277+
className={ccontainerClassName}
278+
ref={(node) => (this.iconContainer = node)}
279+
>
280+
{this.renderSVG({
281+
...pprops,
282+
size,
283+
category,
284+
icon,
285+
fillColor: iconColor,
286+
container,
287+
assetRoot,
288+
})}
289+
</span>
290+
)}
291+
</ComponentSettingsContext.Consumer>
291292
);
292293
}
293294

294-
return this.renderSVG({ ...props, category, icon, size, assetRoot });
295+
return (
296+
<ComponentSettingsContext.Consumer>
297+
{({ assetRoot = getAssetRoot() }) =>
298+
this.renderSVG({ ...props, category, icon, size, assetRoot })
299+
}
300+
</ComponentSettingsContext.Consumer>
301+
);
295302
}
296303
}

0 commit comments

Comments
 (0)