@rokkit/core 1.0.0-next.99 → 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/README.md CHANGED
@@ -1 +1,121 @@
1
- # Core Components
1
+ # @rokkit/core
2
+
3
+ Core utilities for the Rokkit design system — field mapping, constants, calendar, color palette, and event utilities.
4
+
5
+ ## Install
6
+
7
+ ```sh
8
+ npm install @rokkit/core
9
+ # or
10
+ bun add @rokkit/core
11
+ ```
12
+
13
+ ## Overview
14
+
15
+ `@rokkit/core` is the foundation layer shared across all Rokkit packages. It provides:
16
+
17
+ - **Field mapping** — map arbitrary data object shapes to semantic field names
18
+ - **Constants** — `BASE_FIELDS`, `DEFAULT_STATE_ICONS`, `DEFAULT_KEYMAP`, and more
19
+ - **Color palette** — default colors, shades, and theme color mapping
20
+ - **Calendar utilities** — calendar day generation and weekday labels
21
+ - **Event utilities** — lightweight emitter factory
22
+ - **Direction detection** — RTL/LTR helpers
23
+ - **Tick generation** — axis tick utilities for chart packages
24
+
25
+ No runtime dependencies on other `@rokkit/*` packages.
26
+
27
+ ## Usage
28
+
29
+ ### Field mapping
30
+
31
+ `FieldMapper` maps semantic field names (`label`, `value`, `icon`, etc.) to the actual keys present in your data objects. Pass a fields override to adapt any data shape.
32
+
33
+ ```js
34
+ import { FieldMapper } from '@rokkit/core'
35
+
36
+ const mapper = new FieldMapper({ label: 'name', value: 'id', icon: 'iconClass' })
37
+
38
+ const item = { id: 42, name: 'Settings', iconClass: 'i-settings' }
39
+
40
+ mapper.get('label', item) // 'Settings'
41
+ mapper.get('value', item) // 42
42
+ mapper.get('icon', item) // 'i-settings'
43
+ ```
44
+
45
+ ### normalizeFields
46
+
47
+ Merge user-supplied field overrides with `BASE_FIELDS` defaults, remapping any legacy key names automatically.
48
+
49
+ ```js
50
+ import { normalizeFields, BASE_FIELDS } from '@rokkit/core'
51
+
52
+ const fields = normalizeFields({ label: 'title', value: 'slug' })
53
+ // { label: 'title', value: 'slug', icon: 'icon', children: 'children', ... }
54
+ ```
55
+
56
+ ### BASE_FIELDS reference
57
+
58
+ ```js
59
+ import { BASE_FIELDS } from '@rokkit/core'
60
+
61
+ // {
62
+ // id: 'id', value: 'value', label: 'label',
63
+ // icon: 'icon', avatar: 'image', subtext: 'description',
64
+ // tooltip: 'title', badge: 'badge', shortcut: 'shortcut',
65
+ // children: 'children', type: 'type', snippet: 'snippet',
66
+ // href: 'href', hrefTarget: 'target',
67
+ // disabled: 'disabled', expanded: 'expanded', selected: 'selected'
68
+ // }
69
+ ```
70
+
71
+ ### Event emitter
72
+
73
+ ```js
74
+ import { createEmitter } from '@rokkit/core'
75
+
76
+ const emitter = createEmitter()
77
+ emitter.on('select', (item) => console.log('selected', item))
78
+ emitter.emit('select', myItem)
79
+ ```
80
+
81
+ ### Calendar utilities
82
+
83
+ ```js
84
+ import { getCalendarDays, weekdays } from '@rokkit/core'
85
+
86
+ const days = getCalendarDays(2025, 2) // February 2025
87
+ // weekdays: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
88
+ ```
89
+
90
+ ### Tick generation
91
+
92
+ ```js
93
+ import { generateTicks } from '@rokkit/core'
94
+
95
+ const ticks = generateTicks(0, 100, 5)
96
+ // [0, 25, 50, 75, 100]
97
+ ```
98
+
99
+ ## API
100
+
101
+ | Export | Description |
102
+ | -------------------------------- | ------------------------------------------------------------- |
103
+ | `BASE_FIELDS` | Canonical semantic-to-data-key field mapping |
104
+ | `normalizeFields(fields)` | Merge overrides with BASE_FIELDS defaults |
105
+ | `FieldMapper` | Class that resolves semantic field reads from data objects |
106
+ | `DEFAULT_STATE_ICONS` | Icon name map grouped by category (action, state, sort, etc.) |
107
+ | `DEFAULT_KEYMAP` | Default keyboard navigation key mapping |
108
+ | `DEFAULT_THEME_MAPPING` | Default semantic color → palette color mapping |
109
+ | `createEmitter()` | Lightweight event emitter factory |
110
+ | `getCalendarDays(year, month)` | Calendar day array for a given month |
111
+ | `weekdays` | Localized weekday label array |
112
+ | `generateTicks(min, max, count)` | Evenly spaced axis tick values |
113
+ | `defaultColors` | Default color palette array |
114
+ | `shades` | Available shade scale values |
115
+ | `ITEM_SNIPPET` | Constant: default item snippet key (`'itemContent'`) |
116
+ | `GROUP_SNIPPET` | Constant: default group snippet key (`'groupContent'`) |
117
+ | `stateIconsFromNames(names)` | Build a state icon map from an array of icon name strings |
118
+
119
+ ---
120
+
121
+ Part of [Rokkit](https://github.com/jerrythomas/rokkit) — a Svelte 5 component library and design system.
package/package.json CHANGED
@@ -1,59 +1,51 @@
1
1
  {
2
2
  "name": "@rokkit/core",
3
- "version": "1.0.0-next.99",
4
- "description": "Core components, actions and stores for svelte apps.",
3
+ "version": "1.0.1",
4
+ "description": "Contains core utility functions and classes that can be used in various components.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/jerrythomas/rokkit.git"
8
+ },
5
9
  "author": "Jerry Thomas <me@jerrythomas.name>",
6
10
  "license": "MIT",
7
- "main": "src/index.js",
8
- "module": "src/index.js",
9
- "types": "dist/index.d.ts",
10
11
  "type": "module",
11
12
  "publishConfig": {
12
13
  "access": "public"
13
14
  },
14
- "devDependencies": {
15
- "@sveltejs/vite-plugin-svelte": "^3.1.2",
16
- "@testing-library/svelte": "^5.2.1",
17
- "@types/ramda": "^0.30.2",
18
- "@vitest/coverage-v8": "^2.1.1",
19
- "@vitest/ui": "~2.1.1",
20
- "jsdom": "^25.0.0",
21
- "svelte": "^4.2.19",
22
- "typescript": "^5.6.2",
23
- "vite": "^5.4.6",
24
- "vitest": "~2.1.1",
25
- "shared-config": "1.0.0-next.99",
26
- "validators": "1.0.0-next.99"
15
+ "scripts": {
16
+ "prepublishOnly": "cp ../../LICENSE . && bunx tsc --project tsconfig.build.json",
17
+ "postpublish": "rm -f LICENSE",
18
+ "clean": "rm -rf dist",
19
+ "build": "bun clean && bun prepublishOnly"
27
20
  },
28
21
  "files": [
29
22
  "src/**/*.js",
23
+ "src/**/*.ts",
30
24
  "src/**/*.json",
31
- "src/**/*.svelte",
32
- "!spec"
25
+ "dist/**/*.d.ts",
26
+ "README.md",
27
+ "package.json",
28
+ "LICENSE"
33
29
  ],
34
30
  "exports": {
35
31
  "./src": "./src",
36
32
  "./package.json": "./package.json",
37
- "./constants": "./src/constants.js",
38
33
  ".": {
39
34
  "types": "./dist/index.d.ts",
40
35
  "import": "./src/index.js",
41
36
  "svelte": "./src/index.js"
37
+ },
38
+ "./vite": {
39
+ "types": "./dist/vite/index.d.ts",
40
+ "import": "./src/vite/index.js"
42
41
  }
43
42
  },
44
43
  "dependencies": {
45
- "date-fns": "^4.0.0",
46
- "ramda": "^0.30.1"
44
+ "@unocss/preset-mini": "^66.6.1",
45
+ "date-fns": "^4.1.0",
46
+ "ramda": "^0.32.0"
47
47
  },
48
- "scripts": {
49
- "format": "prettier --write .",
50
- "lint": "eslint --fix .",
51
- "test:ct": "playwright test -c playwright.config.js",
52
- "test:ci": "vitest run",
53
- "test:ui": "vitest --ui",
54
- "test": "vitest",
55
- "coverage": "vitest run --coverage",
56
- "latest": "pnpm upgrade --latest && pnpm test:ci",
57
- "release": "pnpm publish --access public"
48
+ "devDependencies": {
49
+ "@rokkit/icons": "1.0.1"
58
50
  }
59
- }
51
+ }
@@ -0,0 +1,16 @@
1
+ {
2
+ "shark": {
3
+ "DEFAULT": "#828C9F",
4
+ "50": "#ffffff",
5
+ "100": "#efefef",
6
+ "200": "#E0E0E0",
7
+ "300": "#C1C6D0",
8
+ "400": "#828C9F",
9
+ "500": "#677287",
10
+ "600": "#51596A",
11
+ "700": "#3B414D",
12
+ "800": "#2E323C",
13
+ "900": "#20242A",
14
+ "950": "#1A1C22"
15
+ }
16
+ }
@@ -0,0 +1,24 @@
1
+ // eslint-disable-next-line @typescript-eslint/ban-ts-comment
2
+ // @ts-nocheck
3
+ import { colors } from '@unocss/preset-mini/colors'
4
+ import type { PresetMiniColors } from '@unocss/preset-mini/colors'
5
+ import syntaxColorPalette from './syntax.json' with { type: 'json' }
6
+ import extraColors from './extra.json' with { type: 'json' }
7
+
8
+ export const shades = [50, 100, 200, 300, 400, 500, 600, 700, 800, 900, 950]
9
+ export const defaultPalette = [
10
+ 'surface',
11
+ 'primary',
12
+ 'secondary',
13
+ 'accent',
14
+ 'success',
15
+ 'warning',
16
+ 'danger',
17
+ 'info'
18
+ ]
19
+
20
+ export const syntaxColors = syntaxColorPalette
21
+ export const defaultColors: PresetMiniColors & typeof extraColors = {
22
+ ...colors,
23
+ ...extraColors
24
+ }
@@ -2,7 +2,7 @@
2
2
  "one-dark": {
3
3
  "dark": {
4
4
  "--tab-size": "2",
5
- "--code-fill": "var(--neutral-100)",
5
+ "--code-fill": "var(--surface-100)",
6
6
  "--code-normal": "#e06c75",
7
7
  "--code-string": "#98c379",
8
8
  "--code-number": "#d19a66",
@@ -14,14 +14,14 @@
14
14
  "--code-operator": "#56b6c2",
15
15
  "--code-function": "#61afef",
16
16
  "--code-gutter-marker": "black",
17
- "--code-gutter-subtle": "#999",
17
+ "--code-gutter-z2": "#999",
18
18
  "--code-cursor": "#24292e",
19
19
  "--code-cursor-block": "rgba(20, 255, 20, 0.5)",
20
20
  "--code-linenumbers": "rgba(27, 31, 35, 0.3)"
21
21
  },
22
22
  "light": {
23
23
  "--tab-size": "2",
24
- "--code-fill": "var(--neutral-100)",
24
+ "--code-fill": "var(--surface-100)",
25
25
  "--code-normal": "#333333",
26
26
  "--code-string": "#9D8248",
27
27
  "--code-number": "#71A15D",
@@ -33,7 +33,7 @@
33
33
  "--code-operator": "#bf5625",
34
34
  "--code-function": "#a71d5d",
35
35
  "--code-gutter-marker": "black",
36
- "--code-gutter-subtle": "#999",
36
+ "--code-gutter-z2": "#999",
37
37
  "--code-cursor": "#24292e",
38
38
  "--code-cursor-block": "rgba(20, 255, 20, 0.5)",
39
39
  "--code-linenumbers": "rgba(27, 31, 35, 0.3)"
@@ -271,19 +271,5 @@
271
271
  "800": "#27272a",
272
272
  "900": "#18181b",
273
273
  "950": "#000000"
274
- },
275
- "shark": {
276
- "DEFAULT": "#677287",
277
- "50": "#ffffff",
278
- "100": "#efefef",
279
- "200": "#E0E0E0",
280
- "300": "#C1C6D0",
281
- "400": "#828C9F",
282
- "500": "#677287",
283
- "600": "#51596A",
284
- "700": "#3B414D",
285
- "800": "#2E323C",
286
- "900": "#20242A",
287
- "950": "#1A1C22"
288
274
  }
289
275
  }
package/src/connector.js CHANGED
@@ -1,29 +1,34 @@
1
+ const LINE_TYPES = {
2
+ CHILD: 'child',
3
+ LAST: 'last',
4
+ SIBLING: 'sibling',
5
+ EMPTY: 'empty',
6
+ ICON: 'icon'
7
+ }
8
+
1
9
  const nextType = {
2
- child: 'sibling',
3
- last: 'empty',
4
- sibling: 'sibling',
5
- empty: 'empty'
10
+ [LINE_TYPES.CHILD]: LINE_TYPES.SIBLING,
11
+ [LINE_TYPES.LAST]: LINE_TYPES.EMPTY,
12
+ [LINE_TYPES.SIBLING]: LINE_TYPES.SIBLING,
13
+ [LINE_TYPES.EMPTY]: LINE_TYPES.EMPTY
6
14
  }
7
15
 
8
16
  /**
9
- * Constructs an array of line types based on the provided parameters.
10
- *
11
- * @param {boolean} hasChildren - Indicates whether the current item has children, affecting the line type for the item itself.
12
- * @param {Array<string>} parentTypes - An array of types from the parent indicating the line type context.
13
- * @param {string} position - The line type to use for the current position (usually 'child' or 'last').
14
- * @returns {Array<string>} An array of line types.
17
+ * Constructs an array of line types for tree visualization
18
+ * @param {boolean} hasChildren - Whether the node has children
19
+ * @param {import('./types').LineType[]} parentTypes - Types from parent nodes
20
+ * @param {import('./types').LineType} position - Current position type
21
+ * @returns {import('./types').LineType[]} Array of line types
15
22
  */
16
- export function getLineTypes(hasChildren = false, parentTypes = [], position = 'child') {
17
- // Map each parent type to a line type, except for the last element.
18
- const types = parentTypes.slice(0, -1).map((type) => nextType[type])
19
-
20
- // Append the position type for the last parent, if any.
21
- if (parentTypes.length > 0) {
22
- types.push(position)
23
- }
24
-
25
- // Append the line type based on whether the current item has children.
26
- types.push(hasChildren ? 'icon' : 'empty')
27
-
28
- return types
23
+ export function getLineTypes(hasChildren = false, parentTypes = [], position = LINE_TYPES.CHILD) {
24
+ return parentTypes
25
+ .reduce((acc, type, index) => {
26
+ // For all but the last parent type, convert to next type
27
+ if (index < parentTypes.length - 1) {
28
+ return [...acc, nextType[type]]
29
+ }
30
+ // For the last parent type, use the position
31
+ return [...acc, position]
32
+ }, [])
33
+ .concat(hasChildren ? LINE_TYPES.ICON : LINE_TYPES.EMPTY)
29
34
  }
package/src/constants.js CHANGED
@@ -1,42 +1,96 @@
1
- export { defaultColors, syntaxColors, shades, defaultPalette } from './colors'
1
+ export { defaultColors, syntaxColors, shades, defaultPalette } from './colors/index'
2
+ export const DATA_IMAGE_REGEX = /^data:image\/(jpeg|png|gif|bmp|webp|svg\+xml)/i
2
3
 
4
+ // ─── Snippet names ────────────────────────────────────────────────────────────
5
+
6
+ export const ITEM_SNIPPET = 'itemContent'
7
+ export const GROUP_SNIPPET = 'groupContent'
3
8
  /**
9
+ * @deprecated Use BASE_FIELDS from @rokkit/core instead.
10
+ * Retained for legacy ListController/FieldMapper/Proxy consumers (Toolbar, Table).
11
+ * Will be removed when those components migrate to Wrapper+Navigator.
4
12
  * @type {import('./types).FieldMapping} Fields
5
13
  */
6
- export const defaultFields = {
14
+ export const DEFAULT_FIELDS = {
7
15
  id: 'id',
8
- url: 'url',
16
+ href: 'href',
17
+ icon: 'icon',
9
18
  text: 'text',
10
19
  value: 'value',
20
+ keywords: 'keywords',
11
21
  children: 'children',
12
- icon: 'icon',
13
22
  iconPrefix: null,
14
23
  image: 'image',
15
- component: 'component',
16
24
  summary: 'summary',
17
25
  notes: 'notes',
18
26
  props: 'props',
19
27
  target: 'target',
20
28
  state: 'state',
21
- isOpen: '_open',
22
- isDeleted: '_deleted',
23
29
  level: 'level',
24
30
  parent: 'parent',
25
- currency: 'currency'
31
+ currency: 'currency',
32
+ label: 'label',
33
+ component: 'component', // to be deprecated in favour of snippet
34
+ snippet: 'snippet',
35
+ disabled: 'disabled',
36
+ /* flag fields */
37
+ deleted: '_deleted',
38
+ expanded: '_expanded',
39
+ selected: '_selected'
26
40
  }
27
41
 
28
- export const defaultIcons = [
42
+ // ─── BASE_FIELDS ─────────────────────────────────────────────────────────────
43
+ // Canonical field mapping for ProxyItem and all Wrapper+Navigator components.
44
+ // Semantic keys map to common raw data keys for backward compatibility.
45
+
46
+ export const BASE_FIELDS = {
47
+ // Identity
48
+ id: 'id',
49
+ value: 'value',
50
+ // Display
51
+ label: 'label',
52
+ icon: 'icon',
53
+ avatar: 'image',
54
+ subtext: 'description',
55
+ tooltip: 'title',
56
+ badge: 'badge',
57
+ shortcut: 'shortcut',
58
+ // Structure
59
+ children: 'children',
60
+ type: 'type',
61
+ snippet: 'snippet',
62
+ href: 'href',
63
+ hrefTarget: 'target',
64
+ // State
65
+ disabled: 'disabled',
66
+ expanded: 'expanded',
67
+ selected: 'selected'
68
+ }
69
+
70
+ export const DEFAULT_ICONS = [
29
71
  'accordion-opened',
30
72
  'accordion-closed',
31
73
  'action-remove',
74
+ 'action-cancel',
75
+ 'action-retry',
32
76
  'action-add',
33
77
  'action-clear',
34
78
  'action-search',
35
79
  'action-close',
80
+ 'action-copy',
81
+ 'action-copysuccess',
82
+ 'action-check',
83
+ 'action-pin',
84
+ 'action-save',
85
+ 'action-unpin',
36
86
  'node-opened',
37
87
  'node-closed',
88
+ 'folder-opened',
89
+ 'folder-closed',
38
90
  'selector-opened',
39
91
  'selector-closed',
92
+ 'menu-opened',
93
+ 'menu-closed',
40
94
  'checkbox-checked',
41
95
  'checkbox-unchecked',
42
96
  'checkbox-unknown',
@@ -47,10 +101,13 @@ export const defaultIcons = [
47
101
  'radio-on',
48
102
  'mode-dark',
49
103
  'mode-light',
104
+ 'mode-system',
50
105
  'navigate-left',
51
106
  'navigate-right',
52
107
  'navigate-up',
53
108
  'navigate-down',
109
+ 'palette-hex',
110
+ 'palette-presets',
54
111
  'state-error',
55
112
  'state-warning',
56
113
  'state-success',
@@ -66,17 +123,27 @@ export const defaultIcons = [
66
123
  'validity-failed',
67
124
  'validity-passed',
68
125
  'validity-unknown',
69
- 'validity-warning'
126
+ 'validity-warning',
127
+ 'alert-clear',
128
+ 'alert-unread',
129
+ 'align-horizontal-left',
130
+ 'align-horizontal-right',
131
+ 'align-horizontal-center',
132
+ 'align-vertical-top',
133
+ 'align-vertical-bottom',
134
+ 'align-vertical-middle',
135
+ 'view-code',
136
+ 'view-chart'
70
137
  ]
71
138
 
72
- export const defaultOptions = {
139
+ export const DEFAULT_OPTIONS = {
73
140
  id: 'id',
74
141
  label: 'label',
75
142
  value: 'value',
76
143
  checked: 'checked'
77
144
  }
78
145
 
79
- export const defaultKeyMap = {
146
+ export const DEFAULT_KEYMAP = {
80
147
  ArrowRight: 'open',
81
148
  ArrowLeft: 'close',
82
149
  ArrowDown: 'down',
@@ -85,8 +152,8 @@ export const defaultKeyMap = {
85
152
  Escape: 'deselect'
86
153
  }
87
154
 
88
- export const defaultThemeMapping = {
89
- neutral: 'slate',
155
+ export const DEFAULT_THEME_MAPPING = {
156
+ surface: 'slate',
90
157
  primary: 'orange',
91
158
  secondary: 'pink',
92
159
  accent: 'sky',
@@ -97,6 +164,30 @@ export const defaultThemeMapping = {
97
164
  info: 'cyan'
98
165
  }
99
166
 
167
+ export const TONE_MAP = {
168
+ z0: 50,
169
+ z1: 100,
170
+ z2: 200,
171
+ z3: 300,
172
+ z4: 400,
173
+ z5: 500,
174
+ z6: 600,
175
+ z7: 700,
176
+ z8: 800,
177
+ z9: 900,
178
+ z10: 950
179
+ }
180
+ /**
181
+ * Splits an icon name into its group and key components.
182
+ * @param {string} name - The icon name to split.
183
+ * @returns {{ group: string, key: string, value: string }} An object containing the group, key, and value of the icon name.
184
+ */
185
+ function splitIconName(name) {
186
+ const parts = name.split('-')
187
+ const group = parts.slice(0, parts.length - 1).join('-')
188
+ return { group, key: parts[parts.length - 1], value: name }
189
+ }
190
+
100
191
  /**
101
192
  * Generate a state icon mapping from a list of icon names
102
193
  *
@@ -104,15 +195,13 @@ export const defaultThemeMapping = {
104
195
  * @returns {import('./types').StateIcons}
105
196
  */
106
197
  export function stateIconsFromNames(icons) {
107
- return icons
108
- .map((k) => [...k.split('-')])
109
- .reduce(
110
- (acc, parts) => ({
111
- ...acc,
112
- [parts[0]]: { ...acc[parts[0]], [parts[1]]: parts.join('-') }
113
- }),
114
- {}
115
- )
198
+ return icons.map(splitIconName).reduce(
199
+ (acc, { group, key, value }) => ({
200
+ ...acc,
201
+ [group]: { ...acc[group], [key]: value }
202
+ }),
203
+ {}
204
+ )
116
205
  }
117
206
 
118
- export const defaultStateIcons = stateIconsFromNames(defaultIcons)
207
+ export const DEFAULT_STATE_ICONS = stateIconsFromNames(DEFAULT_ICONS)
package/src/events.js ADDED
@@ -0,0 +1,32 @@
1
+ import { noop } from './utils.js'
2
+
3
+ /**
4
+ * Creates an emitter object from the given properties.
5
+ *
6
+ * - Filters attributes that start with 'on' and are functions,
7
+ * - Returns an object with keys that are the event names (without the 'on' prefix)
8
+ * - If a default event is not present in the props, it will be set to a no-op function.
9
+ *
10
+ * @param {Object} props - The properties object to filter.
11
+ * @param {Array<string>} defaults - An array of default events.
12
+ * @returns {import('./types.js').EventHandlers} The emitter object.
13
+ */
14
+ export function createEmitter(props, defaults = []) {
15
+ const emit = {}
16
+
17
+ // Filter and add functions that start with 'on'
18
+ Object.entries(props)
19
+ .filter(([key, value]) => key.startsWith('on') && typeof value === 'function')
20
+ .forEach(([key, value]) => {
21
+ emit[key.slice(2)] = value
22
+ })
23
+
24
+ // Add default events with no-op function if not present
25
+ defaults.forEach((event) => {
26
+ if (!emit[event]) {
27
+ emit[event] = noop
28
+ }
29
+ })
30
+
31
+ return emit
32
+ }