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

refactor(selector): tailwind migration #1785

Merged
merged 2 commits into from
Oct 3, 2024
Merged
Show file tree
Hide file tree
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
163 changes: 65 additions & 98 deletions src/components/designSystem/Selector.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { cva } from 'class-variance-authority'
import { ReactElement, useState } from 'react'
import styled, { css } from 'styled-components'

import { theme } from '~/styles'
import { tw } from '~/styles/utils'

import { Avatar } from './Avatar'
import { Icon, IconName } from './Icon'
Expand All @@ -21,6 +21,44 @@ interface SelectorProps {
onClick?: () => Promise<void> | unknown
}

const selectorVariants = cva('flex h-18 items-center rounded-xl border p-4', {
variants: {
selected: {
true: 'border-blue-600 bg-blue-100',
false: 'border-grey-400 bg-white',
},
disabled: {
true: 'cursor-not-allowed bg-grey-100',
false: 'cursor-default',
},
clickable: {
true: 'cursor-pointer focus-not-active:ring',
},
fullWidth: {
true: 'w-full',
false: 'min-w-full max-w-full md:min-w-[calc(50%-32px)] md:max-w-[calc(50%-32px)]',
},
},
compoundVariants: [
{
selected: false,
clickable: true,
disabled: false,
class: 'active:bg-grey-200 hover-not-active:bg-grey-100',
},
{
selected: true,
clickable: true,
disabled: false,
class: 'hover-not-active:bg-blue-200',
},
],
defaultVariants: {
fullWidth: true,
selected: false,
},
})

export const Selector = ({
title,
subtitle,
Expand All @@ -34,10 +72,21 @@ export const Selector = ({
onClick,
}: SelectorProps) => {
const [loading, setLoading] = useState(false)
const clickable = !!onClick && !loading && !disabled

return (
<Container
className={className}
<button
disabled={disabled}
tabIndex={clickable ? 0 : -1}
className={tw(
selectorVariants({
selected,
disabled,
clickable,
fullWidth,
}),
className,
)}
onClick={async () => {
if (loading || disabled) return
let result = !!onClick && onClick()
Expand All @@ -48,21 +97,22 @@ export const Selector = ({
setLoading(false)
}
}}
$clickable={!!onClick && !loading}
$selected={selected}
$disabled={disabled}
$fullWidth={fullWidth}
>
<MainIcon>
<div className="mr-3">
{typeof icon === 'string' ? (
<Avatar size="big" variant="connector">
<Icon color="dark" name={icon as IconName} />
</Avatar>
) : (
icon
)}
</MainIcon>
<Infos $withEndIcon={!!endIcon} $titleFirst={titleFirst}>
</div>
<div
className={tw('mr-4 flex flex-1 overflow-hidden text-left', {
'flex-col': titleFirst,
'flex-col-reverse': !titleFirst,
})}
>
<Typography
variant={!!subtitle ? 'bodyHl' : 'body'}
color={disabled ? 'disabled' : 'textSecondary'}
Expand All @@ -73,106 +123,23 @@ export const Selector = ({
<Typography variant="caption" color={disabled ? 'disabled' : undefined} noWrap>
{subtitle}
</Typography>
</Infos>
</div>
{loading ? (
<Icon animation="spin" color="primary" name="processing" />
) : typeof endIcon === 'string' ? (
<Icon name={endIcon as IconName} color="dark" />
) : (
endIcon
)}
</Container>
</button>
)
}

export const SelectorSkeleton = ({ fullWidth = false }: { fullWidth?: boolean } = {}) => (
<Container $fullWidth={fullWidth}>
<div className={tw(selectorVariants({ fullWidth }))}>
<Skeleton variant="connectorAvatar" size="big" marginRight={12} />
<Skeleton variant="text" width={160} height={12} />
</Container>
</div>
)

export const SELECTOR_HEIGHT = 72

const MainIcon = styled.div`
margin-right: ${theme.spacing(3)};
`

const ICON_CONTAINER_SIZE = 40

const Infos = styled.div<{ $titleFirst?: boolean; $withEndIcon?: boolean }>`
display: flex;
text-align: left;
flex-direction: ${({ $titleFirst }) => ($titleFirst ? 'column' : 'column-reverse')};
flex: 1;
margin-right: ${theme.spacing(4)};
// 100 - Container icon size - end icon size (if present) - (padding left + right)
max-width: ${({ $withEndIcon }) =>
$withEndIcon
? `calc(100% - ${ICON_CONTAINER_SIZE}px - ${theme.spacing(4)} - ${theme.spacing(4 * 2)})`
: `calc(100% - ${ICON_CONTAINER_SIZE}px - ${theme.spacing(4 * 2)})`};
`

const Container = styled.button<{
$selected?: boolean
$disabled?: boolean
$clickable?: boolean
$fullWidth?: boolean
}>`
display: flex;
align-items: center;
padding: ${theme.spacing(4)};
box-sizing: border-box;
height: ${SELECTOR_HEIGHT}px;
background-color: ${({ $selected, $disabled }) =>
$selected
? theme.palette.primary[100]
: $disabled
? theme.palette.grey[100]
: theme.palette.background.default};
border: 1px solid
${({ $selected }) => ($selected ? theme.palette.primary[600] : theme.palette.grey[400])};
cursor: ${({ $disabled }) => ($disabled ? 'not-allowed' : 'default')};

${({ $fullWidth }) =>
!$fullWidth
? css`
min-width: calc(50% - ${theme.spacing(4 * 2)});
max-width: calc(50% - ${theme.spacing(4 * 2)});
${theme.breakpoints.down('sm')} {
min-width: initial;
max-width: 100%;
}
`
: css`
width: 100%;
`}

${({ $clickable, $selected, $disabled }) =>
$clickable &&
!$disabled &&
css`
cursor: pointer;
:focus:not(:active) {
box-shadow: 0px 0px 0px 4px ${theme.palette.primary[200]};
border-radius: 12px;
}

${() =>
!!$clickable &&
css`
:hover:not(:active) {
background-color: ${$selected ? theme.palette.primary[200] : theme.palette.grey[100]};
}
`}
`}

${({ $selected, $clickable }) =>
!$selected &&
$clickable &&
css`
:active {
background-color: ${theme.palette.grey[200]};
}
`}
`
2 changes: 1 addition & 1 deletion src/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ body {
}

a {
@apply text-blue no-underline visited:text-purple hover:underline focus:ring;
@apply text-blue no-underline visited:text-purple hover:underline focus:rounded focus:ring;
}

b {
Expand Down
4 changes: 4 additions & 0 deletions src/pages/__devOnly/DesignSystem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
NavigationTab,
Popper,
Selector,
SelectorSkeleton,
ShowMoreText,
Skeleton,
Status,
Expand Down Expand Up @@ -487,6 +488,9 @@ const DesignSystem = () => {
<Skeleton variant="text" width={120} height={12} marginBottom="16px" />
<Skeleton variant="text" width="50%" height={12} marginBottom="16px" />
</div>
<div>
<SelectorSkeleton fullWidth />
</div>
</Container>
),
},
Expand Down
32 changes: 14 additions & 18 deletions src/pages/settings/Integrations.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,8 @@ const Integrations = () => {
))}
</LoadingContainer>
) : (
<>
<StyledSelector
<div className="flex flex-1 flex-col gap-4">
<Selector
fullWidth
title={translate('text_6668821d94e4da4dfd8b3834')}
subtitle={translate('text_6668821d94e4da4dfd8b3840')}
Expand Down Expand Up @@ -218,7 +218,7 @@ const Integrations = () => {
}
}}
/>
<StyledSelector
<Selector
title={translate('text_645d071272418a14c1c76a6d')}
subtitle={translate('text_634ea0ecc6147de10ddb6631')}
icon={
Expand All @@ -243,7 +243,7 @@ const Integrations = () => {
}}
fullWidth
/>
<StyledSelector
<Selector
title={translate('text_639c334c3fa0e9c6ca3512b2')}
subtitle={translate('text_639c334c3fa0e9c6ca3512b4')}
icon={
Expand All @@ -256,7 +256,7 @@ const Integrations = () => {
}}
fullWidth
/>
<StyledSelector
<Selector
title={translate('text_63e26d8308d03687188221a5')}
subtitle={translate('text_63e26d8308d03687188221a6')}
icon={
Expand All @@ -269,7 +269,7 @@ const Integrations = () => {
}}
fullWidth
/>
<StyledSelector
<Selector
title={translate('text_634ea0ecc6147de10ddb6625')}
subtitle={translate('text_634ea0ecc6147de10ddb6631')}
icon={
Expand All @@ -291,7 +291,7 @@ const Integrations = () => {
}}
fullWidth
/>
<StyledSelector
<Selector
title={translate('text_641b41f3cec373009a265e9e')}
subtitle={translate('text_641b41fa604ef10070cab5ea')}
icon={
Expand All @@ -305,7 +305,7 @@ const Integrations = () => {
fullWidth
/>
{isHubspotFeatureFlagEnabled && (
<StyledSelector
<Selector
title={translate('text_1727189568053s79ks5q07tr')}
subtitle={translate('text_1727189568053q2gpkjzpmxr')}
icon={
Expand Down Expand Up @@ -337,7 +337,7 @@ const Integrations = () => {
fullWidth
/>
)}
<StyledSelector
<Selector
fullWidth
title={translate('text_657078c28394d6b1ae1b9713')}
subtitle={translate('text_657078c28394d6b1ae1b971f')}
Expand All @@ -359,7 +359,7 @@ const Integrations = () => {
}
}}
/>
<StyledSelector
<Selector
fullWidth
title={translate('text_661ff6e56ef7e1b7c542b239')}
subtitle={translate('text_661ff6e56ef7e1b7c542b245')}
Expand Down Expand Up @@ -390,7 +390,7 @@ const Integrations = () => {
}
}}
/>
<StyledSelector
<Selector
title={translate('text_641b42035d62fd004e07cdde')}
subtitle={translate('text_641b420ccd75240062f2386e')}
icon={
Expand All @@ -403,7 +403,7 @@ const Integrations = () => {
}}
fullWidth
/>
<StyledSelector
<Selector
title={translate('text_62b1edddbf5f461ab971277d')}
subtitle={translate('text_62b1edddbf5f461ab9712795')}
icon={
Expand All @@ -429,7 +429,7 @@ const Integrations = () => {
fullWidth
/>

<StyledSelector
<Selector
fullWidth
title={translate('text_6672ebb8b1b50be550eccaf8')}
subtitle={translate('text_661ff6e56ef7e1b7c542b245')}
Expand Down Expand Up @@ -460,7 +460,7 @@ const Integrations = () => {
}
}}
/>
</>
</div>
)}
</SettingsPageContentWrapper>

Expand All @@ -484,10 +484,6 @@ const Title = styled(Typography)`
margin-bottom: ${theme.spacing(2)};
`

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

const Subtitle = styled(Typography)`
margin-bottom: ${theme.spacing(8)};
`
Expand Down
Loading
Loading