{"data":{"slug":"github-lobehub-lobehub-agents-skills-modal-0e082d84a573","title":"Create Modal for User Interaction using @lobehub\/ui\/base-ui","summary":"Use this skill to create modals, dialogs, and confirmations using the imperative API from `@lobehub\/ui\/base-ui` for better control and performance. The skill requires the global `ModalHost` to be mounted near the root of the application for modals to appear correctly.","url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-modal-0e082d84a573","json_url":"https:\/\/skillshunter.net\/api\/public\/v1\/skills\/github-lobehub-lobehub-agents-skills-modal-0e082d84a573","markdown_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-modal-0e082d84a573.md","download_url":"https:\/\/skillshunter.net\/skills\/github-lobehub-lobehub-agents-skills-modal-0e082d84a573\/download","category":{"slug":"software-development","name":"Software & Development"},"editor_note":"Ensure the global `ModalHost` from `@lobehub\/ui\/base-ui` is mounted once near the root of the application. Use `onOpenChangeComplete` for cleanup tasks, as `onOpenChange` may not fire correctly when closing via footer buttons.","instructions":"---\nname: modal\ndescription: 'Use for modals, dialogs and confirmations with createModal, confirmModal, ModalHost or base-ui modal APIs.'\nuser-invocable: false\n---\n\n# Modal Imperative API Guide\n\n## Recommended: `@lobehub\/ui\/base-ui`\n\nNew code should use the **base-ui** modal stack (headless primitives, not antd `Modal`):\n\n- `createModal`, `confirmModal`, `ModalHost` from `@lobehub\/ui\/base-ui`\n- `useModalContext` from `@lobehub\/ui\/base-ui` inside modal **content**\n\nBody slot: pass **`content`** (or `children`; runtime uses `content ?? children`).\n\n### Global `ModalHost` (required)\n\nBase-ui `createModal` renders through a **separate** host from the root package. The app must mount **`ModalHost`** from `@lobehub\/ui\/base-ui` once near the root (e.g. next to other global hosts). Without it, `createModal` calls will not appear.\n\nIf the project only mounts `ModalHost` from `@lobehub\/ui`, add a second lazy `ModalHost` from `@lobehub\/ui\/base-ui` until all imperative modals are migrated.\n\n### Why imperative?\n\n| Mode        | Characteristics                      | Recommended |\n| ----------- | ------------------------------------ | ----------- |\n| Declarative | `open` state + `<Modal \/>`           | \u274c          |\n| Imperative  | Call `createModal()`, no local state | \u2705          |\n\n### File structure\n\n```\nfeatures\/\n\u2514\u2500\u2500 MyFeatureModal\/\n    \u251c\u2500\u2500 index.tsx            # export createXxxModal\n    \u2514\u2500\u2500 MyFeatureContent.tsx # modal body\n```\n\n### 1. Content (`MyFeatureContent.tsx`)\n\n```tsx\n'use client';\n\nimport { useModalContext } from '@lobehub\/ui\/base-ui';\nimport { useTranslation } from 'react-i18next';\n\nexport const MyFeatureContent = () => {\n  const { t } = useTranslation('namespace');\n  const { close } = useModalContext();\n\n  return <div>{\/* ... *\/}<\/div>;\n};\n```\n\n### 2. `createModal` (`index.tsx`)\n\n```tsx\n'use client';\n\nimport { createModal } from '@lobehub\/ui\/base-ui';\nimport { t } from 'i18next';\n\nimport { MyFeatureContent } from '.\/MyFeatureContent';\n\nexport const createMyFeatureModal = () =>\n  createModal({\n    content: <MyFeatureContent \/>,\n    footer: null,\n    maskClosable: true,\n    styles: {\n      content: { overflow: 'hidden', padding: 0 },\n    },\n    title: t('myFeature.title', { ns: 'setting' }),\n    width: 'min(80%, 800px)',\n  });\n```\n\n### 3. Usage\n\n```tsx\nimport { createMyFeatureModal } from '@\/features\/MyFeatureModal';\n\nconst handleOpen = useCallback(() => {\n  createMyFeatureModal();\n}, []);\n\nreturn <Button onClick={handleOpen}>Open<\/Button>;\n```\n\n### i18n\n\n- **Content**: `useTranslation` in components.\n- **`createModal` options**: `import { t } from 'i18next'` where hooks are unavailable.\n\n### `useModalContext`\n\n```tsx\nconst { close, setCanDismissByClickOutside } = useModalContext();\n```\n\n### Closing: which callback actually fires\n\n`close()` \u2014 from `useModalContext()` inside the content, or from the returned\n`ModalInstance` \u2014 only flips the stack entry to `open: false`. It does **not** go\nthrough base-ui's dismissal path, so:\n\n| callback               | user dismissal (Esc \/ backdrop \/ header \u2715) | `close()` from content or instance |\n| ---------------------- | ------------------------------------------ | ---------------------------------- |\n| `onOpenChange`         | fires                                      | **does not fire**                  |\n| `onOpenChangeComplete` | fires with `false`                         | fires with `false`                 |\n\nPut caller-side cleanup (clearing an editing flag, resetting the provider's\n`open` state) on **`onOpenChangeComplete`**. Wiring it to `onOpenChange` looks\ncorrect until a footer button closes the modal, and then the caller never learns\nit went away \u2014 typically leaving a flag set so the modal cannot be reopened.\n\n`createModal` only ever completes with `false` (the imperative renderer supplies\nthe argument itself and never forwards the prop to base-ui), but still guard on\nit \u2014 other base-ui primitives such as `DropdownMenu` do report both directions,\nand the guard keeps the call site from depending on that difference:\n\n```tsx\nonOpenChangeComplete: (open) => {\n  if (!open) onClosed?.();\n},\n```\n\n### Common options (base-ui)\n\n`ImperativeModalProps` builds on `BaseModalProps`: `title`, `width`, `maskClosable`, `open`, `onOpenChange`, `footer`, `styles` \/ `classNames` (keys: `backdrop`, `popup`, `header`, `title`, `close`, `content`, \u2026).\n\n| Property       | Notes                                    |\n| -------------- | ---------------------------------------- |\n| `content`      | Main body (preferred name vs `children`) |\n| `maskClosable` | Click outside to dismiss                 |\n| `styles.*`     | Semantic regions, not antd `styles.body` |\n\n### Confirm\n\n```tsx\nimport { confirmModal } from '@lobehub\/ui\/base-ui';\n\nconfirmModal({\n  title: '\u2026',\n  content: '\u2026',\n  okText: '\u2026',\n  cancelText: '\u2026',\n  onOk: async () => {},\n});\n```\n\n---\n\n## Legacy: `@lobehub\/ui` (root)\n\n`createModal` from the root `@lobehub\/ui` entry is typed as **antd `Modal` props** (`children`, `allowFullscreen`, `getContainer`, `destroyOnHidden`, `styles.body`, etc.). App code no longer imports it; do not reintroduce it \u2014 use **`@lobehub\/ui\/base-ui`**.\n\n---\n\n## Examples\n\n- Base-ui (preferred): follow sections above; ensure **base-ui `ModalHost`** is mounted.\n- Base-ui call sites: `src\/features\/SkillStore\/index.tsx`, `src\/features\/LibraryModal\/CreateNew\/index.tsx`\n","use_cases":["Display a confirmation dialog before deleting an item","Show a modal for user input or form submission","Present additional information in a popup"],"limitations":[],"compatibility":[],"setup_tip":null,"sample_input":null,"sample_output":null,"source_url":"https:\/\/github.com\/lobehub\/lobehub\/blob\/54382b5c8e81544da454fa6d7b50f461a562c1ee\/.agents\/skills\/modal\/SKILL.md","source_commit":"54382b5c8e81544da454fa6d7b50f461a562c1ee","license":null,"version":null,"listing_approved_at":"2026-11-22","created_at":"2026-11-21T11:22:12.000000Z","updated_at":"2026-11-22T11:17:19.000000Z","seo_title":"Create Modal for User Interaction using @lobehub\/ui\/base-ui","seo_description":"Learn how to create modals, dialogs, and confirmations using the imperative API from `@lobehub\/ui\/base-ui` for better control and performance. The skill requires the `@l0","seo_keywords":["create modal","imperative modal","confirmation dialog","modal host","modal content","@lobehub\/ui\/base-ui"]}}