From 3c57cb2e2a0abdf9ac23f4cf011aefaa53173754 Mon Sep 17 00:00:00 2001 From: Aleksandr Shumilov Date: Wed, 31 Jul 2024 13:10:34 +0200 Subject: [PATCH] [FX-5528] Migrate Dropzone to Tailwind (#4428) --- .changeset/calm-cooks-dream.md | 5 + packages/base/Dropzone/package.json | 2 +- .../base/Dropzone/src/Dropzone/Dropzone.tsx | 99 +++++++++++++++---- .../src/Dropzone/__snapshots__/test.tsx.snap | 3 +- packages/base/Dropzone/src/Dropzone/styles.ts | 44 --------- packages/base/Dropzone/src/Dropzone/test.tsx | 2 +- packages/base/Dropzone/tsconfig.json | 1 + 7 files changed, 90 insertions(+), 66 deletions(-) create mode 100644 .changeset/calm-cooks-dream.md delete mode 100644 packages/base/Dropzone/src/Dropzone/styles.ts diff --git a/.changeset/calm-cooks-dream.md b/.changeset/calm-cooks-dream.md new file mode 100644 index 0000000000..b77765fb3c --- /dev/null +++ b/.changeset/calm-cooks-dream.md @@ -0,0 +1,5 @@ +--- +'@toptal/picasso-dropzone': patch +--- + +- migrate Dropzone to Tailwind diff --git a/packages/base/Dropzone/package.json b/packages/base/Dropzone/package.json index 1b6594a359..625058c11c 100644 --- a/packages/base/Dropzone/package.json +++ b/packages/base/Dropzone/package.json @@ -36,7 +36,7 @@ "**/styles.js" ], "peerDependencies": { - "@material-ui/core": "4.12.4", + "@toptal/picasso-tailwind-merge": "^1.0.1", "@toptal/picasso-tailwind": ">=2.7", "react": ">=16.12.0 < 19.0.0" }, diff --git a/packages/base/Dropzone/src/Dropzone/Dropzone.tsx b/packages/base/Dropzone/src/Dropzone/Dropzone.tsx index 198b27bea3..576f90f496 100644 --- a/packages/base/Dropzone/src/Dropzone/Dropzone.tsx +++ b/packages/base/Dropzone/src/Dropzone/Dropzone.tsx @@ -1,17 +1,15 @@ -import React, { forwardRef } from 'react' +import React, { forwardRef, useMemo } from 'react' import { useDropzone } from 'react-dropzone' -import cx from 'classnames' -import type { Theme } from '@material-ui/core/styles' -import { makeStyles } from '@material-ui/core/styles' +import { twJoin } from '@toptal/picasso-tailwind-merge' import type { BaseProps } from '@toptal/picasso-shared' import { Upload24 } from '@toptal/picasso-icons' import { FormHint } from '@toptal/picasso-form' import { Container } from '@toptal/picasso-container' import { FileList } from '@toptal/picasso-file-input' import { Typography } from '@toptal/picasso-typography' +import { SPACING_6 } from '@toptal/picasso-utils' import type { FileUpload, DropzoneOptions } from './types' -import styles from './styles' export interface Props extends BaseProps { /** @@ -46,7 +44,51 @@ export interface Props extends BaseProps { hovered?: boolean } -const useStyles = makeStyles(styles, { name: 'Dropzone' }) +type DropzoneState = { + isDisabled?: boolean + isHovered?: boolean + isFocused?: boolean + isDragActive?: boolean +} + +type StateToClassMatcher = (currentState: DropzoneState) => string + +const getCursorClasses: StateToClassMatcher = ({ + isDisabled, + isHovered, + isFocused, + isDragActive, +}) => { + if (isDisabled) { + return 'cursor-not-allowed' + } + + if (isHovered || isFocused || isDragActive) { + return 'cursor-pointer' + } + + return 'hover:cursor-pointer focus:cursor-pointer' +} + +const getBorderColorClasses: StateToClassMatcher = ({ + isDisabled, + isHovered, + isFocused, + isDragActive, +}) => { + if (isDisabled) { + return 'border-gray-400 hover:border-gray-400' + } + + if (isHovered || isFocused || isDragActive) { + return 'border-blue-500' + } + + return 'border-gray-400 hover:border-blue-500 focus:border-blue-500' +} + +const getBackgroundColorClasses: StateToClassMatcher = ({ isDisabled }) => + isDisabled ? 'bg-gray-100' : 'bg-white' export const Dropzone = forwardRef(function Dropzone( props, @@ -60,8 +102,8 @@ export const Dropzone = forwardRef(function Dropzone( className, style, 'data-testid': dataTestId, - focused, - hovered, + focused: isFocused, + hovered: isHovered, // dropzoneOptions accept, @@ -91,7 +133,15 @@ export const Dropzone = forwardRef(function Dropzone( validator, }) - const classes = useStyles() + const componentState = useMemo( + () => ({ + isDisabled, + isHovered, + isFocused, + isDragActive, + }), + [isDisabled, isHovered, isFocused, isDragActive] + ) return ( @@ -99,24 +149,35 @@ export const Dropzone = forwardRef(function Dropzone( flex direction='column' alignItems='center' + rounded + aria-disabled={isDisabled} + padded={SPACING_6} data-testid={dataTestId} - {...getRootProps({ - className: cx(classes.root, { - [classes.dragActive]: isDragActive, - [classes.hovered]: hovered, - [classes.disabled]: isDisabled, - [classes.focused]: focused, - }), - })} + {...getRootProps({})} + className={twJoin( + 'border border-dashed', + 'box-border', + 'text-graphite-700', + 'gap-2', + 'transition-all ease-out duration-350', + getCursorClasses(componentState), + getBorderColorClasses(componentState), + getBackgroundColorClasses(componentState), + isDisabled && 'hover:no-drop' + )} > - + {!hideContentText && ( Click or drag to upload )} - {hint && {hint}} + {hint && ( + *]:leading-4')}> + {hint} + + )} {value && value.length > 0 && ( diff --git a/packages/base/Dropzone/src/Dropzone/__snapshots__/test.tsx.snap b/packages/base/Dropzone/src/Dropzone/__snapshots__/test.tsx.snap index 3947d1b4e8..d469321416 100644 --- a/packages/base/Dropzone/src/Dropzone/__snapshots__/test.tsx.snap +++ b/packages/base/Dropzone/src/Dropzone/__snapshots__/test.tsx.snap @@ -9,7 +9,8 @@ exports[`Dropzone renders 1`] = ` class="" >