@nordcraft/core 1.0.74 → 1.0.76

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.
Files changed (62) hide show
  1. package/README.md +5 -1
  2. package/dist/api/LegacyToddleApi.d.ts +17 -16
  3. package/dist/api/LegacyToddleApi.js.map +1 -1
  4. package/dist/api/ToddleApiV2.d.ts +46 -45
  5. package/dist/api/ToddleApiV2.js.map +1 -1
  6. package/dist/api/api.d.ts +16 -15
  7. package/dist/api/api.js +3 -3
  8. package/dist/api/api.js.map +1 -1
  9. package/dist/api/apiTypes.d.ts +68 -68
  10. package/dist/api/headers.d.ts +6 -5
  11. package/dist/api/headers.js.map +1 -1
  12. package/dist/component/ToddleComponent.d.ts +11 -11
  13. package/dist/component/ToddleComponent.js +2 -2
  14. package/dist/component/ToddleComponent.js.map +1 -1
  15. package/dist/component/actionUtils.d.ts +2 -2
  16. package/dist/component/actionUtils.js +1 -0
  17. package/dist/component/actionUtils.js.map +1 -1
  18. package/dist/component/component.types.d.ts +140 -140
  19. package/dist/component/component.types.js.map +1 -1
  20. package/dist/component/schemas/zod-schemas.d.ts +4 -0
  21. package/dist/component/schemas/zod-schemas.js +1106 -0
  22. package/dist/component/schemas/zod-schemas.js.map +1 -0
  23. package/dist/formula/formula.d.ts +14 -14
  24. package/dist/formula/formula.js +1 -1
  25. package/dist/formula/formula.js.map +1 -1
  26. package/dist/formula/formulaTypes.d.ts +10 -9
  27. package/dist/formula/formulaUtils.d.ts +11 -10
  28. package/dist/formula/formulaUtils.js +8 -2
  29. package/dist/formula/formulaUtils.js.map +1 -1
  30. package/dist/styling/customProperty.d.ts +2 -1
  31. package/dist/styling/customProperty.js.map +1 -1
  32. package/dist/styling/style.css.js +2 -2
  33. package/dist/styling/style.css.js.map +1 -1
  34. package/dist/styling/theme.d.ts +3 -2
  35. package/dist/styling/theme.js.map +1 -1
  36. package/dist/styling/variantSelector.d.ts +36 -35
  37. package/dist/styling/variantSelector.js.map +1 -1
  38. package/dist/types.d.ts +16 -15
  39. package/dist/utils/collections.d.ts +2 -1
  40. package/dist/utils/collections.js.map +1 -1
  41. package/dist/utils/getNodeSelector.d.ts +4 -3
  42. package/dist/utils/getNodeSelector.js.map +1 -1
  43. package/package.json +4 -1
  44. package/src/api/LegacyToddleApi.ts +2 -1
  45. package/src/api/ToddleApiV2.ts +2 -1
  46. package/src/api/api.ts +10 -9
  47. package/src/api/apiTypes.ts +73 -70
  48. package/src/api/headers.ts +7 -5
  49. package/src/component/ToddleComponent.ts +2 -2
  50. package/src/component/actionUtils.ts +3 -2
  51. package/src/component/component.types.ts +136 -131
  52. package/src/component/schemas/zod-schemas.ts +1535 -0
  53. package/src/formula/formula.ts +21 -18
  54. package/src/formula/formulaTypes.ts +12 -9
  55. package/src/formula/formulaUtils.ts +20 -13
  56. package/src/styling/customProperty.ts +2 -1
  57. package/src/styling/style.css.ts +6 -3
  58. package/src/styling/theme.ts +3 -2
  59. package/src/styling/variantSelector.ts +36 -35
  60. package/src/types.ts +27 -15
  61. package/src/utils/collections.ts +2 -1
  62. package/src/utils/getNodeSelector.ts +4 -3
@@ -5,6 +5,7 @@ import type {
5
5
  FormulaHandler,
6
6
  FormulaLookup,
7
7
  NordcraftMetadata,
8
+ Nullable,
8
9
  Runtime,
9
10
  Toddle,
10
11
  } from '../types'
@@ -16,7 +17,7 @@ declare const document: Document | undefined
16
17
  type ShadowRoot = DocumentFragment
17
18
 
18
19
  interface BaseOperation extends NordcraftMetadata {
19
- label?: string
20
+ label?: Nullable<string>
20
21
  }
21
22
 
22
23
  export interface PathOperation extends BaseOperation {
@@ -25,20 +26,20 @@ export interface PathOperation extends BaseOperation {
25
26
  }
26
27
 
27
28
  export interface FunctionArgument {
28
- name?: string
29
- isFunction?: boolean
29
+ name?: Nullable<string>
30
+ isFunction?: Nullable<boolean>
30
31
  formula: Formula
31
- type?: any
32
- testValue?: any
32
+ type?: Nullable<any>
33
+ testValue?: Nullable<any>
33
34
  }
34
35
 
35
36
  export interface FunctionOperation extends BaseOperation {
36
37
  type: 'function'
37
38
  name: string
38
- display_name?: string | null
39
- package?: string
39
+ display_name?: Nullable<string>
40
+ package?: Nullable<string>
40
41
  arguments: FunctionArgument[]
41
- variableArguments?: boolean
42
+ variableArguments?: Nullable<boolean>
42
43
  }
43
44
 
44
45
  export interface RecordOperation extends BaseOperation {
@@ -48,7 +49,7 @@ export interface RecordOperation extends BaseOperation {
48
49
 
49
50
  export interface ObjectOperation extends BaseOperation {
50
51
  type: 'object'
51
- arguments?: FunctionArgument[]
52
+ arguments?: Nullable<FunctionArgument[]>
52
53
  }
53
54
 
54
55
  export interface ArrayOperation extends BaseOperation {
@@ -102,16 +103,18 @@ export type Formula =
102
103
 
103
104
  export type FormulaContext = {
104
105
  component: Component | undefined
105
- formulaCache?: Record<
106
- string,
107
- {
108
- get: (data: ComponentData) => any
109
- set: (data: ComponentData, result: any) => void
110
- }
106
+ formulaCache?: Nullable<
107
+ Record<
108
+ string,
109
+ {
110
+ get: (data: ComponentData) => any
111
+ set: (data: ComponentData, result: any) => void
112
+ }
113
+ >
111
114
  >
112
115
  data: ComponentData
113
- root?: Document | ShadowRoot | null
114
- package: string | undefined
116
+ root?: Nullable<Document | ShadowRoot>
117
+ package: Nullable<string>
115
118
  toddle: {
116
119
  getFormula: FormulaLookup
117
120
  getCustomFormula: CustomFormulaHandler
@@ -225,7 +228,7 @@ export function applyFormula(
225
228
  return true
226
229
  }
227
230
  case 'function': {
228
- const packageName = formula.package ?? ctx.package
231
+ const packageName = formula.package ?? ctx.package ?? undefined
229
232
  const newFunc = (
230
233
  ctx.toddle ??
231
234
  ((globalThis as any).toddle as Toddle<unknown, unknown> | undefined)
@@ -1,16 +1,17 @@
1
+ import type { Nullable } from '../types'
1
2
  import type { Formula } from './formula'
2
3
 
3
4
  export interface BaseFormula {
4
5
  name: string
5
- description?: string
6
+ description?: Nullable<string>
6
7
  arguments: Array<{
7
8
  name: string
8
- formula?: Formula | null
9
- testValue?: unknown
9
+ formula?: Nullable<Formula>
10
+ testValue?: Nullable<unknown>
10
11
  }>
11
12
  // exported indicates that a formula is exported in a package
12
- exported?: boolean
13
- variableArguments?: boolean | null
13
+ exported?: Nullable<boolean>
14
+ variableArguments?: Nullable<boolean>
14
15
  }
15
16
 
16
17
  export interface ToddleFormula extends BaseFormula {
@@ -21,7 +22,7 @@ export interface ToddleFormula extends BaseFormula {
21
22
  * The Handler generic is a string server side, but a function client side
22
23
  */
23
24
  export interface CodeFormula<Handler = string | Function> extends BaseFormula {
24
- version?: 2 | never
25
+ version?: Nullable<2 | never>
25
26
  handler: Handler
26
27
  }
27
28
 
@@ -30,8 +31,10 @@ export type PluginFormula<Handler = string | Function> =
30
31
  | CodeFormula<Handler>
31
32
 
32
33
  export interface GlobalFormulas<Handler = string | Function> {
33
- formulas?: Record<string, PluginFormula<Handler>>
34
- packages?: Partial<
35
- Record<string, { formulas?: Record<string, PluginFormula<Handler>> }>
34
+ formulas?: Nullable<Record<string, PluginFormula<Handler>>>
35
+ packages?: Nullable<
36
+ Partial<
37
+ Record<string, { formulas?: Record<string, PluginFormula<Handler>> }>
38
+ >
36
39
  >
37
40
  }
@@ -1,4 +1,5 @@
1
1
  import type { ActionModel } from '../component/component.types'
2
+ import type { Nullable } from '../types'
2
3
  import { isDefined } from '../utils/util'
3
4
  import type {
4
5
  Formula,
@@ -35,15 +36,15 @@ export const functionFormula = (
35
36
  export function* getFormulasInFormula<Handler>({
36
37
  formula,
37
38
  globalFormulas,
38
- path = [],
39
- visitedFormulas = new Set<string>(),
40
- packageName,
39
+ path: _path,
40
+ visitedFormulas: _visitedFormulas,
41
+ packageName: _packageName,
41
42
  }: {
42
- formula: Formula | undefined | null
43
+ formula: Nullable<Formula>
43
44
  globalFormulas: GlobalFormulas<Handler>
44
- path?: (string | number)[]
45
- visitedFormulas?: Set<string>
46
- packageName?: string
45
+ path?: Nullable<(string | number)[]>
46
+ visitedFormulas?: Nullable<Set<string>>
47
+ packageName?: Nullable<string>
47
48
  }): Generator<{
48
49
  path: (string | number)[]
49
50
  formula: Formula
@@ -52,6 +53,9 @@ export function* getFormulasInFormula<Handler>({
52
53
  if (!isDefined(formula)) {
53
54
  return
54
55
  }
56
+ const path = _path ?? []
57
+ let packageName = _packageName ?? undefined
58
+ const visitedFormulas = _visitedFormulas ?? new Set<string>()
55
59
 
56
60
  yield {
57
61
  path,
@@ -168,15 +172,15 @@ export function* getFormulasInFormula<Handler>({
168
172
  export function* getFormulasInAction<Handler>({
169
173
  action,
170
174
  globalFormulas,
171
- path = [],
175
+ path: _path,
172
176
  visitedFormulas = new Set<string>(),
173
- packageName,
177
+ packageName: _packageName,
174
178
  }: {
175
- action: ActionModel | null
179
+ action: Nullable<ActionModel>
176
180
  globalFormulas: GlobalFormulas<Handler>
177
- path?: (string | number)[]
178
- visitedFormulas?: Set<string>
179
- packageName?: string
181
+ path?: Nullable<(string | number)[]>
182
+ visitedFormulas?: Nullable<Set<string>>
183
+ packageName?: Nullable<string>
180
184
  }): Generator<{
181
185
  path: (string | number)[]
182
186
  formula: Formula
@@ -185,6 +189,8 @@ export function* getFormulasInAction<Handler>({
185
189
  if (!isDefined(action)) {
186
190
  return
187
191
  }
192
+ const path = _path ?? []
193
+ let packageName = _packageName ?? undefined
188
194
 
189
195
  switch (action.type) {
190
196
  case 'Fetch':
@@ -227,6 +233,7 @@ export function* getFormulasInAction<Handler>({
227
233
  break
228
234
  case 'Custom':
229
235
  case undefined:
236
+ case null:
230
237
  packageName = action.package ?? packageName
231
238
  if (isFormula(action.data)) {
232
239
  yield* getFormulasInFormula({
@@ -1,4 +1,5 @@
1
1
  import type { CustomPropertyName } from '../component/component.types'
2
+ import type { Nullable } from '../types'
2
3
  import { isDefined } from '../utils/util'
3
4
  import type { CustomPropertyDefinition, Theme } from './theme'
4
5
 
@@ -115,7 +116,7 @@ const FALLBACK_VALUES: Record<CssSyntax, string> = {
115
116
  integer: '0',
116
117
  }
117
118
 
118
- export const appendUnit = (value: any, unit: string | null | undefined) =>
119
+ export const appendUnit = (value: any, unit: Nullable<string>) =>
119
120
  isDefined(value) && isDefined(unit) && !String(value).endsWith(unit)
120
121
  ? `${value}${unit}`
121
122
  : value
@@ -5,6 +5,7 @@ import type {
5
5
  ElementNodeModel,
6
6
  NodeStyleModel,
7
7
  } from '../component/component.types'
8
+ import type { Nullable } from '../types'
8
9
  import { omitKeys } from '../utils/collections'
9
10
  import { isDefined } from '../utils/util'
10
11
  import { getClassName, toValidClassName } from './className'
@@ -144,7 +145,7 @@ export const createStylesheet = (
144
145
  const renderVariant = (
145
146
  selector: string,
146
147
  style: NodeStyleModel,
147
- options?: { startingStyle?: boolean },
148
+ options?: Nullable<{ startingStyle?: Nullable<boolean> }>,
148
149
  ) => {
149
150
  const scrollbarStyles = Object.entries(style).filter(
150
151
  ([key]) => key === 'scrollbar-width',
@@ -312,7 +313,7 @@ export const getAllFonts = (components: Component[]) => {
312
313
  return new Set(
313
314
  components
314
315
  .flatMap((component) => {
315
- return Object.values(component.nodes).flatMap((node) => {
316
+ return Object.values(component.nodes ?? {}).flatMap((node) => {
316
317
  if (node.type === 'element') {
317
318
  return [
318
319
  node.style?.fontFamily,
@@ -325,6 +326,8 @@ export const getAllFonts = (components: Component[]) => {
325
326
  return []
326
327
  })
327
328
  })
328
- .map((f) => f.replace('var(', '').replace(')', '').replace(/'/g, '')),
329
+ .map((f) =>
330
+ String(f).replace('var(', '').replace(')', '').replaceAll("'", ''),
331
+ ),
329
332
  )
330
333
  }
@@ -1,4 +1,5 @@
1
1
  import type { CustomPropertyName } from '../component/component.types'
2
+ import type { Nullable } from '../types'
2
3
  import { isDefined } from '../utils/util'
3
4
  import { renderSyntaxDefinition, type CssSyntaxNode } from './customProperty'
4
5
  import { RESET_STYLES } from './theme.const'
@@ -99,11 +100,11 @@ export type Theme = {
99
100
  export type CustomPropertyDefinition = {
100
101
  syntax: CssSyntaxNode
101
102
  inherits: boolean
102
- initialValue: string | null // Required by CSS specs for default-theme, but we can do a fallback so null is allowed
103
+ initialValue: Nullable<string> // Required by CSS specs for default-theme, but we can do a fallback so null is allowed
103
104
  description: string
104
105
  // Values mapped to theme names.
105
106
  // Values are not required, if left out, the default theme value will be used. If no default theme value exists, initialValue will be used.
106
- values: Record<string, string | null>
107
+ values: Record<string, Nullable<string>>
107
108
  }
108
109
 
109
110
  export const getThemeCss = (
@@ -3,6 +3,7 @@ import type {
3
3
  CustomPropertyName,
4
4
  NodeStyleModel,
5
5
  } from '../component/component.types'
6
+ import type { Nullable } from '../types'
6
7
 
7
8
  export type Shadow = {
8
9
  x: number
@@ -24,45 +25,45 @@ export type Filter =
24
25
  }
25
26
 
26
27
  type MediaQuery = {
27
- 'min-width'?: string
28
- 'max-width'?: string
29
- 'min-height'?: string
30
- 'max-height'?: string
28
+ 'min-width'?: Nullable<string>
29
+ 'max-width'?: Nullable<string>
30
+ 'min-height'?: Nullable<string>
31
+ 'max-height'?: Nullable<string>
31
32
  }
32
33
 
33
34
  export interface StyleVariant {
34
- 'even-child'?: boolean
35
- 'first-child'?: boolean
36
- 'first-of-type'?: boolean
37
- 'focus-visible'?: boolean
38
- 'focus-within'?: boolean
39
- 'last-child'?: boolean
40
- 'last-of-type'?: boolean
41
- 'nth-child(even)'?: boolean
42
- 'popover-open'?: boolean
43
- active?: boolean
44
- autofill?: boolean
45
- breakpoint: 'small' | 'medium' | 'large'
46
- checked?: boolean
47
- class?: string
48
- className?: string
49
- customProperties?: Record<CustomPropertyName, CustomProperty>
50
- disabled?: boolean
51
- empty?: boolean
52
- evenChild?: boolean
53
- firstChild?: boolean
54
- focus?: boolean
55
- focusWithin?: boolean
56
- hover?: boolean
57
- id?: string
58
- invalid?: boolean
59
- lastChild?: boolean
60
- link?: boolean
61
- mediaQuery?: MediaQuery
62
- pseudoElement?: string
63
- startingStyle?: boolean
35
+ 'even-child'?: Nullable<boolean>
36
+ 'first-child'?: Nullable<boolean>
37
+ 'first-of-type'?: Nullable<boolean>
38
+ 'focus-visible'?: Nullable<boolean>
39
+ 'focus-within'?: Nullable<boolean>
40
+ 'last-child'?: Nullable<boolean>
41
+ 'last-of-type'?: Nullable<boolean>
42
+ 'nth-child(even)'?: Nullable<boolean>
43
+ 'popover-open'?: Nullable<boolean>
44
+ active?: Nullable<boolean>
45
+ autofill?: Nullable<boolean>
46
+ breakpoint?: Nullable<'small' | 'medium' | 'large'>
47
+ checked?: Nullable<boolean>
48
+ class?: Nullable<string>
49
+ className?: Nullable<string>
50
+ customProperties?: Nullable<Record<CustomPropertyName, CustomProperty>>
51
+ disabled?: Nullable<boolean>
52
+ empty?: Nullable<boolean>
53
+ evenChild?: Nullable<boolean>
54
+ firstChild?: Nullable<boolean>
55
+ focus?: Nullable<boolean>
56
+ focusWithin?: Nullable<boolean>
57
+ hover?: Nullable<boolean>
58
+ id?: Nullable<string>
59
+ invalid?: Nullable<boolean>
60
+ lastChild?: Nullable<boolean>
61
+ link?: Nullable<boolean>
62
+ mediaQuery?: Nullable<MediaQuery>
63
+ pseudoElement?: Nullable<string>
64
+ startingStyle?: Nullable<boolean>
64
65
  style: NodeStyleModel
65
- visited?: boolean
66
+ visited?: Nullable<boolean>
66
67
  }
67
68
 
68
69
  export const variantSelector = (variant: StyleVariant) =>
package/src/types.ts CHANGED
@@ -15,10 +15,14 @@ export type ActionHandlerV2 = (
15
15
  // For v2 of actions
16
16
  args: Record<string, unknown>,
17
17
  ctx: {
18
- triggerActionEvent: (trigger: string, data: any, event?: Event) => void
18
+ triggerActionEvent: (
19
+ trigger: string,
20
+ data: any,
21
+ event?: Nullable<Event>,
22
+ ) => void
19
23
  root: Document | ShadowRoot
20
24
  },
21
- event?: Event,
25
+ event?: Nullable<Event>,
22
26
  // If the action returns a function, that function will be called
23
27
  // from our abort signal (for cleanup)
24
28
  ) => void | (() => void) | Promise<void> | Promise<() => void>
@@ -26,11 +30,15 @@ export type ActionHandlerV2 = (
26
30
  export type ActionHandler<Args = unknown[]> = (
27
31
  args: Args,
28
32
  ctx: {
29
- triggerActionEvent: (trigger: string, data: any, event?: Event) => void
33
+ triggerActionEvent: (
34
+ trigger: string,
35
+ data: any,
36
+ event?: Nullable<Event>,
37
+ ) => void
30
38
  env: ToddleEnv
31
39
  abortSignal: AbortSignal
32
40
  },
33
- event?: Event,
41
+ event?: Nullable<Event>,
34
42
  ) => void
35
43
 
36
44
  export type FormulaHandler<T = void> = (
@@ -45,21 +53,23 @@ export type FormulaHandler<T = void> = (
45
53
 
46
54
  interface PluginActionBase {
47
55
  name: string
48
- description?: string
49
- arguments?: Array<{
50
- name: string
51
- formula: Formula
52
- }> | null
56
+ description?: Nullable<string>
57
+ arguments?: Nullable<
58
+ Array<{
59
+ name: string
60
+ formula: Formula
61
+ }>
62
+ >
53
63
  // eslint-disable-next-line inclusive-language/use-inclusive-words
54
- events?: Record<string, { dummyEvent?: any }>
55
- variableArguments: boolean | null
64
+ events?: Nullable<Record<string, { dummyEvent?: any }>>
65
+ variableArguments: Nullable<boolean>
56
66
  }
57
67
 
58
68
  export interface PluginActionV2 extends PluginActionBase {
59
69
  handler: ActionHandlerV2
60
70
  version: 2
61
71
  // exported indicates that an action is exported in a package
62
- exported?: boolean
72
+ exported?: Nullable<boolean>
63
73
  }
64
74
 
65
75
  export interface LegacyPluginAction extends PluginActionBase {
@@ -148,15 +158,17 @@ export interface Comment {
148
158
  }
149
159
 
150
160
  export interface NordcraftMetadata {
151
- '@nordcraft/metadata'?: {
152
- comments: Record<string, Comment & { index: number }> | null
153
- } | null
161
+ '@nordcraft/metadata'?: Nullable<{
162
+ comments: Nullable<Partial<Record<string, Comment & { index: number }>>>
163
+ }>
154
164
  }
155
165
 
156
166
  export type RequireFields<T, K extends keyof T> = Omit<T, K> & {
157
167
  [P in K]-?: NonNullable<T[P]>
158
168
  }
159
169
 
170
+ export type Nullable<T> = T | null | undefined
171
+
160
172
  export type NestedOmit<
161
173
  Schema,
162
174
  Path extends string,
@@ -1,3 +1,4 @@
1
+ import type { Nullable } from '../types'
1
2
  import { isDefined } from './util'
2
3
 
3
4
  export const isObject = (input: any): input is Record<string, any> =>
@@ -118,7 +119,7 @@ export const easySort = <T>(
118
119
 
119
120
  export const deepSortObject = (
120
121
  obj: any,
121
- ): Record<string, any> | Array<any> | undefined | null => {
122
+ ): Nullable<Record<string, any> | Array<any>> => {
122
123
  if (!isDefined(obj)) {
123
124
  return obj
124
125
  }
@@ -1,15 +1,16 @@
1
1
  import { variantSelector, type StyleVariant } from '../styling/variantSelector'
2
+ import type { Nullable } from '../types'
2
3
 
3
4
  type NodeSelectorOptions =
4
5
  | {
5
6
  componentName: string
6
- nodeId: string | undefined
7
- variant?: StyleVariant
7
+ nodeId: Nullable<string>
8
+ variant?: Nullable<StyleVariant>
8
9
  }
9
10
  | {
10
11
  componentName?: never
11
12
  nodeId?: never
12
- variant?: StyleVariant
13
+ variant?: Nullable<StyleVariant>
13
14
  }
14
15
 
15
16
  export function getNodeSelector(