@nixxie-cms/fields-color 1.0.1

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/LICENSE ADDED
@@ -0,0 +1,23 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Nixxie International DMCC
4
+ Portions Copyright (c) 2023 Thinkmill Labs Pty Ltd and contributors
5
+ (this software is derived from the KeystoneJS project, https://keystonejs.com)
6
+
7
+ Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ of this software and associated documentation files (the "Software"), to deal
9
+ in the Software without restriction, including without limitation the rights
10
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ copies of the Software, and to permit persons to whom the Software is
12
+ furnished to do so, subject to the following conditions:
13
+
14
+ The above copyright notice and this permission notice shall be included in all
15
+ copies or substantial portions of the Software.
16
+
17
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,12 @@
1
+ # @nixxie-cms/fields-color
2
+
3
+ A colour field for Nixxie CMS. Stores a validated, lowercased CSS hex value (`#rgb`, `#rrggbb` or
4
+ `#rrggbbaa`).
5
+
6
+ ```ts
7
+ import { color } from '@nixxie-cms/fields-color'
8
+
9
+ fields: {
10
+ brandColor: color({ validation: { isRequired: true } }),
11
+ }
12
+ ```
@@ -0,0 +1,14 @@
1
+ import type { TextFieldConfig } from '@nixxie-cms/core/fields';
2
+ import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types';
3
+ export type ColorFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<TextFieldConfig<ListTypeInfo>, 'validation'> & {
4
+ validation?: {
5
+ /** Reject saving when no value is present. */
6
+ isRequired?: boolean;
7
+ };
8
+ };
9
+ /**
10
+ * A colour field storing a CSS hex value (`#rgb`, `#rrggbb` or `#rrggbbaa`). Built on the core
11
+ * `text` field with hex validation. Values are normalised to lowercase.
12
+ */
13
+ export declare function color<ListTypeInfo extends BaseListTypeInfo>(config?: ColorFieldConfig<ListTypeInfo>): FieldTypeFunc<ListTypeInfo>;
14
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"../../../src","sources":["index.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,yBAAyB,CAAA;AAC9D,OAAO,KAAK,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,wBAAwB,CAAA;AAI7E,MAAM,MAAM,gBAAgB,CAAC,YAAY,SAAS,gBAAgB,IAAI,IAAI,CACxE,eAAe,CAAC,YAAY,CAAC,EAC7B,YAAY,CACb,GAAG;IACF,UAAU,CAAC,EAAE;QACX,8CAA8C;QAC9C,UAAU,CAAC,EAAE,OAAO,CAAA;KACrB,CAAA;CACF,CAAA;AAED;;;GAGG;AACH,wBAAgB,KAAK,CAAC,YAAY,SAAS,gBAAgB,EACzD,MAAM,GAAE,gBAAgB,CAAC,YAAY,CAAM,GAC1C,aAAa,CAAC,YAAY,CAAC,CAuB7B"}
@@ -0,0 +1,2 @@
1
+ export * from "./declarations/src/index.js";
2
+ //# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibml4eGllLWNtcy1maWVsZHMtY29sb3IuY2pzLmQudHMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyIuL2RlY2xhcmF0aW9ucy9zcmMvaW5kZXguZC50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiQUFBQSJ9
@@ -0,0 +1,50 @@
1
+ 'use strict';
2
+
3
+ Object.defineProperty(exports, '__esModule', { value: true });
4
+
5
+ var fields = require('@nixxie-cms/core/fields');
6
+
7
+ const HEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
8
+ /**
9
+ * A colour field storing a CSS hex value (`#rgb`, `#rrggbb` or `#rrggbbaa`). Built on the core
10
+ * `text` field with hex validation. Values are normalised to lowercase.
11
+ */
12
+ function color(config = {}) {
13
+ const {
14
+ hooks,
15
+ validation,
16
+ ...rest
17
+ } = config;
18
+ return fields.text({
19
+ ...rest,
20
+ validation: {
21
+ isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired
22
+ },
23
+ ui: {
24
+ description: 'Hex colour, e.g. #1a2b3c',
25
+ ...rest.ui
26
+ },
27
+ hooks: {
28
+ ...hooks,
29
+ resolveInput: args => {
30
+ const v = args.resolvedData[args.fieldKey];
31
+ return typeof v === 'string' ? v.toLowerCase() : v;
32
+ },
33
+ validate: args => {
34
+ const {
35
+ resolvedData,
36
+ fieldKey,
37
+ addValidationError,
38
+ operation
39
+ } = args;
40
+ if (operation === 'delete') return;
41
+ const value = resolvedData[fieldKey];
42
+ if (typeof value === 'string' && value.length > 0 && !HEX.test(value)) {
43
+ addValidationError(`${fieldKey} must be a hex colour like #1a2b3c`);
44
+ }
45
+ }
46
+ }
47
+ });
48
+ }
49
+
50
+ exports.color = color;
@@ -0,0 +1,46 @@
1
+ import { text } from '@nixxie-cms/core/fields';
2
+
3
+ const HEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/;
4
+ /**
5
+ * A colour field storing a CSS hex value (`#rgb`, `#rrggbb` or `#rrggbbaa`). Built on the core
6
+ * `text` field with hex validation. Values are normalised to lowercase.
7
+ */
8
+ function color(config = {}) {
9
+ const {
10
+ hooks,
11
+ validation,
12
+ ...rest
13
+ } = config;
14
+ return text({
15
+ ...rest,
16
+ validation: {
17
+ isRequired: validation === null || validation === void 0 ? void 0 : validation.isRequired
18
+ },
19
+ ui: {
20
+ description: 'Hex colour, e.g. #1a2b3c',
21
+ ...rest.ui
22
+ },
23
+ hooks: {
24
+ ...hooks,
25
+ resolveInput: args => {
26
+ const v = args.resolvedData[args.fieldKey];
27
+ return typeof v === 'string' ? v.toLowerCase() : v;
28
+ },
29
+ validate: args => {
30
+ const {
31
+ resolvedData,
32
+ fieldKey,
33
+ addValidationError,
34
+ operation
35
+ } = args;
36
+ if (operation === 'delete') return;
37
+ const value = resolvedData[fieldKey];
38
+ if (typeof value === 'string' && value.length > 0 && !HEX.test(value)) {
39
+ addValidationError(`${fieldKey} must be a hex colour like #1a2b3c`);
40
+ }
41
+ }
42
+ }
43
+ });
44
+ }
45
+
46
+ export { color };
package/package.json ADDED
@@ -0,0 +1,33 @@
1
+ {
2
+ "name": "@nixxie-cms/fields-color",
3
+ "version": "1.0.1",
4
+ "license": "MIT",
5
+ "main": "dist/nixxie-cms-fields-color.cjs.js",
6
+ "module": "dist/nixxie-cms-fields-color.esm.js",
7
+ "exports": {
8
+ ".": {
9
+ "types": "./dist/nixxie-cms-fields-color.cjs.js",
10
+ "module": "./dist/nixxie-cms-fields-color.esm.js",
11
+ "default": "./dist/nixxie-cms-fields-color.cjs.js"
12
+ },
13
+ "./package.json": "./package.json"
14
+ },
15
+ "dependencies": {
16
+ "@babel/runtime": "^7.24.7"
17
+ },
18
+ "devDependencies": {
19
+ "@nixxie-cms/core": "^1.0.1"
20
+ },
21
+ "peerDependencies": {
22
+ "@nixxie-cms/core": "^1.0.1"
23
+ },
24
+ "preconstruct": {
25
+ "entrypoints": [
26
+ "index.ts"
27
+ ]
28
+ },
29
+ "repository": {
30
+ "type": "git",
31
+ "url": "https://github.com/nixxiecms/nixxie/tree/main/packages/fields-color"
32
+ }
33
+ }
package/src/index.ts ADDED
@@ -0,0 +1,46 @@
1
+ import { text } from '@nixxie-cms/core/fields'
2
+ import type { TextFieldConfig } from '@nixxie-cms/core/fields'
3
+ import type { BaseListTypeInfo, FieldTypeFunc } from '@nixxie-cms/core/types'
4
+
5
+ const HEX = /^#(?:[0-9a-fA-F]{3}|[0-9a-fA-F]{6}|[0-9a-fA-F]{8})$/
6
+
7
+ export type ColorFieldConfig<ListTypeInfo extends BaseListTypeInfo> = Omit<
8
+ TextFieldConfig<ListTypeInfo>,
9
+ 'validation'
10
+ > & {
11
+ validation?: {
12
+ /** Reject saving when no value is present. */
13
+ isRequired?: boolean
14
+ }
15
+ }
16
+
17
+ /**
18
+ * A colour field storing a CSS hex value (`#rgb`, `#rrggbb` or `#rrggbbaa`). Built on the core
19
+ * `text` field with hex validation. Values are normalised to lowercase.
20
+ */
21
+ export function color<ListTypeInfo extends BaseListTypeInfo>(
22
+ config: ColorFieldConfig<ListTypeInfo> = {}
23
+ ): FieldTypeFunc<ListTypeInfo> {
24
+ const { hooks, validation, ...rest } = config
25
+
26
+ return text<ListTypeInfo>({
27
+ ...(rest as TextFieldConfig<ListTypeInfo>),
28
+ validation: { isRequired: validation?.isRequired },
29
+ ui: { description: 'Hex colour, e.g. #1a2b3c', ...rest.ui },
30
+ hooks: {
31
+ ...hooks,
32
+ resolveInput: (args: any) => {
33
+ const v = args.resolvedData[args.fieldKey]
34
+ return typeof v === 'string' ? v.toLowerCase() : v
35
+ },
36
+ validate: (args: any) => {
37
+ const { resolvedData, fieldKey, addValidationError, operation } = args
38
+ if (operation === 'delete') return
39
+ const value = resolvedData[fieldKey]
40
+ if (typeof value === 'string' && value.length > 0 && !HEX.test(value)) {
41
+ addValidationError(`${fieldKey} must be a hex colour like #1a2b3c`)
42
+ }
43
+ },
44
+ },
45
+ })
46
+ }