diff --git a/src/Body/MeasureCell.tsx b/src/Body/MeasureCell.tsx index 1db7d06bb..abf0d03ae 100644 --- a/src/Body/MeasureCell.tsx +++ b/src/Body/MeasureCell.tsx @@ -1,6 +1,7 @@ import * as React from 'react'; import ResizeObserver from 'rc-resize-observer'; import useLayoutEffect from 'rc-util/lib/hooks/useLayoutEffect'; +import ShadowComponent from './ShadowComponent'; import type { ColumnType } from '../interface'; export interface MeasureCellProps { @@ -27,7 +28,9 @@ export default function MeasureCell({ return ( -
{column?.title || '\xa0'}
+ + {column?.title || '\xa0'} +
); diff --git a/src/Body/ShadowComponent.tsx b/src/Body/ShadowComponent.tsx new file mode 100644 index 000000000..420f5b7da --- /dev/null +++ b/src/Body/ShadowComponent.tsx @@ -0,0 +1,45 @@ +import * as React from 'react'; +import { createPortal } from 'react-dom'; + +function isShadowDomSupported() { + return typeof window !== 'undefined' && !!HTMLElement.prototype.attachShadow; +} + +export default function ShadowComponent({ + children, + className, +}: { + children: React.ReactNode; + className: string; +}) { + const hostRef = React.useRef(null); + const [shadowRoot, setShadowRoot] = React.useState(null); + const [shadowSupported, setShadowSupported] = React.useState(true); + + React.useEffect(() => { + // 检查是否支持 Shadow DOM + if (!isShadowDomSupported()) { + setShadowSupported(false); + return; + } + if (hostRef.current && !shadowRoot) { + const shadow = hostRef.current.attachShadow({ mode: 'open' }); + setShadowRoot(shadow); + } + }, [shadowRoot]); + + // 如果不支持 Shadow DOM,直接渲染 children + if (!shadowSupported) { + return ( +
+ {children} +
+ ); + } + + return ( +
+ {shadowRoot && createPortal(children, shadowRoot)} +
+ ); +} diff --git a/tests/__snapshots__/ExpandRow.spec.jsx.snap b/tests/__snapshots__/ExpandRow.spec.jsx.snap index 2d85db84f..c693bca3d 100644 --- a/tests/__snapshots__/ExpandRow.spec.jsx.snap +++ b/tests/__snapshots__/ExpandRow.spec.jsx.snap @@ -195,36 +195,28 @@ LoadedCheerio { >
-   -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />
-   -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />
-   -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />
-   -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />
-   -
+ />
- title1 -
+ />
- title2 -
+ />
- title3 -
+ />
- title4 -
+ />
- title5 -
+ />
- title6 -
+ />
- title7 -
+ />
- title8 -
+ />
- title9 -
+ />
- title10 -
+ />
- title11 -
+ />
- title12 -
+ />
- title1 -
+ />
- title2 -
+ />
- title3 -
+ />
- title4 -
+ />
- title5 -
+ />
- title6 -
+ />
- title7 -
+ />
- title8 -
+ />
- title9 -
+ />
- title10 -
+ />
- title11 -
+ />
- title12 -
+ />
- title1 -
+ />
- title2 -
+ />
- title3 -
+ />
- title4 -
+ />
- title5 -
+ />
- title6 -
+ />
- title7 -
+ />
- title8 -
+ />
- title9 -
+ />
- title10 -
+ />
- title11 -
+ />
- title12 -
+ />
- title1 -
+ />
- title2 -
+ />
- title3 -
+ />
- title4 -
+ />
- title5 -
+ />
- title6 -
+ />
- title7 -
+ />
- title8 -
+ />
- title9 -
+ />
- title10 -
+ />
- title11 -
+ />
- title12 -
+ />
- title1 -
+ />
- title2 -
+ />
- title3 -
+ />
- title4 -
+ />
- title5 -
+ />
- title6 -
+ />
- title7 -
+ />
- title8 -
+ />
- title9 -
+ />
- title10 -
+ />
- title11 -
+ />
- title12 -
+ /> shadow should be shown when there are columns where >
- Other -
+ />
- Name -
+ />
- Company -
+ />
- Gender -
+ /> shadow should display correctly 1`] = ` >
- Age -
+ />
- Street -
+ />
- Door No. -
+ />
- Building -
+ />
- Name -
+ />
- Company Address -
+ />
- Company Name -
+ />
- Gender -
+ /> shadow should display correctly 2`] = ` >
- Name0 -
+ />
- Name1 -
+ />
- Name2 -
+ />
- Name3 -
+ />
- Name -
+ />
- Age -
+ />
- Gender -
+ />