Skip to content

Commit

Permalink
add gradient panel & fix zIndex (#117)
Browse files Browse the repository at this point in the history
* add gradient panel & fix zIndex

* fix

* fix & add external component

* add ColorPicker component

* fix

* fix

* fix gradient

* fix btn ui & ts type
  • Loading branch information
PierreCrb authored Feb 8, 2021
1 parent 5b76624 commit 243bbed
Show file tree
Hide file tree
Showing 8 changed files with 443 additions and 151 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
"dependencies": {
"@bugsnag/js": "^6.5.2",
"@bugsnag/plugin-react": "^6.5.0",
"@chakra-ui/icons": "^1.0.1",
"@chakra-ui/react": "^1.0.1",
"@chakra-ui/theme": "^1.0.1",
"@chakra-ui/icons": "^1.0.4",
"@chakra-ui/react": "^1.2.1",
"@chakra-ui/theme": "^1.5.0",
"@emotion/react": "^11.1.1",
"@emotion/styled": "^11.0.0",
"@mdx-js/react": "^1.5.5",
Expand All @@ -18,7 +18,7 @@
"codesandbox": "^2.1.11",
"coloreact": "^0.3.1",
"copy-to-clipboard": "^3.2.1",
"framer-motion": "^2.9.4",
"framer-motion": "^3.3.0",
"lodash": "^4.17.15",
"lodash-es": "^4.17.15",
"lz-string": "^1.4.4",
Expand Down
2 changes: 1 addition & 1 deletion src/components/CodePanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const CodePanel = () => {

return (
<Box
zIndex={40}
zIndex={5}
p={4}
fontSize="sm"
backgroundColor="#011627"
Expand Down
4 changes: 2 additions & 2 deletions src/components/editor/ComponentPreview.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ const componentsToTest = [
// 'Tab',
'Input',
'Radio',
'ListItem',
'ListIcon',
//'ListItem',
//'ListIcon',
// 'AlertIcon',
// 'AccordionIcon',
'Box',
Expand Down
173 changes: 173 additions & 0 deletions src/components/inspector/controls/ColorPickerControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,173 @@
import React from 'react'
import {
Popover,
PopoverTrigger,
PopoverContent,
PopoverArrow,
PopoverBody,
IconButton,
Box,
Tabs,
TabList,
Tab,
TabPanels,
TabPanel,
Input,
Portal,
useTheme,
} from '@chakra-ui/react'
import ColorPicker from 'coloreact'
import HuesPickerControl from './HuesPickerControl'
import { useForm } from '~hooks/useForm'
import omit from 'lodash/omit'
import usePropsSelector from '~hooks/usePropsSelector'

type ColorPickerPropType = {
withFullColor?: boolean
name: string
gradient: boolean
gradientColor?: string
index?: number
updateGradient?: (value: string, index: number) => Promise<void>
}

const ColorPickerControl = (props: ColorPickerPropType) => {
const theme = useTheme()
const themeColors: any = omit(theme.colors, [
'transparent',
'current',
'black',
'white',
])

const { setValue, setValueFromEvent } = useForm()
const value = usePropsSelector(props.name)

let propsIconButton: any = { bg: value }
if (value && themeColors[value]) {
propsIconButton = { colorScheme: value }
}

return (
<>
<Popover placement="bottom">
<PopoverTrigger>
{props.gradient ? (
<IconButton
mr={2}
boxShadow="md"
border={props.gradientColor ? 'none' : '2px solid grey'}
isRound
aria-label="Color"
size="xs"
colorScheme={props.gradientColor}
bg={props.gradientColor}
/>
) : (
<IconButton
mr={2}
boxShadow="md"
border={value ? 'none' : '2px solid grey'}
isRound
aria-label="Color"
size="xs"
{...propsIconButton}
/>
)}
</PopoverTrigger>
<Portal>
<PopoverContent width="200px">
<PopoverArrow />
<PopoverBody>
{props.withFullColor ? (
<Tabs size="sm" variant="soft-rounded" colorScheme="green">
<TabList>
<Tab>Theme</Tab>
<Tab>All</Tab>
</TabList>
<TabPanels mt={4}>
<TabPanel p={0}>
{props.gradient ? (
<HuesPickerControl
name={props.name}
themeColors={themeColors}
enableHues
setValue={setValue}
gradient={true}
index={props.index}
updateGradient={props.updateGradient}
/>
) : (
<HuesPickerControl
name={props.name}
themeColors={themeColors}
enableHues
setValue={setValue}
gradient={props.gradient}
/>
)}
</TabPanel>

<TabPanel p={0}>
<Box position="relative" height="150px">
<ColorPicker
color={value}
onChange={(color: any) => {
props.gradient
? props.updateGradient!(
`#${color.hex}`,
props.index!,
)
: setValue(props.name, `#${color.hex}`)
}}
/>
);
</Box>
</TabPanel>
</TabPanels>
</Tabs>
) : props.gradient ? (
<HuesPickerControl
name={props.name}
themeColors={themeColors}
enableHues
setValue={setValue}
gradient={true}
index={props.index}
updateGradient={props.updateGradient}
/>
) : (
<HuesPickerControl
name={props.name}
themeColors={themeColors}
enableHues={false}
setValue={setValue}
gradient={props.gradient}
/>
)}
</PopoverBody>
</PopoverContent>
</Portal>
</Popover>
{props.gradient ? (
<Input
width="100px"
size="sm"
name={props.name}
onChange={setValueFromEvent}
value={props.gradientColor}
/>
) : (
<Input
width="100px"
size="sm"
name={props.name}
onChange={setValueFromEvent}
value={value}
/>
)}
</>
)
}

export default ColorPickerControl
149 changes: 5 additions & 144 deletions src/components/inspector/controls/ColorsControl.tsx
Original file line number Diff line number Diff line change
@@ -1,31 +1,6 @@
import React, { ReactNode, useState, memo } from 'react'
import {
Popover,
PopoverTrigger,
PopoverContent,
PopoverArrow,
Grid,
PopoverBody,
IconButton,
SliderTrack,
SliderFilledTrack,
SliderThumb,
Box,
Tabs,
TabList,
Tab,
TabPanels,
TabPanel,
Input,
useTheme,
Slider,
Portal,
} from '@chakra-ui/react'
import React, { ReactNode, memo } from 'react'
import FormControl from './FormControl'
import { useForm } from '~hooks/useForm'
import omit from 'lodash/omit'
import ColorPicker from 'coloreact'
import usePropsSelector from '~hooks/usePropsSelector'
import ColorPickerControl from './ColorPickerControl'

type ColorControlPropsType = {
name: string
Expand All @@ -35,126 +10,12 @@ type ColorControlPropsType = {
}

const ColorsControl = (props: ColorControlPropsType) => {
const { setValue, setValueFromEvent } = useForm()
const [hue, setHue] = useState(500)
const value = usePropsSelector(props.name)
const theme = useTheme()

const themeColors: any = omit(theme.colors, [
'transparent',
'current',
'black',
'white',
])

let propsIconButton: any = { bg: value }
if (value && themeColors[value]) {
propsIconButton = { colorScheme: value }
}

const huesPicker = (
<>
<Grid mb={2} templateColumns="repeat(5, 1fr)" gap={0}>
{Object.keys(themeColors).map(colorName => (
<Box
border={colorName.includes('white') ? '1px solid lightgrey' : ''}
key={colorName}
_hover={{ boxShadow: 'lg' }}
cursor="pointer"
bg={`${colorName}.${props.enableHues ? hue : 500}`}
onClick={() =>
setValue(
props.name,
props.enableHues ? `${colorName}.${hue}` : colorName,
)
}
mt={2}
borderRadius="full"
height="30px"
width="30px"
/>
))}
</Grid>

{props.enableHues && (
<>
<Slider
onChange={value => {
value = value === 0 ? 50 : value
setHue(value)
}}
min={0}
max={900}
step={100}
value={hue}
>
<SliderTrack>
<SliderFilledTrack />
</SliderTrack>
<SliderThumb boxSize={8}>
<Box borderRadius="full" fontSize="xs">
{hue}
</Box>
</SliderThumb>
</Slider>
</>
)}
</>
)

return (
<FormControl label={props.label}>
<Popover placement="bottom">
<PopoverTrigger>
<IconButton
mr={2}
boxShadow="md"
border={value ? 'none' : '2px solid grey'}
isRound
aria-label="Color"
size="xs"
{...propsIconButton}
/>
</PopoverTrigger>
<Portal>
<PopoverContent width="200px">
<PopoverArrow />
<PopoverBody>
{props.withFullColor ? (
<Tabs size="sm" variant="soft-rounded" colorScheme="green">
<TabList>
<Tab>Theme</Tab>
<Tab>All</Tab>
</TabList>
<TabPanels mt={4}>
<TabPanel p={0}>{huesPicker}</TabPanel>

<TabPanel p={0}>
<Box position="relative" height="150px">
<ColorPicker
color={value}
onChange={(color: any) => {
setValue(props.name, `#${color.hex}`)
}}
/>
);
</Box>
</TabPanel>
</TabPanels>
</Tabs>
) : (
huesPicker
)}
</PopoverBody>
</PopoverContent>
</Portal>
</Popover>
<Input
width="100px"
size="sm"
<ColorPickerControl
withFullColor={props.withFullColor}
name={props.name}
onChange={setValueFromEvent}
value={value}
gradient={false}
/>
</FormControl>
)
Expand Down
Loading

0 comments on commit 243bbed

Please sign in to comment.