Skip to content

Commit

Permalink
[Feat]: Passing root context to tippy
Browse files Browse the repository at this point in the history
  • Loading branch information
igorDykhta committed Aug 24, 2022
1 parent 34ebb88 commit f828f69
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 54 deletions.
32 changes: 20 additions & 12 deletions src/components/common/tippy-tooltip.tsx
Expand Up @@ -20,6 +20,7 @@

import styled from 'styled-components';
import React, {useState} from 'react';
import {RootContext} from 'components';
import Tippy, {TippyProps} from '@tippyjs/react';

const TippyArrow = styled.div`
Expand Down Expand Up @@ -111,19 +112,26 @@ const TippyTooltip = ({children, render, duration = 200, ...rest}: TippyProps) =
}

return (
<Tippy
{...rest}
animation={true}
render={attrs => (
<TippyTooltipContent {...attrs} style={{opacity, transition: `opacity ${duration}ms`}}>
{render?.(attrs)}
</TippyTooltipContent>
<RootContext.Consumer>
{context => (
<Tippy
{...rest}
// Using document.body would result in the CSS styles not being applied
// to the tooltip content when embedding the map widget as a Shadow DOM element.
appendTo={context?.current || 'parent'}
animation={true}
render={attrs => (
<TippyTooltipContent {...attrs} style={{opacity, transition: `opacity ${duration}ms`}}>
{render?.(attrs)}
</TippyTooltipContent>
)}
onMount={onMount}
onHide={onHide}
>
{children}
</Tippy>
)}
onMount={onMount}
onHide={onHide}
>
{children}
</Tippy>
</RootContext.Consumer>
);
};

Expand Down
2 changes: 1 addition & 1 deletion src/components/map/map-legend-panel.tsx
Expand Up @@ -139,7 +139,7 @@ function MapLegendPanelFactory(MapControlTooltip, MapControlPanel, MapLegend) {
placement="left-start"
onCreate={setTippyInstance}
render={attrs => <div {...attrs}>{mapControlPanel}</div>}
appendTo={document.body}
appendTo="parent"
>
<div>
<MapControlTooltip id="show-legend" message="tooltip.showLegend">
Expand Down
90 changes: 49 additions & 41 deletions src/components/map/map-popover.tsx
Expand Up @@ -26,6 +26,7 @@ import {ArrowLeft, ArrowRight, Pin} from 'components/common/icons';
import {injectIntl, IntlShape} from 'react-intl';
import {FormattedMessage} from '@kepler.gl/localization';
import Tippy from '@tippyjs/react/headless';
import {RootContext} from 'components';
import {LayerHoverProp} from 'reducers/layer-utils';

const MAX_WIDTH = 500;
Expand Down Expand Up @@ -204,48 +205,55 @@ export default function MapPopoverFactory(
const moveLeft = () => setHorizontalPlacement('end');
const moveRight = () => setHorizontalPlacement('start');
return (
<Tippy
popperOptions={getPopperOptions(container)}
zIndex={999} /* should be below Modal which has zIndex=1000 */
visible={true}
interactive={true}
getReferenceClientRect={() => createVirtualReference(container, x, y)}
// @ts-ignore
placement={`bottom-${horizontalPlacement}`}
// @ts-ignore
offset={getOffsetForPlacement}
appendTo={document.body}
render={attrs => (
<StyledMapPopover {...attrs} className="map-popover">
{frozen ? (
<PinnedButtons>
{horizontalPlacement === 'start' && (
<StyledIcon className="popover-arrow-left" onClick={moveLeft}>
<ArrowLeft />
</StyledIcon>
)}
<StyledIcon className="popover-pin" onClick={onClose}>
<Pin height="16px" />
</StyledIcon>
{horizontalPlacement === 'end' && (
<StyledIcon className="popover-arrow-right" onClick={moveRight}>
<ArrowRight />
</StyledIcon>
)}
{isBase && (
<div className="primary-label">
<FormattedMessage id="mapPopover.primary" />
</div>
)}
</PinnedButtons>
) : null}
<PopoverContent>
{Array.isArray(coordinate) && <CoordinateInfo coordinate={coordinate} zoom={zoom} />}
{layerHoverProp && <LayerHoverInfo {...layerHoverProp} />}
</PopoverContent>
</StyledMapPopover>
<RootContext.Consumer>
{context => (
<Tippy
popperOptions={getPopperOptions(container)}
zIndex={999} /* should be below Modal which has zIndex=1000 */
visible={true}
interactive={true}
// @ts-ignore
getReferenceClientRect={() => createVirtualReference(container, x, y)}
// @ts-ignore
placement={`bottom-${horizontalPlacement}`}
// @ts-ignore
offset={getOffsetForPlacement}
appendTo={context?.current || document.body}
render={attrs => (
<StyledMapPopover {...attrs} className="map-popover">
{frozen ? (
<PinnedButtons>
{horizontalPlacement === 'start' && (
<StyledIcon className="popover-arrow-left" onClick={moveLeft}>
<ArrowLeft />
</StyledIcon>
)}
<StyledIcon className="popover-pin" onClick={onClose}>
<Pin height="16px" />
</StyledIcon>
{horizontalPlacement === 'end' && (
<StyledIcon className="popover-arrow-right" onClick={moveRight}>
<ArrowRight />
</StyledIcon>
)}
{isBase && (
<div className="primary-label">
<FormattedMessage id="mapPopover.primary" />
</div>
)}
</PinnedButtons>
) : null}
<PopoverContent>
{Array.isArray(coordinate) && (
<CoordinateInfo coordinate={coordinate} zoom={zoom} />
)}
{layerHoverProp && <LayerHoverInfo {...layerHoverProp} />}
</PopoverContent>
</StyledMapPopover>
)}
/>
)}
/>
</RootContext.Consumer>
);
};
return injectIntl(MapPopover);
Expand Down

0 comments on commit f828f69

Please sign in to comment.