Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -95,3 +95,16 @@ to a different level:
<ComposedModal ... />
</ComposedModalPresence>
```

Instead of wrapping your component with `ComposedModalPresence`, you can also use the higher-order function `withComposedModalPresence`.
This will automatically wrap your component and handle the props as well as their types for you:

```jsx
export const MyComposedModal = withComposedModalPresence(
({ onSubmit, onClose }) => {
...
},
);

<MyComposedModal open={open} onSubmit={noop} onClose={noop} />
```
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import React, {
createContext,
useContext,
useMemo,
type ComponentType,
type FC,
type PropsWithChildren,
} from 'react';
import {
Expand Down Expand Up @@ -80,3 +82,28 @@ export const useExclusiveComposedModalPresenceContext = (id: string) => {
const ctx = useContext(ComposedModalPresenceContext);
return ctx?.isPresenceExclusive(id) ? ctx : undefined;
};

type WithComposedModalPresenceProps = Pick<ComposedModalPresenceProps, 'open'>;

/**
* Higher-order function that wraps a component with ComposedModalPresence
*/
export const withComposedModalPresence = <TProps extends object>(
Component: ComponentType<TProps>
): FC<TProps & WithComposedModalPresenceProps> => {
const WithComposedModalPresence: FC<
TProps & WithComposedModalPresenceProps
> = (props) => {
const { open, ...componentProps } = props;

return (
<ComposedModalPresence open={open}>
<Component {...(componentProps as TProps)} />
</ComposedModalPresence>
);
};

WithComposedModalPresence.displayName = `withComposedModalPresence(${Component.displayName || Component.name || 'Component'})`;

return WithComposedModalPresence;
};
1 change: 1 addition & 0 deletions packages/react/src/components/ComposedModal/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export {
} from './ComposedModal';
export {
ComposedModalPresence,
withComposedModalPresence,
type ComposedModalPresenceProps,
} from './ComposedModalPresence';

Expand Down
13 changes: 13 additions & 0 deletions packages/react/src/components/Modal/Modal.featureflag.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -97,3 +97,16 @@ different level:
<ComposedModal ... />
</ModalPresence>
```

Instead of wrapping your component with `ModalPresence`, you can also use the higher-order function `withModalPresence`.
This will automatically wrap your component and handle the props as well as their types for you:

```jsx
export const MyModal = withModalPresence(
({ onSubmit, onClose }) => {
...
},
);

<MyModal open={open} onSubmit={noop} onClose={noop} />
```
25 changes: 25 additions & 0 deletions packages/react/src/components/Modal/ModalPresence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import React, {
createContext,
useContext,
useMemo,
type ComponentType,
type FC,
type PropsWithChildren,
} from 'react';
import {
Expand Down Expand Up @@ -73,3 +75,26 @@ export const useExclusiveModalPresenceContext = (id: string) => {
const ctx = useContext(ModalPresenceContext);
return ctx?.isPresenceExclusive(id) ? ctx : undefined;
};

type WithModalPresenceProps = Pick<ModalPresenceProps, 'open'>;

/**
* Higher-order function that wraps a component with ModalPresence
*/
export const withModalPresence = <TProps extends object>(
Component: ComponentType<TProps>
): FC<TProps & WithModalPresenceProps> => {
const WithModalPresence: FC<TProps & WithModalPresenceProps> = (props) => {
const { open, ...componentProps } = props;

return (
<ModalPresence open={open}>
<Component {...(componentProps as TProps)} />
</ModalPresence>
);
};

WithModalPresence.displayName = `withModalPresence(${Component.displayName || Component.name || 'Component'})`;

return WithModalPresence;
};
14 changes: 12 additions & 2 deletions packages/react/src/components/Modal/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/
import Modal, { type ModalProps } from './Modal';
import { ModalPresence, type ModalPresenceProps } from './ModalPresence';
import {
ModalPresence,
withModalPresence,
type ModalPresenceProps,
} from './ModalPresence';

export default Modal;
export { Modal, ModalPresence, type ModalProps, type ModalPresenceProps };
export {
Modal,
ModalPresence,
withModalPresence,
type ModalProps,
type ModalPresenceProps,
};
Loading