mtrl-addons 0.4.2 → 0.4.4

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.
@@ -0,0 +1,45 @@
1
+ import { ColorPickerComponent, ColorPickerConfig, ColorPickerState } from "./types";
2
+ import { AreaFeature } from "./features/area";
3
+ import { HueFeature } from "./features/hue";
4
+ import { SwatchesFeature } from "./features/swatches";
5
+ import { InputFeature } from "./features/input";
6
+ import { VariantFeature } from "./features/variant";
7
+ /**
8
+ * API configuration options for color picker component
9
+ * @category Components
10
+ * @internal
11
+ */
12
+ interface ApiOptions {
13
+ element: HTMLElement;
14
+ config: ColorPickerConfig;
15
+ getClass: (name: string) => string;
16
+ emit: (event: string, data?: unknown) => void;
17
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
18
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
19
+ disabled: {
20
+ enable: () => void;
21
+ disable: () => void;
22
+ isDisabled: () => boolean;
23
+ };
24
+ lifecycle: {
25
+ destroy: () => void;
26
+ };
27
+ state?: ColorPickerState;
28
+ area?: AreaFeature;
29
+ hue?: HueFeature;
30
+ swatches?: SwatchesFeature;
31
+ input?: InputFeature;
32
+ variant?: VariantFeature;
33
+ }
34
+ /**
35
+ * Enhances a color picker component with API methods.
36
+ * This follows the higher-order function pattern to add public API methods
37
+ * to the component, making them available to the end user.
38
+ *
39
+ * @param options - API configuration options
40
+ * @returns Higher-order function that adds API methods to component
41
+ * @category Components
42
+ * @internal
43
+ */
44
+ export declare const withAPI: (options: ApiOptions) => (component: unknown) => ColorPickerComponent;
45
+ export {};
@@ -0,0 +1,47 @@
1
+ import { ColorPickerConfig, ColorPickerComponent } from "./types";
2
+ /**
3
+ * Creates a new ColorPicker component with the specified configuration.
4
+ *
5
+ * A color picker with optional saturation/brightness area, hue slider,
6
+ * hex input, and color swatches. Supports inline, dropdown, and dialog variants.
7
+ *
8
+ * The ColorPicker component is created using a functional composition pattern,
9
+ * applying various features through the pipe function.
10
+ *
11
+ * @param {ColorPickerConfig} config - Configuration options for the color picker
12
+ * @returns {ColorPickerComponent} A fully configured color picker component instance
13
+ * @throws {Error} Throws an error if color picker creation fails
14
+ *
15
+ * @example
16
+ * ```ts
17
+ * // Inline variant (default) - always visible
18
+ * const picker = createColorPicker({
19
+ * value: '#ff5722',
20
+ * swatches: ['#f44336', '#e91e63', '#9c27b0'],
21
+ * onChange: (color) => console.log('Color:', color)
22
+ * });
23
+ * document.body.appendChild(picker.element);
24
+ *
25
+ * // Dropdown variant with trigger element
26
+ * const dropdown = createColorPicker({
27
+ * variant: 'dropdown',
28
+ * trigger: document.getElementById('color-button'),
29
+ * value: '#ff5722',
30
+ * onChange: (color) => console.log('Color:', color)
31
+ * });
32
+ *
33
+ * // Minimal picker with only swatches
34
+ * const swatchPicker = createColorPicker({
35
+ * variant: 'dropdown',
36
+ * trigger: myButton,
37
+ * showArea: false,
38
+ * showHue: false,
39
+ * showInput: false,
40
+ * swatches: ['#ff0000', '#00ff00', '#0000ff']
41
+ * });
42
+ * ```
43
+ *
44
+ * @category Components
45
+ */
46
+ declare const createColorPicker: (config?: ColorPickerConfig) => ColorPickerComponent;
47
+ export default createColorPicker;
@@ -0,0 +1,145 @@
1
+ import { ColorPickerConfig, ColorPickerState } from "./types";
2
+ /**
3
+ * Default configuration for the ColorPicker component.
4
+ * These values will be used when not explicitly specified by the user.
5
+ *
6
+ * @category Components
7
+ */
8
+ export declare const defaultConfig: ColorPickerConfig;
9
+ /**
10
+ * Creates the base configuration for ColorPicker component by merging
11
+ * user-provided config with default values.
12
+ *
13
+ * @param config - User provided configuration
14
+ * @returns Complete configuration with defaults applied
15
+ * @category Components
16
+ * @internal
17
+ */
18
+ export declare const createBaseConfig: (config?: ColorPickerConfig) => Required<Pick<ColorPickerConfig, "value" | "variant" | "size" | "swatchSize" | "showInput" | "showPreview" | "showSwatches" | "showHue" | "showArea" | "maxSwatches" | "closeOnSelect" | "disabled" | "prefix" | "componentName">> & ColorPickerConfig;
19
+ /**
20
+ * Gets the element configuration for withElement.
21
+ *
22
+ * @param config - Color picker configuration
23
+ * @returns Element configuration object
24
+ * @category Components
25
+ * @internal
26
+ */
27
+ export declare const getElementConfig: (config: ColorPickerConfig) => {
28
+ tag: string;
29
+ componentName: any;
30
+ className: string[];
31
+ style: string;
32
+ };
33
+ /**
34
+ * Gets the size dimensions based on the size configuration
35
+ *
36
+ * @param size - Size key ('s', 'm', 'l')
37
+ * @returns Dimension object with width, areaHeight, hueHeight
38
+ * @category Components
39
+ * @internal
40
+ */
41
+ export declare const getSizeDimensions: (size: string) => {
42
+ width: number;
43
+ areaHeight: number;
44
+ hueHeight: number;
45
+ };
46
+ /**
47
+ * Creates the initial state for the color picker
48
+ *
49
+ * @param config - Color picker configuration
50
+ * @returns Initial state object
51
+ * @category Components
52
+ * @internal
53
+ */
54
+ export declare const createInitialState: (config: ColorPickerConfig) => ColorPickerState;
55
+ /**
56
+ * Creates the API configuration for the ColorPicker component.
57
+ * This connects the core component features to the public API methods.
58
+ *
59
+ * @param comp - Component with features applied
60
+ * @returns API configuration object
61
+ * @category Components
62
+ * @internal
63
+ */
64
+ export declare const getApiConfig: (comp: {
65
+ element: HTMLElement;
66
+ config: ColorPickerConfig;
67
+ getClass: (name: string) => string;
68
+ emit: (event: string, data?: unknown) => void;
69
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
70
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
71
+ disabled: {
72
+ enable: () => void;
73
+ disable: () => void;
74
+ isDisabled: () => boolean;
75
+ };
76
+ lifecycle: {
77
+ destroy: () => void;
78
+ };
79
+ state?: ColorPickerState;
80
+ area?: {
81
+ updateBackground: () => void;
82
+ updateHandle: () => void;
83
+ };
84
+ hue?: {
85
+ updateHandle: () => void;
86
+ };
87
+ swatches?: {
88
+ update: () => void;
89
+ set: (swatches: unknown[]) => void;
90
+ add: (color: string, label?: string) => void;
91
+ remove: (color: string) => void;
92
+ clear: () => void;
93
+ get: () => unknown[];
94
+ };
95
+ input?: {
96
+ update: () => void;
97
+ };
98
+ variant?: {
99
+ open: () => void;
100
+ close: () => void;
101
+ toggle: () => void;
102
+ isOpen: () => boolean;
103
+ };
104
+ }) => {
105
+ element: HTMLElement;
106
+ config: ColorPickerConfig;
107
+ getClass: (name: string) => string;
108
+ emit: (event: string, data?: unknown) => void;
109
+ on: (event: string, handler: (...args: unknown[]) => void) => unknown;
110
+ off: (event: string, handler: (...args: unknown[]) => void) => unknown;
111
+ disabled: {
112
+ enable: () => void;
113
+ disable: () => void;
114
+ isDisabled: () => boolean;
115
+ };
116
+ lifecycle: {
117
+ destroy: () => void;
118
+ };
119
+ state: ColorPickerState | undefined;
120
+ area: {
121
+ updateBackground: () => void;
122
+ updateHandle: () => void;
123
+ } | undefined;
124
+ hue: {
125
+ updateHandle: () => void;
126
+ } | undefined;
127
+ swatches: {
128
+ update: () => void;
129
+ set: (swatches: unknown[]) => void;
130
+ add: (color: string, label?: string) => void;
131
+ remove: (color: string) => void;
132
+ clear: () => void;
133
+ get: () => unknown[];
134
+ } | undefined;
135
+ input: {
136
+ update: () => void;
137
+ } | undefined;
138
+ variant: {
139
+ open: () => void;
140
+ close: () => void;
141
+ toggle: () => void;
142
+ isOpen: () => boolean;
143
+ } | undefined;
144
+ };
145
+ export default defaultConfig;
@@ -0,0 +1,130 @@
1
+ /**
2
+ * Color picker component constants
3
+ * @module components/colorpicker
4
+ */
5
+ /**
6
+ * Color picker events
7
+ */
8
+ export declare const COLORPICKER_EVENTS: {
9
+ /** Fired when color value changes (committed) */
10
+ readonly CHANGE: "change";
11
+ /** Fired during color selection (live preview) */
12
+ readonly INPUT: "input";
13
+ /** Fired when a swatch is clicked */
14
+ readonly SWATCH_SELECT: "swatchSelect";
15
+ /** Fired when swatches are updated */
16
+ readonly SWATCHES_CHANGE: "swatchesChange";
17
+ /** Fired when picker opens (dropdown/dialog variants) */
18
+ readonly OPEN: "open";
19
+ /** Fired when picker closes (dropdown/dialog variants) */
20
+ readonly CLOSE: "close";
21
+ };
22
+ /**
23
+ * Color picker variants
24
+ */
25
+ export declare const COLORPICKER_VARIANTS: {
26
+ /** Inline - always visible, no trigger */
27
+ readonly INLINE: "inline";
28
+ /** Dropdown - attached to trigger, opens below/above */
29
+ readonly DROPDOWN: "dropdown";
30
+ /** Dialog - modal overlay centered on screen */
31
+ readonly DIALOG: "dialog";
32
+ };
33
+ /**
34
+ * Color picker sizes
35
+ */
36
+ export declare const COLORPICKER_SIZES: {
37
+ /** Small - 200px wide */
38
+ readonly S: "s";
39
+ /** Medium - 280px wide (default) */
40
+ readonly M: "m";
41
+ /** Large - 360px wide */
42
+ readonly L: "l";
43
+ };
44
+ /**
45
+ * Swatch sizes in pixels
46
+ */
47
+ export declare const SWATCH_SIZES: {
48
+ readonly S: 24;
49
+ readonly M: 32;
50
+ readonly L: 40;
51
+ };
52
+ /**
53
+ * CSS classes used by the color picker component
54
+ */
55
+ export declare const COLORPICKER_CLASSES: {
56
+ readonly ROOT: "colorpicker";
57
+ readonly AREA: "colorpicker__area";
58
+ readonly AREA_GRADIENT: "colorpicker__area-gradient";
59
+ readonly AREA_HANDLE: "colorpicker__area-handle";
60
+ readonly HUE: "colorpicker__hue";
61
+ readonly HUE_SLIDER: "colorpicker__hue-slider";
62
+ readonly HUE_HANDLE: "colorpicker__hue-handle";
63
+ readonly SWATCHES: "colorpicker__swatches";
64
+ readonly SWATCH: "colorpicker__swatch";
65
+ readonly SWATCH_SELECTED: "colorpicker__swatch--selected";
66
+ readonly SWATCH_ADD: "colorpicker__swatch--add";
67
+ readonly PREVIEW: "colorpicker__preview";
68
+ readonly VALUE: "colorpicker__value";
69
+ readonly VALUE_INPUT: "colorpicker__value-input";
70
+ readonly CONTAINER: "colorpicker__container";
71
+ readonly BACKDROP: "colorpicker__backdrop";
72
+ readonly DISABLED: "colorpicker--disabled";
73
+ readonly DRAGGING: "colorpicker--dragging";
74
+ readonly OPEN: "colorpicker--open";
75
+ readonly POSITION_TOP: "colorpicker--position-top";
76
+ readonly INLINE: "colorpicker--inline";
77
+ readonly DROPDOWN: "colorpicker--dropdown";
78
+ readonly DIALOG: "colorpicker--dialog";
79
+ };
80
+ /**
81
+ * Default color picker configuration
82
+ */
83
+ export declare const COLORPICKER_DEFAULTS: {
84
+ /** Default color value */
85
+ readonly VALUE: "#ff0000";
86
+ /** Default component size */
87
+ readonly SIZE: "m";
88
+ /** Default swatch size */
89
+ readonly SWATCH_SIZE: 32;
90
+ /** Whether to show the hex input */
91
+ readonly SHOW_INPUT: true;
92
+ /** Whether to show the preview square */
93
+ readonly SHOW_PREVIEW: true;
94
+ /** Whether to show swatches */
95
+ readonly SHOW_SWATCHES: true;
96
+ /** Maximum number of swatches */
97
+ readonly MAX_SWATCHES: 8;
98
+ /** Default variant */
99
+ readonly VARIANT: "inline";
100
+ /** Close on swatch select (dropdown/dialog) */
101
+ readonly CLOSE_ON_SELECT: true;
102
+ };
103
+ /**
104
+ * Size dimensions in pixels
105
+ */
106
+ export declare const SIZE_DIMENSIONS: {
107
+ readonly s: {
108
+ readonly width: 200;
109
+ readonly areaHeight: 120;
110
+ readonly hueHeight: 16;
111
+ };
112
+ readonly m: {
113
+ readonly width: 280;
114
+ readonly areaHeight: 160;
115
+ readonly hueHeight: 20;
116
+ };
117
+ readonly l: {
118
+ readonly width: 360;
119
+ readonly areaHeight: 200;
120
+ readonly hueHeight: 24;
121
+ };
122
+ };
123
+ /**
124
+ * Vibrant palette swatch keys in order of preference
125
+ */
126
+ export declare const PALETTE_SWATCH_ORDER: readonly ["Vibrant", "Muted", "DarkVibrant", "DarkMuted", "LightVibrant", "LightMuted"];
127
+ export type ColorPickerEvent = (typeof COLORPICKER_EVENTS)[keyof typeof COLORPICKER_EVENTS];
128
+ export type ColorPickerSize = (typeof COLORPICKER_SIZES)[keyof typeof COLORPICKER_SIZES];
129
+ export type ColorPickerVariant = (typeof COLORPICKER_VARIANTS)[keyof typeof COLORPICKER_VARIANTS];
130
+ export type SwatchSize = (typeof SWATCH_SIZES)[keyof typeof SWATCH_SIZES];
@@ -0,0 +1,27 @@
1
+ import { ColorPickerConfig, ColorPickerState } from "../types";
2
+ /**
3
+ * Area feature interface
4
+ */
5
+ export interface AreaFeature {
6
+ element: HTMLElement;
7
+ gradient: HTMLElement;
8
+ handle: HTMLElement;
9
+ updateBackground: () => void;
10
+ updateHandle: () => void;
11
+ }
12
+ /**
13
+ * Adds the saturation/brightness area to a color picker component
14
+ *
15
+ * @param config - Color picker configuration
16
+ * @returns Component enhancer function
17
+ */
18
+ export declare const withArea: (config: ColorPickerConfig) => <T extends {
19
+ element: HTMLElement;
20
+ getClass: (name: string) => string;
21
+ emit: (event: string, data?: unknown) => void;
22
+ state?: ColorPickerState;
23
+ pickerContent?: HTMLElement;
24
+ }>(component: T) => T & {
25
+ area: AreaFeature;
26
+ state: ColorPickerState;
27
+ };
@@ -0,0 +1,31 @@
1
+ import { createSlider } from "mtrl";
2
+ import { ColorPickerConfig, ColorPickerState } from "../types";
3
+ /**
4
+ * Hue feature interface
5
+ */
6
+ export interface HueFeature {
7
+ element: HTMLElement;
8
+ slider: ReturnType<typeof createSlider>;
9
+ updateHandle: () => void;
10
+ }
11
+ /**
12
+ * Adds the hue slider to a color picker component using mtrl slider
13
+ *
14
+ * @param config - Color picker configuration
15
+ * @returns Component enhancer function
16
+ */
17
+ export declare const withHue: (config: ColorPickerConfig) => <T extends {
18
+ element: HTMLElement;
19
+ getClass: (name: string) => string;
20
+ emit: (event: string, data?: unknown) => void;
21
+ state?: ColorPickerState;
22
+ pickerContent?: HTMLElement;
23
+ area?: {
24
+ element: HTMLElement;
25
+ updateBackground: () => void;
26
+ updateHandle: () => void;
27
+ };
28
+ }>(component: T) => T & {
29
+ hue: HueFeature;
30
+ state: ColorPickerState;
31
+ };
@@ -0,0 +1,14 @@
1
+ /**
2
+ * ColorPicker features module
3
+ * @module components/colorpicker/features
4
+ */
5
+ export { withArea } from "./area";
6
+ export type { AreaFeature } from "./area";
7
+ export { withHue } from "./hue";
8
+ export type { HueFeature } from "./hue";
9
+ export { withSwatches } from "./swatches";
10
+ export type { SwatchesFeature } from "./swatches";
11
+ export { withInput } from "./input";
12
+ export type { InputFeature } from "./input";
13
+ export { withVariant } from "./variant";
14
+ export type { VariantFeature } from "./variant";
@@ -0,0 +1,37 @@
1
+ import { createTextfield } from "mtrl";
2
+ import { ColorPickerConfig, ColorPickerState } from "../types";
3
+ /**
4
+ * Input feature interface
5
+ */
6
+ export interface InputFeature {
7
+ element: HTMLElement;
8
+ textfield: ReturnType<typeof createTextfield> | null;
9
+ preview: HTMLElement | null;
10
+ update: () => void;
11
+ }
12
+ /**
13
+ * Adds the hex input field (using mtrl textfield) and color preview to a color picker component
14
+ *
15
+ * @param config - Color picker configuration
16
+ * @returns Component enhancer function
17
+ */
18
+ export declare const withInput: (config: ColorPickerConfig) => <T extends {
19
+ element: HTMLElement;
20
+ getClass: (name: string) => string;
21
+ emit: (event: string, data?: unknown) => void;
22
+ state?: ColorPickerState;
23
+ pickerContent?: HTMLElement;
24
+ variant?: {
25
+ close: () => void;
26
+ };
27
+ area?: {
28
+ updateBackground: () => void;
29
+ updateHandle: () => void;
30
+ };
31
+ hue?: {
32
+ updateHandle: () => void;
33
+ };
34
+ }>(component: T) => T & {
35
+ input: InputFeature;
36
+ state: ColorPickerState;
37
+ };
@@ -0,0 +1,39 @@
1
+ import { ColorPickerState } from "../types";
2
+ /**
3
+ * Component interface for popup feature
4
+ */
5
+ interface PopupComponent {
6
+ element: HTMLElement;
7
+ state: ColorPickerState;
8
+ getClass: (name: string) => string;
9
+ emit: (event: string, ...args: unknown[]) => void;
10
+ config: {
11
+ variant?: string;
12
+ trigger?: HTMLElement;
13
+ disabled?: boolean;
14
+ prefix?: string;
15
+ };
16
+ pickerContent: HTMLElement;
17
+ updateUI?: () => void;
18
+ }
19
+ /**
20
+ * Popup feature interface
21
+ */
22
+ export interface PopupFeature {
23
+ backdrop: HTMLElement | null;
24
+ popup: HTMLElement;
25
+ open: () => void;
26
+ close: () => void;
27
+ toggle: () => void;
28
+ isOpen: () => boolean;
29
+ position: () => void;
30
+ }
31
+ /**
32
+ * Adds popup functionality (dropdown/dialog) to a color picker component
33
+ *
34
+ * @returns Component enhancer function
35
+ */
36
+ export declare const withPopup: () => <T extends PopupComponent>(component: T) => T & {
37
+ popup: PopupFeature;
38
+ };
39
+ export {};
@@ -0,0 +1,42 @@
1
+ import { ColorPickerConfig, ColorPickerState, ColorSwatch } from "../types";
2
+ /**
3
+ * Swatches feature interface
4
+ */
5
+ export interface SwatchesFeature {
6
+ element: HTMLElement;
7
+ update: () => void;
8
+ set: (swatches: string[] | ColorSwatch[]) => void;
9
+ add: (color: string, label?: string) => void;
10
+ remove: (color: string) => void;
11
+ clear: () => void;
12
+ get: () => ColorSwatch[];
13
+ }
14
+ /**
15
+ * Adds swatches (color presets) to a color picker component
16
+ *
17
+ * @param config - Color picker configuration
18
+ * @returns Component enhancer function
19
+ */
20
+ export declare const withSwatches: (config: ColorPickerConfig) => <T extends {
21
+ element: HTMLElement;
22
+ getClass: (name: string) => string;
23
+ emit: (event: string, data?: unknown) => void;
24
+ state?: ColorPickerState;
25
+ pickerContent?: HTMLElement;
26
+ variant?: {
27
+ close: () => void;
28
+ };
29
+ area?: {
30
+ updateBackground: () => void;
31
+ updateHandle: () => void;
32
+ };
33
+ hue?: {
34
+ updateHandle: () => void;
35
+ };
36
+ input?: {
37
+ update: () => void;
38
+ };
39
+ }>(component: T) => T & {
40
+ swatches: SwatchesFeature;
41
+ state: ColorPickerState;
42
+ };
@@ -0,0 +1,30 @@
1
+ import { ColorPickerConfig, ColorPickerState } from "../types";
2
+ /**
3
+ * Variant feature interface
4
+ */
5
+ export interface VariantFeature {
6
+ backdrop: HTMLElement | null;
7
+ container: HTMLElement;
8
+ open: () => void;
9
+ close: () => void;
10
+ toggle: () => void;
11
+ isOpen: () => boolean;
12
+ position: () => void;
13
+ }
14
+ /**
15
+ * Adds variant-specific behavior (dropdown/dialog) to a color picker component.
16
+ * Handles positioning, open/close, backdrop, and event management that CSS cannot handle.
17
+ *
18
+ * @param config - Color picker configuration
19
+ * @returns Component enhancer function
20
+ */
21
+ export declare const withVariant: (config: ColorPickerConfig) => <T extends {
22
+ element: HTMLElement;
23
+ getClass: (name: string) => string;
24
+ emit: (event: string, data?: unknown) => void;
25
+ state?: ColorPickerState;
26
+ }>(component: T) => T & {
27
+ variant: VariantFeature;
28
+ pickerContent: HTMLElement;
29
+ state: ColorPickerState;
30
+ };
@@ -0,0 +1,20 @@
1
+ /**
2
+ * ColorPicker component module
3
+ * @module components/colorpicker
4
+ */
5
+ export { default as createColorPicker } from "./colorpicker";
6
+ export { COLORPICKER_EVENTS, COLORPICKER_SIZES, COLORPICKER_VARIANTS, COLORPICKER_CLASSES, COLORPICKER_DEFAULTS, SWATCH_SIZES, SIZE_DIMENSIONS, PALETTE_SWATCH_ORDER, } from "./constants";
7
+ export type { ColorPickerConfig, ColorPickerComponent, ColorPickerState, HSVColor, RGBColor, ColorSwatch, } from "./types";
8
+ export { defaultConfig, createBaseConfig, getSizeDimensions, createInitialState, getApiConfig, } from "./config";
9
+ export { hsvToRgb, rgbToHsv, hsvToHex, hexToHsv, rgbToHex, hexToRgb, isValidHex, normalizeHex, getContrastColor, clamp, } from "./utils";
10
+ export { withArea } from "./features/area";
11
+ export type { AreaFeature } from "./features/area";
12
+ export { withHue } from "./features/hue";
13
+ export type { HueFeature } from "./features/hue";
14
+ export { withSwatches } from "./features/swatches";
15
+ export type { SwatchesFeature } from "./features/swatches";
16
+ export { withInput } from "./features/input";
17
+ export type { InputFeature } from "./features/input";
18
+ export { withVariant } from "./features/variant";
19
+ export type { VariantFeature } from "./features/variant";
20
+ export { withAPI } from "./api";