From 07de1cadfa881f700b83d249b6d757a29d8a9d77 Mon Sep 17 00:00:00 2001 From: acarreras Date: Fri, 29 Nov 2024 09:14:26 +0100 Subject: [PATCH] more --- packages/gestalt/src/HelpButton.tsx | 93 +++++++++++++++-------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/packages/gestalt/src/HelpButton.tsx b/packages/gestalt/src/HelpButton.tsx index 349bf6a3f5..546cdb13df 100644 --- a/packages/gestalt/src/HelpButton.tsx +++ b/packages/gestalt/src/HelpButton.tsx @@ -1,4 +1,4 @@ -import { ComponentProps, ReactElement, Ref, useId, useRef, useState } from 'react'; +import { ComponentProps, ReactElement, useId, useRef, useState } from 'react'; import Box from './Box'; import { useColorScheme } from './contexts/ColorSchemeProvider'; import { useDefaultLabelContext } from './contexts/DefaultLabelProvider'; @@ -12,10 +12,11 @@ import InternalPopover from './Popover/InternalPopover'; import TapArea from './TapArea'; import Text from './Text'; import Tooltip from './Tooltip'; +import useInExperiment from './useInExperiment'; import { CompositeZIndex, FixedZIndex, Indexable } from './zIndex'; type LinkType = { - accessibilityLabel?: string; + accessibilityLabel?: ComponentProps['accessibilityLabel']; externalLinkIcon?: | 'none' | 'default' @@ -23,20 +24,13 @@ type LinkType = { color: ComponentProps['color']; size: ComponentProps['size']; }; - href: string; - onClick?: (arg1: { - event: React.MouseEvent | React.KeyboardEvent; - dangerouslyDisableOnNavigation: () => void; - }) => void; - ref?: Ref<'a'>; + href: ComponentProps['href']; + onClick?: ComponentProps['onClick']; + ref?: ComponentProps['ref']; text: string; - target?: null | 'self' | 'blank'; + target?: ComponentProps['target']; }; -type OnTapType = (arg1: { - event: React.MouseEvent | React.KeyboardEvent; -}) => void; - type Props = { /** * Supply a short, descriptive label screen readers. Follow the pattern `Click to learn more about [description]`. See [Accessibility](https://gestalt.pinterest.systems/web/helpbutton#Accessibility) section for guidance. @@ -66,7 +60,9 @@ type Props = { /** * Callback fired when HelpIcon is clicked (pressed and released) with a mouse or keyboard. */ - onClick?: OnTapType; + onClick?: (arg1: { + event: React.MouseEvent | React.KeyboardEvent; + }) => void; /** * Informational content that's displayed when the user clicks on HelpButton. */ @@ -90,18 +86,25 @@ export default function HelpButton({ text, zIndex, }: Props) { - const tapAreaRef = useRef(null); + const tapAreaRef = useRef(null); const textRef = useRef(null); + const [open, setOpen] = useState(false); const [hovered, setHovered] = useState(false); const [focused, setFocused] = useState(false); + // Define where the focused content stays const [innerModalFocus, setInnerModalFocus] = useState(false); + + const isInVRExperiment = useInExperiment({ + webExperimentName: 'web_gestalt_visualRefresh', + mwebExperimentName: 'web_gestalt_visualRefresh', + }); const { colorSchemeName } = useColorScheme(); const popoverId = useId(); const { tooltipMessage } = useDefaultLabelContext('HelpButton'); - const handlePopoverKeyDown = ({ event }: { event: React.KeyboardEvent }) => { + const handlePopoverKeyDown: ComponentProps['onKeyDown'] = ({ event }) => { // Avoid others KeyDown events to listen this call if (innerModalFocus) event.stopPropagation(); @@ -114,6 +117,7 @@ export default function HelpButton({ `a[href="${link?.href ?? ''}"]`, // Link query ].join(' ,'), ); + const lastElement = elementsInnerPopover[elementsInnerPopover.length - 1]; if (innerModalFocus && event.keyCode === TAB && lastElement === event.target) { event.preventDefault(); @@ -127,7 +131,7 @@ export default function HelpButton({ } }; - const handleTapAreaKeyDown = ({ event }: { event: React.KeyboardEvent }) => { + const handleTapAreaKeyDown: ComponentProps['onKeyDown'] = ({ event }) => { if (event.keyCode === TAB && open) { event.preventDefault(); textRef.current?.focus(); @@ -137,25 +141,18 @@ export default function HelpButton({ event.stopPropagation(); }; - const toggleView = () => { - setOpen((currVal) => !currVal); - }; + const toggleView = () => setOpen((currVal) => !currVal); - const onHandleTap = ( - ...args: ReadonlyArray<{ - event: React.MouseEvent | React.KeyboardEvent; - }> - ) => { - toggleView(); - // @ts-expect-error - TS2556 - A spread argument must either have a tuple type or be passed to a rest parameter. - onClick?.(...args); - }; + let bgIconColor: ComponentProps['color'] = isInVRExperiment ? 'default' : 'tertiary'; + let iconColor: ComponentProps['color'] = isInVRExperiment ? 'subtle' : 'inverse'; - const bgIconColor = open || hovered || focused ? 'selected' : 'tertiary'; + if (!isInVRExperiment && (open || hovered || focused)) { + bgIconColor = 'selected'; + } - const tooltipZIndex = zIndex ?? new FixedZIndex(1); - - const zIndexWrapper = new CompositeZIndex([tooltipZIndex]); + if (isInVRExperiment && (open || hovered || focused)) { + iconColor = 'default'; + } // Overriding color of `Text` components const isDarkMode = colorSchemeName === 'darkMode'; @@ -163,12 +160,9 @@ export default function HelpButton({ ? styles.textColorOverrideLight : styles.textColorOverrideDark; - const textElement = - typeof text === 'string' ? ( - {text} - ) : ( - {text} - ); + const tooltipZIndex = zIndex ?? new FixedZIndex(1); + + const zIndexWrapper = new CompositeZIndex([tooltipZIndex]); const popoverElement = ( - {textElement} + {typeof text === 'string' ? ( + {text} + ) : ( + {text} + )} {link && link?.href && ( | undefined' is not assignable to type 'LegacyRef | undefined'. ref={link.ref} accessibilityLabel={link.accessibilityLabel} externalLinkIcon={link.externalLinkIcon} @@ -228,18 +225,21 @@ export default function HelpButton({ zIndex={tooltipZIndex} > ' is not assignable to type 'LegacyRef | undefined'. ref={tapAreaRef} accessibilityControls={popoverId} accessibilityExpanded={open} accessibilityLabel={accessibilityLabel} fullWidth={false} + innerFocusColor={isInVRExperiment ? 'default' : undefined} onBlur={() => setFocused(false)} onFocus={() => setFocused(true)} onKeyDown={handleTapAreaKeyDown} onMouseEnter={() => setHovered(true)} onMouseLeave={() => setHovered(false)} - onTap={onHandleTap} + onTap={({ event }) => { + toggleView(); + onClick?.({ event }); + }} role="button" rounding="circle" > @@ -252,7 +252,12 @@ export default function HelpButton({ rounding="circle" width={16} > - +