From 4ccbf46b4b33e13d85b69011967b2cfa03bd6fde Mon Sep 17 00:00:00 2001 From: Daniel Barion Date: Tue, 13 Jun 2023 13:31:05 -0300 Subject: [PATCH 1/2] feat: [WIP] inject styles into the shadow root element and attach the event listener --- src/components/Tooltip/Tooltip.tsx | 32 ++++++++++++- src/components/Tooltip/styles.js | 72 ++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 src/components/Tooltip/styles.js diff --git a/src/components/Tooltip/Tooltip.tsx b/src/components/Tooltip/Tooltip.tsx index ab9d2e1b..06b61d35 100644 --- a/src/components/Tooltip/Tooltip.tsx +++ b/src/components/Tooltip/Tooltip.tsx @@ -5,6 +5,7 @@ import { useTooltip } from 'components/TooltipProvider' import useIsomorphicLayoutEffect from 'utils/use-isomorphic-layout-effect' import { computeTooltipPosition } from '../../utils/compute-positions' import styles from './styles.module.css' +import normalStyles from './styles' import type { IPosition, ITooltip, PlacesType } from './TooltipTypes' const Tooltip = ({ @@ -62,6 +63,12 @@ const Tooltip = ({ const shouldOpenOnClick = openOnClick || events.includes('click') + /** + * web components support + */ + const root = tooltipRef.current && tooltipRef.current.getRootNode() + const isOnShadowDOM = root instanceof ShadowRoot + /** * useLayoutEffect runs before useEffect, * but should be used carefully because of caveats @@ -74,6 +81,18 @@ const Tooltip = ({ } }, []) + useEffect(() => { + if (typeof window !== 'undefined' && tooltipRef.current) { + if (isOnShadowDOM && !root.getElementById('react-tooltip-styles')) { + const style = document.createElement('style') + style.id = 'react-tooltip-styles' + style.textContent = normalStyles + + root.appendChild(style) + } + } + }) + useEffect(() => { if (!show) { /** @@ -339,7 +358,11 @@ const Tooltip = ({ enabledEvents.forEach(({ event, listener }) => { elementRefs.forEach((ref) => { - ref.current?.addEventListener(event, listener) + if (isOnShadowDOM) { + root.addEventListener(event, listener) + } else { + ref.current?.addEventListener(event, listener) + } }) }) @@ -547,6 +570,7 @@ const Tooltip = ({ role="tooltip" className={classNames( 'react-tooltip', + 'rt-tooltip', styles['tooltip'], styles[variant], className, @@ -555,6 +579,9 @@ const Tooltip = ({ [styles['show']]: canShow, [styles['fixed']]: positionStrategy === 'fixed', [styles['clickable']]: clickable, + 'rt-show': canShow, + 'rt-fixed': positionStrategy === 'fixed', + 'rt-clickable': clickable, }, )} style={{ ...externalStyles, ...inlineStyles }} @@ -562,12 +589,13 @@ const Tooltip = ({ > {content} Date: Tue, 13 Jun 2023 13:31:43 -0300 Subject: [PATCH 2/2] chore: update dev environment to test the web components implementation --- src/App.tsx | 2 +- src/index-dev.tsx | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/App.tsx b/src/App.tsx index fd85b9ad..e17ab832 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -8,7 +8,7 @@ import styles from './styles.module.css' function App() { const [anchorId, setAnchorId] = useState('button') - const [isDarkOpen, setIsDarkOpen] = useState(false) + const [isDarkOpen, setIsDarkOpen] = useState(true) const [position, setPosition] = useState({ x: 0, y: 0 }) const [toggle, setToggle] = useState(false) diff --git a/src/index-dev.tsx b/src/index-dev.tsx index 257d3242..7f0549cb 100644 --- a/src/index-dev.tsx +++ b/src/index-dev.tsx @@ -6,4 +6,4 @@ import './tokens.css' // eslint-disable-next-line no-console console.log('Parent folder loaded react version: ', version) -ReactDOM.render(, document.getElementById('app')) +ReactDOM.render(, document.getElementById('app')?.attachShadow({ mode: 'open' }))