UNPKG

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