0.2.0 - Mid migration

This commit is contained in:
Daniel Mason 2022-04-25 14:47:15 +12:00
parent 139e6a915e
commit 7e38fdbd7d
42393 changed files with 5358157 additions and 62 deletions

View file

@ -0,0 +1,2 @@
export { default } from './withStyles';
export * from './withStyles';

View file

@ -0,0 +1,15 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
Object.defineProperty(exports, "default", {
enumerable: true,
get: function get() {
return _withStyles.default;
}
});
var _withStyles = _interopRequireDefault(require("./withStyles"));

View file

@ -0,0 +1,5 @@
{
"sideEffects": false,
"module": "../esm/withStyles/index.js",
"typings": "./index.d.ts"
}

View file

@ -0,0 +1,130 @@
import * as React from 'react';
import { PropInjector } from '@material-ui/types';
import * as CSS from 'csstype';
import * as JSS from 'jss';
import { DefaultTheme } from '../defaultTheme';
// Disable automatic export
export {};
type JSSFontface = CSS.FontFace & { fallbacks?: CSS.FontFace[] };
export type PropsFunc<Props extends object, T> = (props: Props) => T;
/**
* Allows the user to augment the properties available
*/
export interface BaseCSSProperties extends CSS.Properties<number | string> {
'@font-face'?: JSSFontface | JSSFontface[];
}
export interface CSSProperties extends BaseCSSProperties {
// Allow pseudo selectors and media queries
// `unknown` is used since TS does not allow assigning an interface without
// an index signature to one with an index signature. This is to allow type safe
// module augmentation.
// Technically we want any key not typed in `BaseCSSProperties` to be of type
// `CSSProperties` but this doesn't work. The index signature needs to cover
// BaseCSSProperties as well. Usually you would use `BaseCSSProperties[keyof BaseCSSProperties]`
// but this would not allow assigning React.CSSProperties to CSSProperties
[k: string]: unknown | CSSProperties;
}
export type BaseCreateCSSProperties<Props extends object = {}> = {
[P in keyof BaseCSSProperties]: BaseCSSProperties[P] | PropsFunc<Props, BaseCSSProperties[P]>;
};
export interface CreateCSSProperties<Props extends object = {}>
extends BaseCreateCSSProperties<Props> {
// Allow pseudo selectors and media queries
[k: string]:
| BaseCreateCSSProperties<Props>[keyof BaseCreateCSSProperties<Props>]
| CreateCSSProperties<Props>;
}
/**
* This is basically the API of JSS. It defines a Map<string, CSS>,
* where
* - the `keys` are the class (names) that will be created
* - the `values` are objects that represent CSS rules (`React.CSSProperties`).
*
* if only `CSSProperties` are matched `Props` are inferred to `any`
*/
export type StyleRules<Props extends object = {}, ClassKey extends string = string> = Record<
ClassKey,
// JSS property bag
| CSSProperties
// JSS property bag where values are based on props
| CreateCSSProperties<Props>
// JSS property bag based on props
| PropsFunc<Props, CreateCSSProperties<Props>>
>;
/**
* @internal
*/
export type StyleRulesCallback<Theme, Props extends object, ClassKey extends string = string> = (
theme: Theme
) => StyleRules<Props, ClassKey>;
export type Styles<Theme, Props extends object, ClassKey extends string = string> =
| StyleRules<Props, ClassKey>
| StyleRulesCallback<Theme, Props, ClassKey>;
export interface WithStylesOptions<Theme = DefaultTheme> extends JSS.StyleSheetFactoryOptions {
defaultTheme?: Theme;
flip?: boolean;
withTheme?: boolean;
name?: string;
}
export type ClassNameMap<ClassKey extends string = string> = Record<ClassKey, string>;
/**
* @internal
*/
export type ClassKeyInferable<Theme, Props extends object> = string | Styles<Theme, Props>;
export type ClassKeyOfStyles<StylesOrClassKey> = StylesOrClassKey extends string
? StylesOrClassKey
: StylesOrClassKey extends StyleRulesCallback<any, any, infer ClassKey>
? ClassKey
: StylesOrClassKey extends StyleRules<any, infer ClassKey>
? ClassKey
: never;
/**
* infers the type of the props used in the styles
*/
export type PropsOfStyles<StylesType> = StylesType extends Styles<any, infer Props> ? Props : {};
/**
* infers the type of the theme used in the styles
*/
export type ThemeOfStyles<StylesType> = StylesType extends Styles<infer Theme, any> ? Theme : {};
export type WithStyles<
StylesType extends ClassKeyInferable<any, any>,
IncludeTheme extends boolean | undefined = false
> = (IncludeTheme extends true ? { theme: ThemeOfStyles<StylesType> } : {}) & {
classes: ClassNameMap<ClassKeyOfStyles<StylesType>>;
innerRef?: React.Ref<any>;
} & PropsOfStyles<StylesType>;
export interface StyledComponentProps<ClassKey extends string = string> {
/**
* Override or extend the styles applied to the component.
*/
classes?: Partial<ClassNameMap<ClassKey>>;
innerRef?: React.Ref<any>;
}
export default function withStyles<
StylesType extends Styles<any, any>,
Options extends WithStylesOptions<ThemeOfStyles<StylesType>> = {}
>(
style: StylesType,
options?: Options
): PropInjector<
WithStyles<StylesType, Options['withTheme']>,
StyledComponentProps<ClassKeyOfStyles<StylesType>> & PropsOfStyles<StylesType>
>;

View file

@ -0,0 +1,143 @@
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _extends2 = _interopRequireDefault(require("@babel/runtime/helpers/extends"));
var _objectWithoutProperties2 = _interopRequireDefault(require("@babel/runtime/helpers/objectWithoutProperties"));
var _react = _interopRequireDefault(require("react"));
var _propTypes = _interopRequireDefault(require("prop-types"));
var _hoistNonReactStatics = _interopRequireDefault(require("hoist-non-react-statics"));
var _utils = require("@material-ui/utils");
var _makeStyles = _interopRequireDefault(require("../makeStyles"));
var _getThemeProps = _interopRequireDefault(require("../getThemeProps"));
var _useTheme = _interopRequireDefault(require("../useTheme"));
// Link a style sheet with a component.
// It does not modify the component passed to it;
// instead, it returns a new component, with a `classes` property.
var withStyles = function withStyles(stylesOrCreator) {
var options = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
return function (Component) {
var defaultTheme = options.defaultTheme,
_options$withTheme = options.withTheme,
withTheme = _options$withTheme === void 0 ? false : _options$withTheme,
name = options.name,
stylesOptions = (0, _objectWithoutProperties2.default)(options, ["defaultTheme", "withTheme", "name"]);
if (process.env.NODE_ENV !== 'production') {
if (Component === undefined) {
throw new Error(['You are calling withStyles(styles)(Component) with an undefined component.', 'You may have forgotten to import it.'].join('\n'));
}
}
var classNamePrefix = name;
if (process.env.NODE_ENV !== 'production') {
if (!name) {
// Provide a better DX outside production.
var displayName = (0, _utils.getDisplayName)(Component);
if (displayName !== undefined) {
classNamePrefix = displayName;
}
}
}
var useStyles = (0, _makeStyles.default)(stylesOrCreator, (0, _extends2.default)({
defaultTheme: defaultTheme,
Component: Component,
name: name || Component.displayName,
classNamePrefix: classNamePrefix
}, stylesOptions));
var WithStyles = /*#__PURE__*/_react.default.forwardRef(function WithStyles(props, ref) {
var classesProp = props.classes,
innerRef = props.innerRef,
other = (0, _objectWithoutProperties2.default)(props, ["classes", "innerRef"]); // The wrapper receives only user supplied props, which could be a subset of
// the actual props Component might receive due to merging with defaultProps.
// So copying it here would give us the same result in the wrapper as well.
var classes = useStyles((0, _extends2.default)({}, Component.defaultProps, props));
var theme;
var more = other;
if (typeof name === 'string' || withTheme) {
// name and withTheme are invariant in the outer scope
// eslint-disable-next-line react-hooks/rules-of-hooks
theme = (0, _useTheme.default)() || defaultTheme;
if (name) {
more = (0, _getThemeProps.default)({
theme: theme,
name: name,
props: other
});
} // Provide the theme to the wrapped component.
// So we don't have to use the `withTheme()` Higher-order Component.
if (withTheme && !more.theme) {
more.theme = theme;
}
}
return /*#__PURE__*/_react.default.createElement(Component, (0, _extends2.default)({
ref: innerRef || ref,
classes: classes
}, more));
});
process.env.NODE_ENV !== "production" ? WithStyles.propTypes = {
/**
* Override or extend the styles applied to the component.
*/
classes: _propTypes.default.object,
/**
* Use that prop to pass a ref to the decorated component.
* @deprecated
*/
innerRef: (0, _utils.chainPropTypes)(_propTypes.default.oneOfType([_propTypes.default.func, _propTypes.default.object]), function (props) {
if (props.innerRef == null) {
return null;
}
return null; // return new Error(
// 'Material-UI: The `innerRef` prop is deprecated and will be removed in v5. ' +
// 'Refs are now automatically forwarded to the inner component.',
// );
})
} : void 0;
if (process.env.NODE_ENV !== 'production') {
WithStyles.displayName = "WithStyles(".concat((0, _utils.getDisplayName)(Component), ")");
}
(0, _hoistNonReactStatics.default)(WithStyles, Component);
if (process.env.NODE_ENV !== 'production') {
// Exposed for test purposes.
WithStyles.Naked = Component;
WithStyles.options = options;
WithStyles.useStyles = useStyles;
}
return WithStyles;
};
};
var _default = withStyles;
exports.default = _default;