-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #60 from haradakunihiko/feature/implement-to-use-c…
…ontext Enable to use context inside confirmation dialog
- Loading branch information
Showing
5 changed files
with
154 additions
and
48 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
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 |
---|---|---|
@@ -1,4 +1,6 @@ | ||
import confirmable from './confirmable'; | ||
import createConfirmation from './createConfirmation'; | ||
import createConfirmation, { createConfirmationCreater } from './createConfirmation'; | ||
import { createDomTreeMounter } from './mounter/domTree'; | ||
import { createReactTreeMounter, createMountPoint } from './mounter/reactTree'; | ||
|
||
export { confirmable, createConfirmation }; | ||
export { confirmable, createConfirmation, createConfirmationCreater, createDomTreeMounter, createReactTreeMounter, createMountPoint }; |
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,35 @@ | ||
import React from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
export function createDomTreeMounter(defaultMountNode) { | ||
const confirms = {}; | ||
const callbacks = {}; | ||
|
||
function mount(Component, props, mountNode){ | ||
const key = Math.floor(Math.random() * (1 << 30)).toString(16); | ||
const wrapper = (mountNode || defaultMountNode || document.body).appendChild(document.createElement('div')); | ||
confirms[key] = wrapper; | ||
|
||
const root = createRoot(wrapper); | ||
|
||
root.render( | ||
<Component | ||
{...props} | ||
/> | ||
); | ||
callbacks.mounted && callbacks.mounted(); | ||
return key; | ||
} | ||
|
||
function unmount(key) { | ||
const wrapper = confirms[key]; | ||
delete confirms[key]; | ||
|
||
if (wrapper && wrapper.parentNode) { | ||
wrapper.parentNode.removeChild(wrapper); | ||
} | ||
} | ||
return { | ||
mount, unmount, options: {} | ||
} | ||
} |
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,55 @@ | ||
import React, { useState, useEffect } from 'react' | ||
import { createPortal } from 'react-dom'; | ||
|
||
export function createReactTreeMounter(mountNode) { | ||
const confirms = {}; | ||
const callbacks = {}; | ||
|
||
function mount(Component, props){ | ||
const key = Math.floor(Math.random() * (1 << 30)).toString(16); | ||
confirms[key] = { Component, props}; | ||
callbacks.mounted && callbacks.mounted(confirms); | ||
return key; | ||
} | ||
function unmount(key) { | ||
delete confirms[key]; | ||
callbacks.mounted && callbacks.mounted(confirms); | ||
} | ||
|
||
function setMountedCallback(func) { | ||
callbacks.mounted = func; | ||
} | ||
|
||
return { | ||
mount, unmount, | ||
options: { | ||
setMountedCallback, mountNode | ||
} | ||
} | ||
} | ||
|
||
export function createMountPoint(reactTreeMounter) { | ||
return () => { | ||
const [confirmComponents, setConfirmComponents] = useState({}); | ||
|
||
useEffect(() => { | ||
return reactTreeMounter.options.setMountedCallback((components) => { | ||
setConfirmComponents({...components}); | ||
}); | ||
}, []); | ||
|
||
let element = ( | ||
<> | ||
{Object.keys(confirmComponents).map((key) => { | ||
const { Component, props } = confirmComponents[key]; | ||
return <Component key={key} {...props} /> | ||
})} | ||
</> | ||
) | ||
if (reactTreeMounter.options.mountNode) { | ||
element = createPortal(element, reactTreeMounter.options.mountNode); | ||
} | ||
|
||
return element; | ||
} | ||
} |