1- import React , { isValidElement , useContext , useEffect } from "react" ;
1+ import React , { isValidElement , useContext , useEffect , useMemo } from "react" ;
2+ import { createPortal } from "react-dom" ;
23import { LayersContext } from "./context" ;
34import { type Elevation } from "@react-ck/elevation" ;
45import { getDisplayName } from "@react-ck/react-utils" ;
6+ import { ThemeProvider , useThemeContext } from "@react-ck/theme" ;
57
68export interface LayerProps {
79 /** The elevation level for the layer */
@@ -12,21 +14,35 @@ export interface LayerProps {
1214
1315/**
1416 * Component for creating a layer with a specific elevation level,
15- * works like React portal - will render its children on app root
17+ * works with React portal - will render its children on document body
18+ * It also wraps the content in a theme provider
1619 *
1720 * @param props - {@link LayerProps}.
1821 * @returns The rendered content of the layer
1922 */
2023
2124export const Layer = ( { elevation, children } : Readonly < LayerProps > ) : React . ReactNode => {
25+ const theme = useThemeContext ( ) ;
2226 const { createLayer } = useContext ( LayersContext ) ;
2327
28+ /** Generates the portal element wrapped by theme */
29+ const layerElement = useMemo (
30+ ( ) =>
31+ createPortal ( < ThemeProvider theme = { theme . theme } > { children } </ ThemeProvider > , document . body ) ,
32+ [ children , theme . theme ] ,
33+ ) ;
34+
35+ /** Renders the layer */
2436 useEffect ( ( ) => {
25- const removeLayer = createLayer ( { elevationKey : elevation , node : children } ) ;
37+ const removeLayer = createLayer ( {
38+ elevationKey : elevation ,
39+ node : layerElement ,
40+ } ) ;
2641
2742 return removeLayer ;
28- } , [ elevation , children , createLayer ] ) ;
43+ } , [ elevation , children , createLayer , theme , layerElement ] ) ;
2944
45+ /** Validate children */
3046 useEffect ( ( ) => {
3147 // Validate icon usage (icon should be set through specific prop)
3248 for ( const i of React . Children . toArray ( children ) . filter ( isValidElement ) ) {
0 commit comments