### Install overlay-kit using npm
Source: https://github.com/toss/overlay-kit/blob/main/README.md
This command installs the overlay-kit library using npm. It's the first step to integrating overlay management into your React application.
```sh
npm install overlay-kit
```
--------------------------------
### Complete Example: Opening Overlays After API Requests
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/more/open-outside-react.mdx
This is a full example demonstrating how to use 'overlay-kit' to open overlays in response to API requests. It utilizes 'ky' for making requests and 'overlay.open' to display an 'Alert' component, managed within an 'OverlayProvider'.
```tsx
import ky from 'ky';
import { OverlayProvider, overlay } from 'overlay-kit';
import Button from '@mui/material/Button';
import { Alert } from './alert';
const api = ky.extend({
hooks: {
afterResponse: [
(_, __, response) => {
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
},
],
},
});
function App() {
return ;
}
export function Example() {
return (
);
}
```
```tsx
import { useEffect } from 'react';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogActions from '@mui/material/DialogActions';
export function Alert({ isOpen, close, onExit }) {
useEffect(() => {
return () => onExit();
}, []);
return (
);
}
```
--------------------------------
### Open Simple Overlays with overlay.open
Source: https://github.com/toss/overlay-kit/blob/main/README.md
This example shows how to open a simple overlay using the `overlay.open` function. It's suitable for overlays that don't require asynchronous handling or returning specific results.
```tsx
import { overlay } from 'overlay-kit';
```
--------------------------------
### Example: Unmounting Multiple Overlays (JSX)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/utils/overlay-unmount-all.mdx
Demonstrates opening multiple overlays and then removing them all at once using overlay.unmountAll(). This is a straightforward way to clear the UI of all active dialogs or modals.
```tsx
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
// Removes all three overlays above
overlay.unmountAll();
```
--------------------------------
### Open a Confirmation Dialog using overlay.open
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/introduction.mdx
Shows how to open the `ConfirmDialog` component using the `overlay.open` function. This example integrates with `OverlayProvider` and demonstrates a common use case for managing dialogs.
```tsx
import { OverlayProvider, overlay } from 'overlay-kit';
import Button from '@mui/material/Button';
import { ConfirmDialog } from './confirm-dialog';
function App() {
return (
);
}
export function Example() {
return (
);
}
```
--------------------------------
### Manage and Display Overlays with OverlayProvider (React)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/hooks.mdx
An example demonstrating the usage of OverlayProvider, overlay.open, and useOverlayData to manage and display overlays. It includes buttons to open overlays that can be closed or unmounted, and lists to show currently opened and all in-memory overlays.
```tsx
import { OverlayProvider, overlay, useOverlayData } from 'overlay-kit';
import Button from '@mui/material/Button';
import Stack from '@mui/material/Stack';
import List from '@mui/material/List';
import ListItem from '@mui/material/ListItem';
import Typography from '@mui/material/Typography';
import { ConfirmDialog } from './confirm-dialog';
function App() {
const overlayData = useOverlayData();
const allOverlayIds = Object.keys(overlayData);
const openOverlayIds = allOverlayIds.filter((id) => overlayData[id].isOpen);
return (
);
}
export const Example = () => {
return (
);
};
```
--------------------------------
### Usage Example: Opening and Closing Multiple Overlays (TSX)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/utils/overlay-close-all.mdx
Demonstrates opening multiple overlay components and then closing them all simultaneously using overlay.closeAll(). This showcases a common pattern for managing multiple dialogs or modals.
```tsx
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
overlay.open(({ isOpen, close, unmount }) => {
return ;
});
// Closes all three overlays above
overlay.closeAll();
```
--------------------------------
### Example: Unmounting Overlays with Animations (JSX)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/utils/overlay-unmount-all.mdx
Illustrates the recommended approach for unmounting overlays that have closing animations. It involves calling overlay.closeAll() first, waiting for animations to complete (e.g., using setTimeout), and then calling overlay.unmountAll() for a smooth user experience.
```tsx
const overlayId = overlay.open(({ isOpen, close, unmount }) => {
return ;
});
overlay.closeAll();
setTimeout(() => {
overlay.unmountAll();
}, 1000);
```
--------------------------------
### Setup OverlayProvider in React Application
Source: https://context7.com/toss/overlay-kit/llms.txt
The OverlayProvider component must wrap your entire React application to provide the necessary context for rendering overlays. It should be placed at the root of your application, typically within your main `index.tsx` or `App.tsx` file.
```tsx
import { OverlayProvider } from 'overlay-kit';
import { createRoot } from 'react-dom/client';
function App() {
return (
My Application
{/* Your app components */}
);
}
// Wrap your entire application with OverlayProvider
const root = createRoot(document.getElementById('root')!);
root.render(
);
```
--------------------------------
### Using OverlayProvider to Render Modals (React)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/components/overlay-provider.mdx
This example shows how to integrate OverlayProvider with a custom modal component. The `overlay.open` function is used within a component to trigger the display of a modal, which is then rendered by the OverlayProvider. It requires the 'overlay-kit' library and a Modal component.
```tsx
import React from 'react';
import { OverlayProvider, overlay } from 'overlay-kit';
import { Modal } from '@src/components';
function App() {
const notify = () =>
overlay.open(({ isOpen, close, unmount }) => (
{/* Modal content */}
));
return ;
}
export function Root() {
return (
{/* All overlays will be rendered here */}
);
}
```
--------------------------------
### Open Overlay and Get Result (TypeScript)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/utils/overlay-open-async.mdx
Demonstrates how to open an overlay asynchronously using overlay.openAsync and await its result. The result is the value passed to the close function within the overlay controller.
```typescript
const result = await overlay.openAsync(controller, options);
```
--------------------------------
### Open Confirm Dialog with Overlay Kit (TypeScript)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/introduction.mdx
This example demonstrates how to open a confirmation dialog using the `overlay.open` function from `overlay-kit`. It passes the `unmount` function as the `onExit` prop to the `ConfirmDialog` component, ensuring memory is released upon closing.
```tsx
import { OverlayProvider, overlay } from 'overlay-kit';
import Button from '@mui/material/Button';
import { ConfirmDialog } from './confirm-dialog';
function App() {
return (
<>
>
);
}
export function Example() {
return (
);
}
```
--------------------------------
### Implement Confirmation Dialog with overlay.openAsync (TypeScript/React)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/api/utils/overlay-open-async.mdx
Shows a practical example of using overlay.openAsync to render a confirmation dialog. The controller function defines confirm and cancel actions, which call the close function with boolean values. The returned promise resolves with true for confirmation or false for cancellation.
```tsx
overlay.openAsync(({ isOpen, close, unmount }) => {
function confirm() {
close(true);
}
function cancel() {
close(false);
}
return ;
});
```
--------------------------------
### Open Simple Overlay with overlay.open
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/more/basic.mdx
Demonstrates how to open a simple overlay using the `overlay.open` function. This function takes a render prop that receives `isOpen` and `close` props to control the overlay's visibility and lifecycle. It requires the `OverlayProvider` to be set up at the root of the application.
```tsx
import { OverlayProvider, overlay } from 'overlay-kit';
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogActions from '@mui/material/DialogActions';
import DialogContent from '@mui/material/DialogContent';
import DialogContentText from '@mui/material/DialogContentText';
import DialogTitle from '@mui/material/DialogTitle';
function App() {
return (
);
}
export function Example() {
return (
);
}
```
--------------------------------
### Get Current Overlay ID with useCurrentOverlay (React)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/hooks.mdx
The `useCurrentOverlay` hook returns the ID of the currently top-most overlay. This ID can be manually set when opening an overlay or automatically generated. It's useful for conditional rendering, focus management, or shortcut control based on the active overlay. This example demonstrates how the hook's output changes as different overlays are opened.
```tsx
import { OverlayProvider, overlay, useCurrentOverlay } from 'overlay-kit';
import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import { ConfirmDialog } from './confirm-dialog';
function App() {
const current = useCurrentOverlay();
return (
{
overlay.open(
({ isOpen, close }) => {
return ;
},
{ overlayId: 'modal-a' }
);
}}
>
Open Overlay A
{
overlay.open(
({ isOpen, close }) => {
return ;
},
{ overlayId: 'modal-b' }
);
}}
>
Open Overlay B
Current value: {current}
);
}
export const Example = () => {
return (
);
};
```
```tsx
import Button from '@mui/material/Button';
import Dialog from '@mui/material/Dialog';
import DialogTitle from '@mui/material/DialogTitle';
import DialogContent from '@mui/material/DialogContent';
import DialogActions from '@mui/material/DialogActions';
import Typography from '@mui/material/Typography';
import { overlay } from 'overlay-kit';
export function ConfirmDialog({ isOpen, close, title }) {
return (
);
}
```
--------------------------------
### Overlay Open vs OpenAsync Comparison
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/faq.mdx
Demonstrates the difference between overlay.open for basic operations and overlay.openAsync for asynchronous handling with Promises, including input and output.
```tsx
overlay.open(({ isOpen, close }) => (
));
const result = await overlay.openAsync(({ isOpen, close }) => (
));
console.log(result ? 'Yes' : 'No');
```
--------------------------------
### Overlay Implementation with overlay-kit (After)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/code-comparison.mdx
This snippet demonstrates how to implement an overlay using the overlay-kit library. It significantly reduces boilerplate by abstracting away the state management for the overlay. The `overlay.open` function handles the visibility and closing logic, making the code more concise and intuitive.
```tsx
import { overlay } from 'overlay-kit';
function MyPage() {
/* Other Hook calls... */
return (
<>
{/* Other components... */}
{
overlay.open(({ isOpen, close }) => {
return ;
});
}}
>
Open
>
);
}
```
--------------------------------
### Overlay Close vs Unmount Comparison
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/faq.mdx
Illustrates the usage of overlay.closeAll to close all overlays while retaining state and overlay.unmountAll to completely remove them from memory.
```tsx
// Close all overlays
overlay.closeAll();
// Remove all overlays
overlay.unmountAll();
```
--------------------------------
### Declarative Overlay Management with overlay-kit
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/think-in-overlay-kit.mdx
Illustrates the declarative approach to overlay management using the overlay-kit library. This method simplifies overlay handling by removing explicit state management, leading to cleaner and more maintainable code. The overlay component is rendered directly within the `overlay.open` function.
```tsx
import { overlay } from 'overlay-kit';
function Overlay() {
return (
{
overlay.open(({ isOpen, close }) => (
));
}}
>
Open
);
}
```
--------------------------------
### Open Asynchronous Overlays with overlay.openAsync
Source: https://github.com/toss/overlay-kit/blob/main/README.md
This code demonstrates how to open an overlay and handle its result as a Promise using `overlay.openAsync`. This is useful when you need to wait for a user's action within the overlay, like confirming a dialog.
```tsx
import { overlay } from 'overlay-kit';
{
const result = await overlay.openAsync(({ isOpen, close, unmount }) => (
```
--------------------------------
### Traditional React Overlay Implementation (Before overlay-kit)
Source: https://github.com/toss/overlay-kit/blob/main/docs/src/pages/en/docs/guides/code-comparison.mdx
This snippet shows a traditional React implementation for managing an overlay. It requires manual state management for the overlay's visibility (`isOpen`) and separate handlers for opening and closing. This approach leads to more boilerplate code and can disrupt the code flow due to React's Hook rules.
```tsx
import { useState } from 'react';
function MyPage() {
const [isOpen, setIsOpen] = useState(false);
/* Other Hook calls... */
return (
<>
{/* Other components... */}
{
setIsOpen(true);
}}
>
Open
{/* Other components... */}