Skip to content

Commit

Permalink
feat(alert): tailwind migration (#1756)
Browse files Browse the repository at this point in the history
* refactor(alert): migrate to tailwind

* refactor: remove styled usage

* fix: layou due to border-box

* fix: responsive override
  • Loading branch information
keellyp authored Sep 23, 2024
1 parent 3f5c89f commit 309c9d0
Show file tree
Hide file tree
Showing 15 changed files with 59 additions and 145 deletions.
8 changes: 3 additions & 5 deletions src/components/coupons/AddBillableMetricToCouponDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,9 @@ export const AddBillableMetricToCouponDialog = forwardRef<
searchQuery={getBillableMetrics}
value={selectedBillableMetric?.id}
/>
<StyledAlertBox type="warning">{translate('text_64352657267c3d916f962763')}</StyledAlertBox>
<Alert type="warning" className="mb-8">
{translate('text_64352657267c3d916f962763')}
</Alert>
</Dialog>
)
})
Expand All @@ -134,7 +136,3 @@ AddBillableMetricToCouponDialog.displayName = 'AddBillableMetricToCouponDialog'
const StyledComboBox = styled(ComboBox)`
margin-bottom: ${theme.spacing(8)};
`

const StyledAlertBox = styled(Alert)`
margin-bottom: ${theme.spacing(8)};
`
8 changes: 2 additions & 6 deletions src/components/creditNote/CreditNoteFormCalculation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -559,13 +559,13 @@ export const CreditNoteFormCalculation = ({
)}

{_get(formikProps.errors, 'payBack.0.value') === LagoApiError.DoesNotMatchItemAmounts && (
<StyledAlert type="danger">
<Alert className="mt-6" type="danger">
{translate('text_637e334680481f653e8caa9d', {
total: intlFormatNumber(totalTaxIncluded || 0, {
currency,
}),
})}
</StyledAlert>
</Alert>
)}
</div>
)
Expand Down Expand Up @@ -624,10 +624,6 @@ const StyledTextInput = styled(AmountInputField)`
}
`

const StyledAlert = styled(Alert)`
margin-top: ${theme.spacing(6)};
`

const InlineLabel = styled.div`
display: flex;
align-items: center;
Expand Down
104 changes: 30 additions & 74 deletions src/components/designSystem/Alert.tsx
Original file line number Diff line number Diff line change
@@ -1,129 +1,85 @@
import { Stack } from '@mui/material'
import { cx } from 'class-variance-authority'
import { cva } from 'class-variance-authority'
import { ReactNode } from 'react'
import styled, { css } from 'styled-components'
import styled from 'styled-components'

import { ResponsiveStyleValue, setResponsiveProperty } from '~/core/utils/responsiveProps'
import { theme } from '~/styles'
import { tw } from '~/styles/utils'

import { Button, ButtonProps as TButtonProps } from './Button'
import { Icon, IconColor, IconName } from './Icon'
import { Typography } from './Typography'

type ContainerSize = 0 | 4 | 16 | 48

enum AlertType {
info = 'info',
success = 'success',
danger = 'danger',
warning = 'warning',
}
type AlertType = 'info' | 'success' | 'danger' | 'warning'

type AlertButtonProps = Partial<Omit<Omit<TButtonProps, 'variant' | 'icon'>, 'size'>> & {
label: string
}

interface AlertProps {
children: ReactNode
type: keyof typeof AlertType
type: AlertType
ButtonProps?: AlertButtonProps
className?: string
containerSize?: ResponsiveStyleValue<ContainerSize>
fullWidth?: boolean
}

const getIcon = (type: keyof typeof AlertType): { name: IconName; color: IconColor } => {
const getIcon = (type: AlertType): { name: IconName; color: IconColor } => {
switch (type) {
case AlertType.success:
case 'success':
return { name: 'validate-unfilled', color: 'success' }
case AlertType.warning:
case 'warning':
return { name: 'warning-unfilled', color: 'warning' }
case AlertType.danger:
case 'danger':
return { name: 'error-unfilled', color: 'error' }
default:
return { name: 'info-circle', color: 'info' }
}
}

const alertStyles = cva('rounded-xl px-4', {
variants: {
backgroundColor: {
info: 'bg-purple-100',
success: 'bg-green-100',
danger: 'bg-red-100',
warning: 'bg-yellow-100',
},
isFullWidth: {
true: 'w-full rounded-none',
},
},
})

export const Alert = ({
ButtonProps: { label, ...ButtonProps } = {} as AlertButtonProps,
children,
className,
containerSize = 16,
fullWidth,
type,
...props
}: AlertProps) => {
const iconConfig = getIcon(type)

return (
<Container
$isFullWidth={fullWidth}
$containerSize={containerSize}
className={cx(className, [`alert-type--${type}`])}
<div
className={tw(alertStyles({ backgroundColor: type, isFullWidth: fullWidth }), className)}
data-test={`alert-type-${type}`}
{...props}
>
<Stack
direction="row"
gap={4}
alignItems="center"
justifyContent="space-between"
py={theme.spacing(4)}
>
<Stack direction="row" gap={4} alignItems="center">
<div className="flex flex-row items-center justify-between gap-4 py-4">
<div className="flex flex-row items-center gap-4">
<Icon name={iconConfig.name} color={iconConfig.color} />
<Content color="textSecondary">{children}</Content>
</Stack>
</div>
{!!ButtonProps.onClick && !!label && (
<Button variant="quaternary-dark" size="medium" {...ButtonProps}>
{label}
</Button>
)}
</Stack>
</Container>
</div>
</div>
)
}

const Container = styled.div<{
$isFullWidth?: boolean
$containerSize: ResponsiveStyleValue<ContainerSize>
}>`
border-radius: 12px;
&.alert-type--${AlertType.info} {
background-color: ${theme.palette.info[100]};
}
&.alert-type--${AlertType.success} {
background-color: ${theme.palette.success[100]};
}
&.alert-type--${AlertType.warning} {
background-color: ${theme.palette.warning[100]};
}
&.alert-type--${AlertType.danger} {
background-color: ${theme.palette.error[100]};
}
${({ $isFullWidth }) =>
$isFullWidth &&
css`
border-radius: 0;
width: 100%;
`}
> div {
${({ $containerSize }) => {
return css`
${setResponsiveProperty('paddingLeft', $containerSize)}
${setResponsiveProperty('paddingRight', $containerSize)}
`
}}
}
`

const Content = styled(Typography)`
&& {
word-break: break-word;
Expand Down
5 changes: 2 additions & 3 deletions src/components/designSystem/Dialog.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { alpha, Dialog as MuiDialog } from '@mui/material'
import { forwardRef, ReactNode, useImperativeHandle, useState } from 'react'
import { useEffect } from 'react'
import { forwardRef, ReactNode, useEffect, useImperativeHandle, useState } from 'react'
import styled from 'styled-components'

import { ButtonGroup, theme } from '~/styles'
Expand Down Expand Up @@ -100,7 +99,7 @@ const StyledDialog = styled(MuiDialog)`
.dialogPaper {
display: flex;
flex-direction: column;
max-width: 496px;
max-width: 576px;
margin: 0 auto;
border-radius: 12px;
box-shadow: ${theme.shadows[4]};
Expand Down
8 changes: 2 additions & 6 deletions src/components/plans/ChargeAccordion.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -455,9 +455,9 @@ export const ChargeAccordion = memo(
{/* Charge main infos */}
<ChargeModelWrapper data-test="charge-model-wrapper">
{!!shouldDisplayAlreadyUsedChargeAlert && (
<ChargeModelWrapperAlert type="warning">
<Alert type="warning" className="mb-4">
{translate('text_6435895831d323008a47911f')}
</ChargeModelWrapperAlert>
</Alert>
)}
<ComboBox
disableClearable
Expand Down Expand Up @@ -985,10 +985,6 @@ const ChargeModelWrapper = styled.div`
padding: ${theme.spacing(4)} ${theme.spacing(4)} 0 ${theme.spacing(4)};
`

const ChargeModelWrapperAlert = styled(Alert)`
margin-bottom: ${theme.spacing(4)};
`

const ValidationIcon = styled(Icon)`
display: flex;
align-items: center;
Expand Down
14 changes: 2 additions & 12 deletions src/components/subscriptions/SubscriptionCurrentUsageTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -192,13 +192,7 @@ export const SubscriptionCurrentUsageTable = ({

return extensions?.details?.taxError?.length
}) ? (
<LocalTaxAlert
fullWidth
containerSize={{
default: 16,
}}
type="warning"
>
<Alert fullWidth type="warning" className="shadow-t">
<Stack>
<Typography variant="body" color="grey700">
{translate('text_1724165657161stcilcabm7x')}
Expand All @@ -208,7 +202,7 @@ export const SubscriptionCurrentUsageTable = ({
{translate(LocalTaxProviderErrorsEnum.GenericErrorMessage)}
</Typography>
</Stack>
</LocalTaxAlert>
</Alert>
) : (
<GenericPlaceholder
title={translate('text_62c3f3fca8a1625624e83379')}
Expand Down Expand Up @@ -399,10 +393,6 @@ const SkeletonItem = styled.div`
}
`

const LocalTaxAlert = styled(Alert)`
box-shadow: ${theme.shadows[5]};
`

const NoFocusLink = styled(Link)`
text-decoration: none !important;
/* Link as a button-like action here, and there is no place to display the focus ring, so better to hide it and break the internet */
Expand Down
9 changes: 1 addition & 8 deletions src/layouts/CustomerInvoiceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -716,14 +716,7 @@ const CustomerInvoiceDetails = () => {
</PageHeader>

{!!errorMessage && (
<Alert
fullWidth
containerSize={{
default: 16,
md: 48,
}}
type="warning"
>
<Alert fullWidth className="md:px-12" type="warning">
<Stack>
<Typography variant="body" color="grey700">
{translate('text_1724165657161stcilcabm7x')}
Expand Down
9 changes: 2 additions & 7 deletions src/pages/Invitation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -403,9 +403,9 @@ const Invitation = () => {
})}
</PasswordValidation>
) : (
<ValidPasswordAlert type="success" data-test="success">
<Alert type="success" data-test="success" className="mt-3">
{translate('text_63246f875e2228ab7b63dd02')}
</ValidPasswordAlert>
</Alert>
)}
</div>
</InputWrapper>
Expand Down Expand Up @@ -453,11 +453,6 @@ const PasswordValidation = styled.div<{ $visible: boolean }>`
flex-wrap: wrap;
`

const ValidPasswordAlert = styled(Alert)`
flex: 1;
margin-top: ${theme.spacing(3)};
`

const ValidationLine = styled.div`
display: flex;
height: 20px;
Expand Down
5 changes: 1 addition & 4 deletions src/pages/__devOnly/DesignSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,7 @@ const DesignSystem = () => {
<Block $marginBottom={theme.spacing(6)}>
<Alert
fullWidth
containerSize={{
default: 16,
md: 48,
}}
className="md:px-12"
type="danger"
ButtonProps={{
label: 'Retry',
Expand Down
9 changes: 2 additions & 7 deletions src/pages/auth/ResetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,9 @@ const ResetPassword = () => {
)
})
) : (
<StyledAlert type="success" data-test="success">
<Alert className="mb-3" type="success" data-test="success">
{translate('text_63246f875e2228ab7b63dd02')}
</StyledAlert>
</Alert>
)}
</PasswordValidation>
</PasswordBlock>
Expand Down Expand Up @@ -250,11 +250,6 @@ const PasswordValidation = styled.div<{ $visible: boolean }>`
flex-wrap: wrap;
`

const StyledAlert = styled(Alert)`
flex: 1;
margin-bottom: ${theme.spacing(3)};
`

const ValidationLine = styled.div`
display: flex;
height: 20px;
Expand Down
9 changes: 2 additions & 7 deletions src/pages/auth/SignUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,9 @@ const SignUp = () => {
})}
</PasswordValidation>
) : (
<ValidPasswordAlert type="success" data-test="success">
<Alert className="mt-4" type="success" data-test="success">
{translate('text_620bc4d4269a55014d493fbe')}
</ValidPasswordAlert>
</Alert>
)}
</div>
</>
Expand Down Expand Up @@ -350,11 +350,6 @@ const PasswordValidation = styled.div<{ $visible: boolean }>`
flex-wrap: wrap;
`

const ValidPasswordAlert = styled(Alert)`
flex: 1;
margin-top: ${theme.spacing(4)};
`

const ValidationLine = styled.div`
display: flex;
height: 20px;
Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/ApiKeys.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ const ApiKeys = () => {

const Page = styled.div`
padding: ${theme.spacing(8)} ${theme.spacing(12)};
max-width: ${theme.spacing(168)};
max-width: ${theme.spacing(192)};
`

const Title = styled(Typography)`
Expand Down
2 changes: 1 addition & 1 deletion src/pages/developers/Webhooks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ const Webhooks = () => {

const Page = styled.div`
padding: ${theme.spacing(8)} ${theme.spacing(12)};
max-width: ${theme.spacing(168)};
max-width: ${theme.spacing(192)};
`

const Title = styled(Typography)`
Expand Down
Loading

0 comments on commit 309c9d0

Please sign in to comment.