fix: prevent tooltip context crashes in chat (#1134)
Keeps VS Code chat from crashing when tooltip context is unavailable Falls back safely for tooltip triggers and content
This commit is contained in:
@@ -8,6 +8,25 @@ type AsChildRenderProps = {
|
|||||||
children?: React.ReactNode;
|
children?: React.ReactNode;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class TooltipPartBoundary extends React.Component<{
|
||||||
|
children: React.ReactNode;
|
||||||
|
fallback?: React.ReactNode;
|
||||||
|
}, { hasError: boolean }> {
|
||||||
|
state = { hasError: false };
|
||||||
|
|
||||||
|
static getDerivedStateFromError() {
|
||||||
|
return { hasError: true };
|
||||||
|
}
|
||||||
|
|
||||||
|
render() {
|
||||||
|
if (this.state.hasError) {
|
||||||
|
return this.props.fallback ?? null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.props.children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type ProviderProps = React.ComponentProps<typeof BaseTooltip.Provider> & {
|
type ProviderProps = React.ComponentProps<typeof BaseTooltip.Provider> & {
|
||||||
delayDuration?: number;
|
delayDuration?: number;
|
||||||
skipDelayDuration?: number;
|
skipDelayDuration?: number;
|
||||||
@@ -54,7 +73,11 @@ function TooltipTrigger({
|
|||||||
const renderProps: AsChildRenderProps = asChild && React.isValidElement(children)
|
const renderProps: AsChildRenderProps = asChild && React.isValidElement(children)
|
||||||
? { render: children as React.ReactElement }
|
? { render: children as React.ReactElement }
|
||||||
: { children };
|
: { children };
|
||||||
return <BaseTooltip.Trigger data-slot="tooltip-trigger" {...props} {...renderProps} />
|
return (
|
||||||
|
<TooltipPartBoundary fallback={children}>
|
||||||
|
<BaseTooltip.Trigger data-slot="tooltip-trigger" {...props} {...renderProps} />
|
||||||
|
</TooltipPartBoundary>
|
||||||
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
type ContentProps = React.ComponentProps<typeof BaseTooltip.Popup> & {
|
type ContentProps = React.ComponentProps<typeof BaseTooltip.Popup> & {
|
||||||
@@ -73,6 +96,7 @@ function TooltipContent({
|
|||||||
...props
|
...props
|
||||||
}: ContentProps) {
|
}: ContentProps) {
|
||||||
return (
|
return (
|
||||||
|
<TooltipPartBoundary>
|
||||||
<BaseTooltip.Portal>
|
<BaseTooltip.Portal>
|
||||||
<BaseTooltip.Positioner sideOffset={sideOffset} side={side} align={align} className="z-50">
|
<BaseTooltip.Positioner sideOffset={sideOffset} side={side} align={align} className="z-50">
|
||||||
<BaseTooltip.Popup
|
<BaseTooltip.Popup
|
||||||
@@ -89,6 +113,7 @@ function TooltipContent({
|
|||||||
</BaseTooltip.Popup>
|
</BaseTooltip.Popup>
|
||||||
</BaseTooltip.Positioner>
|
</BaseTooltip.Positioner>
|
||||||
</BaseTooltip.Portal>
|
</BaseTooltip.Portal>
|
||||||
|
</TooltipPartBoundary>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user