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

Backport dynamic text sizing for non-size usage in Button, TextInput, and Select #1056

Merged
merged 1 commit into from
Sep 24, 2020
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
10 changes: 8 additions & 2 deletions src/buttons/src/Button.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types'
import Box, { spacing, dimensions, position, layout } from 'ui-box'
import { useStyleConfig } from '../../hooks'
import { IconWrapper } from '../../icons/src/IconWrapper'
import { getTextPropsForControlHeight } from '../../lib/deprecated-theme-helpers'
import { Spinner } from '../../spinner'

/* eslint-disable react/prop-types */
Expand Down Expand Up @@ -81,18 +82,22 @@ const Button = memo(
is = 'button',
isActive = false,
isLoading,
size = 'medium',
...restProps
} = props

const { className: themedClassName, ...boxProps } = useStyleConfig(
'Button',
{ appearance, color, intent, size },
{ appearance, color, intent, size: restProps.size || 'medium' },
pseudoSelectors,
internalStyles
)

const height = restProps.height || boxProps.height
// Keep backwards compat font sizing if an explicit height was passed in.
const textProps =
!restProps.size && restProps.height
? getTextPropsForControlHeight(restProps.height)
: {}
const iconSize = getIconSizeForButton(height)

return (
Expand All @@ -104,6 +109,7 @@ const Button = memo(
data-active={isActive || undefined}
{...boxProps}
{...restProps}
{...textProps}
disabled={disabled || isLoading}
>
{isLoading && (
Expand Down
9 changes: 9 additions & 0 deletions src/buttons/stories/index.stories.js
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,15 @@ buttonsStory.add('Common', () => (
</Box>
))

buttonsStory.add('Button height vs. size', () => (
<Box padding={40}>
<Button marginRight={16} height={40}>
With Height
</Button>
<Button size="large">With size</Button>
</Box>
))

buttonsStory.add('Button types', () => (
<Box padding={40}>
<Heading>Default Appearance</Heading>
Expand Down
34 changes: 34 additions & 0 deletions src/lib/deprecated-theme-helpers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/**
* Helper function to be used across multiple components to preserve
* backwards-compat height behavior with the previous Evergreen.
* @param {number} height
*/

const text = {
500: {
fontSize: '16px',
fontWeight: 400,
lineHeight: '20px',
letterSpacing: '-0.05px'
},
400: {
fontSize: '14px',
fontWeight: 400,
lineHeight: '20px',
letterSpacing: '-0.05px'
},
300: {
fontSize: '12px',
fontWeight: 400,
lineHeight: '16px',
letterSpacing: '0'
}
}

export const getTextPropsForControlHeight = height => {
if (height <= 32) return text['300']
if (height <= 40) return text['400']
return text['500']
}

export default getTextPropsForControlHeight
10 changes: 8 additions & 2 deletions src/select/src/Select.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import PropTypes from 'prop-types'
import Box, { dimensions, spacing, position, layout } from 'ui-box'
import { useStyleConfig } from '../../hooks'
import { CaretDownIcon } from '../../icons'
import { getTextPropsForControlHeight } from '../../lib/deprecated-theme-helpers'

const internalStyles = {
textTransform: 'default',
Expand Down Expand Up @@ -52,20 +53,24 @@ const Select = memo(
name,
onChange,
required,
size = 'medium',
value,
...restProps
} = props

const { className: themedClassName, ...boxProps } = useStyleConfig(
'Select',
{ appearance, size },
{ appearance, size: restProps.size || 'medium' },
pseudoSelectors,
internalStyles
)

const height = heightProp || boxProps.height

const textProps =
!restProps.size && restProps.height
? getTextPropsForControlHeight(restProps.height)
: {}

const iconSize = getIconSizeForSelect(height)
const iconMargin = height >= 36 ? 12 : 8

Expand All @@ -78,6 +83,7 @@ const Select = memo(
width="auto"
height={height}
{...restProps}
{...textProps}
>
<Box
is="select"
Expand Down
9 changes: 7 additions & 2 deletions src/text-input/src/TextInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import cx from 'classnames'
import PropTypes from 'prop-types'
import Box, { spacing, dimensions, position, layout } from 'ui-box'
import { useStyleConfig } from '../../hooks'
import { getTextPropsForControlHeight } from '../../lib/deprecated-theme-helpers'
import { useTheme } from '../../theme'

const pseudoSelectors = {
Expand Down Expand Up @@ -34,7 +35,6 @@ const TextInput = memo(
isInvalid = false,
placeholder,
required,
size = 'medium',
spellCheck = true,
width = 280,
...restProps
Expand All @@ -45,12 +45,16 @@ const TextInput = memo(
const themedFontFamily = fontFamilies[fontFamily] || fontFamily
const { className: themedClassName, ...boxProps } = useStyleConfig(
'Input',
{ appearance, size },
{ appearance, size: restProps.size || 'medium' },
pseudoSelectors,
internalStyles
)

const height = restProps.height || boxProps.height
const textProps =
!restProps.size && restProps.height
? getTextPropsForControlHeight(restProps.height)
: {}

return (
<Box
Expand All @@ -67,6 +71,7 @@ const TextInput = memo(
fontFamily={themedFontFamily}
{...boxProps}
{...restProps}
{...textProps}
height={height}
/>
)
Expand Down