@mrpelz/uikit-ish 7.9.0 → 7.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/components/action-sheet/main.d.ts +1 -2
- package/dist/components/action-sheet/main.d.ts.map +1 -1
- package/dist/components/action-sheet/main.js +12 -12
- package/dist/components/action-sheet/main.js.map +1 -1
- package/dist/components/action-sheet/props.d.ts +2 -2
- package/dist/components/action-sheet/props.d.ts.map +1 -1
- package/dist/components/action-sheet/style.css +10 -1
- package/dist/components/activity-view/main.d.ts +1 -1
- package/dist/components/activity-view/main.d.ts.map +1 -1
- package/dist/components/activity-view/main.js +9 -11
- package/dist/components/activity-view/main.js.map +1 -1
- package/dist/components/activity-view/style.css +21 -21
- package/dist/components/alert/main.d.ts +2 -3
- package/dist/components/alert/main.d.ts.map +1 -1
- package/dist/components/alert/main.js +12 -15
- package/dist/components/alert/main.js.map +1 -1
- package/dist/components/alert/props.d.ts +3 -3
- package/dist/components/alert/style.css +23 -23
- package/dist/components/avatar/main.d.ts.map +1 -1
- package/dist/components/avatar/main.js +7 -8
- package/dist/components/avatar/main.js.map +1 -1
- package/dist/components/avatar/style.css +1 -1
- package/dist/components/badge/main.d.ts +1 -1
- package/dist/components/badge/main.js +4 -4
- package/dist/components/badge/main.js.map +1 -1
- package/dist/components/badge/style.css +7 -2
- package/dist/components/button/main.d.ts.map +1 -1
- package/dist/components/button/main.js +9 -9
- package/dist/components/button/main.js.map +1 -1
- package/dist/components/button/props.d.ts +8 -8
- package/dist/components/button/props.d.ts.map +1 -1
- package/dist/components/button/style.css +27 -29
- package/dist/components/checkbox/main.d.ts.map +1 -1
- package/dist/components/checkbox/main.js +5 -7
- package/dist/components/checkbox/main.js.map +1 -1
- package/dist/components/checkbox/style.css +3 -3
- package/dist/components/color-picker/color.d.ts +23 -0
- package/dist/components/color-picker/color.d.ts.map +1 -0
- package/dist/components/color-picker/color.js +150 -0
- package/dist/components/color-picker/color.js.map +1 -0
- package/dist/components/color-picker/main.d.ts +41 -5
- package/dist/components/color-picker/main.d.ts.map +1 -1
- package/dist/components/color-picker/main.js +633 -99
- package/dist/components/color-picker/main.js.map +1 -1
- package/dist/components/color-picker/named-colors.d.ts +3 -0
- package/dist/components/color-picker/named-colors.d.ts.map +1 -0
- package/dist/components/color-picker/named-colors.js +152 -0
- package/dist/components/color-picker/named-colors.js.map +1 -0
- package/dist/components/color-picker/props.d.ts +97 -14
- package/dist/components/color-picker/props.d.ts.map +1 -1
- package/dist/components/color-picker/style.css +316 -50
- package/dist/components/contextual-menu/main.d.ts.map +1 -1
- package/dist/components/contextual-menu/main.js +189 -236
- package/dist/components/contextual-menu/main.js.map +1 -1
- package/dist/components/contextual-menu/props.d.ts +10 -33
- package/dist/components/contextual-menu/props.d.ts.map +1 -1
- package/dist/components/contextual-menu/style.css +60 -34
- package/dist/components/date-picker/main.d.ts.map +1 -1
- package/dist/components/date-picker/main.js +100 -66
- package/dist/components/date-picker/main.js.map +1 -1
- package/dist/components/date-picker/props.d.ts +10 -10
- package/dist/components/date-picker/props.d.ts.map +1 -1
- package/dist/components/date-picker/style.css +51 -47
- package/dist/components/drawer/main.d.ts.map +1 -1
- package/dist/components/drawer/main.js +52 -7
- package/dist/components/drawer/main.js.map +1 -1
- package/dist/components/drawer/style.css +1 -1
- package/dist/components/edit-menu/main.d.ts.map +1 -1
- package/dist/components/edit-menu/main.js +41 -30
- package/dist/components/edit-menu/main.js.map +1 -1
- package/dist/components/edit-menu/props.d.ts +4 -4
- package/dist/components/edit-menu/props.d.ts.map +1 -1
- package/dist/components/edit-menu/style.css +27 -1
- package/dist/components/form/main.js +3 -3
- package/dist/components/form/main.js.map +1 -1
- package/dist/components/glass/main.d.ts +1 -1
- package/dist/components/glass/main.js +4 -4
- package/dist/components/glass/main.js.map +1 -1
- package/dist/components/glass/props.d.ts +5 -5
- package/dist/components/glass/props.d.ts.map +1 -1
- package/dist/components/glass/style.css +157 -157
- package/dist/components/header/main.d.ts.map +1 -1
- package/dist/components/header/main.js +27 -26
- package/dist/components/header/main.js.map +1 -1
- package/dist/components/header/style.css +13 -52
- package/dist/components/index.d.ts +6 -6
- package/dist/components/index.d.ts.map +1 -1
- package/dist/components/index.js +2 -2
- package/dist/components/index.js.map +1 -1
- package/dist/components/layout/main.js +5 -5
- package/dist/components/layout/main.js.map +1 -1
- package/dist/components/layout/style.css +2 -2
- package/dist/components/list/main.d.ts +11 -3
- package/dist/components/list/main.d.ts.map +1 -1
- package/dist/components/list/main.js +296 -300
- package/dist/components/list/main.js.map +1 -1
- package/dist/components/list/props.d.ts +49 -16
- package/dist/components/list/props.d.ts.map +1 -1
- package/dist/components/list/reorder.d.ts +146 -0
- package/dist/components/list/reorder.d.ts.map +1 -0
- package/dist/components/list/reorder.js +819 -0
- package/dist/components/list/reorder.js.map +1 -0
- package/dist/components/list/select.d.ts +6 -0
- package/dist/components/list/select.d.ts.map +1 -0
- package/dist/components/list/select.js +141 -0
- package/dist/components/list/select.js.map +1 -0
- package/dist/components/list/style.css +206 -158
- package/dist/components/menu/main.d.ts +81 -30
- package/dist/components/menu/main.d.ts.map +1 -1
- package/dist/components/menu/main.js +568 -93
- package/dist/components/menu/main.js.map +1 -1
- package/dist/components/menu/props.d.ts +3 -4
- package/dist/components/menu/props.d.ts.map +1 -1
- package/dist/components/menu/style.css +30 -12
- package/dist/components/navigation/main.d.ts +5 -2
- package/dist/components/navigation/main.d.ts.map +1 -1
- package/dist/components/navigation/main.js +287 -57
- package/dist/components/navigation/main.js.map +1 -1
- package/dist/components/navigation/props.d.ts +37 -4
- package/dist/components/navigation/props.d.ts.map +1 -1
- package/dist/components/navigation/style.css +32 -2
- package/dist/components/page/main.d.ts.map +1 -1
- package/dist/components/page/main.js +12 -7
- package/dist/components/page/main.js.map +1 -1
- package/dist/components/page/props.d.ts +1 -1
- package/dist/components/page/style.css +3 -6
- package/dist/components/page-control/main.d.ts +1 -1
- package/dist/components/page-control/main.js +9 -9
- package/dist/components/page-control/main.js.map +1 -1
- package/dist/components/page-control/style.css +2 -2
- package/dist/components/popover/main.d.ts +1 -1
- package/dist/components/popover/main.d.ts.map +1 -1
- package/dist/components/popover/main.js +41 -17
- package/dist/components/popover/main.js.map +1 -1
- package/dist/components/popover/props.d.ts +2 -2
- package/dist/components/popover/style.css +10 -1
- package/dist/components/progress/main.d.ts +1 -2
- package/dist/components/progress/main.d.ts.map +1 -1
- package/dist/components/progress/main.js +45 -19
- package/dist/components/progress/main.js.map +1 -1
- package/dist/components/progress/props.d.ts +22 -8
- package/dist/components/progress/props.d.ts.map +1 -1
- package/dist/components/progress/style.css +15 -15
- package/dist/components/roll/main.js +10 -10
- package/dist/components/roll/main.js.map +1 -1
- package/dist/components/roll/props.d.ts +1 -1
- package/dist/components/roll/style.css +11 -11
- package/dist/components/segmented/main.d.ts.map +1 -1
- package/dist/components/segmented/main.js +183 -31
- package/dist/components/segmented/main.js.map +1 -1
- package/dist/components/segmented/props.d.ts +7 -0
- package/dist/components/segmented/props.d.ts.map +1 -1
- package/dist/components/segmented/style.css +148 -14
- package/dist/components/sheet/main.d.ts +6 -5
- package/dist/components/sheet/main.d.ts.map +1 -1
- package/dist/components/sheet/main.js +53 -27
- package/dist/components/sheet/main.js.map +1 -1
- package/dist/components/sheet/props.d.ts +29 -1
- package/dist/components/sheet/props.d.ts.map +1 -1
- package/dist/components/sheet/style.css +138 -33
- package/dist/components/sidebar/main.d.ts.map +1 -1
- package/dist/components/sidebar/main.js +9 -13
- package/dist/components/sidebar/main.js.map +1 -1
- package/dist/components/sidebar/style.css +8 -8
- package/dist/components/slider/main.d.ts.map +1 -1
- package/dist/components/slider/main.js +19 -19
- package/dist/components/slider/main.js.map +1 -1
- package/dist/components/slider/props.d.ts +1 -2
- package/dist/components/slider/props.d.ts.map +1 -1
- package/dist/components/slider/style.css +25 -25
- package/dist/components/stepper/main.d.ts.map +1 -1
- package/dist/components/stepper/main.js +13 -15
- package/dist/components/stepper/main.js.map +1 -1
- package/dist/components/stepper/style.css +8 -8
- package/dist/components/switch/main.d.ts.map +1 -1
- package/dist/components/switch/main.js +25 -18
- package/dist/components/switch/main.js.map +1 -1
- package/dist/components/switch/props.d.ts +5 -3
- package/dist/components/switch/props.d.ts.map +1 -1
- package/dist/components/switch/style.css +13 -17
- package/dist/components/symbol/main.d.ts +1 -1
- package/dist/components/symbol/main.d.ts.map +1 -1
- package/dist/components/symbol/main.js +6 -5
- package/dist/components/symbol/main.js.map +1 -1
- package/dist/components/symbol/props.d.ts +3 -3
- package/dist/components/symbol/style.css +1 -1
- package/dist/components/tab-bar/main.d.ts +2 -2
- package/dist/components/tab-bar/main.d.ts.map +1 -1
- package/dist/components/tab-bar/main.js +78 -59
- package/dist/components/tab-bar/main.js.map +1 -1
- package/dist/components/tab-bar/props.d.ts +3 -3
- package/dist/components/tab-bar/style.css +92 -56
- package/dist/components/text-field/main.d.ts +3 -2
- package/dist/components/text-field/main.d.ts.map +1 -1
- package/dist/components/text-field/main.js +26 -28
- package/dist/components/text-field/main.js.map +1 -1
- package/dist/components/text-field/props.d.ts +16 -1
- package/dist/components/text-field/props.d.ts.map +1 -1
- package/dist/components/text-field/style.css +105 -31
- package/dist/components/toolbar/main.d.ts.map +1 -1
- package/dist/components/toolbar/main.js +18 -20
- package/dist/components/toolbar/main.js.map +1 -1
- package/dist/components/toolbar/props.d.ts +10 -4
- package/dist/components/toolbar/props.d.ts.map +1 -1
- package/dist/components/toolbar/style.css +13 -13
- package/dist/components/tooltip/main.js +4 -4
- package/dist/components/tooltip/main.js.map +1 -1
- package/dist/components/tooltip/style.css +5 -5
- package/dist/components/typography/main.d.ts +2 -2
- package/dist/components/typography/main.d.ts.map +1 -1
- package/dist/components/typography/main.js +10 -12
- package/dist/components/typography/main.js.map +1 -1
- package/dist/components/typography/props.d.ts +9 -9
- package/dist/components/typography/props.d.ts.map +1 -1
- package/dist/components/typography/style.css +503 -503
- package/dist/components/utils/main.d.ts +52 -6
- package/dist/components/utils/main.d.ts.map +1 -1
- package/dist/components/utils/main.js +163 -6
- package/dist/components/utils/main.js.map +1 -1
- package/dist/components/utils/props.d.ts +24 -3
- package/dist/components/utils/props.d.ts.map +1 -1
- package/dist/components/utils/style.css +4 -4
- package/dist/components/viewport/edge-effect.d.ts +10 -0
- package/dist/components/viewport/edge-effect.d.ts.map +1 -0
- package/dist/components/viewport/edge-effect.js +100 -0
- package/dist/components/viewport/edge-effect.js.map +1 -0
- package/dist/components/viewport/edge.d.ts +39 -0
- package/dist/components/viewport/edge.d.ts.map +1 -0
- package/dist/components/viewport/edge.js +151 -0
- package/dist/components/viewport/edge.js.map +1 -0
- package/dist/components/viewport/input.d.ts +19 -3
- package/dist/components/viewport/input.d.ts.map +1 -1
- package/dist/components/viewport/input.js +197 -12
- package/dist/components/viewport/input.js.map +1 -1
- package/dist/components/viewport/main.d.ts +10 -5
- package/dist/components/viewport/main.d.ts.map +1 -1
- package/dist/components/viewport/main.js +327 -90
- package/dist/components/viewport/main.js.map +1 -1
- package/dist/components/viewport/padding.d.ts +31 -3
- package/dist/components/viewport/padding.d.ts.map +1 -1
- package/dist/components/viewport/padding.js +116 -17
- package/dist/components/viewport/padding.js.map +1 -1
- package/dist/components/viewport/props.d.ts +69 -8
- package/dist/components/viewport/props.d.ts.map +1 -1
- package/dist/components/viewport/style.css +230 -6
- package/dist/styles.css +285 -302
- package/package.json +4 -4
|
@@ -1,42 +1,54 @@
|
|
|
1
1
|
import { jsx as _jsx, jsxs as _jsxs, Fragment as _Fragment } from "react/jsx-runtime";
|
|
2
2
|
import '../button/style.css';
|
|
3
|
+
import '../list/style.css';
|
|
3
4
|
import '../menu/style.css';
|
|
4
5
|
import '../page-control/style.css';
|
|
5
6
|
import '../segmented/style.css';
|
|
6
|
-
import '../slider/style.css';
|
|
7
7
|
import './style.css';
|
|
8
8
|
import { Slider as BaseSlider } from '@base-ui/react/slider';
|
|
9
|
-
import {
|
|
9
|
+
import { useRender } from '@base-ui/react/use-render';
|
|
10
|
+
import { Children, cloneElement, createContext, forwardRef, Fragment, isValidElement, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState, } from 'react';
|
|
10
11
|
import { Button } from '../button/main.js';
|
|
12
|
+
import { List } from '../list/main.js';
|
|
11
13
|
import { Menu } from '../menu/main.js';
|
|
12
14
|
import { PageControl } from '../page-control/main.js';
|
|
13
15
|
import { Segmented } from '../segmented/main.js';
|
|
14
|
-
import {
|
|
15
|
-
import {
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
//
|
|
16
|
+
import { Checkmark, cn, useForwardedRef, useTouchHold, withStyle, } from '../utils/main.js';
|
|
17
|
+
import { Viewport } from '../viewport/main.js';
|
|
18
|
+
import { codeGroups, parseCssColor } from './color.js';
|
|
19
|
+
import { cssNamedColors } from './named-colors.js';
|
|
20
|
+
// A color picker and a color well, in the app's popover (a regular width)
|
|
21
|
+
// or sheet (compact), its header the app's (a `Toolbar`: the
|
|
19
22
|
// eyedropper, “Colors”, close):
|
|
20
23
|
// - a segmented control (Grid, Spectrum, Sliders), 24px above the view;
|
|
21
24
|
// - Grid: 12 by 10 swatches (a gray row, then hues by lightness), its
|
|
22
25
|
// corners 18px, the selected one outlined white (3px);
|
|
23
26
|
// - Spectrum: hues across, light to dark down (white at the top, pure hues
|
|
24
27
|
// halfway, black at the bottom), its corners 18px, the color ringed;
|
|
25
|
-
// - Sliders: red, green and blue (
|
|
26
|
-
//
|
|
27
|
-
//
|
|
28
|
-
//
|
|
29
|
-
//
|
|
28
|
+
// - Sliders: red, green and blue (0 to 255; or 0 to 100%, see `Sliders`),
|
|
29
|
+
// each a channel's row (see `ChannelRow`): its title (all caps), a slider
|
|
30
|
+
// mixing from the color (the channel along its range), and its value,
|
|
31
|
+
// typed;
|
|
32
|
+
// - Each channel's row kept above the keyboard as its field's typed into
|
|
33
|
+
// (a `Viewport.KeyboardAccessory`; in a `Viewport.Provider`);
|
|
34
|
+
// - Opacity (optional: its part, `Opacity`), a channel's row too: the
|
|
35
|
+
// color fading in over a checkerboard, its knob the color (translucent),
|
|
36
|
+
// its value a percentage;
|
|
30
37
|
// - the color (82px, 18px corners) beside the saved colors (30px dots, 25px
|
|
31
38
|
// apart, rows 22px apart; the color's ringed white, 2px, 4px in) and an
|
|
32
39
|
// add button (on the system fill, its “+” the label's color).
|
|
33
40
|
// A color well (`ColorPicker.Well`): a 28px (or 36px) disk of hues around
|
|
34
41
|
// white; with a color, the hues as a ring around it.
|
|
35
42
|
//
|
|
36
|
-
//
|
|
37
|
-
// - Colors are hex strings (`#rrggbb`, `#rrggbbaa` with opacity
|
|
43
|
+
// Also:
|
|
44
|
+
// - Colors are hex strings (`#rrggbb`, `#rrggbbaa` with opacity; without
|
|
45
|
+
// opacity, always opaque).
|
|
38
46
|
// - The eyedropper (`ColorPicker.Eyedropper`) is the browser's
|
|
39
47
|
// (`EyeDropper`; not where it has none, e.g. Safari).
|
|
48
|
+
// - The system's color picker (`ColorPicker.SystemColors`) is a color
|
|
49
|
+
// input's (over its button, pressed itself: on iOS, nothing else opens
|
|
50
|
+
// it; with the opacity rendered, its alpha too where the input has one,
|
|
51
|
+
// e.g. Safari; else its colors opaque, the opacity kept).
|
|
40
52
|
// - Saved colors are deleted by their menu (a secondary click: right click,
|
|
41
53
|
// long press).
|
|
42
54
|
// eslint-disable-next-line unicorn/no-unnecessary-parameters
|
|
@@ -62,7 +74,6 @@ const toHex = (value) => Math.round(clamp(value, 0, 255))
|
|
|
62
74
|
/** Channels as a hex color (#rrggbb, or #rrggbbaa if not opaque). */
|
|
63
75
|
export const formatColor = ({ a, b, g, r }) => `#${toHex(r)}${toHex(g)}${toHex(b)}${a < 1 ? toHex(a * 255) : ''}`.toUpperCase();
|
|
64
76
|
// HSL (hue 0–360, saturation and lightness 0–1), for the spectrum.
|
|
65
|
-
// eslint-disable-next-line unicorn/no-unnecessary-parameters
|
|
66
77
|
const fromHsl = (h, s, l) => {
|
|
67
78
|
const k = (n) => (n + h / 30) % 12;
|
|
68
79
|
const amount = s * Math.min(l, 1 - l);
|
|
@@ -87,7 +98,7 @@ const toHsl = ({ b, g, r }) => {
|
|
|
87
98
|
h = (red - green) / d + 4;
|
|
88
99
|
return { h: (h * 60 + 360) % 360, l, s };
|
|
89
100
|
};
|
|
90
|
-
/**
|
|
101
|
+
/** The grid: a gray row, then hues by lightness. */
|
|
91
102
|
const grid = [
|
|
92
103
|
'#FEFFFE #EBEBEB #D6D6D6 #C2C2C2 #ADADAD #999999 #858585 #707070 #5C5C5C #474747 #333333 #000000',
|
|
93
104
|
'#00374A #011D57 #11053B #2E063D #3C071B #5C0701 #5A1C00 #583300 #563D00 #666100 #4F5504 #263E0F',
|
|
@@ -100,7 +111,7 @@ const grid = [
|
|
|
100
111
|
'#93E3FC #A7C6FF #B18CFE #E292FE #F4A4C0 #FFB5AF #FFC5AB #FED9A8 #FDE4A8 #FFFBB9 #F1F7B7 #CDE8B5',
|
|
101
112
|
'#CBF0FF #D2E2FE #D8C9FE #EFCAFE #F9D3E0 #FFDAD8 #FFE2D6 #FEECD4 #FEF1D5 #FDFBDD #F6FADB #DEEED4',
|
|
102
113
|
].map((row) => row.split(' '));
|
|
103
|
-
/**
|
|
114
|
+
/** The default saved colors. */
|
|
104
115
|
export const defaultSavedColors = [
|
|
105
116
|
'#000000',
|
|
106
117
|
'#007AFF',
|
|
@@ -115,22 +126,49 @@ export const defaultSavedColors = [
|
|
|
115
126
|
const sameColor = (a, b) => Math.round(a.r) === Math.round(b.r) &&
|
|
116
127
|
Math.round(a.g) === Math.round(b.g) &&
|
|
117
128
|
Math.round(a.b) === Math.round(b.b);
|
|
129
|
+
const unregister = () => undefined;
|
|
118
130
|
const PickerContext = createContext({
|
|
119
131
|
color: { a: 1, b: 0, g: 0, r: 0 },
|
|
132
|
+
hasOpacity: false,
|
|
133
|
+
registerOpacity: () => unregister,
|
|
120
134
|
setColor: () => undefined,
|
|
121
135
|
});
|
|
122
136
|
/**
|
|
123
|
-
*
|
|
124
|
-
*
|
|
137
|
+
* Its modes (see `Root`): the one shown, chosen by its `Modes`; the first
|
|
138
|
+
* `Mode` (or `View`) unless a default is given.
|
|
139
|
+
*/
|
|
140
|
+
const PanelContext = createContext({
|
|
141
|
+
isGridHeight: false,
|
|
142
|
+
mode: null,
|
|
143
|
+
registerGridHeight: () => unregister,
|
|
144
|
+
registerMode: () => undefined,
|
|
145
|
+
setMode: () => undefined,
|
|
146
|
+
});
|
|
147
|
+
/**
|
|
148
|
+
* A color picker: its color and its mode, for its parts (`Header`,
|
|
149
|
+
* `Modes`, `Panel`, `Eyedropper`, anywhere in it), in the app's popover or
|
|
150
|
+
* sheet. Its element (`render`) may be the sheet's popup, so its modes'
|
|
151
|
+
* control can be in the sheet's header, not scrolling.
|
|
125
152
|
*/
|
|
126
|
-
const Root = forwardRef(({ className, defaultValue = '#000000', onValueChange, value, ...props }, ref) => {
|
|
153
|
+
const Root = forwardRef(({ className, defaultMode, defaultValue = '#000000', mode: modeProp, onModeChange, onValueChange, render, style, value, ...props }, ref) => {
|
|
127
154
|
const [own, setOwn] = useState(defaultValue);
|
|
128
|
-
|
|
155
|
+
// Its `Opacity`s rendered: without one, the color opaque (its alpha,
|
|
156
|
+
// if given one, dropped).
|
|
157
|
+
const [opacities, setOpacities] = useState(0);
|
|
158
|
+
const hasOpacity = opacities > 0;
|
|
159
|
+
const parsed = parseColor(value ?? own);
|
|
160
|
+
const color = hasOpacity ? parsed : { ...parsed, a: 1 };
|
|
161
|
+
const registerOpacity = useCallback(() => {
|
|
162
|
+
setOpacities((count) => count + 1);
|
|
163
|
+
return () => setOpacities((count) => count - 1);
|
|
164
|
+
}, []);
|
|
129
165
|
const state = useMemo(() => ({
|
|
130
166
|
color,
|
|
167
|
+
hasOpacity,
|
|
168
|
+
registerOpacity,
|
|
131
169
|
// Uncontrolled: its own state; controlled, only the app's `value`.
|
|
132
170
|
setColor: (next, details) => {
|
|
133
|
-
const hex = formatColor(next);
|
|
171
|
+
const hex = formatColor(hasOpacity ? next : { ...next, a: 1 });
|
|
134
172
|
if (value === undefined)
|
|
135
173
|
setOwn(hex);
|
|
136
174
|
onValueChange?.(hex, details);
|
|
@@ -138,17 +176,86 @@ const Root = forwardRef(({ className, defaultValue = '#000000', onValueChange, v
|
|
|
138
176
|
}),
|
|
139
177
|
// (By its hex value.)
|
|
140
178
|
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
141
|
-
[value ?? own, onValueChange]);
|
|
142
|
-
|
|
179
|
+
[value ?? own, onValueChange, hasOpacity]);
|
|
180
|
+
const [ownMode, setOwnMode] = useState(defaultMode);
|
|
181
|
+
// (Without a default: the first mode, as it's rendered.)
|
|
182
|
+
const [firstMode, setFirstMode] = useState();
|
|
183
|
+
// (Controlled from the start: none yet, before its first mode is
|
|
184
|
+
// known, not undefined, which would leave its control uncontrolled.)
|
|
185
|
+
const mode = modeProp ?? ownMode ?? firstMode ?? null;
|
|
186
|
+
// Its views of the grid's height (a `Grid`, a `Spectrum`): with one,
|
|
187
|
+
// all as high (see `View`).
|
|
188
|
+
const [gridHeights, setGridHeights] = useState(0);
|
|
189
|
+
const registerGridHeight = useCallback(() => {
|
|
190
|
+
setGridHeights((count) => count + 1);
|
|
191
|
+
return () => setGridHeights((count) => count - 1);
|
|
192
|
+
}, []);
|
|
193
|
+
const modes = useMemo(() => ({
|
|
194
|
+
isGridHeight: gridHeights > 0,
|
|
195
|
+
mode,
|
|
196
|
+
registerGridHeight,
|
|
197
|
+
registerMode: (next) => setFirstMode((first) => first ?? next),
|
|
198
|
+
// Uncontrolled: its own state; controlled, only the app's `mode`.
|
|
199
|
+
setMode: (next, details) => {
|
|
200
|
+
if (modeProp === undefined)
|
|
201
|
+
setOwnMode(next);
|
|
202
|
+
onModeChange?.(next, details);
|
|
203
|
+
},
|
|
204
|
+
}), [gridHeights, mode, modeProp, onModeChange, registerGridHeight]);
|
|
205
|
+
const element = useRender({
|
|
206
|
+
defaultTagName: 'div',
|
|
207
|
+
props: {
|
|
208
|
+
style: withStyle({
|
|
209
|
+
// (For its parts.)
|
|
210
|
+
'--uikit-color-picker-color': formatColor(color),
|
|
211
|
+
'--uikit-color-picker-color-opaque': formatColor({
|
|
212
|
+
...color,
|
|
213
|
+
a: 1,
|
|
214
|
+
}),
|
|
215
|
+
}, style),
|
|
216
|
+
...props,
|
|
217
|
+
className: cn('uikit-color-picker-root', className),
|
|
218
|
+
'data-color-picker-part': 'root',
|
|
219
|
+
},
|
|
220
|
+
ref,
|
|
221
|
+
render,
|
|
222
|
+
});
|
|
223
|
+
return (_jsx(PickerContext.Provider, { value: state, children: _jsx(PanelContext.Provider, { value: modes, children: element }) }));
|
|
224
|
+
});
|
|
225
|
+
/**
|
|
226
|
+
* Its panel: its modes' control (`Modes`, unless elsewhere, e.g. a sheet's
|
|
227
|
+
* header) and views (a `View` each, paired with its `Mode` by their
|
|
228
|
+
* `value`s), the opacity, the color and the saved colors, as composed (in
|
|
229
|
+
* its padding, its parts spaced).
|
|
230
|
+
*/
|
|
231
|
+
const Panel = forwardRef(({ className, ...props }, ref) => (_jsx("div", { ref: ref, ...props, className: cn('uikit-color-picker-panel', className), "data-color-picker-part": "panel" })));
|
|
232
|
+
/**
|
|
233
|
+
* Its modes' control: a segmented control (its `Mode`s, its highlight),
|
|
234
|
+
* choosing the `View` shown; 24px above it (in its panel, or at the end of
|
|
235
|
+
* a header).
|
|
236
|
+
*/
|
|
237
|
+
const Modes = forwardRef(({ children, ...props }, ref) => {
|
|
238
|
+
const { mode, setMode } = useContext(PanelContext);
|
|
239
|
+
return (_jsxs(Segmented.Root, { ref: ref, "aria-label": "Mode",
|
|
240
|
+
// (Its modes, however many, scrolled when they don't fit.)
|
|
241
|
+
overflow: "scroll", ...props, className: cn('uikit-color-picker-modes', props.className), value: mode, onValueChange: (next, details) => setMode(String(next), details), children: [children, _jsx(Segmented.Highlight, {})] }));
|
|
242
|
+
});
|
|
243
|
+
/** A mode, in its `Modes`: its label, choosing its `View` (by `value`). */
|
|
244
|
+
const Mode = forwardRef(({ value, ...props }, ref) => {
|
|
245
|
+
const { registerMode } = useContext(PanelContext);
|
|
246
|
+
useLayoutEffect(() => registerMode(value), [registerMode, value]);
|
|
247
|
+
return (_jsx(Segmented.Item, { ref: ref, value: value, ...props }));
|
|
143
248
|
});
|
|
144
|
-
|
|
249
|
+
/** Swatches in rows (a gray row, then hues by lightness), its corners 18px. */
|
|
250
|
+
const Grid = forwardRef(({ className, ...props }, ref) => {
|
|
145
251
|
const { color, setColor } = useContext(PickerContext);
|
|
146
|
-
return (_jsx("div", { "aria-label": "Grid", className:
|
|
252
|
+
return (_jsx("div", { ref: ref, "aria-label": "Grid", role: "group", ...props, className: cn('uikit-color-picker-grid', className), "data-color-picker-part": "grid", children: grid.flatMap((row) => row.map((hex) => {
|
|
147
253
|
const swatch = parseColor(hex);
|
|
148
254
|
return (_jsx("button", { "aria-label": hex, "aria-pressed": sameColor(swatch, color), style: { backgroundColor: hex }, type: "button", onClick: (event) => setColor({ ...swatch, a: color.a }, { event, reason: 'grid' }) }, hex));
|
|
149
255
|
})) }));
|
|
150
|
-
};
|
|
151
|
-
|
|
256
|
+
});
|
|
257
|
+
/** Hues across, white to black down, the color ringed; its corners 18px. */
|
|
258
|
+
const Spectrum = forwardRef(({ className, ...props }, ref) => {
|
|
152
259
|
const { color, setColor } = useContext(PickerContext);
|
|
153
260
|
const { h, l } = toHsl(color);
|
|
154
261
|
const pick = (event) => {
|
|
@@ -183,43 +290,242 @@ const Spectrum = () => {
|
|
|
183
290
|
a: color.a,
|
|
184
291
|
}, { event, reason: 'spectrum' });
|
|
185
292
|
};
|
|
186
|
-
return (_jsx("div", { "aria-label": "Spectrum", "aria-valuetext": formatColor({ ...color, a: 1 }), className:
|
|
293
|
+
return (_jsx("div", { ref: ref, "aria-label": "Spectrum", "aria-valuetext": formatColor({ ...color, a: 1 }), role: "slider", tabIndex: 0, ...props, className: cn('uikit-color-picker-spectrum', className), "data-base-ui-swipe-ignore": "", "data-color-picker-part": "spectrum", onKeyDown: handleKeyDown, onPointerDown: handlePointerDown, onPointerMove: handlePointerMove, children: _jsx("span", { style: {
|
|
187
294
|
backgroundColor: formatColor({ ...color, a: 1 }),
|
|
188
295
|
left: `${String((h / 360) * 100)}%`,
|
|
189
296
|
top: `${String((1 - l) * 100)}%`,
|
|
190
297
|
} }) }));
|
|
298
|
+
});
|
|
299
|
+
/**
|
|
300
|
+
* A mode's view (by `value`, its `Mode`'s): shown while it's chosen. With a
|
|
301
|
+
* view of the grid's height (a `Grid` or `Spectrum` in it) among them, all
|
|
302
|
+
* as high (so a popover doesn't move as they're switched); without, each as
|
|
303
|
+
* high as its content (e.g. sliders alone).
|
|
304
|
+
*/
|
|
305
|
+
const View = forwardRef(({ children, className, value, ...props }, ref) => {
|
|
306
|
+
const { isGridHeight, mode, registerGridHeight, registerMode } = useContext(PanelContext);
|
|
307
|
+
// (A mode, too: without `Modes`, its only view shown.)
|
|
308
|
+
useLayoutEffect(() => registerMode(value), [registerMode, value]);
|
|
309
|
+
// (Rendered or not: its own height known by its children's types.)
|
|
310
|
+
const isOwnGridHeight = Children.toArray(children).some((child) => isValidElement(child) &&
|
|
311
|
+
(child.type === Grid || child.type === Spectrum));
|
|
312
|
+
useLayoutEffect(() => (isOwnGridHeight ? registerGridHeight() : undefined), [isOwnGridHeight, registerGridHeight]);
|
|
313
|
+
if (mode !== value)
|
|
314
|
+
return null;
|
|
315
|
+
return (_jsx("div", { ref: ref, "data-color-picker-view": value, ...props, className: cn('uikit-color-picker-view', className), "data-color-picker-part": "view", "data-grid-height": isGridHeight ? '' : undefined, children: children }));
|
|
316
|
+
});
|
|
317
|
+
/**
|
|
318
|
+
* A gradient's stops, across a channel's track (from where its knob's
|
|
319
|
+
* center is at its least to where it is at its most), evenly.
|
|
320
|
+
*/
|
|
321
|
+
const trackStops = (colors) => colors
|
|
322
|
+
.map((stop, index) => `${stop} calc(var(--uikit-color-picker-channel-size) / 2 + (100% - var(--uikit-color-picker-channel-size)) * ${String(index / Math.max(colors.length - 1, 1))})`)
|
|
323
|
+
.join(', ');
|
|
324
|
+
/**
|
|
325
|
+
* A channel's row (the sliders', the opacity's): its title (all caps,
|
|
326
|
+
* secondary), a slider (32px high, mixing from the color: its gradient
|
|
327
|
+
* the color with the channel along its range; its knob as high, in the
|
|
328
|
+
* color, ringed white, 3px) and its value (a 75px by 32px field on the system
|
|
329
|
+
* fill, 8px corners, Body emphasized: typed, applied as it's typed, within
|
|
330
|
+
* its range).
|
|
331
|
+
*/
|
|
332
|
+
/**
|
|
333
|
+
* A typed field's handlers (a channel's value, the code): focused, all of
|
|
334
|
+
* it selected (to type over); left (or Enter), `onLeave` (its value again,
|
|
335
|
+
* e.g. emptied) and its selection collapsed (kept, the next tap would be on
|
|
336
|
+
* selected text). The tap focusing it is on its text, selected: Chrome opens
|
|
337
|
+
* its context menu for that; not for this tap (a later press, held, opens
|
|
338
|
+
* it).
|
|
339
|
+
*/
|
|
340
|
+
const useFieldSelection = (onLeave) => {
|
|
341
|
+
// Focused by a tap (until its click).
|
|
342
|
+
const focusingRef = useRef(false);
|
|
343
|
+
return {
|
|
344
|
+
onBlur: (event) => {
|
|
345
|
+
onLeave();
|
|
346
|
+
focusingRef.current = false;
|
|
347
|
+
const { length } = event.currentTarget.value;
|
|
348
|
+
event.currentTarget.setSelectionRange(length, length);
|
|
349
|
+
},
|
|
350
|
+
onClick: () => {
|
|
351
|
+
focusingRef.current = false;
|
|
352
|
+
},
|
|
353
|
+
onContextMenu: (event) => {
|
|
354
|
+
if (focusingRef.current)
|
|
355
|
+
event.preventDefault();
|
|
356
|
+
},
|
|
357
|
+
onFocus: (event) => {
|
|
358
|
+
focusingRef.current = true;
|
|
359
|
+
event.currentTarget.select();
|
|
360
|
+
},
|
|
361
|
+
onKeyDown: (event) => {
|
|
362
|
+
if (event.key === 'Enter')
|
|
363
|
+
onLeave();
|
|
364
|
+
},
|
|
365
|
+
};
|
|
191
366
|
};
|
|
192
|
-
|
|
193
|
-
const channels = [
|
|
194
|
-
['r', 'Red', 'var(--color-ios-system-red)'],
|
|
195
|
-
['g', 'Green', 'var(--color-ios-system-green)'],
|
|
196
|
-
['b', 'Blue', 'var(--color-ios-system-blue)'],
|
|
197
|
-
];
|
|
198
|
-
const Sliders = () => {
|
|
199
|
-
const { color, setColor } = useContext(PickerContext);
|
|
200
|
-
return (_jsx("div", { className: "uikit-color-picker-sliders", "data-color-picker-part": "sliders", children: channels.map(([channel, label, tint]) => (_jsxs(Slider.Root, { max: 255, min: 0, tint: tint, value: Math.round(color[channel]), onValueChange: (next, { event }) => setColor({ ...color, [channel]: next }, { event, reason: 'sliders' }), children: [_jsx(Slider.Label, { placement: "block-start", children: label }), _jsx(Slider.Value, { placement: "block-start" }), _jsx(Slider.Control, { children: _jsxs(Slider.Track, { children: [_jsx(Slider.Indicator, {}), _jsx(Slider.Thumb, {})] }) })] }, channel))) }));
|
|
201
|
-
};
|
|
202
|
-
const Opacity = () => {
|
|
203
|
-
const { color, setColor } = useContext(PickerContext);
|
|
204
|
-
const percent = Math.round(color.a * 100);
|
|
367
|
+
const ChannelRow = ({ fieldLabel, isCheckered = false, label, max, onValueChange, stops, thumbColor, unit, value, }) => {
|
|
205
368
|
const [draft, setDraft] = useState(undefined);
|
|
206
|
-
const
|
|
207
|
-
|
|
369
|
+
const selection = useFieldSelection(() => setDraft(undefined));
|
|
370
|
+
// Held, its touch doesn't scroll (see `useTouchHold`).
|
|
371
|
+
const holdHandlers = useTouchHold('x');
|
|
372
|
+
// Typed: its digits (a whole number), within its range, applied at once;
|
|
373
|
+
// shown as applied (e.g. 300 as 255), or empty while it's typed over.
|
|
374
|
+
const type = (text, event) => {
|
|
375
|
+
const digits = text.replaceAll(/\D/gu, '');
|
|
376
|
+
if (digits === '') {
|
|
377
|
+
setDraft('');
|
|
208
378
|
return;
|
|
209
|
-
const next = Number.parseFloat(draft);
|
|
210
|
-
if (Number.isFinite(next)) {
|
|
211
|
-
setColor({ ...color, a: clamp(next, 0, 100) / 100 }, { event, reason: 'opacity' });
|
|
212
379
|
}
|
|
213
|
-
|
|
380
|
+
const next = clamp(Number.parseInt(digits, 10), 0, max);
|
|
381
|
+
setDraft(String(next));
|
|
382
|
+
onValueChange(next, event);
|
|
214
383
|
};
|
|
215
|
-
return (_jsxs(
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
384
|
+
return (_jsxs(Viewport.KeyboardAccessory, { className: "uikit-color-picker-channel", "data-color-picker-part": "channel", gap: 20, children: [_jsx("div", { "aria-hidden": "true", className: "uikit-color-picker-channel-title", "data-color-picker-part": "channel-title", children: label }), _jsxs("div", { className: "uikit-color-picker-channel-row", "data-color-picker-part": "channel-row", children: [_jsx(BaseSlider.Root, { "aria-label": label, max: max, min: 0, style: { display: 'contents' }, thumbAlignment: "center", value: value, onValueChange: (next, { event }) => onValueChange(next, event), children: _jsx(BaseSlider.Control, { className: "uikit-color-picker-channel-control", "data-base-ui-swipe-ignore": "", "data-checkered": isCheckered ? '' : undefined, "data-color-picker-part": "channel-control",
|
|
385
|
+
// (Its knob's place, for the hole in its gradient: the slider's
|
|
386
|
+
// value, not the color's, which is rounded otherwise.)
|
|
387
|
+
style: {
|
|
388
|
+
'--uikit-color-picker-channel-position': value / max,
|
|
389
|
+
'--uikit-color-picker-channel-stops': trackStops(stops),
|
|
390
|
+
'--uikit-color-picker-channel-thumb': thumbColor,
|
|
391
|
+
}, ...holdHandlers, children: _jsx(BaseSlider.Track, { children: _jsx(BaseSlider.Thumb, { className: "uikit-color-picker-channel-thumb", "data-color-picker-part": "channel-thumb" }) }) }) }), _jsxs("label", { className: "uikit-color-picker-channel-field", "data-color-picker-part": "channel-field", children: [_jsx("input", { "aria-label": fieldLabel ?? `${label} value`, inputMode: "numeric", value: draft ?? String(value),
|
|
392
|
+
// (Left, or Enter: its value again, e.g. emptied; see
|
|
393
|
+
// `useFieldSelection`.)
|
|
394
|
+
onChange: (event) => type(event.currentTarget.value, event), ...selection }), unit ? _jsx("span", { "aria-hidden": "true", children: unit }) : null] })] })] }));
|
|
395
|
+
};
|
|
396
|
+
/** Hue (0–360°), saturation and lightness (0–100%). */
|
|
397
|
+
const hsl = {
|
|
398
|
+
channels: [
|
|
399
|
+
{ label: 'Hue', max: 360, unit: '°' },
|
|
400
|
+
{ label: 'Saturation', max: 100, unit: '%' },
|
|
401
|
+
{ label: 'Lightness', max: 100, unit: '%' },
|
|
402
|
+
],
|
|
403
|
+
from: (color) => {
|
|
404
|
+
const { h, l, s } = toHsl(color);
|
|
405
|
+
return [Math.round(h) % 360, Math.round(s * 100), Math.round(l * 100)];
|
|
406
|
+
},
|
|
407
|
+
to: ([h = 0, s = 0, l = 0]) => fromHsl(h % 360, s / 100, l / 100),
|
|
408
|
+
};
|
|
409
|
+
/**
|
|
410
|
+
* Cyan, magenta, yellow and black (0–100%): naive (from sRGB, no ink's
|
|
411
|
+
* profile), as on screen.
|
|
412
|
+
*/
|
|
413
|
+
const cmyk = {
|
|
414
|
+
channels: [
|
|
415
|
+
{ label: 'Cyan', max: 100, unit: '%' },
|
|
416
|
+
{ label: 'Magenta', max: 100, unit: '%' },
|
|
417
|
+
{ label: 'Yellow', max: 100, unit: '%' },
|
|
418
|
+
{ label: 'Black', max: 100, unit: '%' },
|
|
419
|
+
],
|
|
420
|
+
from: ({ b, g, r }) => {
|
|
421
|
+
const k = 1 - Math.max(r, g, b) / 255;
|
|
422
|
+
const ink = (channel) => k >= 1 ? 0 : Math.round(((1 - channel / 255 - k) / (1 - k)) * 100);
|
|
423
|
+
return [ink(r), ink(g), ink(b), Math.round(k * 100)];
|
|
424
|
+
},
|
|
425
|
+
to: ([c = 0, m = 0, y = 0, k = 0]) => {
|
|
426
|
+
const channel = (ink) => 255 * (1 - ink / 100) * (1 - k / 100);
|
|
427
|
+
return { b: channel(y), g: channel(m), r: channel(c) };
|
|
428
|
+
},
|
|
221
429
|
};
|
|
222
|
-
|
|
430
|
+
/** Red, green and blue (0–255). */
|
|
431
|
+
const rgb = {
|
|
432
|
+
channels: [
|
|
433
|
+
{ label: 'Red', max: 255 },
|
|
434
|
+
{ label: 'Green', max: 255 },
|
|
435
|
+
{ label: 'Blue', max: 255 },
|
|
436
|
+
],
|
|
437
|
+
from: ({ b, g, r }) => [r, g, b].map((channel) => Math.round(channel)),
|
|
438
|
+
to: ([r = 0, g = 0, b = 0]) => ({ b, g, r }),
|
|
439
|
+
};
|
|
440
|
+
const rgbKeys = ['r', 'g', 'b'];
|
|
441
|
+
/**
|
|
442
|
+
* Red, green and blue in percent (0–100%): coarser than bytes, so a channel
|
|
443
|
+
* set leaves the others as they are (not as rounded, see `set`).
|
|
444
|
+
*/
|
|
445
|
+
const rgbPercentage = {
|
|
446
|
+
channels: [
|
|
447
|
+
{ label: 'Red', max: 100, unit: '%' },
|
|
448
|
+
{ label: 'Green', max: 100, unit: '%' },
|
|
449
|
+
{ label: 'Blue', max: 100, unit: '%' },
|
|
450
|
+
],
|
|
451
|
+
from: ({ b, g, r }) => [r, g, b].map((channel) => Math.round((channel / 255) * 100)),
|
|
452
|
+
set: ({ b, g, r }, index, value) => ({
|
|
453
|
+
b,
|
|
454
|
+
g,
|
|
455
|
+
r,
|
|
456
|
+
[rgbKeys[index] ?? 'r']: (value * 255) / 100,
|
|
457
|
+
}),
|
|
458
|
+
to: ([r = 0, g = 0, b = 0]) => ({
|
|
459
|
+
b: (b * 255) / 100,
|
|
460
|
+
g: (g * 255) / 100,
|
|
461
|
+
r: (r * 255) / 100,
|
|
462
|
+
}),
|
|
463
|
+
};
|
|
464
|
+
const models = { cmyk, hsl, rgb };
|
|
465
|
+
/** A model's color, one channel set (see `Model`'s `set`). */
|
|
466
|
+
const withChannel = (model, color, values, index, value) => model.set
|
|
467
|
+
? model.set(color, index, value)
|
|
468
|
+
: model.to(values.with(index, value));
|
|
469
|
+
/**
|
|
470
|
+
* A channel's track's colors: the color with the channel along its range,
|
|
471
|
+
* enough of them to be exact (linear between them): a hue around (every
|
|
472
|
+
* 60°), a lightness through the pure hue (halfway), the others its ends.
|
|
473
|
+
*/
|
|
474
|
+
const trackColors = (model, color, values, index) => {
|
|
475
|
+
const { max } = model.channels[index] ?? { max: 1 };
|
|
476
|
+
const steps = model === hsl ? ([6, 1, 2][index] ?? 1) : 1;
|
|
477
|
+
return Array.from({ length: steps + 1 }, (_, step) => formatColor({
|
|
478
|
+
...withChannel(model, color, values, index, (max * step) / steps),
|
|
479
|
+
a: 1,
|
|
480
|
+
}));
|
|
481
|
+
};
|
|
482
|
+
/**
|
|
483
|
+
* Sliders: a color model's channels (`model`: red, green and blue, in bytes
|
|
484
|
+
* or percent, its `range`; hue, saturation and lightness; cyan, magenta,
|
|
485
|
+
* yellow and black), each mixing from the color, its value typed too; then
|
|
486
|
+
* its children (e.g. the `Code`), spaced as the channels.
|
|
487
|
+
*/
|
|
488
|
+
const Sliders = forwardRef(({ children, className, model: modelName = 'rgb', range = 'byte', ...props }, ref) => {
|
|
489
|
+
const { color, setColor } = useContext(PickerContext);
|
|
490
|
+
const model = modelName === 'rgb' && range === 'percentage'
|
|
491
|
+
? rgbPercentage
|
|
492
|
+
: models[modelName];
|
|
493
|
+
const opaque = formatColor({ ...color, a: 1 });
|
|
494
|
+
// Its values as last set, while they're the color's (and its model's):
|
|
495
|
+
// so a hue isn't lost to a gray (no saturation), nor inks to black (all
|
|
496
|
+
// of it).
|
|
497
|
+
const [last, setLast] = useState();
|
|
498
|
+
const values = last?.color === opaque && last.model === model
|
|
499
|
+
? last.values
|
|
500
|
+
: model.from(color);
|
|
501
|
+
return (_jsxs("div", { ref: ref, ...props, className: cn('uikit-color-picker-sliders', className), "data-color-picker-part": "sliders", "data-model": modelName, "data-range": modelName === 'rgb' ? range : undefined, children: [model.channels.map(({ label, max, unit }, index) => (_jsx(ChannelRow, { label: label, max: max, stops: trackColors(model, color, values, index), thumbColor: opaque, unit: unit, value: values[index] ?? 0, onValueChange: (next, event) => {
|
|
502
|
+
const nextValues = values.with(index, next);
|
|
503
|
+
const nextColor = {
|
|
504
|
+
...withChannel(model, color, values, index, next),
|
|
505
|
+
a: color.a,
|
|
506
|
+
};
|
|
507
|
+
setLast({
|
|
508
|
+
color: formatColor({ ...nextColor, a: 1 }),
|
|
509
|
+
model,
|
|
510
|
+
values: nextValues,
|
|
511
|
+
});
|
|
512
|
+
setColor(nextColor, { event, reason: 'sliders' });
|
|
513
|
+
} }, label))), children] }));
|
|
514
|
+
});
|
|
515
|
+
/**
|
|
516
|
+
* The opacity: the color fading in over a checkerboard, in percent; 22px
|
|
517
|
+
* above what follows. Rendered, the color has it (see `Root`): without
|
|
518
|
+
* it, it's opaque.
|
|
519
|
+
*/
|
|
520
|
+
const Opacity = forwardRef(({ className, ...props }, ref) => {
|
|
521
|
+
const { color, registerOpacity, setColor } = useContext(PickerContext);
|
|
522
|
+
useLayoutEffect(registerOpacity, [registerOpacity]);
|
|
523
|
+
return (_jsx("div", { ref: ref, ...props, className: cn('uikit-color-picker-opacity', className), "data-color-picker-part": "opacity", children: _jsx(ChannelRow, { isCheckered: true, fieldLabel: "Opacity percentage", label: "Opacity", max: 100, thumbColor: formatColor(color), unit: "%", value: Math.round(color.a * 100), stops: [
|
|
524
|
+
formatColor({ ...color, a: 0 }),
|
|
525
|
+
formatColor({ ...color, a: 1 }),
|
|
526
|
+
], onValueChange: (next, event) => setColor({ ...color, a: next / 100 }, { event, reason: 'opacity' }) }) }));
|
|
527
|
+
});
|
|
528
|
+
// A page of saved colors: 2 rows of 5, always as high (so its
|
|
223
529
|
// popover doesn't move).
|
|
224
530
|
const Saved = forwardRef(({ className, ...props }, ref) => (_jsx("div", { ref: ref, className: cn('uikit-color-picker-saved', className), "data-color-picker-part": "saved", ...props })));
|
|
225
531
|
/** Saved colors per page (with the add button, on the last). */
|
|
@@ -228,24 +534,17 @@ const SavedSwatch = ({ hex, onDelete }) => {
|
|
|
228
534
|
const { color, setColor } = useContext(PickerContext);
|
|
229
535
|
const swatch = parseColor(hex);
|
|
230
536
|
// Its menu (a secondary click: right click, or a long press): delete it.
|
|
231
|
-
return (_jsxs(Menu.Root, { children: [_jsx(Menu.Trigger, { openOn: "secondaryClick", render: _jsx("button", { "aria-label": hex, "aria-pressed": sameColor(swatch, color) && swatch.a === color.a, style: {
|
|
537
|
+
return (_jsxs(Menu.Root, { children: [_jsx(Menu.Trigger, { openOn: "secondaryClick", render: _jsx("button", { "aria-label": hex, "aria-pressed": sameColor(swatch, color) && swatch.a === color.a, style: { '--uikit-color-picker-color': hex }, type: "button", onClick: (event) => setColor(swatch, { event, reason: 'saved' }) }) }), _jsx(Menu.Portal, { children: _jsx(Menu.Positioner, { children: _jsx(Menu.Popup, { children: _jsx(Menu.Item, { destructive: true, onClick: (event) => onDelete(event), children: _jsx(Menu.Label, { children: "Delete" }) }) }) }) })] }));
|
|
232
538
|
};
|
|
233
|
-
const modes = [
|
|
234
|
-
['grid', 'Grid'],
|
|
235
|
-
['spectrum', 'Spectrum'],
|
|
236
|
-
['sliders', 'Sliders'],
|
|
237
|
-
];
|
|
238
539
|
/**
|
|
239
|
-
*
|
|
240
|
-
* colors (in pages; adding the color to
|
|
540
|
+
* The color (82px, 18px corners; translucent, over black and white) beside
|
|
541
|
+
* the saved colors (in pages, with a page control; adding the color to
|
|
542
|
+
* them, deleting one by its menu).
|
|
241
543
|
*/
|
|
242
|
-
const
|
|
544
|
+
const Swatches = forwardRef(({ className, defaultSavedColors: initialSaved = defaultSavedColors, onSavedColorsChange, savedColors: savedProp, ...props }, ref) => {
|
|
243
545
|
const { color } = useContext(PickerContext);
|
|
244
|
-
const [ownMode, setOwnMode] = useState(defaultMode);
|
|
245
|
-
const mode = modeProp ?? ownMode;
|
|
246
546
|
const [ownSaved, setOwnSaved] = useState(initialSaved);
|
|
247
547
|
const saved = savedProp ?? ownSaved;
|
|
248
|
-
const opaque = formatColor({ ...color, a: 1 });
|
|
249
548
|
// Uncontrolled: their own state; controlled, only the app's.
|
|
250
549
|
const changeSaved = (next, details) => {
|
|
251
550
|
if (savedProp === undefined)
|
|
@@ -294,43 +593,30 @@ const Panel = forwardRef(({ className, defaultMode = 'grid', defaultSavedColors:
|
|
|
294
593
|
setPage(next);
|
|
295
594
|
setScrollTarget(next);
|
|
296
595
|
};
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
inert: index !== shownPage, children: items
|
|
313
|
-
.slice(index * pageSize, (index + 1) * pageSize)
|
|
314
|
-
.map((hex) => hex === undefined ? (_jsx("button", { "aria-label": "Add color", "data-add": "", type: "button", onClick: (event) => {
|
|
315
|
-
const next = formatColor(color);
|
|
316
|
-
if (saved.includes(next))
|
|
317
|
-
return;
|
|
318
|
-
changeSaved([...saved, next], {
|
|
319
|
-
event,
|
|
320
|
-
reason: 'add',
|
|
321
|
-
});
|
|
322
|
-
// On the page it lands on.
|
|
323
|
-
goTo(Math.floor(saved.length / pageSize));
|
|
324
|
-
}, children: "+" }, "add")) : (_jsx(SavedSwatch, { hex: hex, onDelete: (event) => changeSaved(saved.filter((other) => other !== hex), { event, reason: 'delete' }) }, hex))) }, index))) }), _jsx(PageControl, { count: Math.max(pages, 2), style: pages > 1 ? undefined : { visibility: 'hidden' }, value: shownPage, variant: "plain", onValueChange: goTo })] })] })] }));
|
|
596
|
+
return (_jsxs("div", { ref: ref, ...props, className: cn('uikit-color-picker-swatches', className), "data-color-picker-part": "swatches", children: [_jsx("div", { "aria-hidden": "true", className: "uikit-color-picker-current", "data-color-picker-part": "current" }), _jsxs("div", { className: "uikit-color-picker-saved-column", "data-color-picker-part": "saved-column", children: [_jsx("div", { ref: pagesRef, "aria-label": "Saved colors", className: "uikit-color-picker-saved-pages", "data-color-picker-part": "saved-pages", role: "group", onScroll: handleScroll, children: Array.from({ length: pages }, (_, index) => (_jsx(Saved, {
|
|
597
|
+
// (Only the page shown can be reached.)
|
|
598
|
+
inert: index !== shownPage, children: items
|
|
599
|
+
.slice(index * pageSize, (index + 1) * pageSize)
|
|
600
|
+
.map((hex) => hex === undefined ? (_jsx("button", { "aria-label": "Add color", "data-add": "", type: "button", onClick: (event) => {
|
|
601
|
+
const next = formatColor(color);
|
|
602
|
+
if (saved.includes(next))
|
|
603
|
+
return;
|
|
604
|
+
changeSaved([...saved, next], {
|
|
605
|
+
event,
|
|
606
|
+
reason: 'add',
|
|
607
|
+
});
|
|
608
|
+
// On the page it lands on.
|
|
609
|
+
goTo(Math.floor(saved.length / pageSize));
|
|
610
|
+
}, children: "+" }, "add")) : (_jsx(SavedSwatch, { hex: hex, onDelete: (event) => changeSaved(saved.filter((other) => other !== hex), { event, reason: 'delete' }) }, hex))) }, index))) }), pages > 1 ? (_jsx(PageControl, { count: pages, value: shownPage, variant: "plain", onValueChange: goTo })) : null] })] }));
|
|
325
611
|
});
|
|
326
|
-
// Its header (
|
|
327
|
-
//
|
|
612
|
+
// Its header (in the panel's popover: 16px from its top, 10px above the
|
|
613
|
+
// panel): the app's (e.g. a `Toolbar`: eyedropper, title,
|
|
328
614
|
// close); in a sheet, its `Sheet.Header` instead.
|
|
329
615
|
const Header = forwardRef(({ className, ...props }, ref) => (_jsx("div", { ref: ref, className: cn('uikit-color-picker-header', className), "data-color-picker-part": "header", ...props })));
|
|
330
616
|
/** The browser's eyedropper (`EyeDropper`), if it has one. */
|
|
331
617
|
const eyeDropper = () => globalThis.EyeDropper;
|
|
332
618
|
/**
|
|
333
|
-
* Its eyedropper (
|
|
619
|
+
* Its eyedropper (the leading button): a `Button` (or what it
|
|
334
620
|
* renders), picking a color off the screen (the browser's `EyeDropper`;
|
|
335
621
|
* none without it).
|
|
336
622
|
*/
|
|
@@ -351,13 +637,261 @@ const Eyedropper = ({ render, ...props }) => {
|
|
|
351
637
|
onClick: pick,
|
|
352
638
|
})) : (_jsx(Button.Root, { "aria-label": "Eyedropper", ...props, onClick: pick }));
|
|
353
639
|
};
|
|
354
|
-
/**
|
|
640
|
+
/** Whether the browser has a color input (its picker the system's). */
|
|
641
|
+
const hasColorInput = () => {
|
|
642
|
+
if (typeof document === 'undefined')
|
|
643
|
+
return false;
|
|
644
|
+
const input = document.createElement('input');
|
|
645
|
+
input.type = 'color';
|
|
646
|
+
return input.type === 'color';
|
|
647
|
+
};
|
|
648
|
+
/**
|
|
649
|
+
* Whether its color input has an alpha (its `alpha` attribute: e.g.
|
|
650
|
+
* Safari's; without, a value with one is invalid, black).
|
|
651
|
+
*/
|
|
652
|
+
const hasColorInputAlpha = () => typeof HTMLInputElement !== 'undefined' &&
|
|
653
|
+
'alpha' in HTMLInputElement.prototype;
|
|
654
|
+
/**
|
|
655
|
+
* Its system colors (a leading button, after the eyedropper): a `Button`
|
|
656
|
+
* (or what it renders), opening the system's color picker: a color input's
|
|
657
|
+
* (none without one), over the button, transparent: pressed itself (on iOS,
|
|
658
|
+
* nothing else opens it: neither `showPicker` nor a click, see WebKit's bug
|
|
659
|
+
* 261703), the button showing the press; the button clicking it from the
|
|
660
|
+
* keyboard. With the opacity rendered, its alpha too, where the input has
|
|
661
|
+
* one (its `alpha`: e.g. Safari); else its colors opaque, the opacity kept.
|
|
662
|
+
*/
|
|
663
|
+
const SystemColors = ({ render, ...props }) => {
|
|
664
|
+
const { color, hasOpacity, setColor } = useContext(PickerContext);
|
|
665
|
+
// (Its input, as rendered: for its button's press.)
|
|
666
|
+
const [input, setInput] = useState(null);
|
|
667
|
+
if (!hasColorInput())
|
|
668
|
+
return null;
|
|
669
|
+
// (With the opacity, the input's alpha too, where it has one.)
|
|
670
|
+
const hasAlpha = hasOpacity && hasColorInputAlpha();
|
|
671
|
+
const open = () => input?.click();
|
|
672
|
+
return (_jsxs("span", { className: "uikit-color-picker-system-colors", "data-color-picker-part": "system-colors", children: [render ? (cloneElement(render, {
|
|
673
|
+
'aria-label': 'System colors',
|
|
674
|
+
...props,
|
|
675
|
+
onClick: open,
|
|
676
|
+
})) : (_jsx(Button.Root, { "aria-label": "System colors", ...props, onClick: open })), _jsx("input", { ref: setInput, ...(hasAlpha ? { alpha: true } : {}), "aria-hidden": "true", className: "uikit-color-picker-system-colors-input", disabled: props.disabled, tabIndex: -1, type: "color",
|
|
677
|
+
// (The color, as it is: its picker opened from it.)
|
|
678
|
+
value: formatColor(hasAlpha ? color : { ...color, a: 1 }).toLowerCase(),
|
|
679
|
+
// (As it's picked, live; without its alpha, the opacity kept.)
|
|
680
|
+
onChange: (event) => {
|
|
681
|
+
// (Any CSS color, with an alpha: see color.ts.)
|
|
682
|
+
const picked = parseCssColor(event.currentTarget.value);
|
|
683
|
+
if (!picked)
|
|
684
|
+
return;
|
|
685
|
+
setColor(hasAlpha ? picked : { ...picked, a: color.a }, {
|
|
686
|
+
event,
|
|
687
|
+
reason: 'system-colors',
|
|
688
|
+
});
|
|
689
|
+
} })] }));
|
|
690
|
+
};
|
|
691
|
+
/**
|
|
692
|
+
* Its code (a row; its place the app's: e.g. under the sliders), as high as a
|
|
693
|
+
* channel's, its field where a channel's value is: “Hex” (a channel's title,
|
|
694
|
+
* beside them, centered), then its children (e.g. a `CodeCopy`), the color's
|
|
695
|
+
* hex code typed (`#` before it, not typed: its digits, applied as they make
|
|
696
|
+
* a code, 3, 4, 6 or 8 of them; with an alpha, 4 or 8), at its end. Kept above the keyboard as it's typed into (as a
|
|
697
|
+
* channel's row).
|
|
698
|
+
*/
|
|
699
|
+
const Code = forwardRef(({ children, className, ...props }, ref) => {
|
|
700
|
+
const { color, setColor } = useContext(PickerContext);
|
|
701
|
+
const [draft, setDraft] = useState(undefined);
|
|
702
|
+
const selection = useFieldSelection(() => setDraft(undefined));
|
|
703
|
+
const type = (text, event) => {
|
|
704
|
+
const digits = text
|
|
705
|
+
.replaceAll(/[^\da-f]/giu, '')
|
|
706
|
+
.slice(0, 8)
|
|
707
|
+
.toUpperCase();
|
|
708
|
+
setDraft(digits);
|
|
709
|
+
if (![3, 4, 6, 8].includes(digits.length))
|
|
710
|
+
return;
|
|
711
|
+
const typed = parseColor(`#${digits}`);
|
|
712
|
+
// (Without an alpha typed, the color's kept.)
|
|
713
|
+
setColor(digits.length % 4 === 0 ? typed : { ...typed, a: color.a }, {
|
|
714
|
+
event,
|
|
715
|
+
reason: 'code',
|
|
716
|
+
});
|
|
717
|
+
};
|
|
718
|
+
return (_jsx(Viewport.KeyboardAccessory, { ref: ref, gap: 20, ...props, className: cn('uikit-color-picker-code', className), "data-color-picker-part": "code", children: _jsxs("div", { className: "uikit-color-picker-channel-row uikit-color-picker-code-row", "data-color-picker-part": "code-row", children: [_jsx("div", { "aria-hidden": "true", className: "uikit-color-picker-channel-title", "data-color-picker-part": "code-title", children: "Hex" }), children, _jsxs("label", { className: "uikit-color-picker-channel-field uikit-color-picker-code-field", "data-color-picker-part": "code-field", children: [_jsx("span", { "aria-hidden": "true", children: "#" }), _jsx("input", { spellcheck: false, "aria-label": "Hex color", autoCapitalize: "characters", autoComplete: "off", autoCorrect: "off", value: draft ?? formatColor(color).slice(1), onChange: (event) => type(event.currentTarget.value, event), ...selection })] })] }) }));
|
|
719
|
+
});
|
|
720
|
+
/** A color well: its color, opening the picker. */
|
|
355
721
|
const Well = forwardRef(({ className, color, size = 'small', style, ...props }, ref) => (_jsx("button", { ref: ref, "aria-label": "Color", "data-color": color === undefined ? undefined : '', "data-size": size, style: withStyle({ '--uikit-color-picker-well-color': color }, style), type: "button", ...props, className: cn('uikit-color-picker-well', className), "data-color-picker-part": "well" })));
|
|
722
|
+
// How long a copy shows as done (its checkmark).
|
|
723
|
+
const copiedFor = 1500;
|
|
724
|
+
/**
|
|
725
|
+
* Its code's copy button: a `Button` (or what it renders, its content the
|
|
726
|
+
* app's: e.g. a symbol), opening a menu of the color's codes (see color.ts):
|
|
727
|
+
* hex's, then a submenu each of the others' groups, each code its format and
|
|
728
|
+
* its values (`Menu.Label`, `Menu.Subtitle`). One picked, copied to the
|
|
729
|
+
* clipboard (`onCodeCopy`); for a moment, a checkmark instead of its content
|
|
730
|
+
* (`data-copied`). Its place the app's (in a `Code`, or not).
|
|
731
|
+
*/
|
|
732
|
+
const CodeCopy = ({ children, onCodeCopy, render, ...props }) => {
|
|
733
|
+
const { color } = useContext(PickerContext);
|
|
734
|
+
const [isCopied, setIsCopied] = useState(false);
|
|
735
|
+
useEffect(() => {
|
|
736
|
+
if (!isCopied)
|
|
737
|
+
return undefined;
|
|
738
|
+
const timeout = setTimeout(() => setIsCopied(false), copiedFor);
|
|
739
|
+
return () => clearTimeout(timeout);
|
|
740
|
+
}, [isCopied]);
|
|
741
|
+
const copy = (code, event) => {
|
|
742
|
+
navigator.clipboard
|
|
743
|
+
.writeText(code)
|
|
744
|
+
.then(() => {
|
|
745
|
+
setIsCopied(true);
|
|
746
|
+
onCodeCopy?.(code, event);
|
|
747
|
+
})
|
|
748
|
+
.catch(() => undefined);
|
|
749
|
+
};
|
|
750
|
+
// (Hex's: its color in a well at its end, inert.)
|
|
751
|
+
const item = ({ code, label, subtitle, well }) => (_jsxs(Menu.Item, { onClick: (event) => copy(code, event), children: [_jsx(Menu.Label, { children: label }), _jsx(Menu.Subtitle, { children: subtitle }), well === undefined ? null : (_jsx(Well, { inert: true, "aria-hidden": "true", className: "uikit-color-picker-code-copy-well", color: well, tabIndex: -1 }))] }, code));
|
|
752
|
+
const content = isCopied ? (_jsx(Button.Icon, { children: _jsx(Checkmark, {}) })) : (children);
|
|
753
|
+
const trigger = render ? (cloneElement(render, {
|
|
754
|
+
'aria-label': 'Copy color code',
|
|
755
|
+
...props,
|
|
756
|
+
'data-copied': isCopied ? '' : undefined,
|
|
757
|
+
}, content)) : (_jsx(Button.Root, { "aria-label": "Copy color code", ...props, "data-copied": isCopied ? '' : undefined, children: content }));
|
|
758
|
+
return (_jsxs(Menu.Root, { children: [_jsx(Menu.Trigger, { render: trigger }), _jsx(Menu.Portal, { children: _jsx(Menu.Positioner, { children: _jsx(Menu.Popup, { children: codeGroups(color, formatColor({ ...color, a: 1 })).map(([title, codes], index) => index === 0 ? (_jsx(Fragment, { children: codes.map((entry) => item(entry)) }, title)) : (_jsxs(Menu.SubmenuRoot, { children: [_jsx(Menu.SubmenuTrigger, { children: _jsx(Menu.Label, { children: title }) }), _jsx(Menu.Portal, { children: _jsx(Menu.Positioner, { children: _jsx(Menu.Popup, { children: codes.map((entry) => item(entry)) }) }) })] }, title))) }) }) })] }));
|
|
759
|
+
};
|
|
760
|
+
/**
|
|
761
|
+
* Its named colors' rows (see `Named`): one registered (its label's first
|
|
762
|
+
* letter, for the index), while mounted.
|
|
763
|
+
*/
|
|
764
|
+
const NamedContext = createContext(() => unregister);
|
|
765
|
+
/** The scroll container `element` is in (its block axis), or the page. */
|
|
766
|
+
const scrollerOf = (element) => {
|
|
767
|
+
for (let parent = element.parentElement; parent && parent !== document.body; parent = parent.parentElement) {
|
|
768
|
+
const { overflowY } = getComputedStyle(parent);
|
|
769
|
+
if (overflowY === 'auto' || overflowY === 'scroll')
|
|
770
|
+
return parent;
|
|
771
|
+
}
|
|
772
|
+
return document.scrollingElement ?? document.documentElement;
|
|
773
|
+
};
|
|
774
|
+
/** A row at the top of its scroll container (below its scroll padding). */
|
|
775
|
+
const jumpTo = (element) => {
|
|
776
|
+
const scroller = scrollerOf(element);
|
|
777
|
+
const padding = Number.parseFloat(getComputedStyle(scroller).scrollPaddingBlockStart) || 0;
|
|
778
|
+
const top = scroller === document.scrollingElement
|
|
779
|
+
? 0
|
|
780
|
+
: scroller.getBoundingClientRect().top;
|
|
781
|
+
scroller.scrollTo({
|
|
782
|
+
top: scroller.scrollTop + element.getBoundingClientRect().top - top - padding,
|
|
783
|
+
});
|
|
784
|
+
};
|
|
785
|
+
/**
|
|
786
|
+
* A row in view (between its scroll container's scroll paddings: e.g. a
|
|
787
|
+
* sheet's header), at once: already, it stays; else, centered there.
|
|
788
|
+
*/
|
|
789
|
+
const revealCentered = (element) => {
|
|
790
|
+
const scroller = scrollerOf(element);
|
|
791
|
+
const style = getComputedStyle(scroller);
|
|
792
|
+
const paddingStart = Number.parseFloat(style.scrollPaddingBlockStart) || 0;
|
|
793
|
+
const paddingEnd = Number.parseFloat(style.scrollPaddingBlockEnd) || 0;
|
|
794
|
+
const scrollerTop = scroller === document.scrollingElement
|
|
795
|
+
? 0
|
|
796
|
+
: scroller.getBoundingClientRect().top;
|
|
797
|
+
const { bottom, height, top } = element.getBoundingClientRect();
|
|
798
|
+
const viewStart = scrollerTop + paddingStart;
|
|
799
|
+
const viewHeight = scroller.clientHeight - paddingStart - paddingEnd;
|
|
800
|
+
if (top >= viewStart && bottom <= viewStart + viewHeight)
|
|
801
|
+
return;
|
|
802
|
+
scroller.scrollTo({
|
|
803
|
+
behavior: 'instant',
|
|
804
|
+
top: scroller.scrollTop + top - viewStart - (viewHeight - height) / 2,
|
|
805
|
+
});
|
|
806
|
+
};
|
|
807
|
+
/**
|
|
808
|
+
* Its named colors (a mode's content, e.g. “Named”): one list of their rows
|
|
809
|
+
* (`NamedColor`s: e.g. the app's, `CssNamedColors`'), as high as they are
|
|
810
|
+
* (scrolled by its container: e.g. a sheet's content, a popover's body). At
|
|
811
|
+
* its end (in its panel's inset), an index of their labels' first letters,
|
|
812
|
+
* kept centered in view as it's scrolled; one picked, the first row with it
|
|
813
|
+
* at the top (below the container's scroll padding: e.g. a sheet's header).
|
|
814
|
+
* Shown, the color's row in view at once (see `revealCentered`).
|
|
815
|
+
*/
|
|
816
|
+
const Named = forwardRef(({ children, className, ...props }, forwardedRef) => {
|
|
817
|
+
const ref = useForwardedRef(forwardedRef);
|
|
818
|
+
// Shown (e.g. its mode chosen): the color's row in view, at once.
|
|
819
|
+
useLayoutEffect(() => {
|
|
820
|
+
const picked = ref.current?.querySelector('[data-color-picker-part="named-color"][data-picked]');
|
|
821
|
+
if (picked)
|
|
822
|
+
revealCentered(picked);
|
|
823
|
+
}, [ref]);
|
|
824
|
+
const [entries, setEntries] = useState([]);
|
|
825
|
+
const register = useCallback((entry) => {
|
|
826
|
+
// (In the document's order; rows apart, neither in the other: only
|
|
827
|
+
// following, or not.)
|
|
828
|
+
setEntries((current) => [...current, entry].toSorted((a, b) => a.element.compareDocumentPosition(b.element) ===
|
|
829
|
+
Node.DOCUMENT_POSITION_FOLLOWING
|
|
830
|
+
? -1
|
|
831
|
+
: 1));
|
|
832
|
+
return () => setEntries((current) => current.filter((other) => other !== entry));
|
|
833
|
+
}, []);
|
|
834
|
+
// Each letter's first row.
|
|
835
|
+
const letters = useMemo(() => {
|
|
836
|
+
const first = new Map();
|
|
837
|
+
for (const { element, label } of entries) {
|
|
838
|
+
if (!first.has(label))
|
|
839
|
+
first.set(label, element);
|
|
840
|
+
}
|
|
841
|
+
return [...first];
|
|
842
|
+
}, [entries]);
|
|
843
|
+
return (_jsx(NamedContext.Provider, { value: register, children: _jsxs("div", { ref: ref, ...props, className: cn('uikit-color-picker-named', className), "data-color-picker-part": "named", children: [_jsx(List.Root, { listStyle: "insetGrouped", children: _jsx(List.Section, { children: _jsx(List.Group, { children: children }) }) }), letters.length > 0 ? (_jsx("div", { className: "uikit-color-picker-named-index", "data-color-picker-part": "named-index", children: _jsx(List.Index, { "aria-label": "Index", children: letters.map(([letter, element]) => (_jsx(List.IndexItem, { onClick: () => jumpTo(element), children: letter }, letter))) }) })) : null] }) }));
|
|
844
|
+
});
|
|
845
|
+
/**
|
|
846
|
+
* A named color's row (in a `Named`): its color in a well (inert), its label
|
|
847
|
+
* (or its color), a checkmark while it's the color (its opacity aside); its
|
|
848
|
+
* label's first letter in the index. Pressed, the color (its opacity kept,
|
|
849
|
+
* as a grid swatch's).
|
|
850
|
+
*/
|
|
851
|
+
const NamedColor = forwardRef(({ children, color: cssColor, ...props }, forwardedRef) => {
|
|
852
|
+
const { color, setColor } = useContext(PickerContext);
|
|
853
|
+
const register = useContext(NamedContext);
|
|
854
|
+
const ref = useForwardedRef(forwardedRef);
|
|
855
|
+
const named = useMemo(() => parseCssColor(cssColor), [cssColor]);
|
|
856
|
+
const isPicked = named !== undefined && sameColor(named, color);
|
|
857
|
+
const letter = (typeof children === 'string' ? children : cssColor)
|
|
858
|
+
.charAt(0)
|
|
859
|
+
.toUpperCase();
|
|
860
|
+
useLayoutEffect(() => {
|
|
861
|
+
const { current: element } = ref;
|
|
862
|
+
return element ? register({ element, label: letter }) : undefined;
|
|
863
|
+
}, [letter, ref, register]);
|
|
864
|
+
return (_jsxs(List.Item, { ref: ref, ...props, "data-color-picker-part": "named-color", "data-picked": isPicked ? '' : undefined, children: [_jsx(List.Leading, { children: _jsx(Well, { inert: true, "aria-hidden": "true", color: cssColor, tabIndex: -1 }) }), _jsx(List.Content, { children: _jsx(List.Title, { "aria-pressed": isPicked, render: _jsx("button", { type: "button" }), onClick: (event) => {
|
|
865
|
+
if (!named)
|
|
866
|
+
return;
|
|
867
|
+
setColor({ ...named, a: color.a }, { event, reason: 'named' });
|
|
868
|
+
}, children: children ?? cssColor }) }), _jsx(List.Trailing, { children: isPicked ? _jsx(List.Checkmark, {}) : null })] }));
|
|
869
|
+
});
|
|
870
|
+
/**
|
|
871
|
+
* The CSS named colors (148, synonyms too: e.g. gray and grey; not
|
|
872
|
+
* `transparent`; see named-colors.ts), as a `Named`'s rows, labeled by their
|
|
873
|
+
* names.
|
|
874
|
+
*/
|
|
875
|
+
const CssNamedColors = () => (_jsx(_Fragment, { children: cssNamedColors.map((name) => (_jsx(NamedColor, { color: name }, name))) }));
|
|
356
876
|
export const ColorPicker = {
|
|
357
877
|
Root,
|
|
358
878
|
Header,
|
|
359
879
|
Panel,
|
|
880
|
+
Modes,
|
|
881
|
+
Mode,
|
|
882
|
+
View,
|
|
883
|
+
Grid,
|
|
884
|
+
Spectrum,
|
|
885
|
+
Sliders,
|
|
886
|
+
Opacity,
|
|
887
|
+
Swatches,
|
|
360
888
|
Eyedropper,
|
|
889
|
+
SystemColors,
|
|
890
|
+
Code,
|
|
891
|
+
CodeCopy,
|
|
892
|
+
Named,
|
|
893
|
+
NamedColor,
|
|
894
|
+
CssNamedColors,
|
|
361
895
|
Well,
|
|
362
896
|
};
|
|
363
897
|
//# sourceMappingURL=main.js.map
|