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

New package combobox #36

Merged
merged 2 commits into from
Oct 31, 2017
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
1 change: 1 addition & 0 deletions .storybook/addons.js
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
import '@storybook/addon-options/register'
import '@storybook/addon-actions/register'
import '@storybook/addon-links/register'
4 changes: 1 addition & 3 deletions .storybook/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,8 @@ setOptions({
selectedAddonPanel: undefined, // The order of addons in the "Addons Panel" is the same as you import them in 'addons.js'. The first panel will be opened by default as you run Storybook
})

const req = require.context('../packages', true, /.stories.js$/)

function loadStories() {
req.keys().forEach(filename => req(filename))
require('./requireStories!./empty')
}

configure(loadStories, module)
2 changes: 2 additions & 0 deletions .storybook/empty.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// This file exists to satisfy the `require('./requireStories!…')` call in
// `config.js`. It doesn't need to contain anything, it just has to exist.
34 changes: 34 additions & 0 deletions .storybook/requireStories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const glob = require('glob')

module.exports = function noop() {}
module.exports.pitch = function pitch() {
// This is a temp fix for storybooks. If we are running static builds we'll have the PACKAGE var
// and we need to work from within that package. Otherwise we are running the server and we know
// we are inside the packages directory already
const packagesRoot = process.env.PACKAGE
? `${__dirname}/../../packages/${process.env.PACKAGE}`
: process.cwd()

// Prevent node_modules from being loaded
const storiesPattern = `${packagesRoot}/packages/*/stories/**/*.stories.js`
const storybookFiles = glob.sync(storiesPattern, { cwd: __dirname })

console.log('require the following stories:')

// Pretty print the stories that are being loaded for testing
console.log(
JSON.stringify(
storybookFiles.map(x => x.substr(x.indexOf('evergreen/packages'))),
null,
2,
),
)

console.log(`Loading ${storybookFiles.length} storybook files`)

const storyRequireStatements = storybookFiles
.map(storyPath => `require(${JSON.stringify(storyPath)});`)
.join('\n')

return storyRequireStatements
}
41 changes: 24 additions & 17 deletions packages/evergreen-autocomplete/src/components/Autocomplete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,34 @@ import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import fuzzaldrin from 'fuzzaldrin-plus'
import Downshift from 'downshift'
import VirtualList from 'react-tiny-virtual-list'
import AutocompleteItem from './AutocompleteItem'
import { Pane } from 'evergreen-layers'
import { Text } from 'evergreen-typography'
import { Popover } from 'evergreen-popover'
import VirtualList from 'react-tiny-virtual-list'
import AutocompleteItem from './AutocompleteItem'

const fuzzyFilter = (items, input) => fuzzaldrin.filter(items, input)

const autocompleteItemRenderer = props => <AutocompleteItem {...props} />

// https://github.com/paypal/downshift/issues/164
export default class Autocomplete extends PureComponent {
static propTypes = {
children: PropTypes.func,
itemSize: PropTypes.number,
renderItem: PropTypes.func,
itemsFilter: PropTypes.func,
isFilterDisabled: PropTypes.bool,
popoverMaxHeight: PropTypes.number,
useSmartPositioning: PropTypes.bool,
...Downshift.propTypes,
}

static defaultProps = {
itemsFilter: fuzzyFilter,
useSmartPositioning: false,
itemSize: 32,
itemsFilter: fuzzyFilter,
isFilterDisabled: false,
popoverMaxHeight: 240,
useSmartPositioning: false,
renderItem: autocompleteItemRenderer,
}

Expand All @@ -40,14 +42,16 @@ export default class Autocomplete extends PureComponent {
getItemProps,
}) => {
const {
itemsFilter,
itemSize,
itemsFilter,
items: originalItems,
popoverMaxHeight,
renderItem,
popoverMaxHeight,
isFilterDisabled,
} = this.props

const items =
inputValue.trim() === ''
isFilterDisabled || inputValue.trim() === ''
? originalItems
: itemsFilter(originalItems, inputValue)

Expand All @@ -56,12 +60,12 @@ export default class Autocomplete extends PureComponent {
{items.length > 0 && (
<VirtualList
width="100%"
scrollToIndex={highlightedIndex || 0}
scrollToAlignment="auto"
height={Math.min(items.length * itemSize, popoverMaxHeight)}
itemCount={items.length}
itemSize={itemSize}
itemCount={items.length}
scrollToIndex={highlightedIndex || 0}
overscanCount={3}
scrollToAlignment="auto"
renderItem={({ index, style }) => {
const item = items[index]
return renderItem(
Expand All @@ -70,11 +74,13 @@ export default class Autocomplete extends PureComponent {
key: item,
index,
style,
onClick: () => selectItemAtIndex(index),
isHighlighted: highlightedIndex === index,
isEven: index % 2 === 1,
isSelected: selectedItem === item,
children: item,
onMouseUp: () => {
selectItemAtIndex(index)
},
isSelected: selectedItem === item,
isHighlighted: highlightedIndex === index,
}),
)
}}
Expand Down Expand Up @@ -108,7 +114,8 @@ export default class Autocomplete extends PureComponent {
}) => (
<div>
<Popover
useSmartPositioning={useSmartPositioning}
isOpen={isOpen}
display="inline-block"
content={({ targetRect }) =>
this.renderResults({
width: targetRect.width,
Expand All @@ -118,8 +125,8 @@ export default class Autocomplete extends PureComponent {
highlightedIndex,
selectItemAtIndex,
})}
display="inline-block"
isOpen={isOpen}
animationDuration={0}
useSmartPositioning={useSmartPositioning}
>
{({ isOpen: isOpenPopover, toggle, getRef, key }) =>
children({
Expand Down
14 changes: 12 additions & 2 deletions packages/evergreen-autocomplete/src/components/AutocompleteItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,25 @@ export default class AutocompleteItem extends PureComponent {
children,
...props
} = this.props

return (
<Pane
style={style}
display="flex"
paddingX={8}
alignItems="center"
cursor="pointer"
{...(isEven && !isHighlighted
{...(isEven && !isSelected
? {
backgroundColor: colors.neutral['5'],
}
: {})}
{...(isHighlighted
? {
backgroundColor: colors.blue['15'],
}
: {})}
{...(isSelected
? {
backgroundColor: colors.blue['300'],
}
Expand All @@ -46,7 +52,11 @@ export default class AutocompleteItem extends PureComponent {
: {})}
{...props}
>
<Text size={300} {...(isHighlighted ? { color: '#ffffff' } : {})}>
<Text
size={300}
{...(isHighlighted ? { color: colors.blue['500'] } : {})}
{...(isSelected ? { color: '#ffffff' } : {})}
>
{children}
</Text>
</Pane>
Expand Down
1 change: 1 addition & 0 deletions packages/evergreen-buttons/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
},
"dependencies": {
"evergreen-colors": "^2.2.6",
"evergreen-shared-styles": "^2.2.6",
"evergreen-typography": "^2.2.6",
"ui-box": "^0.4.1"
}
Expand Down
26 changes: 12 additions & 14 deletions packages/evergreen-buttons/src/components/Button.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
import React, { PureComponent } from 'react'
import PropTypes from 'prop-types'
import { Text, TextStyles } from 'evergreen-typography'
import { Text } from 'evergreen-typography'
import {
getBorderRadiusForControlHeight,
getTextStyleForControlHeight,
} from 'evergreen-shared-styles'
import ButtonAppearances from '../styles/button-appearances'

const getTextStyleForButton = ({ height }) => {
// HACK: add padding top to visually align in center
if (height <= 24) return { ...TextStyles['200'], paddingTop: 1 }
if (height <= 28) return TextStyles['300']
if (height <= 32) return TextStyles['300']
if (height <= 36) return TextStyles['400']
if (height <= 40) return TextStyles['400']
if (height <= 48) return TextStyles['500']
if (height <= 56) return TextStyles['700']
return TextStyles['800']
}

export default class Button extends PureComponent {
static propTypes = {
...Text.propTypes,
Expand All @@ -24,6 +16,7 @@ export default class Button extends PureComponent {

static defaultProps = {
is: 'button',
position: 'relative',
appearance: 'default',
paddingTop: 0,
paddingBottom: 0,
Expand All @@ -47,10 +40,15 @@ export default class Button extends PureComponent {
...props
} = this.props
const appearanceStyle = ButtonAppearances[appearance]
const textStyle = getTextStyleForButton({ height })
const textStyle = getTextStyleForControlHeight({ height })
const borderRadius = getBorderRadiusForControlHeight({ height })

return (
<Text
borderTopRightRadius={borderRadius}
borderBottomRightRadius={borderRadius}
borderTopLeftRadius={borderRadius}
borderBottomLeftRadius={borderRadius}
paddingTop={paddingTop}
paddingBottom={paddingBottom}
paddingRight={
Expand Down
6 changes: 5 additions & 1 deletion packages/evergreen-buttons/src/styles/button-appearances.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const baseStyle = {
textDecoration: 'none',
transition: 'box-shadow 80ms ease-in-out',
WebkitAppearance: 'none',
borderRadius: 5,
border: 'none',
outline: 'none',
cursor: 'pointer',
Expand Down Expand Up @@ -42,6 +41,7 @@ const ButtonAppearances = {
]}, inset 0 -1px 1px 0 ${colors.neutral['15A']}`,
},
[focusState]: {
zIndex: 2,
boxShadow: `0 0 0 2px ${colors.blue['20A']}, inset 0 0 0 1px ${colors
.neutral['70A']}, inset 0 -1px 1px 0 ${colors.neutral['10A']}`,
},
Expand All @@ -66,6 +66,7 @@ const ButtonAppearances = {
.blue['500']})`,
},
[focusState]: {
zIndex: 2,
boxShadow: `0 0 0 2px ${colors.blue['50A']}, inset 0 0 0 1px ${colors
.neutral['30A']}, inset 0 -1px 1px 0 ${colors.neutral['30A']}`,
},
Expand All @@ -91,6 +92,7 @@ const ButtonAppearances = {
.green['600']})`,
},
[focusState]: {
zIndex: 2,
boxShadow: `0 0 0 2px ${colors.green['100A']}, inset 0 0 0 1px ${colors
.neutral['30A']}, inset 0 -1px 1px 0 ${colors.neutral['30A']}`,
},
Expand All @@ -116,6 +118,7 @@ const ButtonAppearances = {
.red['600']})`,
},
[focusState]: {
zIndex: 2,
boxShadow: `0 0 0 2px ${colors.red['100A']}, inset 0 0 0 1px ${colors
.neutral['30A']}, inset 0 -1px 1px 0 ${colors.neutral['30A']}`,
},
Expand All @@ -134,6 +137,7 @@ const ButtonAppearances = {
backgroundColor: colors.neutral['7A'],
},
[focusState]: {
zIndex: 2,
boxShadow: `0 0 0 2px ${colors.blue['50A']}`,
},
[activeState]: {
Expand Down
31 changes: 16 additions & 15 deletions packages/evergreen-buttons/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,24 @@ encoding@^0.1.11:
dependencies:
iconv-lite "~0.4.13"

evergreen-colors@^2.0.0-0:
version "2.0.0-0"
resolved "https://registry.yarnpkg.com/evergreen-colors/-/evergreen-colors-2.0.0-0.tgz#f11e80410d1f2eb7ee73ef5b1815b393b3f16324"
evergreen-colors@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/evergreen-colors/-/evergreen-colors-2.2.6.tgz#229277e934eab18de8ac67d4fedf565f59502497"

evergreen-typography@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/evergreen-typography/-/evergreen-typography-2.0.0.tgz#561a1bef47e294c20361314c96342c9858b9b161"
evergreen-shared-styles@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/evergreen-shared-styles/-/evergreen-shared-styles-2.2.6.tgz#0acb2263c06f1ad648e975e95b42d96f13361e39"
dependencies:
evergreen-colors "^2.0.0-0"
ui-box "^0.4.0"
evergreen-colors "^2.2.6"
evergreen-typography "^2.2.6"
ui-box "^0.4.1"

evergreen-typography@^2.2.6:
version "2.2.6"
resolved "https://registry.yarnpkg.com/evergreen-typography/-/evergreen-typography-2.2.6.tgz#9d7fa8a2892e353069c6f6d37eddf6843bd1f765"
dependencies:
evergreen-colors "^2.2.6"
ui-box "^0.4.1"

fbjs@^0.8.12, fbjs@^0.8.9:
version "0.8.14"
Expand Down Expand Up @@ -135,13 +143,6 @@ ua-parser-js@^0.7.9:
version "0.7.14"
resolved "https://registry.yarnpkg.com/ua-parser-js/-/ua-parser-js-0.7.14.tgz#110d53fa4c3f326c121292bbeac904d2e03387ca"

ui-box@^0.4.0:
version "0.4.0"
resolved "https://registry.yarnpkg.com/ui-box/-/ui-box-0.4.0.tgz#ec49e2b1e70dfd84d555e6bf3391690155a550e7"
dependencies:
classnames "^2.2.5"
glamor "^2.20.22"

ui-box@^0.4.1:
version "0.4.1"
resolved "https://registry.yarnpkg.com/ui-box/-/ui-box-0.4.1.tgz#410f7b14ef9abdfac10973e21ba6fc1e1d0fa072"
Expand Down
Loading