Skip to content

Commit

Permalink
Re-align button sizing
Browse files Browse the repository at this point in the history
  • Loading branch information
estrattonbailey committed Sep 18, 2024
1 parent 5387574 commit bcec243
Show file tree
Hide file tree
Showing 4 changed files with 217 additions and 91 deletions.
160 changes: 119 additions & 41 deletions src/components/Button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
} from 'react-native'
import {LinearGradient} from 'expo-linear-gradient'

import {android, atoms as a, flatten, select, tokens, useTheme} from '#/alf'
import {atoms as a, flatten, select, tokens, useTheme, web} from '#/alf'
import {Props as SVGIconProps} from '#/components/icons/common'
import {Text} from '#/components/Typography'

Expand Down Expand Up @@ -342,40 +342,47 @@ export const Button = React.forwardRef<View, ButtonProps>(
}

if (shape === 'default') {
if (size === 'large') {
baseStyles.push(
{paddingVertical: 15},
a.px_2xl,
a.rounded_sm,
a.gap_md,
)
} else if (size === 'medium') {
baseStyles.push(
{paddingVertical: 12},
a.px_2xl,
a.rounded_sm,
a.gap_md,
)
} else if (size === 'small') {
baseStyles.push({paddingVertical: 9}, a.px_lg, a.rounded_sm, a.gap_sm)
} else if (size === 'xsmall') {
baseStyles.push({paddingVertical: 6}, a.px_sm, a.rounded_sm, a.gap_sm)
if (size === 'large' || size === 'medium') {
baseStyles.push({
paddingVertical: 13,
paddingHorizontal: 20,
borderRadius: 8,
gap: 8,
})
} else if (size === 'small' || size === 'xsmall') {
baseStyles.push({
paddingVertical: 9,
paddingHorizontal: 14,
borderRadius: 6,
gap: 6,
})
} else if (size === 'tiny') {
baseStyles.push({paddingVertical: 4}, a.px_sm, a.rounded_xs, a.gap_xs)
baseStyles.push({
paddingVertical: 4,
paddingHorizontal: 8,
borderRadius: 4,
gap: 4,
})
}
} else if (shape === 'round' || shape === 'square') {
if (size === 'large') {
if (size === 'large' || size === 'medium') {
if (shape === 'round') {
baseStyles.push({height: 54, width: 54})
baseStyles.push({height: 46, width: 46})
} else {
baseStyles.push({height: 50, width: 50})
baseStyles.push({height: 44, width: 44})
}
} else if (size === 'small' || size === 'xsmall') {
if (shape === 'round') {
baseStyles.push({height: 36, width: 36})
} else {
baseStyles.push({height: 34, width: 34})
}
} else if (size === 'small') {
baseStyles.push({height: 34, width: 34})
} else if (size === 'xsmall') {
baseStyles.push({height: 28, width: 28})
} else if (size === 'tiny') {
baseStyles.push({height: 20, width: 20})
if (shape === 'round') {
baseStyles.push({height: 22, width: 22})
} else {
baseStyles.push({height: 21, width: 21})
}
}

if (shape === 'round') {
Expand Down Expand Up @@ -618,12 +625,12 @@ export function useSharedButtonTextStyles() {
}
}

if (size === 'large') {
baseStyles.push(a.text_md, android({paddingBottom: 1}))
if (size === 'large' || size === 'medium') {
baseStyles.push(a.text_md, a.leading_tight, web({paddingTop: 1}))
} else if (size === 'small' || size === 'xsmall') {
baseStyles.push(a.text_sm, a.leading_tight, web({paddingTop: 1}))
} else if (size === 'tiny') {
baseStyles.push(a.text_xs, android({paddingBottom: 1}))
} else {
baseStyles.push(a.text_sm, android({paddingBottom: 1}))
baseStyles.push(a.text_xs, a.leading_tight)
}

return StyleSheet.flatten(baseStyles)
Expand All @@ -643,31 +650,102 @@ export function ButtonText({children, style, ...rest}: ButtonTextProps) {
export function ButtonIcon({
icon: Comp,
position,
size: iconSize,
size,
}: {
icon: React.ComponentType<SVGIconProps>
position?: 'left' | 'right'
size?: SVGIconProps['size']
}) {
const {size, disabled} = useButtonContext()
const {size: buttonSize, disabled} = useButtonContext()
const textStyles = useSharedButtonTextStyles()
const {iconSize, iconContainerSize} = React.useMemo(() => {
/**
* Pre-set icon sizes for different button sizes
*/
const iconSizeShorthand =
size ??
(({
large: 'sm',
medium: 'sm',
small: 'xs',
xsmall: 'xs',
tiny: 'xs',
}[buttonSize || 'small'] || 'sm') as Exclude<
SVGIconProps['size'],
undefined
>)

/*
* Copied here from icons/common.tsx so we can tweak if we need to, but
* also so that we can calculate transforms.
*/
const iconSize = {
xs: 12,
sm: 16,
md: 20,
lg: 24,
xl: 28,
'2xl': 32,
}[iconSizeShorthand]

/*
* Goal here is to match rendered text size so that different size icons
* don't increase button size
*/
const iconContainerSize = {
large: 18,
medium: 18,
small: 16,
xsmall: 16,
tiny: 13,
}[buttonSize || 'small']

return {
iconSize,
iconContainerSize,
}
}, [buttonSize, size])

return (
<View
style={[
a.z_20,
{
width: iconContainerSize,
height: iconContainerSize,
opacity: disabled ? 0.7 : 1,
marginLeft: position === 'left' ? -2 : 0,
marginRight: position === 'right' ? -2 : 0,
},
]}>
<Comp
size={
iconSize ?? (size === 'large' ? 'md' : size === 'tiny' ? 'xs' : 'sm')
}
style={[{color: textStyles.color, pointerEvents: 'none'}]}
/>
<View
style={[
a.absolute,
{
width: iconSize,
height: iconSize,
top: '50%',
left: '50%',
transform: [
{
translateX: (iconSize / 2) * -1,
},
{
translateY: (iconSize / 2) * -1,
},
],
},
]}>
<Comp
width={iconSize}
style={[
{
color: textStyles.color,
pointerEvents: 'none',
},
]}
/>
</View>
</View>
)
}
2 changes: 1 addition & 1 deletion src/state/shell/composer/useComposerKeyboardShortcut.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ export function useComposerKeyboardShortcut() {
function handler(event: KeyboardEvent) {
if (shouldIgnore(event)) return
if (
openDialogs.current.size > 0 ||
openDialogs?.current.size > 0 ||
isModalActive ||
activeLightbox ||
isDrawerOpen
Expand Down
Loading

0 comments on commit bcec243

Please sign in to comment.