@rokkit/core 1.0.0-next.100

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/src/theme.js ADDED
@@ -0,0 +1,197 @@
1
+ import { defaultThemeMapping, defaultColors, syntaxColors } from './constants'
2
+ import { shades, defaultPalette } from './colors'
3
+
4
+ const modifiers = {
5
+ hsl: (value) => `hsl(${value})`,
6
+ rgb: (value) => `rgb(${value})`,
7
+ none: (value) => value
8
+ }
9
+
10
+ /**
11
+ * Generate shades for a color using css varuable
12
+ *
13
+ * @param {string} name
14
+ * @param {string} modifier
15
+ * @returns
16
+ */
17
+ export function shadesOf(name, modifier = 'none') {
18
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
19
+
20
+ return shades.reduce(
21
+ (result, shade) => ({
22
+ ...result,
23
+ [shade]: fn(`var(--${name}-${shade})`)
24
+ }),
25
+ {
26
+ DEFAULT: fn(`var(--${name}-500)`),
27
+ base: fn(`var(--${name}-50)`),
28
+ inset: fn(`var(--${name}-100)`),
29
+ subtle: fn(`var(--${name}-200)`),
30
+ muted: fn(`var(--${name}-300)`),
31
+ raised: fn(`var(--${name}-400)`),
32
+ elevated: fn(`var(--${name}-600)`),
33
+ floating: fn(`var(--${name}-700)`),
34
+ contrast: fn(`var(--${name}-800)`),
35
+ overlay: fn(`var(--${name}-900)`)
36
+ }
37
+ )
38
+ }
39
+
40
+ /**
41
+ * Generate shades for a color using css varuable
42
+ *
43
+ * @param {string} name
44
+ * @param {string} modifier
45
+ * @returns {object}
46
+ */
47
+ export function stateColors(name, modifier = 'none') {
48
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
49
+ return {
50
+ DEFAULT: fn(`var(--${name}-500)`),
51
+ light: fn(`var(--${name}-200)`),
52
+ dark: fn(`var(--${name}-700)`)
53
+ }
54
+ }
55
+
56
+ export function themeColors(modifier = 'none') {
57
+ const fn = modifier in modifiers ? modifiers[modifier] : modifiers.none
58
+
59
+ const states = ['info', 'danger', 'warning', 'success', 'error']
60
+ const variants = ['neutral', 'primary', 'secondary', 'accent']
61
+ let colors = states.reduce(
62
+ (acc, state) => ({ ...acc, [state]: stateColors(state, modifier) }),
63
+ {}
64
+ )
65
+ colors = variants.reduce(
66
+ (acc, variant) => ({ ...acc, [variant]: shadesOf(variant, modifier) }),
67
+ colors
68
+ )
69
+ colors.neutral = {
70
+ ...colors.neutral,
71
+ // contrast: fn(`var(--neutral-800)`),
72
+ zebra: fn('var(--neutral-zebra)')
73
+ }
74
+
75
+ return colors
76
+ }
77
+
78
+ /**
79
+ * Creates an array of shade mapping objects for a given theme variant and mode.
80
+ * Each object represents a CSS custom property (variable) with its value set based on a provided condition.
81
+ *
82
+ * @param {string} variant - The name of the theme variant (e.g., 'primary', 'secondary').
83
+ * @param {'light' | 'dark'} mode - The theme mode for which the mappings are being created.
84
+ * @param {function(number): string} valueCondition - A function that takes a shade value and returns the color value
85
+ * based on the condition appropriate for light or dark mode.
86
+ * @returns {{import('./types'}.ShadeMapping[]>} An array of objects, where each object contains key, value, and mode
87
+ * properties corresponding to a CSS custom property definition.
88
+ */
89
+ function createShadeMappings(variant, mode, valueCondition) {
90
+ return shades.map((shade) => ({
91
+ key: `--on-${variant}-${shade}`,
92
+ value: valueCondition(shade),
93
+ mode
94
+ }))
95
+ }
96
+
97
+ /**
98
+ * Generates contrast colors for light and dark modes based on a given palette. Each color variant in the
99
+ * palette is mapped to either a light or dark contrast color depending on the shade's value.
100
+ *
101
+ * @param {string} [light='#ffffff'] - The default light color used when the shade is >= 500 in light mode or <= 500 in dark mode.
102
+ * @param {string} [dark='#000000'] - The default dark color used when the shade is < 500 in light mode or > 500 in dark mode.
103
+ * @param {Array<string>} [palette=defaultPalette] - An array of color variant names to generate contrast colors for.
104
+ * @returns {Array<Object>} An array containing contrast color rules organized by light and dark modes for each variant in the palette.
105
+ */
106
+ export function contrastColors(light = '#ffffff', dark = '#000000', palette = defaultPalette) {
107
+ const colors = palette
108
+ .flatMap((variant) => [
109
+ createShadeMappings(variant, 'light', (shade) => (shade < 500 ? dark : light)),
110
+ createShadeMappings(variant, 'dark', (shade) => (shade > 500 ? dark : light))
111
+ ])
112
+ .reduce((acc, item) => [...acc, ...item], [])
113
+ return colors
114
+ }
115
+ /**
116
+ * Generates color rules for a specific theme variant, for both light and dark modes.
117
+ *
118
+ * @param {string} variant - The name of the variant to generate rules for.
119
+ * @param {Object} colors - The object containing color definitions.
120
+ * @param {Object} mapping - An object that maps variant names to color property names.
121
+ * @returns {import('./types').ShadeMappings} An array containing the color rules for both light and dark modes.
122
+ */
123
+ function generateColorRules(variant, colors, mapping) {
124
+ return shades.flatMap((shade, index) => [
125
+ {
126
+ key: `--${variant}-${shade}`,
127
+ value: colors[mapping[variant]][shade],
128
+ mode: 'light'
129
+ },
130
+ {
131
+ key: `--${variant}-${shade}`,
132
+ value: colors[mapping[variant]][shades[shades.length - index - 1]],
133
+ mode: 'dark'
134
+ }
135
+ ])
136
+ }
137
+
138
+ /**
139
+ * Filters the rules for a specific mode and transforms them into an object mapping
140
+ * CSS variable names to their values.
141
+ *
142
+ * @param {Array<Object>} rules - The array of rules to filter and transform.
143
+ * @param {'light' | 'dark'} mode - The mode to filter by.
144
+ * @returns {Object} An object containing the rules specific to the provided mode.
145
+ */
146
+ function filterRulesByMode(rules, mode) {
147
+ return rules
148
+ .filter((rule) => rule.mode === mode)
149
+ .reduce((acc, { key, value }) => ({ ...acc, [key]: value }), {})
150
+ }
151
+
152
+ /**
153
+ * Creates a theme variant object with a given mode, a collection of color rules, and additional colors.
154
+ *
155
+ * @param {string} name - The base name for the theme variant.
156
+ * @param {'light' | 'dark'} mode - The mode of the theme variant.
157
+ * @param {Object} colors - The object containing color rules for the theme.
158
+ * @param {Object} extraColors - Any additional color properties for the theme.
159
+ * @returns {Array} An array where the first element is the theme's name and the second element
160
+ * is an object containing all color rules and extra properties for the theme variant.
161
+ */
162
+ function createThemeVariant(name, mode, colors, extraColors) {
163
+ return [
164
+ `${name}-mode-${mode}`,
165
+ {
166
+ ...colors,
167
+ ...extraColors,
168
+ '--plot-background': 'var(--neutral-200)'
169
+ }
170
+ ]
171
+ }
172
+
173
+ /**
174
+ * Constructs and returns the light and dark theme variants based on provided color mapping and color definitions.
175
+ *
176
+ * @param {string} name - The base name for the theme, defaults to 'rokkit' if not provided.
177
+ * @param {Object} [mapping=defaultThemeMapping] - An object mapping variant names to color property names.
178
+ * @param {Object} [colors=defaultColors] - The object containing default color definitions.
179
+ * @returns {Array<Array>} An array containing two arrays, one for the light theme variant and another for the dark theme.
180
+ */
181
+ export function themeRules(name = 'rokkit', mapping = defaultThemeMapping, colors = defaultColors) {
182
+ mapping = { ...defaultThemeMapping, ...mapping }
183
+ const variants = Object.keys(mapping)
184
+
185
+ const rules = variants.reduce(
186
+ (acc, variant) => [...acc, ...generateColorRules(variant, colors, mapping)],
187
+ []
188
+ )
189
+
190
+ const lightRules = filterRulesByMode(rules, 'light')
191
+ const darkRules = filterRulesByMode(rules, 'dark')
192
+
193
+ const lightTheme = createThemeVariant(name, 'light', lightRules, syntaxColors['one-dark'].light)
194
+ const darkTheme = createThemeVariant(name, 'dark', darkRules, syntaxColors['one-dark'].dark)
195
+
196
+ return [lightTheme, darkTheme]
197
+ }
package/src/ticks.js ADDED
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Generates an array of tick marks for a range of values.
3
+ *
4
+ * @param {number} lowerBound - The lower bound of the range.
5
+ * @param {number} upperBound - The upper bound of the range.
6
+ * @param {number} [minorTickStep=upperBound-lowerBound] - The step size for minor ticks.
7
+ * @param {number} [majorTickStep=1] - The step size for major ticks.
8
+ * @returns {import('./types').TickMark[]>} An array of tick mark objects.
9
+ */
10
+ export function generateTicks(
11
+ lowerBound,
12
+ upperBound,
13
+ minorTickStep = upperBound - lowerBound,
14
+ majorTickStep = 1
15
+ ) {
16
+ const length = 1 + Math.ceil((upperBound - lowerBound) / minorTickStep)
17
+ return Array.from({ length }, (_, i) => {
18
+ const value = i === length - 1 ? upperBound : lowerBound + minorTickStep * i
19
+ const major = i === 0 || i === length - 1 || i % majorTickStep === 0
20
+ return {
21
+ value,
22
+ label: major ? value : '',
23
+ major
24
+ }
25
+ })
26
+ }
package/src/types.js ADDED
@@ -0,0 +1,115 @@
1
+ /**
2
+ * Component map to be used to render the item.
3
+ * @typedef {Object<string, import('svelte').SvelteComponent>} ComponentMap
4
+ */
5
+
6
+ /**
7
+ * Options for the sort order of the column.
8
+ *
9
+ * @typedef {'ascending'|'descending'|'none'} SortOptions
10
+ */
11
+
12
+ /**
13
+ * @typedef {'checked'|'unchecked'|'indeterminate'} SelectionState
14
+ */
15
+
16
+ /**
17
+ * Options for the horizontal alignment of the column content.
18
+ *
19
+ * @typedef {'left'|'middle'|'right'} HorizontalAlignOptions
20
+ */
21
+
22
+ /**
23
+ * Options for the action type of the column.
24
+ *
25
+ * @typedef {'select'|'edit'|'delete'} ActionTypes
26
+ */
27
+
28
+ /**
29
+ * Structure to map custom fields for rendering. This is used to identofy the attributes for various purposes.
30
+ *
31
+ * @typedef FieldMapping
32
+ * @property {string} [id='id'] Unique id for the item
33
+ * @property {string} [text='text'] the text to render
34
+ * @property {string} [value='value'] the value associated with the item
35
+ * @property {string} [url='url'] a URL
36
+ * @property {string} [icon='icon'] icon to render
37
+ * @property {string} [image='image'] the image to render
38
+ * @property {string} [children='children'] children of the item
39
+ * @property {string} [summary='summary'] summary of the item
40
+ * @property {string} [notes='notes'] notes for the item
41
+ * @property {string} [props='props'] additional properties
42
+ * @property {string} [isOpen='_open'] item is open or closed
43
+ * @property {string} [level='level'] level of item
44
+ * @property {string} [parent='parent'] item is a parent
45
+ * @property {string} [currency='currency] column specifying currency to be used for the current value
46
+ * @property {string} [isDeleted='_deleted'] item is deleted
47
+ * @property {FieldMapping} [fields] Field mapping to be used on children in the next level
48
+ */
49
+
50
+ /**
51
+ * Column metadata for the table.
52
+ *
53
+ * @typedef {Object} ColumnMetadata
54
+ *
55
+ * @property {string} name - The name of the column.
56
+ * @property {string} dataType - The data type of the column (e.g., "string", "number", "date").
57
+ * @property {FieldMapping} [fields] - Additional attributes for the column.
58
+ * @property {number} [digits=0] - The number of digits for numeric values (defaults to 0).
59
+ * @property {Function} formatter - A function to format the column value.
60
+ * @property {boolean} [virtual] - Indicates if the column is virtual (true/false).
61
+ * @property {boolean} [sortable] - Indicates if the column is sortable (true/false).
62
+ * @property {SortOptions} [sortOrder] - The sort order of the column.
63
+ * @property {HorizontalAlignOptions} [align] - The alignment of the column content.
64
+ * @property {ActionTypes} [action] - Action attribute for action columns.
65
+ */
66
+
67
+ /**
68
+ * Track the state of a row in the table.
69
+ *
70
+ * @typedef {Object} RowState
71
+ * @property {number} index - The index of the node in the flat list.
72
+ * @property {number} depth - The depth of the node in the hierarchy.
73
+ * @property {string} [value] - The value of the hierarchy node.
74
+ * @property {boolean} [isHidden] - Indicates whether the node is visible (true/false).
75
+ * @property {boolean} [isParent] - Indicates if this node is a parent (true/false).
76
+ * @property {boolean} [isExpanded] - Indicates whether the node is expanded (true/false).
77
+ * @property {number} [parentIndex] - The index of the parent node in the flat list.
78
+ * @property {SelectionState} [selected] - Indicates whether the node is selected (true/false/indeterminate).
79
+ * @property {Array<number>} childred - The indices of the children in the flat list.
80
+ */
81
+
82
+ /**
83
+ * Track the state of all rows in the table.
84
+ *
85
+ * @typedef {Object} RowStateMap
86
+ * @property {RowState[]} rows - Flat list of hierarchy nodes.
87
+ */
88
+
89
+ /**
90
+ * Shade mapping for color variables
91
+ *
92
+ * @typedef {Object} ShadeMapping
93
+ * @property {string} key - the variable name to be used
94
+ * @property {string} value - the value to be used
95
+ * @property {string} mode - light or dark mode
96
+ */
97
+
98
+ /**
99
+ * @typedef {Object} TickMark
100
+ * @property {number} value - The value of the tick mark.
101
+ * @property {string} label - The label for the tick mark.
102
+ * @property {boolean} major - Indicates if the tick mark is a major tick.
103
+ */
104
+
105
+ /**
106
+ * @typedef {Object} CalendarDay
107
+ * @property {number} day - The day of the month.
108
+ * @property {number} offset - indicates the offset for positioning
109
+ * @property {date} date - Datevalue for the day.
110
+ * @property {string} text - formatted text for the day.
111
+ * @property {boolean} holiday - Indicates if the day is a holiday.
112
+ * @property {boolean} weekend - Indicates if the day is on the weekend.
113
+ */
114
+
115
+ export default {}
package/src/utils.js ADDED
@@ -0,0 +1,72 @@
1
+ /**
2
+ * A function that performs no operations.
3
+ */
4
+ export function noop() {
5
+ // intentionally empty to support default actions
6
+ }
7
+
8
+ /**
9
+ * Generates a random id
10
+ *
11
+ * @returns {string} A random id
12
+ */
13
+ export function id() {
14
+ return Math.random().toString(36).substring(2, 9)
15
+ }
16
+
17
+ /**
18
+ * Check if a value is a json object
19
+ *
20
+ * @param {*} val
21
+ * @returns {boolean}
22
+ */
23
+ export function isObject(val) {
24
+ return typeof val === 'object' && val !== null && !(val instanceof Date)
25
+ }
26
+
27
+ /**
28
+ * Converts the value to a string. If the value is an object, it will convert it to a JSON string.
29
+ *
30
+ * @param {*} value
31
+ * @returns {string}
32
+ */
33
+ export function toString(value) {
34
+ if (value === null || value === undefined) return value
35
+ if (isObject(value)) return JSON.stringify(value, null, 2)
36
+ return value.toString()
37
+ }
38
+
39
+ /**
40
+ * Generates icon shortcuts for a collection of icons
41
+ *
42
+ * @param {string[]} icons
43
+ * @param {string} collection
44
+ * @param {string} variants
45
+ * @returns {Object}
46
+ */
47
+ export function iconShortcuts(icons, collection, variants) {
48
+ const suffix = variants ? `-${variants}` : ''
49
+ const shortcuts = !collection
50
+ ? {}
51
+ : icons.reduce(
52
+ (acc, name) => ({
53
+ ...acc,
54
+ [name]: [collection, name].join(':') + suffix
55
+ }),
56
+ {}
57
+ )
58
+
59
+ return shortcuts
60
+ }
61
+
62
+ /**
63
+ * Scales the path by the size
64
+ *
65
+ * @param {number} size
66
+ * @param {string|number} x
67
+ * @returns {string|number}
68
+ */
69
+ export function scaledPath(size, x) {
70
+ if (Array.isArray(x)) return x.map((v) => scaledPath(size, v)).join(' ')
71
+ return typeof x === 'number' ? x * size : x
72
+ }