-
Notifications
You must be signed in to change notification settings - Fork 30
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: add a new customization page for "Shadow DOM"
- Loading branch information
Showing
4 changed files
with
203 additions
and
0 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 |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { useRouter } from 'next/router'; | ||
import { useOnce } from '@tonic-ui/react-hooks'; | ||
|
||
const Root = () => { | ||
const router = useRouter(); | ||
|
||
useOnce(() => { | ||
router.push('/customization/shadow-dom'); | ||
}); | ||
|
||
return null; | ||
}; | ||
|
||
export default Root; |
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,176 @@ | ||
import createCache from '@emotion/cache'; | ||
import { CacheProvider } from '@emotion/react'; | ||
import { | ||
Button, | ||
Flex, | ||
Grid, | ||
Modal, | ||
ModalOverlay, | ||
ModalContent, | ||
ModalHeader, | ||
ModalBody, | ||
ModalFooter, | ||
PortalManager, | ||
Stack, | ||
Text, | ||
TonicProvider, | ||
createTheme, | ||
useColorMode, | ||
usePortalManager, | ||
} from '@tonic-ui/react'; | ||
import BorderedBox from '@/components/BorderedBox'; | ||
import React, { useEffect } from 'react'; | ||
import { createRoot } from 'react-dom/client'; | ||
|
||
const TestModal = ({ onClose }) => { | ||
return ( | ||
<Modal | ||
closeOnEsc | ||
closeOnOutsideClick | ||
isClosable | ||
isOpen | ||
onClose={onClose} | ||
size="sm" | ||
> | ||
<ModalOverlay /> | ||
<ModalContent> | ||
<ModalHeader> | ||
Modal Header | ||
</ModalHeader> | ||
<ModalBody> | ||
Modal Body | ||
</ModalBody> | ||
<ModalFooter> | ||
<Grid templateColumns="repeat(2, 1fr)" columnGap="2x"> | ||
<Button variant="primary" onClick={onClose}> | ||
OK | ||
</Button> | ||
<Button variant="default" onClick={onClose}> | ||
Cancel | ||
</Button> | ||
</Grid> | ||
</ModalFooter> | ||
</ModalContent> | ||
</Modal> | ||
); | ||
}; | ||
|
||
const ShadowDOMContainer = () => { | ||
const portal = usePortalManager(); | ||
|
||
return ( | ||
<BorderedBox py="4x" px="6x"> | ||
<Stack | ||
spacing="2x" | ||
sx={{ textAlign: 'center' }} | ||
> | ||
<Text>Shadow DOM</Text> | ||
<Button | ||
variant="primary" | ||
onClick={() => { | ||
portal((close) => <TestModal onClose={close} />); | ||
}} | ||
> | ||
Open Modal | ||
</Button> | ||
</Stack> | ||
</BorderedBox> | ||
); | ||
}; | ||
|
||
const NativeDOMContainer = () => { | ||
const portal = usePortalManager(); | ||
|
||
return ( | ||
<BorderedBox py="4x" px="6x"> | ||
<Stack | ||
spacing="2x" | ||
sx={{ textAlign: 'center' }} | ||
> | ||
<Text>Native DOM</Text> | ||
<Button | ||
variant="primary" | ||
onClick={() => { | ||
portal((close) => <TestModal onClose={close} />); | ||
}} | ||
> | ||
Open Modal | ||
</Button> | ||
</Stack> | ||
</BorderedBox> | ||
); | ||
}; | ||
|
||
const App = () => { | ||
const [colorMode] = useColorMode(); | ||
|
||
useEffect(() => { | ||
const container = document.querySelector('#shadow-root'); | ||
if (!container) { | ||
return; | ||
} | ||
|
||
let shadowContainer = container.shadowRoot; | ||
if (!shadowContainer) { | ||
shadowContainer = container.attachShadow({ mode: 'open' }); | ||
} | ||
|
||
let shadowRootElement = shadowContainer.querySelector('#shadow-content'); | ||
if (!shadowRootElement) { | ||
shadowRootElement = document.createElement('div'); | ||
shadowRootElement.id = 'shadow-content'; | ||
} | ||
|
||
if (!shadowContainer.contains(shadowRootElement)) { | ||
shadowContainer.appendChild(shadowRootElement); | ||
} | ||
|
||
const cache = createCache({ | ||
key: 'tonic-ui-shadow', | ||
prepend: true, | ||
container: shadowContainer, | ||
}); | ||
|
||
const shadowTheme = createTheme({ | ||
cssVariables: { | ||
prefix: 'tonic-shadow', | ||
rootSelector: ':host', | ||
}, | ||
}); | ||
|
||
const root = createRoot(shadowRootElement); | ||
root.render( | ||
<CacheProvider value={cache}> | ||
<TonicProvider | ||
colorMode={{ | ||
value: colorMode, | ||
}} | ||
theme={shadowTheme} | ||
> | ||
<PortalManager> | ||
<ShadowDOMContainer /> | ||
</PortalManager> | ||
</TonicProvider> | ||
</CacheProvider> | ||
); | ||
|
||
return () => { | ||
// Unmount the React component | ||
root.unmount(); | ||
|
||
// Remove all children of the shadow container | ||
while (shadowContainer.firstChild) { | ||
shadowContainer.removeChild(shadowContainer.firstChild); | ||
} | ||
}; | ||
}, [colorMode]); | ||
|
||
return ( | ||
<Flex columnGap="8x"> | ||
<div id="shadow-root" /> | ||
<NativeDOMContainer /> | ||
</Flex> | ||
); | ||
}; | ||
|
||
export default App; |
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,3 @@ | ||
# Shadow DOM | ||
|
||
{render('./shadow-dom')} |