From 7f6bfa2aa4366730329380c77c7ec66651ebb296 Mon Sep 17 00:00:00 2001 From: Tigran Date: Thu, 25 Jul 2024 19:36:50 +0400 Subject: [PATCH] docs: add input examples to color picker story --- .../components/color-picker/color-picker.css | 2 +- .../color-picker/color-picker.stories.tsx | 206 +++++++++++++++++- 2 files changed, 202 insertions(+), 6 deletions(-) diff --git a/figma-kit/src/components/color-picker/color-picker.css b/figma-kit/src/components/color-picker/color-picker.css index 612bea0..1a6129e 100644 --- a/figma-kit/src/components/color-picker/color-picker.css +++ b/figma-kit/src/components/color-picker/color-picker.css @@ -1,4 +1,4 @@ -.fp-ColorPickerHueSlider:where(.fp-color-model-hsv, .fp-color-model-hsl) { +.fp-ColorPickerHueSlider:where(.fp-color-model-hsv, .fp-color-model-hsl, .fp-color-model-rgb, .fp-color-model-hex) { --slider-track-bg: linear-gradient( 90deg, rgba(255, 0, 0, 1) calc(var(--slider-track-size) / 2), diff --git a/figma-kit/src/components/color-picker/color-picker.stories.tsx b/figma-kit/src/components/color-picker/color-picker.stories.tsx index ab098a4..da563ad 100644 --- a/figma-kit/src/components/color-picker/color-picker.stories.tsx +++ b/figma-kit/src/components/color-picker/color-picker.stories.tsx @@ -1,7 +1,12 @@ import type { Meta, StoryObj } from '@storybook/react'; import { useState } from 'react'; +import * as ValueField from '@components/value-field'; +import * as Select from '@components/select'; import * as ColorPicker from '@components/color-picker'; import { Flex } from '@components/flex'; +import type { ColorModel, ColorSpace } from '@components/color-picker'; +import type { RGBA } from '@lib/color'; +import { hslaToRgba, hsvaToRgba, rgbaToCssString, rgbaToHsla, rgbaToHsva } from '@lib/color'; const Picker = () => { return
; @@ -17,14 +22,205 @@ const meta: Meta = { export default meta; export const Story = () => { - const [color, setColor] = useState({ r: 0.48235294, g: 0.81176471, b: 0.48235294, a: 1 }); + const [color, setColor] = useState({ r: 1, g: 0.21176471, b: 0.18235294, a: 1 }); + const [model, setModel] = useState('hsv'); + const [space, setSpace] = useState('srgb'); + const Input = inputs[model]; + + const handleModelChange = (value: ColorModel) => { + setModel(value); + }; return ( - - - - + + setSpace(value)}> + + + sRGB + Display P3 + + + +
+ + + + + + + + + HEX + RGB + HSL + HSB + + + + + ); }; + +type InputProps = { + color: RGBA; + onColorChange: (color: RGBA) => void; +}; + +const HexInput = (props: InputProps) => { + const { color, onColorChange } = props; + + return ( + + + + + + {}} + value={color.a} + min={0} + max={1} + targetRange={[0, 100]} + precision={2} + allowedUnits={['%']} + /> + % + + + ); +}; + +const RgbaInput = (props: InputProps) => { + const { color, onColorChange } = props; + + const onChange = (channel: 'r' | 'g' | 'b' | 'a') => (value: number) => { + onColorChange({ ...color, [channel]: value }); + }; + + return ( + + + + + + + + + + + + + % + + + ); +}; + +const HsvaInput = (props: InputProps) => { + const { color, onColorChange } = props; + const hsva = rgbaToHsva(color); + + const onChange = (channel: 'h' | 's' | 'v' | 'a') => (value: number) => { + onColorChange(hsvaToRgba({ ...hsva, [channel]: value })); + }; + + return ( + + + + + + + + + + + + + % + + + ); +}; + +const HslaInput = (props: InputProps) => { + const { color, onColorChange } = props; + const hsla = rgbaToHsla(color); + + const onChange = (channel: 'h' | 's' | 'l' | 'a') => (value: number) => { + onColorChange(hslaToRgba({ ...hsla, [channel]: value })); + }; + + return ( + + + + + + + + + + + + + % + + + ); +}; + +const inputs = { + hex: HexInput, + hsv: HsvaInput, + hsl: HslaInput, + rgb: RgbaInput, +};