-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of https://github.com/getpingback/ui
- Loading branch information
Showing
12 changed files
with
321 additions
and
5 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
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,27 @@ | ||
import React, { useState } from 'react'; | ||
import type { Meta, StoryObj } from '@storybook/react'; | ||
import { ColorPicker } from './color-picker'; | ||
|
||
const meta: Meta<typeof ColorPicker> = { | ||
title: 'Components/ColorPicker', | ||
component: ColorPicker, | ||
tags: ['autodocs'] | ||
}; | ||
|
||
export default meta; | ||
type Story = StoryObj<typeof ColorPicker>; | ||
|
||
export const Default: Story = { | ||
args: { | ||
color: '#000000', | ||
opacity: 1, | ||
saveText: 'Save', | ||
cancelText: 'Cancel' | ||
}, | ||
render: ({ color: defaultColor, opacity: defaultOpacity, ...args }) => { | ||
const [color, setColor] = useState(defaultColor); | ||
const [opacity, setOpacity] = useState(defaultOpacity); | ||
|
||
return <ColorPicker {...args} color={color} onChange={setColor} opacity={opacity} onChangeOpacity={setOpacity} />; | ||
} | ||
}; |
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,95 @@ | ||
import React from 'react'; | ||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { ColorPicker } from './color-picker'; | ||
|
||
describe('ColorPicker', () => { | ||
const defaultProps = { | ||
color: '#000000', | ||
onChange: jest.fn(), | ||
opacity: 1, | ||
onChangeOpacity: jest.fn() | ||
}; | ||
|
||
beforeEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('renders correctly with default props', () => { | ||
render(<ColorPicker {...defaultProps} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
expect(trigger).toHaveStyle({ backgroundColor: '#000000' }); | ||
}); | ||
|
||
it('opens color picker dropdown when clicked', () => { | ||
render(<ColorPicker {...defaultProps} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
expect(screen.getByTestId('color-picker-dialog')).toBeInTheDocument(); | ||
}); | ||
|
||
it('updates color value when hex input changes', () => { | ||
render(<ColorPicker {...defaultProps} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
const hexInput = screen.getByDisplayValue('#000000'); | ||
fireEvent.change(hexInput, { target: { value: '#FF0000' } }); | ||
|
||
expect(defaultProps.onChange).toHaveBeenCalledWith('#FF0000'); | ||
}); | ||
|
||
it('updates opacity when opacity input changes', () => { | ||
render(<ColorPicker {...defaultProps} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
const opacityInput = screen.getByDisplayValue('100%'); | ||
fireEvent.change(opacityInput, { target: { value: '50' } }); | ||
|
||
expect(defaultProps.onChangeOpacity).toHaveBeenCalledWith(0.5); | ||
}); | ||
|
||
it('calls onSave when save button is clicked', () => { | ||
const onSave = jest.fn(); | ||
render(<ColorPicker {...defaultProps} onSave={onSave} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
const saveButton = screen.getByText('Save'); | ||
fireEvent.click(saveButton); | ||
|
||
expect(onSave).toHaveBeenCalled(); | ||
}); | ||
|
||
it('calls onCancel when cancel button is clicked', () => { | ||
const onCancel = jest.fn(); | ||
render(<ColorPicker {...defaultProps} onCancel={onCancel} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
const cancelButton = screen.getByText('Cancel'); | ||
fireEvent.click(cancelButton); | ||
|
||
expect(onCancel).toHaveBeenCalled(); | ||
}); | ||
|
||
it('updates color when theme color is clicked', () => { | ||
render(<ColorPicker {...defaultProps} />); | ||
|
||
const trigger = screen.getByRole('button'); | ||
fireEvent.click(trigger); | ||
|
||
const themeColors = screen.getAllByTestId('theme-color'); | ||
fireEvent.click(themeColors[0]); | ||
|
||
expect(defaultProps.onChange).toHaveBeenCalled(); | ||
expect(defaultProps.onChangeOpacity).toHaveBeenCalledWith(1); | ||
}); | ||
}); |
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,122 @@ | ||
import * as React from 'react'; | ||
|
||
import * as DropdownMenuPrimitive from '@radix-ui/react-dropdown-menu'; | ||
import { HexColorPicker } from 'react-colorful'; | ||
import { Button } from '../button'; | ||
import { THEME_COLORS } from './constants'; | ||
import { opacityToHex } from './utils'; | ||
|
||
interface ColorPickerProps { | ||
color: string; | ||
onChange: (color: string) => void; | ||
opacity: number; | ||
onChangeOpacity: (opacity: number) => void; | ||
onSave?: () => void; | ||
onCancel?: () => void; | ||
cancelText?: string; | ||
saveText?: string; | ||
} | ||
|
||
export const ColorPicker = ({ | ||
color = '#000000', | ||
onChange, | ||
opacity = 1, | ||
onChangeOpacity, | ||
onSave, | ||
onCancel, | ||
cancelText = 'Cancel', | ||
saveText = 'Save' | ||
}: ColorPickerProps) => { | ||
const [isOpen, setIsOpen] = React.useState(false); | ||
|
||
const handleChangeOpacity = (e: React.ChangeEvent<HTMLInputElement>) => { | ||
const rawValue = e.target.value.replace('%', ''); | ||
const value = Math.min(100, Math.max(0, Number(rawValue) || 0)); | ||
const newOpacity = value / 100; | ||
onChangeOpacity(newOpacity); | ||
|
||
if (newOpacity < 1) { | ||
const baseColor = color.slice(0, 7); | ||
onChange(baseColor + opacityToHex(newOpacity)); | ||
} | ||
}; | ||
|
||
const handleOpacityInputClick = (e: React.MouseEvent<HTMLInputElement>) => { | ||
const input = e.target as HTMLInputElement; | ||
const valueLength = Math.round(opacity * 100).toString().length; | ||
input.setSelectionRange(0, valueLength); | ||
}; | ||
|
||
const handleChangeHexColor = (color: string) => { | ||
onChange(color); | ||
onChangeOpacity(1); | ||
}; | ||
|
||
const handleSave = () => { | ||
setIsOpen(false); | ||
onSave?.(); | ||
}; | ||
|
||
const handleCancel = () => { | ||
setIsOpen(false); | ||
onCancel?.(); | ||
}; | ||
|
||
return ( | ||
<DropdownMenuPrimitive.Root open={isOpen} onOpenChange={setIsOpen}> | ||
<DropdownMenuPrimitive.Trigger asChild> | ||
<button | ||
style={{ backgroundColor: color }} | ||
className="w-6 h-6 rounded-md shadow-[0px_0px_0px_1.33px_#00000014_inset] cursor-pointer" | ||
onClick={() => setIsOpen(true)} | ||
/> | ||
</DropdownMenuPrimitive.Trigger> | ||
<DropdownMenuPrimitive.Content | ||
side="bottom" | ||
align="start" | ||
className="w-[252px] p-4 flex flex-col z-50 rounded-lg shadow-modal" | ||
data-testid="color-picker-dialog" | ||
> | ||
<div className="custom-color-picker"> | ||
<HexColorPicker color={color} onChange={handleChangeHexColor} /> | ||
</div> | ||
<div className="flex flex-col gap-4 mt-4"> | ||
<div className="flex"> | ||
<input | ||
type="text" | ||
className="w-full border border-gray-500/10 rounded-l-lg rounded-r-none text-gray-600 text-sm py-2 px-3" | ||
value={color.toUpperCase()} | ||
onChange={(e) => onChange(e.target.value)} | ||
/> | ||
<input | ||
type="text" | ||
className="w-full max-w-[60px] border border-gray-500/10 rounded-r-lg rounded-l-none border-l-0 text-gray-600 text-sm py-2 px-[11px]" | ||
value={`${Math.round(opacity * 100)}%`} | ||
onChange={handleChangeOpacity} | ||
onClick={handleOpacityInputClick} | ||
/> | ||
</div> | ||
<div className="flex flex-wrap gap-[9px]"> | ||
{THEME_COLORS.map((themeColor) => ( | ||
<div | ||
key={themeColor} | ||
data-testid="theme-color" | ||
style={{ backgroundColor: themeColor }} | ||
className="w-[13px] h-[13px] rounded-sm cursor-pointer" | ||
onClick={() => handleChangeHexColor(themeColor)} | ||
/> | ||
))} | ||
</div> | ||
<div className="flex gap-3"> | ||
<Button variant="ghost" className="w-full" onClick={handleCancel}> | ||
{cancelText} | ||
</Button> | ||
<Button variant="outline" className="w-full" onClick={handleSave}> | ||
{saveText} | ||
</Button> | ||
</div> | ||
</div> | ||
</DropdownMenuPrimitive.Content> | ||
</DropdownMenuPrimitive.Root> | ||
); | ||
}; |
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,22 @@ | ||
export const THEME_COLORS = [ | ||
'#5521B5', | ||
'#9061F9', | ||
'#D946EF', | ||
'#E74694', | ||
'#FB3855', | ||
'#FF1111', | ||
'#F05252', | ||
'#FF8B8B', | ||
'#FF8A4C', | ||
'#FACA15', | ||
'#84CC16', | ||
'#2DD4BF', | ||
'#03543F', | ||
'#31C48D', | ||
'#0694A2', | ||
'#00E1E2', | ||
'#76A9FA', | ||
'#1E429F', | ||
'#A07553', | ||
'#27272A' | ||
]; |
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 @@ | ||
export * from './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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
export const opacityToHex = (opacity: number): string => { | ||
return Math.round(opacity * 255) | ||
.toString(16) | ||
.padStart(2, '0'); | ||
}; |
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
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
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