UNPKG

2.72 kBJavaScriptView Raw
1import { __assign, __rest } from "tslib";
2import * as React from 'react';
3import { concatStyleSetsWithProps } from '@uifabric/merge-styles';
4import { useCustomizationSettings } from './customizations/useCustomizationSettings';
5var DefaultFields = ['theme', 'styles'];
6export function styled(Component, baseStyles, getProps, customizable, pure) {
7 customizable = customizable || { scope: '', fields: undefined };
8 var scope = customizable.scope, _a = customizable.fields, fields = _a === void 0 ? DefaultFields : _a;
9 var Wrapped = React.forwardRef(function (props, forwardedRef) {
10 var styles = React.useRef();
11 var settings = useCustomizationSettings(fields, scope);
12 var customizedStyles = settings.styles, dir = settings.dir, rest = __rest(settings, ["styles", "dir"]);
13 var additionalProps = getProps ? getProps(props) : undefined;
14 // eslint-disable-next-line @typescript-eslint/no-explicit-any
15 var cache = (styles.current && styles.current.__cachedInputs__) || [];
16 if (!styles.current || customizedStyles !== cache[1] || props.styles !== cache[2]) {
17 // Using styled components as the Component arg will result in nested styling arrays.
18 var concatenatedStyles = function (styleProps) {
19 return concatStyleSetsWithProps(styleProps, baseStyles, customizedStyles, props.styles);
20 };
21 // The __cachedInputs__ array is attached to the function and consumed by the
22 // classNamesFunction as a list of keys to include for memoizing classnames.
23 concatenatedStyles.__cachedInputs__ = [
24 baseStyles,
25 customizedStyles,
26 props.styles,
27 ];
28 concatenatedStyles.__noStyleOverride__ =
29 !customizedStyles && !props.styles;
30 styles.current = concatenatedStyles;
31 }
32 return React.createElement(Component, __assign({ ref: forwardedRef }, rest, additionalProps, props, { styles: styles.current }));
33 });
34 // Function.prototype.name is an ES6 feature, so the cast to any is required until we're
35 // able to drop IE 11 support and compile with ES6 libs
36 // eslint-disable-next-line @typescript-eslint/no-explicit-any
37 Wrapped.displayName = "Styled" + (Component.displayName || Component.name);
38 // This preserves backwards compatibility.
39 var pureComponent = pure ? React.memo(Wrapped) : Wrapped;
40 // Check if the wrapper has a displayName after it has been memoized. Then assign it to the pure component.
41 if (Wrapped.displayName) {
42 pureComponent.displayName = Wrapped.displayName;
43 }
44 return pureComponent;
45}
46//# sourceMappingURL=styled.js.map
\No newline at end of file