Skip to content

Commit

Permalink
Merge pull request #2873 from udecode/fix/toolbar-toggle-button
Browse files Browse the repository at this point in the history
Fix/toolbar toggle button
  • Loading branch information
zbeyens committed Jan 9, 2024
1 parent 711ad1b commit 125bc34
Showing 1 changed file with 45 additions and 0 deletions.
45 changes: 45 additions & 0 deletions src/components/plate-ui/tooltip.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import React from 'react';
import * as TooltipPrimitive from '@radix-ui/react-tooltip';
import { withCn, withProps } from '@udecode/cn';

Expand All @@ -14,3 +15,47 @@ export const TooltipContent = withCn(
}),
'z-50 overflow-hidden rounded-md border bg-popover px-3 py-1.5 text-sm text-popover-foreground shadow-md'
);

export function withTooltip<
T extends React.ComponentType<any> | keyof HTMLElementTagNameMap,
>(Component: T) {
return React.forwardRef<
React.ElementRef<T>,
React.ComponentPropsWithoutRef<T> & {
tooltip?: React.ReactNode;
tooltipContentProps?: Omit<
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Content>,
'children'
>;
tooltipProps?: Omit<
React.ComponentPropsWithoutRef<typeof TooltipPrimitive.Root>,
'children'
>;
}
>(function ExtendComponent(
{ tooltip, tooltipContentProps, tooltipProps, ...props },
ref
) {
const [mounted, setMounted] = React.useState(false);

React.useEffect(() => {
setMounted(true);
}, []);

const component = <Component ref={ref} {...(props as any)} />;

if (tooltip && mounted) {
return (
<Tooltip {...tooltipProps}>
<TooltipTrigger asChild>{component}</TooltipTrigger>

<TooltipPortal>
<TooltipContent {...tooltipContentProps}>{tooltip}</TooltipContent>
</TooltipPortal>
</Tooltip>
);
}

return component;
});
}

0 comments on commit 125bc34

Please sign in to comment.