diff --git a/code/ui/components/package.json b/code/ui/components/package.json index 96a71a04c7b7..68fef47503bc 100644 --- a/code/ui/components/package.json +++ b/code/ui/components/package.json @@ -65,7 +65,7 @@ "overlayscrollbars": "^1.13.1", "polished": "^4.2.2", "prettier": "^2.8.0", - "react-popper-tooltip": "^3.1.1", + "react-popper-tooltip": "^4.4.2", "react-syntax-highlighter": "^15.4.5", "react-textarea-autosize": "^8.3.0", "ts-dedent": "^2.0.0", diff --git a/code/ui/components/src/tooltip/Tooltip.tsx b/code/ui/components/src/tooltip/Tooltip.tsx index 80ff9fc05534..942550e88202 100644 --- a/code/ui/components/src/tooltip/Tooltip.tsx +++ b/code/ui/components/src/tooltip/Tooltip.tsx @@ -1,4 +1,3 @@ -import type { FC } from 'react'; import React from 'react'; import memoize from 'memoizerific'; @@ -42,10 +41,10 @@ const Arrow = styled.div( return { transform }; }, ({ theme, color, placement }) => ({ - bottom: `${match('top', placement, ArrowSpacing * -1, 'auto')}px`, - top: `${match('bottom', placement, ArrowSpacing * -1, 'auto')}px`, - right: `${match('left', placement, ArrowSpacing * -1, 'auto')}px`, - left: `${match('right', placement, ArrowSpacing * -1, 'auto')}px`, + bottom: `${match('top', placement, `${ArrowSpacing * -1}px`, 'auto')}`, + top: `${match('bottom', placement, `${ArrowSpacing * -1}px`, 'auto')}`, + right: `${match('left', placement, `${ArrowSpacing * -1}px`, 'auto')}`, + left: `${match('right', placement, `${ArrowSpacing * -1}px`, 'auto')}`, borderBottomWidth: `${match('top', placement, '0', ArrowSpacing)}px`, borderTopWidth: `${match('bottom', placement, '0', ArrowSpacing)}px`, @@ -89,7 +88,6 @@ const Arrow = styled.div( export interface WrapperProps { color: keyof Color; - placement: string; hidden?: boolean; hasChrome: boolean; } @@ -118,7 +116,6 @@ const Wrapper = styled.div( export interface TooltipProps { children?: React.ReactNode; - arrowRef?: any; tooltipRef?: any; hasChrome?: boolean; arrowProps?: any; @@ -126,27 +123,20 @@ export interface TooltipProps { color?: keyof Color; } -export const Tooltip: FC = ({ - placement, - hasChrome, - children, - arrowProps, - tooltipRef, - arrowRef, - color, - ...props -}) => { - return ( - - {hasChrome && } - {children} - - ); -}; +export const Tooltip = React.forwardRef( + ({ placement, hasChrome, children, arrowProps, tooltipRef, color, ...props }, ref) => { + return ( + + {hasChrome && } + {children} + + ); + } +); +Tooltip.displayName = 'Tooltip'; Tooltip.defaultProps = { color: undefined, - arrowRef: undefined, tooltipRef: undefined, hasChrome: true, placement: 'top', diff --git a/code/ui/components/src/tooltip/WithTooltip.tsx b/code/ui/components/src/tooltip/WithTooltip.tsx index 78c909e8ecf9..80a3c7a578d9 100644 --- a/code/ui/components/src/tooltip/WithTooltip.tsx +++ b/code/ui/components/src/tooltip/WithTooltip.tsx @@ -3,7 +3,8 @@ import React, { useCallback, useState, useEffect } from 'react'; import { styled } from '@storybook/theming'; import { global } from '@storybook/global'; -import TooltipTrigger from 'react-popper-tooltip'; +import type { TriggerType } from 'react-popper-tooltip'; +import { usePopperTooltip } from 'react-popper-tooltip'; import type { Modifier, Placement } from '@popperjs/core'; import { Tooltip } from './Tooltip'; @@ -25,7 +26,7 @@ interface WithHideFn { export interface WithTooltipPureProps { svg?: boolean; - trigger?: 'none' | 'hover' | 'click' | 'right-click'; + trigger?: TriggerType; closeOnClick?: boolean; placement?: Placement; modifiers?: Array>>; @@ -52,26 +53,30 @@ const WithTooltipPure: FC = ({ ...props }) => { const Container = svg ? TargetSvgContainer : TargetContainer; + const { getArrowProps, getTooltipProps, setTooltipRef, setTriggerRef, visible, state } = + usePopperTooltip( + { + trigger, + placement, + defaultVisible: tooltipShown, + closeOnOutsideClick: closeOnClick, + onVisibleChange: onVisibilityChange, + }, + { + modifiers, + } + ); return ( - ( + <> + + {children} + + {visible && ( @@ -80,14 +85,7 @@ const WithTooltipPure: FC = ({ : tooltip} )} - > - {({ getTriggerProps, triggerRef }) => ( - // @ts-expect-error (Converted from ts-ignore) - - {children} - - )} - + ); }; diff --git a/code/ui/manager/src/components/sidebar/Menu.stories.tsx b/code/ui/manager/src/components/sidebar/Menu.stories.tsx index b4e06a9b3902..cb57d6d6e003 100644 --- a/code/ui/manager/src/components/sidebar/Menu.stories.tsx +++ b/code/ui/manager/src/components/sidebar/Menu.stories.tsx @@ -66,6 +66,9 @@ export const Expanded = () => { // @ts-expect-error (needs to be converted to CSF3) Expanded.play = async ({ canvasElement }) => { const canvas = within(canvasElement); + await new Promise((res) => { + setTimeout(res, 500); + }); const menuButton = await canvas.findByRole('button'); await userEvent.click(menuButton); const aboutStorybookBtn = await screen.findByText(/About your Storybook/); @@ -97,6 +100,9 @@ export const ExpandedWithoutReleaseNotes = () => { // @ts-expect-error (needs to be converted to CSF3) ExpandedWithoutReleaseNotes.play = async (context) => { const canvas = within(context.canvasElement); + await new Promise((res) => { + setTimeout(res, 500); + }); await Expanded.play(context); const releaseNotes = await canvas.queryByText(/Release notes/); await expect(releaseNotes).not.toBeInTheDocument(); diff --git a/code/ui/manager/src/components/sidebar/Menu.tsx b/code/ui/manager/src/components/sidebar/Menu.tsx index d5775ee908ea..c1e72f64dd8d 100644 --- a/code/ui/manager/src/components/sidebar/Menu.tsx +++ b/code/ui/manager/src/components/sidebar/Menu.tsx @@ -122,6 +122,14 @@ export const ToolbarMenu: FC<{ placement="bottom" trigger="click" closeOnClick + modifiers={[ + { + name: 'flip', + options: { + allowedAutoPlacements: [], + }, + }, + ]} tooltip={({ onHide }) => } > diff --git a/code/yarn.lock b/code/yarn.lock index 9b67de8df997..76cd030ab1eb 100644 --- a/code/yarn.lock +++ b/code/yarn.lock @@ -4950,7 +4950,7 @@ __metadata: languageName: node linkType: hard -"@popperjs/core@npm:^2.5.4, @popperjs/core@npm:^2.6.0": +"@popperjs/core@npm:^2.11.5, @popperjs/core@npm:^2.6.0": version: 2.11.6 resolution: "@popperjs/core@npm:2.11.6" checksum: 90b1361ab1a19cd351e482a88cb6cf13cf25973e20797bf9b97223e64b87bde8d9668ab2f33be24475e2a092d362ac256c361b16ea4e4ab5b21f2aeaef5f9bb7 @@ -6066,7 +6066,7 @@ __metadata: overlayscrollbars: ^1.13.1 polished: ^4.2.2 prettier: ^2.8.0 - react-popper-tooltip: ^3.1.1 + react-popper-tooltip: ^4.4.2 react-syntax-highlighter: ^15.4.5 react-textarea-autosize: ^8.3.0 ts-dedent: ^2.0.0 @@ -23808,21 +23808,21 @@ __metadata: languageName: node linkType: hard -"react-popper-tooltip@npm:^3.1.1": - version: 3.1.1 - resolution: "react-popper-tooltip@npm:3.1.1" +"react-popper-tooltip@npm:^4.4.2": + version: 4.4.2 + resolution: "react-popper-tooltip@npm:4.4.2" dependencies: - "@babel/runtime": ^7.12.5 - "@popperjs/core": ^2.5.4 - react-popper: ^2.2.4 + "@babel/runtime": ^7.18.3 + "@popperjs/core": ^2.11.5 + react-popper: ^2.3.0 peerDependencies: - react: ^16.6.0 || ^17.0.0 - react-dom: ^16.6.0 || ^17.0.0 - checksum: 782a7a4a066251dd794a790b83e9764a97dbb616a16b0fe501020b0cf7a9fec6804449e7b9b7714bfc4bb5eeba073550b850483aea25245e32317b9ead23c9ec + react: ">=16.6.0" + react-dom: ">=16.6.0" + checksum: 1a080d30027dd702b225cbad35194bea942b3f73324a838021fef415c1717be9548552a13b6862f423475407fdd58e2b5bc406af9638d297b974d2e222eb8ff2 languageName: node linkType: hard -"react-popper@npm:^2.2.4": +"react-popper@npm:^2.3.0": version: 2.3.0 resolution: "react-popper@npm:2.3.0" dependencies: