Command palette

@yasinsensoy/native

React Native · Components

ColorPicker

Color picker (same API as ColorPicker on web).

30 exports

Live example

The preview runs in the browser with the web component; the native component takes the same props and looks the same on iOS and Android.

Preview

Click the color in the box or type: #f00, rgb(…), hsl(…), oklch(…), red.

Code

import { ColorField } from "@yasinsensoy/native";

<ColorField label="Brand color" name="renk" recent clearable
  defaultValue="#6366f1"
  onValueCommit={save} />

Events

  1. Events appear here when you interact with the preview.

Inline, OKLCH and opacity

Theme colors: a chosen var(--ui-*) changes with the theme

Readable text color (WCAG contrast)

Readable text4.7:1

ColorSwatch: for showing in tables and lists

Import

import {
  ColorPicker,
  ColorField,
  ColorSwatch,
  COLOR_FORMATS,
  rgbToHsv,
  THEME_SWATCHES,
  hsvToRgb,
  DEFAULT_SWATCHES,
  rgbToHsl,
  hslToRgb,
  rgbToOklch,
  oklchToRgb,
  resolveColor,
  keepHue,
  parseColor,
  isColor,
  detectColorFormat,
  formatColor,
  convertColor,
  luminance,
  contrastRatio,
  readableColor,
} from "@yasinsensoy/native";

ColorPicker

component

Color picker (same API as ColorPicker on web). Tapping the box opens a panel with a saturation/brightness area, hue and opacity sliders, a color input and swatches; on a phone it is a bottom sheet.

  • Formats: The value is returned in the format format (hex, rgb, hsl, oklch). The text field understands every format; the format button changes the display.
  • Theme: swatches="theme" offers theme colors; the picked var(--ui-*) value is returned as is (the same value as on web) and is drawn with each theme's own color.
  • Touch: Dragging is claimed as soon as the touch starts; a bottom sheet or scroll view does not interfere. With a screen reader the area and sliders are adjusted by swiping up/down.
  • Hue is preserved: The hue is not lost when going down to gray or black.
  • Form: Binds inside a Form with name.

Picking a color from the screen (eyedropper) on web is not available on mobile platforms.

ColorPicker(props: ColorPickerProps): ReactElement<unknown, string | JSXElementConstructor<any>>

Examples

<ColorPicker name="color" label="Color" defaultValue="#3b82f6" />
<ColorPicker value={color} onValueChange={setColor} format="oklch" alpha swatches="theme" recent />
<ColorPicker inline defaultValue="#22c55e" />

ColorField

component

ColorPicker with label, description and error (ColorField on web). On native every field is already drawn with a label/error frame, so it is the same as ColorPicker; it exists so code ported from web works unchanged.

ColorField(props: ColorFieldProps): JSX.Element

Examples

<ColorField label="Brand color" name="brand" defaultValue="#3b82f6" required />

ColorSwatch

component

Shows a color in a small box (ColorSwatch on web). Transparent colors are drawn over a checkerboard; an empty value is shown with a "no color" stripe. Theme colors are resolved against the active theme.

ColorSwatch({ color, size, shape, label, style, }: ColorSwatchProps): JSX.Element

Examples

<ColorSwatch color="#3b82f6" />
<ColorSwatch color="var(--ui-primary)" shape="circle" size="lg" label="Primary color" />

Supported formats, in picker order.

COLOR_FORMATS: readonly ColorFormat[]

rgbToHsv

function

RGB → HSV. Grays return hue 0; to keep the hue see keepHue.

rgbToHsv({ r, g, b, a }: Rgba): Hsva

Theme colors: swatches="theme" shows this list. The values are the same as on web (var(--ui-*)); on native resolveColor turns them into the active theme's colors.

THEME_SWATCHES: readonly ColorSwatchOption[]

hsvToRgb

function

HSV → RGB (fractional channels; rounded when written).

hsvToRgb({ h, s, v, a }: Hsva): Rgba

Default swatches (same as web): the palette's 500 shades, white and black.

DEFAULT_SWATCHES: readonly ColorSwatchOption[]

rgbToHsl

function

RGB → HSL (h 0–360, s/l 0–1).

rgbToHsl({ r, g, b }: Rgba): {
    h: number

hslToRgb

function

HSL → RGB.

hslToRgb(h: number, s: number, l: number, a?: number): Rgba

rgbToOklch

function

RGB → OKLCH (l 0–1, c ~0–0.4, h 0–360).

rgbToOklch(rgba: Rgba): {
    l: number

oklchToRgb

function

OKLCH → RGB.

oklchToRgb(l: number, c: number, h: number, a?: number): Rgba

resolveColor

function

Parses a color string. In addition to what parseColor understands, it also understands theme colors: var(--ui-primary) (the same notation as on web) or just the name ("primary", "muted-foreground"). These are turned into the given theme's color.

resolveColor(input: string, colors?: ThemeColors): Rgba | undefined

keepHue

function

Keeps the hue (and saturation on black) from the previous value when the new HSV value loses it. A picker dragged to gray or black does not jump to red.

keepHue(next: Hsva, previous: Hsva | undefined): Hsva

parseColor

function

Parses a color string. Supported forms: #rgb, #rgba, #rrggbb, #rrggbbaa (with or without #), rgb()/rgba(), hsl()/hsla(), hwb(), oklch(), oklab(), color(srgb …) (comma and space separated, with percentage and angle units) and transparent.

For forms the browser has to resolve, such as names (red) and theme variables (var(--ui-primary)), see resolveCssColor.

parseColor(input: string): Rgba | undefined

Examples

parseColor("#3b82f6");            // { r: 59, g: 130, b: 246, a: 1 }
parseColor("hsl(217 91% 60% / .5)");
parseColor("oklch(0.62 0.19 260)");

isColor

function

Is the string a valid color? See parseColor.

isColor(input: string): boolean

Format the string is written in; undefined when unknown.

detectColorFormat(input: string): ColorFormat | undefined

formatColor

function

Writes a color in the requested format. See ColorFormat.

formatColor(color: Rgba, format?: ColorFormat, options?: FormatColorOptions): string

Examples

formatColor({ r: 59, g: 130, b: 246, a: 1 }, "hex");   // "#3b82f6"
formatColor({ r: 59, g: 130, b: 246, a: 0.5 }, "rgb"); // "rgb(59 130 246 / 0.5)"

convertColor

function

Converts a string to another format; undefined when it cannot be parsed.

convertColor(input: string, format: ColorFormat, options?: FormatColorOptions): string | undefined

Examples

`convertColor("#ff0000", "oklch") // "oklch(0.628 0.258 29.2)"`

luminance

function

WCAG relative luminance (0 black – 1 white).

luminance(color: Rgba): number

WCAG contrast ratio of two colors (1–21). At least 4.5 is recommended for normal text, 3 for large text. 1 when a color cannot be parsed.

contrastRatio(a: string | Rgba, b: string | Rgba): number

Examples

`contrastRatio("#ffffff", "#3b82f6") // 3.68`

A readable text color on a color background: "#000000" or "#ffffff" (whichever has the higher contrast).

readableColor(background: string | Rgba): "#000000" | "#ffffff"

Rgba

interface

sRGB color: channels 0–255 (may be fractional), alpha 0–1. See parseColor, formatColor.

NameTypeDescription
rnumberRed, 0–255.
gnumberGreen, 0–255.
bnumberBlue, 0–255.
anumberAlpha: 0 fully transparent, 1 fully opaque.

A color in the swatch list.

NameTypeDescription
valuestringColor string: #3b82f6, oklch(0.62 0.19 260) or a theme color var(--ui-primary). When picked, the value is returned as is; theme colors follow the theme when it changes (the same values as on web).
label?stringScreen reader name. Defaults to name's translation, then value.
name?ColorNameA built-in color name; its text comes from the colorNames messages (used by THEME_SWATCHES and DEFAULT_SWATCHES).

Hsva

interface

HSV (hue, saturation, value) color: the color picker's internal model. h 0–360, s and v 0–1, a 0–1.

NameTypeDescription
hnumberHue, 0–360 degrees.
snumberSaturation, 0–1.
vnumberValue (brightness), 0–1.
anumberAlpha, 0–1.

Format of a color string.

  • "hex": #3b82f6 (with alpha #3b82f680)
  • "rgb": rgb(59 130 246) (with alpha rgb(59 130 246 / 0.5))
  • "hsl": hsl(217 91% 60%)
  • "oklch": oklch(0.623 0.188 259.8): perceptually even steps; the library's themes use this format.
type ColorFormat = "hex" | "rgb" | "hsl" | "oklch"

formatColor options.

NameTypeDefaultDescription
alpha?boolean | "auto""auto"Write the alpha? "auto" writes it only when the color is transparent; false drops the alpha.

Props of ColorPicker (same names as on web).

Extends: FieldProps

NameTypeDefaultDescription
value?stringColor in controlled use; "" when there is no color.
defaultValue?stringInitial color in uncontrolled use.
onValueChange?(value: string) => voidWhen the color changes; on every step while dragging. The value is in the format format; theme colors from a swatch (var(--ui-*)) are returned as is.
onValueCommit?(value: string) => voidWhen a selection is complete: when dragging ends, a swatch is picked or a typed color is confirmed. Use this to save to a server.
format?ColorFormat"hex"Format of the returned value.
alpha?booleanfalseShow an opacity slider and include it in the value?
formats?readonly ColorFormat[]["hex", "rgb", "hsl", "oklch"]Formats the format button in the text field cycles through. With a single format the button is hidden.
swatches?readonly (string | ColorSwatchOption)[] | "theme" | falseSwatches: an array of strings or { value, label }; "theme" for theme colors (THEME_SWATCHES); false hides them. Defaults to DEFAULT_SWATCHES.
recent?boolean | stringfalseRemember recently picked colors (at most 8, stored on the device). true shares one list across all pickers; a string keeps a separate list under that key.
inline?booleanfalseShow the picker inline in the page instead of a popover.
placeholder?string"Renk seç"Text shown in the box when empty.
clearable?booleantrueClear button.
readOnly?booleanRead-only: the color is visible but cannot be changed.
open?booleanWhether the panel is open in controlled use.
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
variant?FieldVariant"outline"Field appearance.
aria-label?stringScreen reader name; give it when there is no visible label.
style?StyleProp<ViewStyle>Container style.

Props of ColorField: the same as ColorPickerProps.

type ColorFieldProps = ColorPickerProps

Extends: FieldProps

NameTypeDefaultDescription
value?stringColor in controlled use; "" when there is no color.
defaultValue?stringInitial color in uncontrolled use.
onValueChange?(value: string) => voidWhen the color changes; on every step while dragging. The value is in the format format; theme colors from a swatch (var(--ui-*)) are returned as is.
onValueCommit?(value: string) => voidWhen a selection is complete: when dragging ends, a swatch is picked or a typed color is confirmed. Use this to save to a server.
format?ColorFormat"hex"Format of the returned value.
alpha?booleanfalseShow an opacity slider and include it in the value?
formats?readonly ColorFormat[]["hex", "rgb", "hsl", "oklch"]Formats the format button in the text field cycles through. With a single format the button is hidden.
swatches?readonly (string | ColorSwatchOption)[] | "theme" | falseSwatches: an array of strings or { value, label }; "theme" for theme colors (THEME_SWATCHES); false hides them. Defaults to DEFAULT_SWATCHES.
recent?boolean | stringfalseRemember recently picked colors (at most 8, stored on the device). true shares one list across all pickers; a string keeps a separate list under that key.
inline?booleanfalseShow the picker inline in the page instead of a popover.
placeholder?string"Renk seç"Text shown in the box when empty.
clearable?booleantrueClear button.
readOnly?booleanRead-only: the color is visible but cannot be changed.
open?booleanWhether the panel is open in controlled use.
onOpenChange?(open: boolean) => voidWhen the panel opens or closes.
variant?FieldVariant"outline"Field appearance.
aria-label?stringScreen reader name; give it when there is no visible label.
style?StyleProp<ViewStyle>Container style.

Props of ColorSwatch.

NameTypeDefaultDescription
colorstring | undefinedColor to show (any format, or a theme color like var(--ui-primary)). When empty, a diagonally striped "no color" box is drawn.
size?"sm" | "md" | "lg""md"` (20 px)Size.
shape?"rounded" | "circle" | "square""rounded"Corner shape.
label?stringScreen reader name. If omitted it is decorative (screen readers skip it).
style?StyleProp<ViewStyle>Container style.