Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: clear hover state when platform opens in new tab #224

Merged
merged 1 commit into from
Feb 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 17 additions & 13 deletions src/PlatformSwitch/PlatformSwitch.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Box, SxProps } from '@mui/material';

import React, { FC, useState } from 'react';
import React, { FC, MouseEvent, useState } from 'react';

import { AnalyticsIcon, BuildIcon, LibraryIcon, PlayIcon } from '../icons';
import { PRIMARY_COLOR, SECONDARY_COLOR } from '../theme';
Expand Down Expand Up @@ -33,12 +33,12 @@ export type PlatformSwitchProps = {
/** Whether this platform should be disabled (non-clickable) */
disabled?: boolean;
/** Action when this platform button is clicked */
onClick?: React.MouseEventHandler<HTMLElement>;
onClick?: React.MouseEventHandler;
/**
* Action when this platform button is clicked
* (any mouse button, use the {@see MouseEvent} parameter to discriminate)
*/
onMouseDown?: React.MouseEventHandler<HTMLElement>;
onMouseDown?: React.MouseEventHandler;
/** Style overrides for this platform's icon */
sx?: SxProps;
}
Expand Down Expand Up @@ -88,15 +88,23 @@ export const PlatformSwitch: FC<PlatformSwitchProps> = ({
// Emulate mouseover: we want to change the color of the icons that are props
const [isHover, setHover] = useState(false);

const mouseHoverEvents = {
onMouseEnter: () => setHover(true),
onMouseLeave: () => setHover(false),
};

const isSelectedPlatform = platform === selected;

const platformProps = platformsProps?.[platform];

const mouseEvents = {
onMouseEnter: () => setHover(true),
onMouseLeave: () => setHover(false),
onClick: platformProps?.disabled ? undefined : platformProps?.onClick,
onMouseDown: platformProps?.disabled
? undefined
: (event: MouseEvent) => {
platformProps?.onMouseDown?.(event);
// when middle-clicked, opens in new tab => hover state may not be cleared
setHover(false);
},
};

const Icon = PlatformIcons[platform];

const iconProps = {
Expand Down Expand Up @@ -132,11 +140,7 @@ export const PlatformSwitch: FC<PlatformSwitchProps> = ({
display: 'flex',
cursor: platformProps?.disabled ? 'default' : 'pointer',
}}
{...mouseHoverEvents}
onClick={platformProps?.disabled ? undefined : platformProps?.onClick}
onMouseDown={
platformProps?.disabled ? undefined : platformProps?.onMouseDown
}
{...mouseEvents}
>
<Icon {...iconProps} {...hoverStyles} {...disabledStyles} />
</a>
Expand Down