-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(react): add color picker (#766)
- Loading branch information
1 parent
8a5256a
commit cd463a6
Showing
27 changed files
with
577 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@ark-ui/react': minor | ||
--- | ||
|
||
Add color picker |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
packages/react/src/color-picker/color-picker-area-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import type { AreaProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { createContext } from '../create-context' | ||
|
||
export const [ColorPickerAreaProvider, useColorPickerAreaContext] = createContext<AreaProps>({ | ||
name: 'ColorPickerAreaContext', | ||
hookName: 'useColorPickerAreaContext', | ||
providerName: '<ColorPickerAreaProvider />', | ||
}) |
17 changes: 17 additions & 0 deletions
17
packages/react/src/color-picker/color-picker-area-gradient.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import { mergeProps } from '@zag-js/react' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerAreaContext } from './color-picker-area-context' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerAreaGradientProps = HTMLArkProps<'div'> | ||
|
||
export const ColorPickerAreaGradient = forwardRef<'div', ColorPickerAreaGradientProps>( | ||
(props, ref) => { | ||
const areaContext = useColorPickerAreaContext() | ||
const { getAreaGradientProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getAreaGradientProps(areaContext), props) | ||
|
||
return <ark.div {...mergedProps} ref={ref} /> | ||
}, | ||
) |
15 changes: 15 additions & 0 deletions
15
packages/react/src/color-picker/color-picker-area-thumb.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import { mergeProps } from '@zag-js/react' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerAreaContext } from './color-picker-area-context' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerAreaThumbProps = HTMLArkProps<'div'> | ||
|
||
export const ColorPickerAreaThumb = forwardRef<'div', ColorPickerAreaThumbProps>((props, ref) => { | ||
const areaContext = useColorPickerAreaContext() | ||
const { getAreaThumbProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getAreaThumbProps(areaContext), props) | ||
|
||
return <ark.div {...mergedProps} ref={ref} /> | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import type { AreaProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { mergeProps } from '@zag-js/react' | ||
import { createSplitProps } from '../create-split-props' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { ColorPickerAreaProvider } from './color-picker-area-context' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerAreaProps = HTMLArkProps<'div'> & AreaProps | ||
|
||
export const ColorPickerArea = forwardRef<'div', ColorPickerAreaProps>((props, ref) => { | ||
const [channelProps, divprops] = createSplitProps<AreaProps>()(props, ['xChannel', 'yChannel']) | ||
const { getAreaProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getAreaProps(channelProps), divprops) | ||
|
||
return ( | ||
<ColorPickerAreaProvider value={channelProps}> | ||
<ark.div {...mergedProps} ref={ref} /> | ||
</ColorPickerAreaProvider> | ||
) | ||
}) |
21 changes: 21 additions & 0 deletions
21
packages/react/src/color-picker/color-picker-channel-input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import type { ChannelInputProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { mergeProps } from '@zag-js/react' | ||
import { createSplitProps } from '../create-split-props' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerChannelInputProps = HTMLArkProps<'input'> & ChannelInputProps | ||
|
||
export const ColorPickerChannelInput = forwardRef<'input', ColorPickerChannelInputProps>( | ||
(props, ref) => { | ||
const [channelProps, inputProps] = createSplitProps<ChannelInputProps>()(props, [ | ||
'channel', | ||
'orientation', | ||
]) | ||
const { getChannelInputProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getChannelInputProps(channelProps), inputProps) | ||
|
||
return <ark.input {...mergedProps} ref={ref} /> | ||
}, | ||
) |
10 changes: 10 additions & 0 deletions
10
packages/react/src/color-picker/color-picker-channel-slider-context.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import type { ChannelProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { createContext } from '../create-context' | ||
|
||
export const [ColorPickerSliderProvider, useColorPickerSliderContext] = createContext<ChannelProps>( | ||
{ | ||
name: 'ColorPickerSliderContext', | ||
hookName: 'useColorPickerSliderContext', | ||
providerName: '<ColorPickerSliderProvider />', | ||
}, | ||
) |
17 changes: 17 additions & 0 deletions
17
packages/react/src/color-picker/color-picker-channel-slider-thumb.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import { mergeProps } from '@zag-js/react' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerSliderContext } from './color-picker-channel-slider-context' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerSliderThumbProps = HTMLArkProps<'div'> | ||
|
||
export const ColorPickerSliderThumb = forwardRef<'div', ColorPickerSliderThumbProps>( | ||
(props, ref) => { | ||
const sliderContext = useColorPickerSliderContext() | ||
const { getChannelSliderThumbProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getChannelSliderThumbProps(sliderContext), props) | ||
|
||
return <ark.div {...mergedProps} ref={ref} /> | ||
}, | ||
) |
29 changes: 29 additions & 0 deletions
29
packages/react/src/color-picker/color-picker-channel-slider-track.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import type { ChannelProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { mergeProps } from '@zag-js/react' | ||
import { createSplitProps } from '../create-split-props' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { ColorPickerSliderProvider } from './color-picker-channel-slider-context' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerSliderTrackProps = HTMLArkProps<'div'> & ChannelProps | ||
|
||
export const ColorPickerSliderTrack = forwardRef<'div', ColorPickerSliderTrackProps>( | ||
(props, ref) => { | ||
const [channelProps, { children, ...divProps }] = createSplitProps<ChannelProps>()(props, [ | ||
'channel', | ||
'orientation', | ||
]) | ||
const { getChannelSliderTrackProps, getChannelSliderBackgroundProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getChannelSliderTrackProps(channelProps), divProps) | ||
|
||
return ( | ||
<ColorPickerSliderProvider value={channelProps}> | ||
<ark.div {...mergedProps} ref={ref}> | ||
<ark.div {...getChannelSliderBackgroundProps(channelProps)} /> | ||
{children} | ||
</ark.div> | ||
</ColorPickerSliderProvider> | ||
) | ||
}, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import { mergeProps } from '@zag-js/react' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerContentProps = HTMLArkProps<'div'> | ||
|
||
export const ColorPickerContent = forwardRef<'div', ColorPickerContentProps>((props, ref) => { | ||
const { contentProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(contentProps, props) | ||
|
||
return <ark.div {...mergedProps} ref={ref} /> | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { createContext } from '../create-context' | ||
import { type UseColorPickerReturn } from './use-color-picker' | ||
|
||
export type ColorPickerContext = UseColorPickerReturn | ||
|
||
export const [ColorPickerProvider, useColorPickerContext] = createContext<ColorPickerContext>({ | ||
name: 'ColorPickerContext', | ||
hookName: 'useColorPickerContext', | ||
providerName: '<ColorPickerProvider />', | ||
}) |
11 changes: 11 additions & 0 deletions
11
packages/react/src/color-picker/color-picker-eye-dropper-trigger.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { Children, cloneElement, type ReactElement } from 'react' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerEyeDropperTriggerProps = { children: ReactElement } | ||
|
||
export const ColorPickerEyeDropperTrigger = (props: ColorPickerEyeDropperTriggerProps) => { | ||
const { eyeDropperTriggerProps } = useColorPickerContext() | ||
|
||
const onlyChild = Children.only(props.children) | ||
return cloneElement(onlyChild, eyeDropperTriggerProps) | ||
} |
9 changes: 9 additions & 0 deletions
9
packages/react/src/color-picker/color-picker-swatch-group.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { parts } from './color-picker.anatomy' | ||
|
||
export type ColorPickerSwatchGroupProps = HTMLArkProps<'div'> | ||
|
||
export const ColorPickerSwatchGroup = forwardRef<'div', ColorPickerSwatchGroupProps>( | ||
(props, ref) => <ark.div {...parts.swatchGroup.attrs} {...props} ref={ref} />, | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { forwardRef } from '@polymorphic-factory/react' | ||
import type { SwatchProps } from '@zag-js/color-picker/dist/color-picker.types' | ||
import { mergeProps } from '@zag-js/react' | ||
import { createSplitProps } from '../create-split-props' | ||
import { ark, type HTMLArkProps } from '../factory' | ||
import { useColorPickerContext } from './color-picker-context' | ||
|
||
export type ColorPickerSwatchProps = HTMLArkProps<'button'> & SwatchProps | ||
|
||
export const ColorPickerSwatch = forwardRef<'button', ColorPickerSwatchProps>((props, ref) => { | ||
const [swatchProps, buttonProps] = createSplitProps<SwatchProps>()(props, ['readOnly', 'value']) | ||
const { getSwatchProps, getSwatchBackgroundProps } = useColorPickerContext() | ||
const mergedProps = mergeProps(getSwatchProps(swatchProps), buttonProps) | ||
|
||
return ( | ||
<ark.button {...mergedProps} ref={ref} disabled={swatchProps.readOnly}> | ||
<ark.div {...getSwatchBackgroundProps(swatchProps)} /> | ||
</ark.button> | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { createAnatomy } from '@zag-js/anatomy' | ||
// TODO replace with anatomy from @zag-js/color-picker | ||
// import { anatomy } from '@zag-js/color-picker' | ||
|
||
const anatomy = createAnatomy('color-picker', [ | ||
'area', | ||
'areaThumb', | ||
'areaGradient', | ||
'channelSliderTrack', | ||
'channelSliderTrackBg', | ||
'channelSliderThumb', | ||
'channelInput', | ||
'swatch', | ||
'swatchBg', | ||
'content', | ||
'label', | ||
'eyeDropTrigger', | ||
]) | ||
|
||
export const colorPickerAnatomy = anatomy.extendWith('swatchGroup') | ||
export const parts = colorPickerAnatomy.build() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
[data-scope='color-picker'][data-part='content'] { | ||
max-width: 260px; | ||
display: flex; | ||
flex-direction: column; | ||
box-sizing: border-box; | ||
gap: 16px; | ||
padding: 24px; | ||
border: 1px solid #d5d5d5; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='area'] { | ||
height: 200px; | ||
border-radius: 4px; | ||
border: 1px solid #ebebeb; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='area-gradient'] { | ||
background: rgb(142, 142, 142); | ||
border-radius: 4px; | ||
height: 200px; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='area-thumb'], | ||
[data-scope='color-picker'][data-part='channel-slider-thumb'] { | ||
border: 2px solid white; | ||
border-radius: 9999px; | ||
box-sizing: border-box; | ||
transform: translate(-50%, -50%); | ||
box-shadow: black 0px 0px 0px 1px, black 0px 0px 0px 1px inset; | ||
width: 16px; | ||
height: 16px; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='channel-slider-track'] { | ||
height: 20px; | ||
border-radius: 4px; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='channel-slider-track-bg'] { | ||
border-radius: 4px; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='channel-input'] { | ||
border-radius: 4px; | ||
width: 100%; | ||
border: 1px solid #c2c2c2; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='channel-input']::-webkit-outer-spin-button, | ||
[data-scope='color-picker'][data-part='channel-input']::-webkit-inner-spin-button { | ||
-webkit-appearance: none; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='swatch-group'] { | ||
display: flex; | ||
gap: 4px; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='swatch'] { | ||
all: unset; | ||
border-radius: 4px; | ||
width: 20px; | ||
height: 20px; | ||
flex-shrink: 0; | ||
} | ||
|
||
[data-scope='color-picker'][data-part='swatch-bg'] { | ||
border-radius: 4px; | ||
} |
Oops, something went wrong.
cd463a6
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
ark-website – ./
ark-website-chakra-ui.vercel.app
ark-ui.com
ark-website-three.vercel.app
ark-website-git-main-chakra-ui.vercel.app