1
0
mirror of https://github.com/checktheroads/hyperglass synced 2024-05-11 05:55:08 +00:00
Files
checktheroads-hyperglass/hyperglass/ui/components/ChakraSelect.js

191 lines
5.9 KiB
JavaScript
Raw Normal View History

2020-10-07 09:41:58 -07:00
import * as React from 'react';
import { Text, useColorMode, useTheme } from '@chakra-ui/core';
import Select from 'react-select';
import { opposingColor } from 'app/util';
2020-01-17 02:50:57 -07:00
export const ChakraSelect = React.forwardRef(
2020-10-07 09:41:58 -07:00
({ placeholder = 'Select...', isFullWidth, size, children, ...props }, ref) => {
2020-04-26 09:06:54 -07:00
const theme = useTheme();
const { colorMode } = useColorMode();
const sizeMap = {
lg: { height: theme.space[12] },
md: { height: theme.space[10] },
2020-10-07 09:41:58 -07:00
sm: { height: theme.space[8] },
2020-04-26 09:06:54 -07:00
};
const colorSetPrimaryBg = {
dark: theme.colors.primary[300],
2020-10-07 09:41:58 -07:00
light: theme.colors.primary[500],
2020-04-26 09:06:54 -07:00
};
2020-10-07 09:41:58 -07:00
const colorSetPrimaryColor = opposingColor(theme, colorSetPrimaryBg[colorMode]);
2020-04-26 09:06:54 -07:00
const bg = {
dark: theme.colors.whiteAlpha[100],
2020-10-07 09:41:58 -07:00
light: theme.colors.white,
2020-04-26 09:06:54 -07:00
};
const color = {
dark: theme.colors.whiteAlpha[800],
2020-10-07 09:41:58 -07:00
light: theme.colors.black,
2020-04-26 09:06:54 -07:00
};
const borderFocused = theme.colors.secondary[500];
const borderDisabled = theme.colors.whiteAlpha[100];
const border = {
dark: theme.colors.whiteAlpha[50],
2020-10-07 09:41:58 -07:00
light: theme.colors.gray[100],
2020-04-26 09:06:54 -07:00
};
const borderRadius = theme.space[1];
const hoverColor = {
dark: theme.colors.whiteAlpha[200],
2020-10-07 09:41:58 -07:00
light: theme.colors.gray[300],
2020-04-26 09:06:54 -07:00
};
const { height } = sizeMap[size];
const optionBgActive = {
dark: theme.colors.primary[400],
2020-10-07 09:41:58 -07:00
light: theme.colors.primary[600],
2020-04-26 09:06:54 -07:00
};
const optionBgColor = opposingColor(theme, optionBgActive[colorMode]);
const optionSelectedBg = {
dark: theme.colors.whiteAlpha[400],
2020-10-07 09:41:58 -07:00
light: theme.colors.blackAlpha[400],
2020-04-26 09:06:54 -07:00
};
2020-10-07 09:41:58 -07:00
const optionSelectedColor = opposingColor(theme, optionSelectedBg[colorMode]);
2020-04-26 09:06:54 -07:00
const selectedDisabled = theme.colors.whiteAlpha[400];
const placeholderColor = {
2020-05-02 16:09:03 -07:00
dark: theme.colors.whiteAlpha[700],
2020-10-07 09:41:58 -07:00
light: theme.colors.gray[600],
2020-04-26 09:06:54 -07:00
};
2020-07-04 14:58:39 -07:00
const menuBg = {
dark: theme.colors.blackFaded[800],
2020-10-07 09:41:58 -07:00
light: theme.colors.whiteFaded[50],
2020-07-04 14:58:39 -07:00
};
2020-04-26 09:06:54 -07:00
const menuColor = {
dark: theme.colors.white,
2020-10-07 09:41:58 -07:00
light: theme.colors.blackAlpha[800],
2020-04-26 09:06:54 -07:00
};
2020-07-04 14:58:39 -07:00
const scrollbar = {
dark: theme.colors.whiteAlpha[300],
2020-10-07 09:41:58 -07:00
light: theme.colors.blackAlpha[300],
2020-07-04 14:58:39 -07:00
};
const scrollbarHover = {
dark: theme.colors.whiteAlpha[400],
2020-10-07 09:41:58 -07:00
light: theme.colors.blackAlpha[400],
2020-07-04 14:58:39 -07:00
};
const scrollbarBg = {
dark: theme.colors.whiteAlpha[50],
2020-10-07 09:41:58 -07:00
light: theme.colors.blackAlpha[50],
2020-07-04 14:58:39 -07:00
};
2020-04-26 09:06:54 -07:00
return (
<Select
ref={ref}
styles={{
container: base => ({
...base,
minHeight: height,
borderRadius: borderRadius,
2020-10-07 09:41:58 -07:00
width: '100%',
2020-04-26 09:06:54 -07:00
}),
control: (base, state) => ({
...base,
minHeight: height,
backgroundColor: bg[colorMode],
color: color[colorMode],
borderColor: state.isDisabled
? borderDisabled
: state.isFocused
? borderFocused
: border[colorMode],
borderRadius: borderRadius,
2020-10-07 09:41:58 -07:00
'&:hover': {
borderColor: hoverColor[colorMode],
},
2020-04-26 09:06:54 -07:00
}),
menu: base => ({
...base,
backgroundColor: menuBg[colorMode],
2020-10-07 09:41:58 -07:00
borderRadius: borderRadius,
2020-04-26 09:06:54 -07:00
}),
2020-07-04 14:58:39 -07:00
menuList: base => ({
...base,
2020-10-07 09:41:58 -07:00
'&::-webkit-scrollbar': { width: '5px' },
'&::-webkit-scrollbar-track': {
backgroundColor: scrollbarBg[colorMode],
2020-07-04 14:58:39 -07:00
},
2020-10-07 09:41:58 -07:00
'&::-webkit-scrollbar-thumb': {
backgroundColor: scrollbar[colorMode],
2020-07-04 14:58:39 -07:00
},
2020-10-07 09:41:58 -07:00
'&::-webkit-scrollbar-thumb:hover': {
backgroundColor: scrollbarHover[colorMode],
2020-07-04 14:58:39 -07:00
},
2020-10-07 09:41:58 -07:00
'-ms-overflow-style': { display: 'none' },
2020-07-04 14:58:39 -07:00
}),
2020-04-26 09:06:54 -07:00
option: (base, state) => ({
...base,
backgroundColor: state.isDisabled
? selectedDisabled
: state.isSelected
? optionSelectedBg[colorMode]
: state.isFocused
? colorSetPrimaryBg[colorMode]
2020-10-07 09:41:58 -07:00
: 'transparent',
2020-04-26 09:06:54 -07:00
color: state.isDisabled
? selectedDisabled
: state.isFocused
? colorSetPrimaryColor
: state.isSelected
? optionSelectedColor
: menuColor[colorMode],
fontSize: theme.fontSizes[size],
2020-10-07 09:41:58 -07:00
'&:active': {
2020-04-26 09:06:54 -07:00
backgroundColor: optionBgActive[colorMode],
2020-10-07 09:41:58 -07:00
color: optionBgColor,
},
2020-04-26 09:06:54 -07:00
}),
indicatorSeparator: base => ({
...base,
2020-10-07 09:41:58 -07:00
backgroundColor: placeholderColor[colorMode],
2020-04-26 09:06:54 -07:00
}),
dropdownIndicator: base => ({
...base,
color: placeholderColor[colorMode],
2020-10-07 09:41:58 -07:00
'&:hover': {
color: color[colorMode],
},
2020-04-26 09:06:54 -07:00
}),
valueContainer: base => ({
...base,
paddingLeft: theme.space[4],
2020-10-07 09:41:58 -07:00
paddingRight: theme.space[4],
2020-04-26 09:06:54 -07:00
}),
multiValue: base => ({
...base,
2020-10-07 09:41:58 -07:00
backgroundColor: colorSetPrimaryBg[colorMode],
2020-04-26 09:06:54 -07:00
}),
multiValueLabel: base => ({
...base,
2020-10-07 09:41:58 -07:00
color: colorSetPrimaryColor,
2020-04-26 09:06:54 -07:00
}),
multiValueRemove: base => ({
...base,
color: colorSetPrimaryColor,
2020-10-07 09:41:58 -07:00
'&:hover': {
2020-04-26 09:06:54 -07:00
color: colorSetPrimaryColor,
2020-10-07 09:41:58 -07:00
backgroundColor: 'inherit',
},
2020-04-26 09:06:54 -07:00
}),
singleValue: base => ({
...base,
color: color[colorMode],
2020-10-07 09:41:58 -07:00
fontSize: theme.fontSizes[size],
}),
2020-04-26 09:06:54 -07:00
}}
placeholder={
2020-10-07 09:41:58 -07:00
<Text color={placeholderColor[colorMode]} fontSize={size} fontFamily={theme.fonts.body}>
2020-04-26 09:06:54 -07:00
{placeholder}
</Text>
}
2020-10-07 09:41:58 -07:00
{...props}>
2020-04-26 09:06:54 -07:00
{children}
</Select>
);
2020-10-07 09:41:58 -07:00
},
2020-04-18 07:57:55 -07:00
);