## Description
This PR upgrades Prettier to v2 + enforces TypeScript’s [`import
type`](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-8.html#type-only-imports-and-export)
syntax where applicable. It’s submitted as a separate PR so we can merge
it easily.
As a part of this PR, we reformat the codebase heavily:
- add `import type` everywhere where it’s required, and
- re-format the code to account for Prettier 2’s breaking changes:
https://prettier.io/blog/2020/03/21/2.0.0.html#breaking-changes
This PR is submitted against `release` to make sure all new code by team
members will adhere to new formatting standards, and we’ll have fewer
conflicts when merging `bundle-optimizations` into `release`. (I’ll
merge `release` back into `bundle-optimizations` once this PR is
merged.)
### Why is this needed?
This PR is needed because, for the Lodash optimization from
7cbb12af88,
we need to use `import type`. Otherwise, `babel-plugin-lodash` complains
that `LoDashStatic` is not a lodash function.
However, just using `import type` in the current codebase will give you
this:
<img width="962" alt="Screenshot 2023-03-08 at 17 45 59"
src="https://user-images.githubusercontent.com/2953267/223775744-407afa0c-e8b9-44a1-90f9-b879348da57f.png">
That’s because Prettier 1 can’t parse `import type` at all. To parse it,
we need to upgrade to Prettier 2.
### Why enforce `import type`?
Apart from just enabling `import type` support, this PR enforces
specifying `import type` everywhere it’s needed. (Developers will get
immediate TypeScript and ESLint errors when they forget to do so.)
I’m doing this because I believe `import type` improves DX and makes
refactorings easier.
Let’s say you had a few imports like below. Can you tell which of these
imports will increase the bundle size? (Tip: it’s not all of them!)
```ts
// app/client/src/workers/Linting/utils.ts
import { Position } from "codemirror";
import { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
It’s pretty hard, right?
What about now?
```ts
// app/client/src/workers/Linting/utils.ts
import type { Position } from "codemirror";
import type { LintError as JSHintError, LintOptions } from "jshint";
import { get, isEmpty, isNumber, keys, last, set } from "lodash";
```
Now, it’s clear that only `lodash` will be bundled.
This helps developers to see which imports are problematic, but it
_also_ helps with refactorings. Now, if you want to see where
`codemirror` is bundled, you can just grep for `import \{.*\} from
"codemirror"` – and you won’t get any type-only imports.
This also helps (some) bundlers. Upon transpiling, TypeScript erases
type-only imports completely. In some environment (not ours), this makes
the bundle smaller, as the bundler doesn’t need to bundle type-only
imports anymore.
## Type of change
- Chore (housekeeping or task changes that don't impact user perception)
## How Has This Been Tested?
This was tested to not break the build.
### Test Plan
> Add Testsmith test cases links that relate to this PR
### Issues raised during DP testing
> Link issues raised during DP testing for better visiblity and tracking
(copy link from comments dropped on this PR)
## Checklist:
### Dev activity
- [x] My code follows the style guidelines of this project
- [ ] I have performed a self-review of my own code
- [ ] I have commented my code, particularly in hard-to-understand areas
- [ ] I have made corresponding changes to the documentation
- [x] My changes generate no new warnings
- [ ] I have added tests that prove my fix is effective or that my
feature works
- [ ] New and existing unit tests pass locally with my changes
- [ ] PR is being merged under a feature flag
### QA activity:
- [ ] Test plan has been approved by relevant developers
- [ ] Test plan has been peer reviewed by QA
- [ ] Cypress test cases have been added and approved by either SDET or
manual QA
- [ ] Organized project review call with relevant stakeholders after
Round 1/2 of QA
- [ ] Added Test Plan Approved label after reveiwing all Cypress test
---------
Co-authored-by: Satish Gandham <hello@satishgandham.com>
Co-authored-by: Satish Gandham <satish.iitg@gmail.com>
413 lines
11 KiB
TypeScript
413 lines
11 KiB
TypeScript
import * as React from "react";
|
|
import styled, { createGlobalStyle } from "styled-components";
|
|
import {
|
|
Alignment,
|
|
Button,
|
|
Icon,
|
|
Menu,
|
|
MenuItem as BlueprintMenuItem,
|
|
Classes as BlueprintClasses,
|
|
} from "@blueprintjs/core";
|
|
import { Classes, Popover2 } from "@blueprintjs/popover2";
|
|
import type { IconName } from "@blueprintjs/icons";
|
|
import tinycolor from "tinycolor2";
|
|
|
|
import { darkenActive, darkenHover } from "constants/DefaultTheme";
|
|
import type { ButtonPlacement, ButtonVariant } from "components/constants";
|
|
import { ButtonVariantTypes } from "components/constants";
|
|
import {
|
|
getCustomBackgroundColor,
|
|
getCustomBorderColor,
|
|
getCustomHoverColor,
|
|
getComplementaryGrayscaleColor,
|
|
getCustomJustifyContent,
|
|
getAlignText,
|
|
WidgetContainerDiff,
|
|
lightenColor,
|
|
} from "widgets/WidgetUtils";
|
|
import type { RenderMode } from "constants/WidgetConstants";
|
|
import { DragContainer } from "widgets/ButtonWidget/component/DragContainer";
|
|
import { THEMEING_TEXT_SIZES } from "constants/ThemeConstants";
|
|
import type {
|
|
MenuButtonComponentProps,
|
|
MenuItem,
|
|
PopoverContentProps,
|
|
} from "../constants";
|
|
import type { ThemeProp } from "widgets/constants";
|
|
|
|
const PopoverStyles = createGlobalStyle<{
|
|
parentWidth: number;
|
|
menuDropDownWidth: number;
|
|
id: string;
|
|
borderRadius: string;
|
|
}>`
|
|
.menu-button-popover, .${BlueprintClasses.MINIMAL}.menu-button-popover.${
|
|
Classes.POPOVER2
|
|
} {
|
|
background: none;
|
|
box-shadow: 0 6px 20px 0px rgba(0, 0, 0, 0.15) !important;
|
|
margin-top: 8px !important;
|
|
margin-bottom: 8px !important;
|
|
border-radius: ${({ borderRadius }) =>
|
|
borderRadius >= THEMEING_TEXT_SIZES.lg ? `0.375rem` : borderRadius};
|
|
overflow-y: scroll;
|
|
max-height: 384px;
|
|
}
|
|
|
|
.menu-button-popover .${BlueprintClasses.MENU_ITEM} {
|
|
padding: 9px 12px;
|
|
border-radius: 0;
|
|
}
|
|
|
|
& > .${Classes.POPOVER2_TARGET} {
|
|
height: 100%;
|
|
}
|
|
|
|
${({ id, menuDropDownWidth, parentWidth }) => `
|
|
.menu-button-width-${id} {
|
|
|
|
max-width: ${
|
|
menuDropDownWidth > parentWidth
|
|
? `${menuDropDownWidth}px`
|
|
: `${parentWidth}px`
|
|
} !important;
|
|
min-width: ${
|
|
parentWidth > menuDropDownWidth ? parentWidth : menuDropDownWidth
|
|
}px !important;
|
|
}
|
|
`}
|
|
`;
|
|
|
|
export interface BaseStyleProps {
|
|
backgroundColor?: string;
|
|
borderRadius?: string;
|
|
boxShadow?: string;
|
|
buttonColor?: string;
|
|
buttonVariant?: ButtonVariant;
|
|
isCompact?: boolean;
|
|
textColor?: string;
|
|
placement?: ButtonPlacement;
|
|
}
|
|
|
|
const BaseButton = styled(Button)<ThemeProp & BaseStyleProps>`
|
|
height: 100%;
|
|
background-image: none !important;
|
|
font-weight: ${(props) => props.theme.fontWeights[2]};
|
|
outline: none;
|
|
padding: 0px 10px;
|
|
overflow: hidden;
|
|
border: 1.2px solid #ebebeb;
|
|
border-radius: 0;
|
|
box-shadow: none !important;
|
|
|
|
${({ buttonColor, buttonVariant, theme }) => `
|
|
background: ${
|
|
getCustomBackgroundColor(buttonVariant, buttonColor) !== "none"
|
|
? getCustomBackgroundColor(buttonVariant, buttonColor)
|
|
: buttonVariant === ButtonVariantTypes.PRIMARY
|
|
? theme.colors.button.primary.primary.bgColor
|
|
: "none"
|
|
} !important;
|
|
|
|
&:hover, &:active, &:focus {
|
|
background: ${
|
|
getCustomHoverColor(theme, buttonVariant, buttonColor) !== "none"
|
|
? getCustomHoverColor(theme, buttonVariant, buttonColor)
|
|
: buttonVariant === ButtonVariantTypes.SECONDARY
|
|
? theme.colors.button.primary.secondary.hoverColor
|
|
: buttonVariant === ButtonVariantTypes.TERTIARY
|
|
? theme.colors.button.primary.tertiary.hoverColor
|
|
: theme.colors.button.primary.primary.hoverColor
|
|
} !important;
|
|
}
|
|
|
|
&:disabled {
|
|
border: ${
|
|
buttonVariant === ButtonVariantTypes.SECONDARY
|
|
? "1px solid var(--wds-color-border-disabled)"
|
|
: "none"
|
|
} !important;
|
|
background: ${
|
|
buttonVariant !== ButtonVariantTypes.TERTIARY
|
|
? "var(--wds-color-bg-disabled)"
|
|
: "transparent"
|
|
} !important;
|
|
color: var(--wds-color-text-disabled) !important;
|
|
|
|
span {
|
|
color: var(--wds-color-text-disabled) !important;
|
|
}
|
|
}
|
|
|
|
border: ${
|
|
getCustomBorderColor(buttonVariant, buttonColor) !== "none"
|
|
? `1px solid ${getCustomBorderColor(buttonVariant, buttonColor)}`
|
|
: buttonVariant === ButtonVariantTypes.SECONDARY
|
|
? `1px solid ${theme.colors.button.primary.secondary.borderColor}`
|
|
: "none"
|
|
} !important;
|
|
& > span {
|
|
text-overflow: ellipsis;
|
|
display: -webkit-box;
|
|
-webkit-line-clamp: 1;
|
|
-webkit-box-orient: vertical;
|
|
|
|
max-height: 100%;
|
|
overflow: hidden;
|
|
color: ${
|
|
buttonVariant === ButtonVariantTypes.PRIMARY
|
|
? getComplementaryGrayscaleColor(buttonColor)
|
|
: getCustomBackgroundColor(
|
|
ButtonVariantTypes.PRIMARY,
|
|
buttonColor,
|
|
) !== "none"
|
|
? getCustomBackgroundColor(ButtonVariantTypes.PRIMARY, buttonColor)
|
|
: `${theme.colors.button.primary.secondary.textColor}`
|
|
} !important;
|
|
}
|
|
`}
|
|
|
|
border-radius: ${({ borderRadius }) => borderRadius};
|
|
box-shadow: ${({ boxShadow }) => boxShadow} !important;
|
|
${({ placement }) =>
|
|
placement
|
|
? `
|
|
justify-content: ${getCustomJustifyContent(placement)};
|
|
& > span.bp3-button-text {
|
|
flex: unset !important;
|
|
}
|
|
`
|
|
: ""}
|
|
`;
|
|
|
|
const BaseMenuItem = styled(BlueprintMenuItem)<ThemeProp & BaseStyleProps>`
|
|
font-family: var(--wds-font-family);
|
|
|
|
${({ backgroundColor, theme }) =>
|
|
backgroundColor
|
|
? `
|
|
background-color: ${backgroundColor} !important;
|
|
&:hover {
|
|
background-color: ${darkenHover(backgroundColor)} !important;
|
|
}
|
|
&:active {
|
|
background-color: ${darkenActive(backgroundColor)} !important;
|
|
}
|
|
`
|
|
: `
|
|
background: none !important
|
|
&:hover {
|
|
background-color: ${tinycolor(
|
|
theme.colors.button.primary.primary.textColor,
|
|
)
|
|
.darken()
|
|
.toString()} !important;
|
|
}
|
|
&:active {
|
|
background-color: ${tinycolor(
|
|
theme.colors.button.primary.primary.textColor,
|
|
)
|
|
.darken()
|
|
.toString()} !important;
|
|
}
|
|
`}
|
|
${({ textColor }) =>
|
|
textColor &&
|
|
`
|
|
color: ${textColor} !important;
|
|
`}
|
|
${({ isCompact }) =>
|
|
isCompact &&
|
|
`
|
|
padding-top: 3px !important;
|
|
padding-bottom: 3px !important;
|
|
font-size: 12px;
|
|
`}
|
|
`;
|
|
|
|
const StyledMenu = styled(Menu)<{
|
|
backgroundColor?: string;
|
|
}>`
|
|
padding: 0;
|
|
min-width: 0px;
|
|
overflow: hidden;
|
|
|
|
${BlueprintClasses.MENU_ITEM}:hover {
|
|
background-color: ${({ backgroundColor }) => lightenColor(backgroundColor)};
|
|
}
|
|
`;
|
|
|
|
function PopoverContent(props: PopoverContentProps) {
|
|
const { backgroundColor, getVisibleItems, isCompact, onItemClicked } = props;
|
|
|
|
const visibleItems = getVisibleItems();
|
|
|
|
if (!visibleItems?.length) {
|
|
return <StyledMenu />;
|
|
} else {
|
|
const listItems = visibleItems.map((item: MenuItem, index: number) => {
|
|
const {
|
|
backgroundColor,
|
|
iconAlign,
|
|
iconColor,
|
|
iconName,
|
|
id,
|
|
isDisabled,
|
|
label,
|
|
onClick,
|
|
textColor,
|
|
} = item;
|
|
|
|
return (
|
|
<BaseMenuItem
|
|
backgroundColor={backgroundColor}
|
|
disabled={isDisabled}
|
|
icon={
|
|
iconAlign !== Alignment.RIGHT && iconName ? (
|
|
<Icon color={iconColor} icon={iconName} />
|
|
) : null
|
|
}
|
|
isCompact={isCompact}
|
|
key={id}
|
|
labelElement={
|
|
iconAlign === Alignment.RIGHT && iconName ? (
|
|
<Icon color={iconColor} icon={iconName} />
|
|
) : null
|
|
}
|
|
onClick={() => onItemClicked(onClick, index)}
|
|
text={label}
|
|
textColor={textColor}
|
|
/>
|
|
);
|
|
});
|
|
|
|
return (
|
|
<StyledMenu backgroundColor={backgroundColor}>{listItems}</StyledMenu>
|
|
);
|
|
}
|
|
}
|
|
|
|
export interface PopoverTargetButtonProps {
|
|
borderRadius?: string;
|
|
boxShadow?: string;
|
|
buttonColor?: string;
|
|
buttonVariant?: ButtonVariant;
|
|
iconName?: IconName;
|
|
iconAlign?: Alignment;
|
|
isDisabled?: boolean;
|
|
label?: string;
|
|
placement?: ButtonPlacement;
|
|
renderMode?: RenderMode;
|
|
}
|
|
|
|
function PopoverTargetButton(props: PopoverTargetButtonProps) {
|
|
const {
|
|
borderRadius,
|
|
boxShadow,
|
|
buttonColor,
|
|
buttonVariant,
|
|
iconAlign,
|
|
iconName,
|
|
isDisabled,
|
|
label,
|
|
placement,
|
|
renderMode,
|
|
} = props;
|
|
|
|
const isRightAlign = iconAlign === Alignment.RIGHT;
|
|
|
|
return (
|
|
<DragContainer
|
|
buttonColor={buttonColor}
|
|
buttonVariant={buttonVariant}
|
|
disabled={isDisabled}
|
|
renderMode={renderMode}
|
|
>
|
|
<BaseButton
|
|
alignText={getAlignText(isRightAlign, iconName)}
|
|
borderRadius={borderRadius}
|
|
boxShadow={boxShadow}
|
|
buttonColor={buttonColor}
|
|
buttonVariant={buttonVariant}
|
|
disabled={isDisabled}
|
|
fill
|
|
icon={!isRightAlign && iconName ? iconName : null}
|
|
placement={placement}
|
|
rightIcon={isRightAlign && iconName ? iconName : null}
|
|
text={label}
|
|
/>
|
|
</DragContainer>
|
|
);
|
|
}
|
|
|
|
function MenuButtonComponent(props: MenuButtonComponentProps) {
|
|
const {
|
|
borderRadius,
|
|
boxShadow,
|
|
configureMenuItems,
|
|
getVisibleItems,
|
|
iconAlign,
|
|
iconName,
|
|
isCompact,
|
|
isDisabled,
|
|
label,
|
|
menuColor,
|
|
menuDropDownWidth,
|
|
menuItems,
|
|
menuItemsSource,
|
|
menuVariant,
|
|
onItemClicked,
|
|
placement,
|
|
renderMode,
|
|
sourceData,
|
|
widgetId,
|
|
width,
|
|
} = props;
|
|
|
|
return (
|
|
<>
|
|
<PopoverStyles
|
|
borderRadius={borderRadius}
|
|
id={widgetId}
|
|
menuDropDownWidth={menuDropDownWidth}
|
|
parentWidth={width - WidgetContainerDiff}
|
|
/>
|
|
<Popover2
|
|
content={
|
|
<PopoverContent
|
|
backgroundColor={menuColor}
|
|
borderRadius={borderRadius}
|
|
configureMenuItems={configureMenuItems}
|
|
getVisibleItems={getVisibleItems}
|
|
isCompact={isCompact}
|
|
menuItems={menuItems}
|
|
menuItemsSource={menuItemsSource}
|
|
onItemClicked={onItemClicked}
|
|
sourceData={sourceData}
|
|
/>
|
|
}
|
|
disabled={isDisabled}
|
|
fill
|
|
minimal
|
|
placement="bottom-end"
|
|
popoverClassName={`menu-button-popover menu-button-width-${widgetId}`}
|
|
>
|
|
<PopoverTargetButton
|
|
borderRadius={borderRadius}
|
|
boxShadow={boxShadow}
|
|
buttonColor={menuColor}
|
|
buttonVariant={menuVariant}
|
|
iconAlign={iconAlign}
|
|
iconName={iconName}
|
|
isDisabled={isDisabled}
|
|
label={label}
|
|
placement={placement}
|
|
renderMode={renderMode}
|
|
/>
|
|
</Popover2>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export default MenuButtonComponent;
|