Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Popper] Allow setting default props in a theme #30118

Merged
merged 4 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions packages/mui-material/src/Popper/Popper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,4 +284,20 @@ describe('<Popper />', () => {
expect(getByRole('tooltip', { hidden: true }).style.display).to.equal('none');
});
});

describe('default props', () => {
it('should consume theme default props', () => {
const container = document.createElement('div');
const theme = createTheme({ components: { MuiPopper: { defaultProps: { container } } } });
render(
<ThemeProvider theme={theme}>
<Popper {...defaultProps} open>
<p id="content">Hello World</p>
</Popper>
</ThemeProvider>,
);

expect(container).to.have.text('Hello World');
});
});
});
4 changes: 3 additions & 1 deletion packages/mui-material/src/Popper/Popper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types';
import PopperUnstyled, { PopperUnstyledProps } from '@mui/base/PopperUnstyled';
import { HTMLElementType, refType } from '@mui/utils';
import { Direction, useThemeWithoutDefault as useTheme } from '@mui/system';
import useThemeProps from '../styles/useThemeProps';

export type PopperProps = Omit<PopperUnstyledProps, 'direction'>;

Expand All @@ -19,10 +20,11 @@ export type PopperProps = Omit<PopperUnstyledProps, 'direction'>;
* - [Popper API](https://mui.com/api/popper/)
*/
const Popper = React.forwardRef(function Popper(
props: PopperProps,
inProps: PopperProps,
ref: React.ForwardedRef<HTMLDivElement>,
) {
const theme = useTheme<{ direction?: Direction }>();
const props = useThemeProps({ props: inProps, name: 'MuiPopper' });
return <PopperUnstyled direction={theme?.direction} {...props} ref={ref} />;
});

Expand Down
3 changes: 3 additions & 0 deletions packages/mui-material/src/styles/components.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,9 @@ export interface Components<Theme = unknown> {
styleOverrides?: ComponentsOverrides<Theme>['MuiPaper'];
variants?: ComponentsVariants['MuiPaper'];
};
MuiPopper?: {
defaultProps?: ComponentsProps['MuiPopper'];
};
MuiPopover?: {
defaultProps?: ComponentsProps['MuiPopover'];
styleOverrides?: ComponentsOverrides<Theme>['MuiPopover'];
Expand Down
2 changes: 2 additions & 0 deletions packages/mui-material/src/styles/props.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ import { ToolbarProps } from '../Toolbar';
import { TooltipProps } from '../Tooltip';
import { TouchRippleProps } from '../ButtonBase/TouchRipple';
import { TypographyProps } from '../Typography';
import { PopperProps } from '../Popper';

export type ComponentsProps = {
[Name in keyof ComponentsPropsList]?: Partial<ComponentsPropsList[Name]>;
Expand Down Expand Up @@ -195,6 +196,7 @@ export interface ComponentsPropsList {
MuiPagination: PaginationProps;
MuiPaginationItem: PaginationItemProps;
MuiPaper: PaperProps;
MuiPopper: PopperProps;
MuiPopover: PopoverProps;
MuiRadio: RadioProps;
MuiRadioGroup: RadioGroupProps;
Expand Down