-
-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[core] Remove @mui/utils and @mui/types dependencies (#827)
- Loading branch information
1 parent
bdbe328
commit 09c0946
Showing
35 changed files
with
778 additions
and
94 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
docs/src/blocks/PackageManagerSnippet/PackageManagerSnippetProvider.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
3 changes: 1 addition & 2 deletions
3
docs/src/blocks/PackageManagerSnippet/PackageManagerSnippetRoot.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { expect } from 'chai'; | ||
import { clamp } from './clamp'; | ||
|
||
describe('clamp', () => { | ||
it('clamps a value based on min and max', () => { | ||
expect(clamp(1, 2, 4)).to.equal(2); | ||
expect(clamp(5, 2, 4)).to.equal(4); | ||
expect(clamp(-5, -1, 5)).to.equal(-1); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,7 @@ | ||
export { default as clamp } from '@mui/utils/clamp'; | ||
export function clamp( | ||
val: number, | ||
min: number = Number.MIN_SAFE_INTEGER, | ||
max: number = Number.MAX_SAFE_INTEGER, | ||
): number { | ||
return Math.max(min, Math.min(val, max)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import * as React from 'react'; | ||
import { expect } from 'chai'; | ||
import { getReactElementRef } from '@base-ui-components/react/utils'; | ||
|
||
describe('getReactElementRef', () => { | ||
it('should return undefined when not used correctly', () => { | ||
// @ts-expect-error | ||
expect(getReactElementRef(false)).to.equal(null); | ||
// @ts-expect-error | ||
expect(getReactElementRef()).to.equal(null); | ||
// @ts-expect-error | ||
expect(getReactElementRef(1)).to.equal(null); | ||
|
||
const children = [<div key="1" />, <div key="2" />]; | ||
// @ts-expect-error | ||
expect(getReactElementRef(children)).to.equal(null); | ||
}); | ||
|
||
it('should return the ref of a React element', () => { | ||
const ref = React.createRef<HTMLDivElement>(); | ||
const element = <div ref={ref} />; | ||
expect(getReactElementRef(element)).to.equal(ref); | ||
}); | ||
|
||
it('should return null for a fragment', () => { | ||
const element = ( | ||
<React.Fragment> | ||
<p>Hello</p> | ||
<p>Hello</p> | ||
</React.Fragment> | ||
); | ||
expect(getReactElementRef(element)).to.equal(null); | ||
}); | ||
|
||
it('should return null for element with no ref', () => { | ||
const element = <div />; | ||
expect(getReactElementRef(element)).to.equal(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import * as React from 'react'; | ||
|
||
/** | ||
* Returns the ref of a React element handling differences between React 19 and older versions. | ||
* It will throw runtime error if the element is not a valid React element. | ||
* | ||
* @param element React.ReactElement | ||
* @returns React.Ref<any> | null | ||
*/ | ||
export function getReactElementRef(element: React.ReactElement): React.Ref<any> | null { | ||
// 'ref' is passed as prop in React 19, whereas 'ref' is directly attached to children in older versions | ||
if (parseInt(React.version, 10) >= 19) { | ||
return (element?.props as any)?.ref || null; | ||
} | ||
// @ts-expect-error element.ref is not included in the ReactElement type | ||
// https://github.com/DefinitelyTyped/DefinitelyTyped/discussions/70189 | ||
return element?.ref || null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
// Public utils | ||
|
||
export * from './prepareForSlot'; | ||
export * from './getReactElementRef'; | ||
export * from './MuiCancellableEvent'; | ||
export * from './useControlled'; | ||
export * from './useEnhancedEffect'; | ||
export * from './useForkRef'; | ||
export * from './useId'; | ||
export * from './useScrollLock'; | ||
export * from './useTransitionStatus'; | ||
export * from './visuallyHidden'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,8 @@ | ||
export { default as ownerDocument } from '@mui/utils/ownerDocument'; | ||
export { default as ownerWindow } from '@mui/utils/ownerWindow'; | ||
export function ownerDocument(node: Node | null | undefined): Document { | ||
return (node && node.ownerDocument) || document; | ||
} | ||
|
||
export function ownerWindow(node: Node | undefined): Window { | ||
const doc = ownerDocument(node); | ||
return doc.defaultView || window; | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { expect } from 'chai'; | ||
import PropTypes from 'prop-types'; | ||
import { exactProp } from './proptypes'; | ||
|
||
describe('exactProp()', () => { | ||
beforeEach(() => { | ||
PropTypes.resetWarningCache(); | ||
}); | ||
|
||
it('should return null for supported props', () => { | ||
const props = { | ||
bar: false, | ||
}; | ||
const propTypes = { | ||
bar: PropTypes.bool, | ||
}; | ||
|
||
expect(() => { | ||
PropTypes.checkPropTypes(exactProp(propTypes), props, 'props', 'Component'); | ||
}).not.toErrorDev(); | ||
}); | ||
|
||
it('should return an error for unsupported props', () => { | ||
const props = { | ||
foo: false, | ||
}; | ||
const propTypes = { | ||
bar: PropTypes.bool, | ||
}; | ||
|
||
expect(() => { | ||
PropTypes.checkPropTypes(exactProp(propTypes), props, 'props', 'Component'); | ||
}).toErrorDev('The following props are not supported: `foo`. Please remove them'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,57 @@ | ||
export { default as refType } from '@mui/utils/refType'; | ||
export { default as HTMLElementType } from '@mui/utils/HTMLElementType'; | ||
import PropTypes, { ValidationMap } from 'prop-types'; | ||
|
||
export const refType = PropTypes.oneOfType([PropTypes.func, PropTypes.object]); | ||
|
||
export function HTMLElementType( | ||
props: { [key: string]: unknown }, | ||
propName: string, | ||
componentName: string, | ||
location: string, | ||
propFullName: string, | ||
): Error | null { | ||
if (process.env.NODE_ENV === 'production') { | ||
return null; | ||
} | ||
|
||
const propValue = props[propName]; | ||
const safePropName = propFullName || propName; | ||
|
||
if (propValue == null) { | ||
return null; | ||
} | ||
|
||
if (propValue && (propValue as any).nodeType !== 1) { | ||
return new Error( | ||
`Invalid ${location} \`${safePropName}\` supplied to \`${componentName}\`. ` + | ||
`Expected an HTMLElement.`, | ||
); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
const specialProperty = 'exact-prop: \u200b'; | ||
|
||
// This module is based on https://github.com/airbnb/prop-types-exact repository. | ||
// However, in order to reduce the number of dependencies and to remove some extra safe checks | ||
// the module was forked. | ||
export function exactProp<T>(propTypes: ValidationMap<T>): ValidationMap<T> { | ||
if (process.env.NODE_ENV === 'production') { | ||
return propTypes; | ||
} | ||
|
||
return { | ||
...propTypes, | ||
[specialProperty]: (props: { [key: string]: unknown }) => { | ||
const unsupportedProps = Object.keys(props).filter((prop) => !propTypes.hasOwnProperty(prop)); | ||
if (unsupportedProps.length > 0) { | ||
return new Error( | ||
`The following props are not supported: ${unsupportedProps | ||
.map((prop) => `\`${prop}\``) | ||
.join(', ')}. Please remove them.`, | ||
); | ||
} | ||
return null; | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.