-
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
196 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -168,6 +168,7 @@ | |
"install": false | ||
}, | ||
"modal": { | ||
"react": true, | ||
"icon": true, | ||
"version": "1.0.0", | ||
"style": true, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
"version": "1.0.0", | ||
"description": "Create modal dialogs", | ||
"luna": { | ||
"react": true, | ||
"icon": true | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
import { FC, PropsWithChildren, useEffect, useRef } from 'react' | ||
import { createPortal } from 'react-dom' | ||
import h from 'licia/h' | ||
import types from 'licia/types' | ||
import Modal from './index' | ||
|
||
interface IModalProps { | ||
title: string | ||
visible: boolean | ||
width?: number | ||
onClose?: () => void | ||
} | ||
|
||
const LunaModal: FC<PropsWithChildren<IModalProps>> = (props) => { | ||
const modalRef = useRef<HTMLDivElement>(null) | ||
const modal = useRef<Modal>() | ||
const content = useRef<HTMLDivElement>(h('div') as HTMLDivElement) | ||
const doHide = useRef<types.AnyFn>() | ||
|
||
useEffect(() => { | ||
modal.current = new Modal(modalRef.current!, { | ||
title: props.title, | ||
content: content.current, | ||
}) | ||
doHide.current = modal.current.hide | ||
modal.current.hide = function () { | ||
props.onClose && props.onClose() | ||
} | ||
if (props.visible) { | ||
modal.current.show() | ||
} | ||
if (props.width) { | ||
modal.current.setOption('width', props.width) | ||
} | ||
|
||
return () => modal.current?.destroy() | ||
}, []) | ||
|
||
useEffect(() => { | ||
if (modal.current) { | ||
modal.current.setOption('title', props.title) | ||
} | ||
}, [props.title]) | ||
|
||
useEffect(() => { | ||
if (modal.current) { | ||
if (props.visible) { | ||
modal.current.show() | ||
} else { | ||
doHide.current && doHide.current.call(modal.current) | ||
} | ||
} | ||
}, [props.visible]) | ||
|
||
useEffect(() => { | ||
if (modal.current) { | ||
modal.current.setOption('width', props.width) | ||
} | ||
}, [props.width]) | ||
|
||
return <div ref={modalRef}> | ||
{createPortal(<>{props.children}</>, content.current)} | ||
</div> | ||
} | ||
|
||
export default LunaModal |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.