Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 | 1x 1x 1x 1x 4x 4x 1x 3x 1x 1x | import React from 'react';
import {useResizeObserver} from '@gravity-ui/uikit';
import {SplitLayout, StyledSplitPane} from '../SplitPane';
import type {SplitLayoutType} from '../SplitPane';
export const SPLIT_TOOLTIP_RESIZER_SIZE = 24;
export const SPLIT_TOOLTIP_MAIN_PANE_RATIO = 0.6;
export {SplitLayout as SplitTooltipLayout};
export type SplitTooltipLayout = SplitLayoutType;
export interface SplitTooltipRenderProps {
/** The active pane layout. */
layout: SplitTooltipLayout;
/** The current size of the main pane in pixels. */
size: number;
}
export interface SplitTooltipProps {
/**
* The controlled pane layout. When omitted, the layout is derived from the container size:
* vertical when width is greater than height, horizontal otherwise.
*/
layout?: SplitTooltipLayout;
/** The share of the container width occupied by the main pane in vertical layout. Defaults to `0.6`. */
mainPaneRatio?: number;
/** Additional styles for the main pane. */
mainPaneStyle?: React.CSSProperties;
/** Called after initialization and whenever the main pane size or layout changes. */
onMainPaneSizeChange?: (size: number, layout: SplitTooltipLayout) => void;
/** Renders the main pane with its current layout and size. */
renderMainPane: (props: SplitTooltipRenderProps) => React.ReactNode;
/** Renders the tooltip pane with the current main pane layout and size. */
renderTooltip: (props: SplitTooltipRenderProps) => React.ReactNode;
/** Controls the resizer visibility without hiding the tooltip pane. Defaults to `false`. */
resizerVisible?: boolean;
/** Additional styles for the split pane container. */
style?: React.CSSProperties;
/** Additional styles for the tooltip pane. */
tooltipPaneStyle?: React.CSSProperties;
}
type SplitTooltipContentProps = SplitTooltipProps & {
containerHeight: number;
containerWidth: number;
};
const hiddenResizerStyle = {display: 'none'};
const defaultTooltipPaneStyle = {overflow: 'auto'};
export function getSplitTooltipLayout(
width: number,
height: number,
layout?: SplitTooltipLayout,
): SplitTooltipLayout {
return layout ?? (width > height ? SplitLayout.VERTICAL : SplitLayout.HORIZONTAL);
}
export function getSplitTooltipMainPaneSize({
containerHeight,
containerWidth,
layout,
mainPaneRatio = SPLIT_TOOLTIP_MAIN_PANE_RATIO,
tooltipHeight,
}: {
containerHeight: number;
containerWidth: number;
layout: SplitTooltipLayout;
mainPaneRatio?: number;
tooltipHeight: number;
}) {
if (layout === SplitLayout.VERTICAL) {
return containerWidth * mainPaneRatio;
}
return Math.max(0, containerHeight - SPLIT_TOOLTIP_RESIZER_SIZE - tooltipHeight);
}
export function getSplitTooltipSizeLimits(containerHeight: number, tooltipHeight: number) {
const maxSize = getSplitTooltipMainPaneSize({
containerHeight,
containerWidth: 0,
layout: SplitLayout.HORIZONTAL,
tooltipHeight,
});
return {
minSize: Math.min(containerHeight / 3, maxSize),
maxSize,
};
}
type SplitTooltipDimensions = {
containerHeight: number;
containerWidth: number;
layout: SplitTooltipLayout;
mainPaneRatio: number;
tooltipHeight: number;
};
function getNextMainPaneSize({
currentSize,
nextDimensions,
previousDimensions,
}: {
currentSize: number;
nextDimensions: SplitTooltipDimensions;
previousDimensions: SplitTooltipDimensions;
}) {
if (
nextDimensions.layout !== previousDimensions.layout ||
nextDimensions.layout === SplitLayout.VERTICAL
) {
return getSplitTooltipMainPaneSize(nextDimensions);
}
const {maxSize: previousMaxSize} = getSplitTooltipSizeLimits(
previousDimensions.containerHeight,
previousDimensions.tooltipHeight,
);
const {minSize: nextMinSize, maxSize: nextMaxSize} = getSplitTooltipSizeLimits(
nextDimensions.containerHeight,
nextDimensions.tooltipHeight,
);
if (currentSize === previousMaxSize) {
return nextMaxSize;
}
return Math.max(nextMinSize, Math.min(nextMaxSize, currentSize));
}
function SplitTooltipContent(props: SplitTooltipContentProps) {
const {
containerHeight,
containerWidth,
renderMainPane,
renderTooltip,
layout: layoutProp,
mainPaneRatio = SPLIT_TOOLTIP_MAIN_PANE_RATIO,
resizerVisible,
onMainPaneSizeChange,
style,
mainPaneStyle,
tooltipPaneStyle,
} = props;
const tooltipRef = React.useRef<HTMLDivElement | null>(null);
const layout = getSplitTooltipLayout(containerWidth, containerHeight, layoutProp);
const [tooltipHeight, setTooltipHeight] = React.useState(0);
const [size, setSize] = React.useState(() =>
getSplitTooltipMainPaneSize({
containerHeight,
containerWidth,
layout,
mainPaneRatio,
tooltipHeight: 0,
}),
);
const dimensions = React.useMemo(
() => ({
containerHeight,
containerWidth,
layout,
mainPaneRatio,
tooltipHeight,
}),
[containerHeight, containerWidth, layout, mainPaneRatio, tooltipHeight],
);
const previousDimensionsRef = React.useRef(dimensions);
const lastNotificationRef = React.useRef<{layout: SplitTooltipLayout; size: number} | null>(
null,
);
const onMainPaneSizeChangeRef = React.useRef(onMainPaneSizeChange);
onMainPaneSizeChangeRef.current = onMainPaneSizeChange;
const handleTooltipResize = React.useCallback(() => {
const nextTooltipHeight = tooltipRef.current?.getBoundingClientRect().height ?? 0;
setTooltipHeight(nextTooltipHeight);
}, []);
useResizeObserver({
ref: tooltipRef,
onResize: handleTooltipResize,
});
const effectiveSize = getNextMainPaneSize({
currentSize: size,
nextDimensions: dimensions,
previousDimensions: previousDimensionsRef.current,
});
React.useLayoutEffect(() => {
previousDimensionsRef.current = dimensions;
if (effectiveSize !== size) {
setSize(effectiveSize);
}
}, [dimensions, effectiveSize, size]);
React.useLayoutEffect(() => {
const previousNotification = lastNotificationRef.current;
if (
previousNotification?.size === effectiveSize &&
previousNotification.layout === layout
) {
return undefined;
}
const callback = onMainPaneSizeChangeRef.current;
if (!callback) {
return undefined;
}
lastNotificationRef.current = {layout, size: effectiveSize};
return callback(effectiveSize, layout);
});
const allowResize = layout === SplitLayout.HORIZONTAL;
const sizeLimits = getSplitTooltipSizeLimits(containerHeight, tooltipHeight);
const maxSize = allowResize ? sizeLimits.maxSize : undefined;
const minSize = allowResize ? sizeLimits.minSize : undefined;
const renderProps = {layout, size: effectiveSize};
return (
<StyledSplitPane
allowResize={allowResize}
maxSize={maxSize}
minSize={minSize}
size={effectiveSize}
split={layout}
style={style}
onChange={setSize}
resizerStyle={resizerVisible ? undefined : hiddenResizerStyle}
paneOneRender={() => renderMainPane(renderProps)}
paneTwoRender={() => <div ref={tooltipRef}>{renderTooltip(renderProps)}</div>}
pane1Style={mainPaneStyle}
pane2Style={{...defaultTooltipPaneStyle, ...tooltipPaneStyle}}
/>
);
}
export function SplitTooltip(props: SplitTooltipProps) {
const containerRef = React.useRef<HTMLDivElement | null>(null);
const [dimensions, setDimensions] = React.useState({height: 0, width: 0});
const updateDimensions = React.useCallback(() => {
const rect = containerRef.current?.getBoundingClientRect();
setDimensions({
height: rect?.height ?? 0,
width: rect?.width ?? 0,
});
}, []);
useResizeObserver({
ref: containerRef,
onResize: updateDimensions,
});
React.useLayoutEffect(updateDimensions, [updateDimensions]);
return (
<div ref={containerRef} style={{position: 'relative', height: '100%'}}>
{dimensions.height > 0 && dimensions.width > 0 ? (
<SplitTooltipContent
{...props}
containerHeight={dimensions.height}
containerWidth={dimensions.width}
/>
) : null}
</div>
);
}
|