Skip to content

Commit

Permalink
feat(PortalProvider): improve code quality
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerrrkd committed Sep 7, 2024
1 parent 4658f08 commit 82cc469
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
36 changes: 35 additions & 1 deletion src/components/portal-provider/demos/demo1.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import {
Button,
ConfigProvider,
Modal,
Popup,
PortalProvider,
Space,
useModal,
usePortal,
} from 'antd-mobile'
import enUS from 'antd-mobile/es/locales/en-US'
import { DemoBlock } from 'demos'
Expand All @@ -14,8 +17,9 @@ export default () => {
<>
<DemoBlock title='English'>
<ConfigProvider locale={enUS}>
{/* to make sure that `<PortalProvider />` is wrapped inner `<ConfigProvider />` so that `<ComponentWantsToUseModal />` can access context from `<ConfigProvider />` */}
{/* to make sure that `<PortalProvider />` is wrapped inner `<ConfigProvider />` so that its children can access context from `<ConfigProvider />` */}
<PortalProvider>
<ComponentWantsToUsePortal />
<ComponentWantsToUseModal />
</PortalProvider>
</ConfigProvider>
Expand All @@ -24,6 +28,36 @@ export default () => {
)
}

const ComponentWantsToUsePortal = () => {
const { renderModalInPortal } = usePortal()
return (
<Space direction='vertical'>
<Button
block
shape='rounded'
color='primary'
size='large'
onClick={() => {
renderModalInPortal(<Modal />)
}}
>
render modal
</Button>
<Button
block
shape='rounded'
color='primary'
size='large'
onClick={() => {
renderModalInPortal(<Popup />)
}}
>
render popup
</Button>
</Space>
)
}

const ComponentWantsToUseModal = () => {
const { show, confirm, alert, clear } = useModal()
return (
Expand Down
4 changes: 2 additions & 2 deletions src/components/portal-provider/index.zh.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
### 属性

| 属性 | 说明 | 类型 | 默认值 |
| --- | --- | --- | --- | --- |
| children | 需要用到 `useModal` 的子组件 (一般是应用入口) | `Locale` | [zh-CN] | - |
| --- | --- | --- | --- |
| children | 需要用到 `useModal` 的子组件 (一般是应用入口) | `ReactNode` | - |
3 changes: 3 additions & 0 deletions src/components/portal-provider/portal-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ interface WrapperProps {
unmount: () => void
}

/**
* @description refer to `src/utils/render-imperatively`
*/
const Wrapper = React.forwardRef<ImperativeHandler, WrapperProps>(
({ element, unmount }, ref) => {
const [visible, setVisible] = useState(false)
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,10 @@ export type {
} from './components/popover'
export { default as Popup } from './components/popup'
export type { PopupProps } from './components/popup'
export { default as PortalProvider } from './components/portal-provider'
export {
default as PortalProvider,
usePortal,
} from './components/portal-provider'
export type { PortalProviderProps } from './components/portal-provider'
export { default as ProgressBar } from './components/progress-bar'
export type { ProgressBarProps } from './components/progress-bar'
Expand Down

0 comments on commit 82cc469

Please sign in to comment.