@joralvmel/cosmos-core 0.2.0 → 0.4.0

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 (157) hide show
  1. package/dist/catalog.d.ts +3 -3
  2. package/dist/catalog.d.ts.map +1 -1
  3. package/dist/catalog.js +25 -6
  4. package/dist/catalog.js.map +1 -1
  5. package/dist/codegen.d.ts +16 -0
  6. package/dist/codegen.d.ts.map +1 -1
  7. package/dist/codegen.js +396 -57
  8. package/dist/codegen.js.map +1 -1
  9. package/dist/headless/calendar.d.ts +32 -0
  10. package/dist/headless/calendar.d.ts.map +1 -0
  11. package/dist/headless/calendar.js +242 -0
  12. package/dist/headless/calendar.js.map +1 -0
  13. package/dist/headless/listbox.d.ts +9 -0
  14. package/dist/headless/listbox.d.ts.map +1 -0
  15. package/dist/headless/listbox.js +103 -0
  16. package/dist/headless/listbox.js.map +1 -0
  17. package/dist/headless/pagination.d.ts +34 -0
  18. package/dist/headless/pagination.d.ts.map +1 -0
  19. package/dist/headless/pagination.js +92 -0
  20. package/dist/headless/pagination.js.map +1 -0
  21. package/dist/headless/radio-group.d.ts +10 -0
  22. package/dist/headless/radio-group.d.ts.map +1 -0
  23. package/dist/headless/radio-group.js +59 -0
  24. package/dist/headless/radio-group.js.map +1 -0
  25. package/dist/headless/select.d.ts +4 -0
  26. package/dist/headless/select.d.ts.map +1 -0
  27. package/dist/headless/select.js +7 -0
  28. package/dist/headless/select.js.map +1 -0
  29. package/dist/headless/size.d.ts +3 -0
  30. package/dist/headless/size.d.ts.map +1 -0
  31. package/dist/headless/size.js +7 -0
  32. package/dist/headless/size.js.map +1 -0
  33. package/dist/headless/text-field.d.ts +25 -0
  34. package/dist/headless/text-field.d.ts.map +1 -0
  35. package/dist/headless/text-field.js +12 -0
  36. package/dist/headless/text-field.js.map +1 -0
  37. package/dist/index.d.ts +1709 -19
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +60 -1
  40. package/dist/index.js.map +1 -1
  41. package/dist/markdown.d.ts.map +1 -1
  42. package/dist/markdown.js +7 -3
  43. package/dist/markdown.js.map +1 -1
  44. package/dist/specs/badge.d.ts +65 -0
  45. package/dist/specs/badge.d.ts.map +1 -0
  46. package/dist/specs/badge.js +64 -0
  47. package/dist/specs/badge.js.map +1 -0
  48. package/dist/specs/badge.scenarios.d.ts +21 -0
  49. package/dist/specs/badge.scenarios.d.ts.map +1 -0
  50. package/dist/specs/badge.scenarios.js +35 -0
  51. package/dist/specs/badge.scenarios.js.map +1 -0
  52. package/dist/specs/calendar.d.ts +120 -0
  53. package/dist/specs/calendar.d.ts.map +1 -0
  54. package/dist/specs/calendar.js +121 -0
  55. package/dist/specs/calendar.js.map +1 -0
  56. package/dist/specs/calendar.scenarios.d.ts +39 -0
  57. package/dist/specs/calendar.scenarios.d.ts.map +1 -0
  58. package/dist/specs/calendar.scenarios.js +139 -0
  59. package/dist/specs/calendar.scenarios.js.map +1 -0
  60. package/dist/specs/checkbox.d.ts +139 -0
  61. package/dist/specs/checkbox.d.ts.map +1 -0
  62. package/dist/specs/checkbox.js +129 -0
  63. package/dist/specs/checkbox.js.map +1 -0
  64. package/dist/specs/checkbox.scenarios.d.ts +28 -0
  65. package/dist/specs/checkbox.scenarios.d.ts.map +1 -0
  66. package/dist/specs/checkbox.scenarios.js +54 -0
  67. package/dist/specs/checkbox.scenarios.js.map +1 -0
  68. package/dist/specs/dialog.d.ts +167 -0
  69. package/dist/specs/dialog.d.ts.map +1 -0
  70. package/dist/specs/dialog.js +136 -0
  71. package/dist/specs/dialog.js.map +1 -0
  72. package/dist/specs/dialog.scenarios.d.ts +42 -0
  73. package/dist/specs/dialog.scenarios.d.ts.map +1 -0
  74. package/dist/specs/dialog.scenarios.js +129 -0
  75. package/dist/specs/dialog.scenarios.js.map +1 -0
  76. package/dist/specs/icon-button.d.ts +136 -0
  77. package/dist/specs/icon-button.d.ts.map +1 -0
  78. package/dist/specs/icon-button.js +119 -0
  79. package/dist/specs/icon-button.js.map +1 -0
  80. package/dist/specs/icon-button.scenarios.d.ts +28 -0
  81. package/dist/specs/icon-button.scenarios.d.ts.map +1 -0
  82. package/dist/specs/icon-button.scenarios.js +95 -0
  83. package/dist/specs/icon-button.scenarios.js.map +1 -0
  84. package/dist/specs/icon.d.ts +11 -20
  85. package/dist/specs/icon.d.ts.map +1 -1
  86. package/dist/specs/icon.js +11 -20
  87. package/dist/specs/icon.js.map +1 -1
  88. package/dist/specs/icon.scenarios.d.ts +9 -1
  89. package/dist/specs/icon.scenarios.d.ts.map +1 -1
  90. package/dist/specs/icon.scenarios.js +70 -6
  91. package/dist/specs/icon.scenarios.js.map +1 -1
  92. package/dist/specs/listbox.d.ts +157 -0
  93. package/dist/specs/listbox.d.ts.map +1 -0
  94. package/dist/specs/listbox.js +144 -0
  95. package/dist/specs/listbox.js.map +1 -0
  96. package/dist/specs/listbox.scenarios.d.ts +37 -0
  97. package/dist/specs/listbox.scenarios.d.ts.map +1 -0
  98. package/dist/specs/listbox.scenarios.js +110 -0
  99. package/dist/specs/listbox.scenarios.js.map +1 -0
  100. package/dist/specs/pagination.d.ts +147 -0
  101. package/dist/specs/pagination.d.ts.map +1 -0
  102. package/dist/specs/pagination.js +122 -0
  103. package/dist/specs/pagination.js.map +1 -0
  104. package/dist/specs/pagination.scenarios.d.ts +15 -0
  105. package/dist/specs/pagination.scenarios.d.ts.map +1 -0
  106. package/dist/specs/pagination.scenarios.js +29 -0
  107. package/dist/specs/pagination.scenarios.js.map +1 -0
  108. package/dist/specs/popover.d.ts +201 -0
  109. package/dist/specs/popover.d.ts.map +1 -0
  110. package/dist/specs/popover.js +169 -0
  111. package/dist/specs/popover.js.map +1 -0
  112. package/dist/specs/popover.scenarios.d.ts +83 -0
  113. package/dist/specs/popover.scenarios.d.ts.map +1 -0
  114. package/dist/specs/popover.scenarios.js +368 -0
  115. package/dist/specs/popover.scenarios.js.map +1 -0
  116. package/dist/specs/radio-group.d.ts +154 -0
  117. package/dist/specs/radio-group.d.ts.map +1 -0
  118. package/dist/specs/radio-group.js +133 -0
  119. package/dist/specs/radio-group.js.map +1 -0
  120. package/dist/specs/radio-group.scenarios.d.ts +35 -0
  121. package/dist/specs/radio-group.scenarios.d.ts.map +1 -0
  122. package/dist/specs/radio-group.scenarios.js +67 -0
  123. package/dist/specs/radio-group.scenarios.js.map +1 -0
  124. package/dist/specs/select.d.ts +167 -0
  125. package/dist/specs/select.d.ts.map +1 -0
  126. package/dist/specs/select.js +148 -0
  127. package/dist/specs/select.js.map +1 -0
  128. package/dist/specs/select.scenarios.d.ts +37 -0
  129. package/dist/specs/select.scenarios.d.ts.map +1 -0
  130. package/dist/specs/select.scenarios.js +77 -0
  131. package/dist/specs/select.scenarios.js.map +1 -0
  132. package/dist/specs/switch.d.ts +123 -0
  133. package/dist/specs/switch.d.ts.map +1 -0
  134. package/dist/specs/switch.js +113 -0
  135. package/dist/specs/switch.js.map +1 -0
  136. package/dist/specs/switch.scenarios.d.ts +27 -0
  137. package/dist/specs/switch.scenarios.d.ts.map +1 -0
  138. package/dist/specs/switch.scenarios.js +54 -0
  139. package/dist/specs/switch.scenarios.js.map +1 -0
  140. package/dist/specs/text-field.d.ts +232 -0
  141. package/dist/specs/text-field.d.ts.map +1 -0
  142. package/dist/specs/text-field.js +220 -0
  143. package/dist/specs/text-field.js.map +1 -0
  144. package/dist/specs/text-field.scenarios.d.ts +52 -0
  145. package/dist/specs/text-field.scenarios.d.ts.map +1 -0
  146. package/dist/specs/text-field.scenarios.js +106 -0
  147. package/dist/specs/text-field.scenarios.js.map +1 -0
  148. package/dist/specs/textarea.d.ts +144 -0
  149. package/dist/specs/textarea.d.ts.map +1 -0
  150. package/dist/specs/textarea.js +134 -0
  151. package/dist/specs/textarea.js.map +1 -0
  152. package/dist/specs/textarea.scenarios.d.ts +33 -0
  153. package/dist/specs/textarea.scenarios.d.ts.map +1 -0
  154. package/dist/specs/textarea.scenarios.js +82 -0
  155. package/dist/specs/textarea.scenarios.js.map +1 -0
  156. package/llms.md +797 -6
  157. package/package.json +1 -1
package/dist/codegen.js CHANGED
@@ -1,3 +1,77 @@
1
+ /** Small, dependency-free collections shared by generated examples and documentation controls. */
2
+ export const listboxItemExamples = {
3
+ flat: [
4
+ { value: 'apple', label: 'Apple' },
5
+ { value: 'apricot', label: 'Apricot' },
6
+ { value: 'banana', label: 'Banana', disabled: true },
7
+ { value: 'cherry', label: 'Cherry', description: 'Seasonal' },
8
+ ],
9
+ grouped: [
10
+ {
11
+ label: 'Fruit',
12
+ options: [
13
+ { value: 'apple', label: 'Apple' },
14
+ { value: 'cherry', label: 'Cherry', description: 'Seasonal' },
15
+ ],
16
+ },
17
+ {
18
+ label: 'Vegetables',
19
+ options: [
20
+ { value: 'carrot', label: 'Carrot' },
21
+ { value: 'broccoli', label: 'Broccoli', disabled: true },
22
+ ],
23
+ },
24
+ ],
25
+ };
26
+ export const selectItemExamples = {
27
+ flat: [
28
+ { value: 'apple', label: 'Apple' },
29
+ { value: 'cherry', label: 'Cherry' },
30
+ { value: 'lime', label: 'Lime', disabled: true },
31
+ ],
32
+ };
33
+ export const listboxValueExamples = {
34
+ empty: [],
35
+ apple: ['apple'],
36
+ cherry: ['cherry'],
37
+ 'apple-cherry': ['apple', 'cherry'],
38
+ };
39
+ export const radioGroupOptionsExamples = {
40
+ 'contact-methods': [
41
+ { value: 'email', label: 'Email', description: 'Receive updates by email.' },
42
+ { value: 'phone', label: 'Phone', description: 'Receive updates by text.' },
43
+ { value: 'mail', label: 'Mail', disabled: true },
44
+ ],
45
+ plans: [
46
+ { value: 'basic', label: 'Basic' },
47
+ { value: 'plus', label: 'Plus' },
48
+ { value: 'premium', label: 'Premium' },
49
+ ],
50
+ frequency: [
51
+ { value: 'weekly', label: 'Weekly' },
52
+ { value: 'monthly', label: 'Monthly' },
53
+ ],
54
+ };
55
+ export const radioGroupValueExamples = {
56
+ none: null,
57
+ email: 'email',
58
+ phone: 'phone',
59
+ basic: 'basic',
60
+ plus: 'plus',
61
+ premium: 'premium',
62
+ weekly: 'weekly',
63
+ monthly: 'monthly',
64
+ };
65
+ export const selectValueExamples = {
66
+ none: null,
67
+ apple: 'apple',
68
+ cherry: 'cherry',
69
+ basic: 'basic',
70
+ plus: 'plus',
71
+ premium: 'premium',
72
+ weekly: 'weekly',
73
+ monthly: 'monthly',
74
+ };
1
75
  /** Code tabs of the Playground. */
2
76
  export const snippetTargets = ['html', 'react', 'angular', 'native'];
3
77
  const platformOf = {
@@ -28,6 +102,50 @@ const referenceBinding = (value) => {
28
102
  const binding = referenceSlug(value).replace(/-([a-z0-9])/g, (_, character) => character.toUpperCase());
29
103
  return reservedBindings.has(binding) ? `${binding}Icon` : binding;
30
104
  };
105
+ function referenceImports(props, indent = '', typescript = true) {
106
+ return [
107
+ ...new Set(props
108
+ .filter(({ spec }) => spec.type === 'reference')
109
+ .map(({ spec, value }) => {
110
+ const slug = referenceSlug(String(value));
111
+ if (spec.type === 'reference' &&
112
+ (spec.reference === 'ListboxItems' ||
113
+ spec.reference === 'ListboxValues' ||
114
+ spec.reference === 'SelectItems')) {
115
+ const samples = spec.reference === 'ListboxItems'
116
+ ? listboxItemExamples
117
+ : spec.reference === 'SelectItems'
118
+ ? selectItemExamples
119
+ : listboxValueExamples;
120
+ const sample = samples[slug];
121
+ if (!sample)
122
+ throw new Error(`Unknown ${spec.reference} example "${slug}".`);
123
+ return `${indent}const ${referenceBinding(slug)} = ${JSON.stringify(sample)}${typescript ? ' as const' : ''};`;
124
+ }
125
+ if (spec.type === 'reference' && spec.reference === 'RadioGroupOptions') {
126
+ const sample = radioGroupOptionsExamples[slug];
127
+ if (!sample)
128
+ throw new Error(`Unknown ${spec.reference} example "${slug}".`);
129
+ return `${indent}const ${referenceBinding(slug)} = ${JSON.stringify(sample)}${typescript ? ' as const' : ''};`;
130
+ }
131
+ if (spec.type === 'reference' && spec.reference === 'RadioGroupValue') {
132
+ if (!Object.hasOwn(radioGroupValueExamples, slug)) {
133
+ throw new Error(`Unknown ${spec.reference} example "${slug}".`);
134
+ }
135
+ const sample = radioGroupValueExamples[slug];
136
+ return `${indent}const ${referenceBinding(slug)} = ${JSON.stringify(sample)}${typescript ? ' as const' : ''};`;
137
+ }
138
+ if (spec.type === 'reference' && spec.reference === 'SelectValue') {
139
+ if (!Object.hasOwn(selectValueExamples, slug)) {
140
+ throw new Error(`Unknown ${spec.reference} example "${slug}".`);
141
+ }
142
+ const sample = selectValueExamples[slug];
143
+ return `${indent}const ${referenceBinding(slug)} = ${JSON.stringify(sample)}${typescript ? ' as const' : ''};`;
144
+ }
145
+ return `${indent}import { ${referenceBinding(slug)} } from '@joralvmel/cosmos-icons/${slug}';`;
146
+ })),
147
+ ];
148
+ }
31
149
  /** Props that differ from their contract default and exist on the platform. */
32
150
  export function activeProps(contract, values, platform) {
33
151
  const result = [];
@@ -35,7 +153,10 @@ export function activeProps(contract, values, platform) {
35
153
  if (spec.platforms && !spec.platforms.includes(platform))
36
154
  continue;
37
155
  const value = values[name];
38
- if (value === undefined || ('default' in spec && value === spec.default))
156
+ if (value === undefined ||
157
+ ('default' in spec &&
158
+ value === spec.default &&
159
+ !contract.states.some(state => state.prop === name)))
39
160
  continue;
40
161
  result.push({ name, spec, value });
41
162
  }
@@ -50,18 +171,23 @@ function html(contract, values, content) {
50
171
  ? toAttribute(name)
51
172
  : `${toAttribute(name)}="${escapeAttribute(String(value))}"`);
52
173
  const properties = props.filter(({ spec, value }) => spec.type === 'reference' || (spec.type === 'boolean' && value === false));
174
+ const trigger = Object.hasOwn(contract.slots, 'trigger')
175
+ ? contract.name === 'popover'
176
+ ? '<cosm-icon-button slot="trigger" label="Details" variant="ghost" tone="neutral"><cosm-icon></cosm-icon></cosm-icon-button>'
177
+ : '<button slot="trigger" type="button">Details</button>'
178
+ : '';
53
179
  const lines = [
54
- `<${[contract.tag, ...attributes].join(' ')}>${escapeText(content)}</${contract.tag}>`,
180
+ `<${[contract.tag, ...attributes].join(' ')}>${trigger}${escapeText(content)}</${contract.tag}>`,
55
181
  '',
56
182
  '<script type="module">',
57
183
  ` import '@joralvmel/cosmos-elements/${contract.name}';`,
58
- ...properties
59
- .filter(({ spec }) => spec.type === 'reference')
60
- .map(({ value }) => {
61
- const slug = referenceSlug(String(value));
62
- const binding = referenceBinding(slug);
63
- return ` import { ${binding} } from '@joralvmel/cosmos-icons/${slug}';`;
64
- }),
184
+ ...(contract.name === 'popover'
185
+ ? [
186
+ " import { info } from '@joralvmel/cosmos-icons/info';",
187
+ ' document.querySelector(\'cosm-popover [slot="trigger"] cosm-icon\').icon = info;',
188
+ ]
189
+ : []),
190
+ ...referenceImports(properties, ' ', false),
65
191
  ];
66
192
  const events = Object.values(contract.events);
67
193
  if (events.length > 0 || properties.length > 0) {
@@ -87,16 +213,18 @@ function jsxAttributes(props) {
87
213
  ? `${name}={${value}}`
88
214
  : `${name}=${jsxString(String(value))}`);
89
215
  }
90
- function jsxElement(component, attributes, content) {
216
+ function jsxElement(component, attributes, content, trigger = '', nativeText = false) {
217
+ const child = content && nativeText ? `<Text>${jsxText(content)}</Text>` : jsxText(content);
91
218
  const open = `<${[component, ...attributes].join(' ')}>`;
92
- const inline = ` ${open}${jsxText(content)}</${component}>`;
219
+ const inline = ` ${open}${trigger}${child}</${component}>`;
93
220
  if (inline.length <= 80)
94
221
  return [inline];
95
222
  return [
96
223
  ` <${component}`,
97
224
  ...attributes.map(item => ` ${item}`),
98
225
  ' >',
99
- ` ${jsxText(content)}`,
226
+ ...(trigger ? [` ${trigger}`] : []),
227
+ ...(content ? [` ${child}`] : []),
100
228
  ` </${component}>`,
101
229
  ];
102
230
  }
@@ -112,6 +240,137 @@ function example(importLine, element, body = []) {
112
240
  '}',
113
241
  ].join('\n');
114
242
  }
243
+ function dialogExample(contract, values, content, target) {
244
+ const props = activeProps(contract, values, platformOf[target]).filter(prop => prop.name !== 'open');
245
+ if (props.some(({ spec }) => spec.type === 'reference')) {
246
+ throw new Error('Dialog focus targets must be assigned as platform-specific references.');
247
+ }
248
+ const controlled = typeof values.open === 'boolean';
249
+ const initialOpen = values.open === true;
250
+ const body = content || 'Review the details before continuing.';
251
+ if (target === 'html') {
252
+ const attributes = props
253
+ .filter(({ spec, value }) => spec.type !== 'reference' && value !== false)
254
+ .map(({ name, value }) => value === true
255
+ ? toAttribute(name)
256
+ : `${toAttribute(name)}="${escapeAttribute(String(value))}"`);
257
+ const properties = props.filter(({ spec, value }) => spec.type === 'reference' || value === false);
258
+ return [
259
+ '<cosm-button type="button" id="open-dialog">Open dialog</cosm-button>',
260
+ `<cosm-dialog${attributes.length ? ` ${attributes.join(' ')}` : ''}>`,
261
+ ' <h2 slot="title">Review details</h2>',
262
+ ` <p>${escapeText(body)}</p>`,
263
+ ' <cosm-button slot="actions" type="button" id="done">Done</cosm-button>',
264
+ '</cosm-dialog>',
265
+ '<script type="module">',
266
+ " import '@joralvmel/cosmos-elements/dialog';",
267
+ " import '@joralvmel/cosmos-elements/button';",
268
+ ...referenceImports(properties, ' ', false),
269
+ " const dialog = document.querySelector('cosm-dialog');",
270
+ ...properties.map(({ name, spec, value }) => ` dialog.${name} = ${spec.type === 'reference' ? referenceBinding(String(value)) : 'false'};`),
271
+ ...(controlled ? [` dialog.open = ${initialOpen};`] : []),
272
+ " document.querySelector('#open-dialog').addEventListener('cosm-press', () => dialog.requestOpen());",
273
+ " document.querySelector('#done').addEventListener('cosm-press', () => dialog.requestClose());",
274
+ " dialog.addEventListener('cosm-open-change', event => {",
275
+ ...(controlled ? [' if (!event.defaultPrevented) dialog.open = event.detail.open;'] : []),
276
+ ' console.log(event.detail);',
277
+ ' });',
278
+ '</script>',
279
+ ].join('\n');
280
+ }
281
+ if (target === 'angular') {
282
+ const attributes = props.map(({ name, spec, value }) => spec.type === 'reference'
283
+ ? `[${name}]="${referenceBinding(String(value))}"`
284
+ : spec.type === 'boolean' && value === false
285
+ ? `[${name}]="false"`
286
+ : value === true
287
+ ? name
288
+ : typeof value === 'number'
289
+ ? `[${name}]="${value}"`
290
+ : `${name}="${escapeAttribute(String(value))}"`);
291
+ return [
292
+ "import { Component, ViewChild } from '@angular/core';",
293
+ "import { CosmButtonComponent, CosmDialogComponent } from '@joralvmel/cosmos-angular';",
294
+ "import type { DialogOpenChangeDetail } from '@joralvmel/cosmos-core';",
295
+ ...referenceImports(props),
296
+ '',
297
+ '@Component({',
298
+ " selector: 'app-example',",
299
+ ' imports: [CosmButtonComponent, CosmDialogComponent],',
300
+ ' template: `',
301
+ ' <cosm-button type="button" (press)="dialog?.requestOpen()">Open dialog</cosm-button>',
302
+ ` <cosm-dialog${attributes.length ? ` ${attributes.join(' ')}` : ''}${controlled ? ' [open]="isOpen"' : ''} (openChange)="onOpenChange($event)">`,
303
+ ' <h2 slot="title">Review details</h2>',
304
+ ` <p>${escapeText(body).replaceAll('`', '\\`')}</p>`,
305
+ ' <cosm-button slot="actions" type="button" (press)="dialog?.requestClose()">Done</cosm-button>',
306
+ ' </cosm-dialog>',
307
+ ' `,',
308
+ '})',
309
+ 'export class ExampleComponent {',
310
+ ' @ViewChild(CosmDialogComponent) dialog?: CosmDialogComponent;',
311
+ ...(controlled ? [` isOpen = ${initialOpen};`] : []),
312
+ ...props
313
+ .filter(({ spec }) => spec.type === 'reference')
314
+ .map(({ value }) => ` readonly ${referenceBinding(String(value))} = ${referenceBinding(String(value))};`),
315
+ ' onOpenChange(event: CustomEvent<DialogOpenChangeDetail>) {',
316
+ ...(controlled ? [' if (!event.defaultPrevented) this.isOpen = event.detail.open;'] : []),
317
+ ' console.log(event.detail);',
318
+ ' }',
319
+ '}',
320
+ ].join('\n');
321
+ }
322
+ const isNative = target === 'native';
323
+ const attributes = jsxAttributes(props);
324
+ return [
325
+ `import { ${isNative ? 'type ComponentRef, ' : ''}useRef${controlled ? ', useState' : ''} } from 'react';`,
326
+ isNative
327
+ ? "import { Button, Dialog, type DialogHandle } from '@joralvmel/cosmos-native';"
328
+ : "import { Button, Dialog } from '@joralvmel/cosmos-react';",
329
+ ...(isNative
330
+ ? ["import { type Pressable, Text } from 'react-native';"]
331
+ : ["import type { CosmDialog } from '@joralvmel/cosmos-elements/dialog';"]),
332
+ ...referenceImports(props),
333
+ '',
334
+ 'export function Example() {',
335
+ ` const dialog = useRef<${isNative ? 'DialogHandle' : 'CosmDialog'}>(null);`,
336
+ ...(isNative ? [' const opener = useRef<ComponentRef<typeof Pressable>>(null);'] : []),
337
+ ...(controlled ? [` const [open, setOpen] = useState(${initialOpen});`] : []),
338
+ ' return (',
339
+ ' <>',
340
+ isNative
341
+ ? ' <Button ref={opener} onPress={() => dialog.current?.requestOpen()}>Open dialog</Button>'
342
+ : ' <Button onPress={() => dialog.current?.requestOpen()}>Open dialog</Button>',
343
+ ' <Dialog',
344
+ ' ref={dialog}',
345
+ ...(isNative ? [' title="Review details"', ' returnFocusTarget={opener}'] : []),
346
+ ...attributes.map(attribute => ` ${attribute}`),
347
+ ...(controlled ? [' open={open}'] : []),
348
+ ` onOpenChange={${isNative ? 'detail' : 'event'} => {`,
349
+ ...(!isNative ? [' const detail = event.detail;'] : []),
350
+ ...(controlled
351
+ ? [
352
+ ` if (${isNative ? 'detail.open || !detail.defaultPrevented' : '!event.defaultPrevented'}) setOpen(detail.open);`,
353
+ ]
354
+ : []),
355
+ ' console.log(detail);',
356
+ ' }}',
357
+ ...(isNative
358
+ ? [' actions={<Button onPress={() => dialog.current?.requestClose()}>Done</Button>}']
359
+ : []),
360
+ ' >',
361
+ ...(!isNative ? [' <h2 slot="title">Review details</h2>'] : []),
362
+ isNative ? ` <Text>${jsxText(body)}</Text>` : ` <p>${jsxText(body)}</p>`,
363
+ ...(!isNative
364
+ ? [
365
+ ' <Button slot="actions" onPress={() => dialog.current?.requestClose()}>Done</Button>',
366
+ ]
367
+ : []),
368
+ ' </Dialog>',
369
+ ' </>',
370
+ ' );',
371
+ '}',
372
+ ].join('\n');
373
+ }
115
374
  function react(contract, values, content) {
116
375
  content = contentFor(contract, content);
117
376
  const component = pascalCase(contract.name);
@@ -120,14 +379,17 @@ function react(contract, values, content) {
120
379
  ...jsxAttributes(props),
121
380
  ...Object.values(contract.events).map(event => `${event.names.react}={event => console.log(event.detail)}`),
122
381
  ];
123
- const referenceImports = props
124
- .filter(({ spec }) => spec.type === 'reference')
125
- .map(({ value }) => {
126
- const slug = referenceSlug(String(value));
127
- const binding = referenceBinding(slug);
128
- return `import { ${binding} } from '@joralvmel/cosmos-icons/${slug}';`;
129
- });
130
- return example([`import { ${component} } from '@joralvmel/cosmos-react';`, ...referenceImports].join('\n'), jsxElement(component, attributes, content));
382
+ return example([
383
+ `import { ${component}${contract.name === 'popover' ? ', IconButton, Icon' : ''} } from '@joralvmel/cosmos-react';`,
384
+ ...(contract.name === 'popover'
385
+ ? ["import { info } from '@joralvmel/cosmos-icons/info';"]
386
+ : []),
387
+ ...referenceImports(props),
388
+ ].join('\n'), jsxElement(component, attributes, content, Object.hasOwn(contract.slots, 'trigger')
389
+ ? contract.name === 'popover'
390
+ ? '<IconButton slot="trigger" label="Details" variant="ghost" tone="neutral"><Icon icon={info} /></IconButton>'
391
+ : '<button slot="trigger" type="button">Details</button>'
392
+ : ''));
131
393
  }
132
394
  function angular(contract, values, content) {
133
395
  content = contentFor(contract, content);
@@ -135,49 +397,59 @@ function angular(contract, values, content) {
135
397
  const props = activeProps(contract, values, 'angular');
136
398
  const attributes = props.map(({ name, spec, value }) => spec.type === 'reference'
137
399
  ? `[${name}]="${referenceBinding(String(value))}"`
138
- : spec.type === 'boolean' && value === false
139
- ? `[${name}]="false"`
140
- : value === true
141
- ? name
142
- : typeof value === 'number'
143
- ? `[${name}]="${value}"`
144
- : `${name}="${escapeAttribute(String(value))}"`);
400
+ : spec.type === 'boolean' && contract.states.some(state => state.prop === name)
401
+ ? `[${name}]="${value}"`
402
+ : spec.type === 'boolean' && value === false
403
+ ? `[${name}]="false"`
404
+ : value === true
405
+ ? name
406
+ : typeof value === 'number'
407
+ ? `[${name}]="${value}"`
408
+ : `${name}="${escapeAttribute(String(value))}"`);
145
409
  const events = Object.values(contract.events);
146
410
  const handler = (output) => `on${pascalCase(output)}`;
147
411
  const outputs = events.map(event => `(${event.names.angular})="${handler(event.names.angular)}($event.detail)"`);
148
- const template = `<${[contract.tag, ...attributes, ...outputs].join(' ')}>${escapeText(content)}</${contract.tag}>`;
412
+ const trigger = Object.hasOwn(contract.slots, 'trigger')
413
+ ? contract.name === 'popover'
414
+ ? '<cosm-icon-button slot="trigger" label="Details" variant="ghost" tone="neutral"><cosm-icon [icon]="info" /></cosm-icon-button>'
415
+ : '<button slot="trigger" type="button">Details</button>'
416
+ : '';
417
+ const template = `<${[contract.tag, ...attributes, ...outputs].join(' ')}>${trigger}${escapeText(content)}</${contract.tag}>`;
149
418
  const detailTypes = [...new Set(events.map(event => event.detail))];
150
- const referenceImports = props
151
- .filter(({ spec }) => spec.type === 'reference')
152
- .map(({ value }) => {
153
- const slug = referenceSlug(String(value));
154
- const binding = referenceBinding(slug);
155
- return `import { ${binding} } from '@joralvmel/cosmos-icons/${slug}';`;
156
- });
157
- const referenceFields = props
158
- .filter(({ spec }) => spec.type === 'reference')
159
- .map(({ value }) => {
160
- const binding = referenceBinding(String(value));
161
- return ` readonly ${binding} = ${binding};`;
162
- });
419
+ const referenceFields = [
420
+ ...new Set(props
421
+ .filter(({ spec }) => spec.type === 'reference')
422
+ .map(({ value }) => {
423
+ const binding = referenceBinding(String(value));
424
+ return ` readonly ${binding} = ${binding};`;
425
+ })),
426
+ ];
163
427
  const body = events.flatMap((event, index) => [
164
428
  ...(index > 0 ? [''] : []),
165
429
  ` ${handler(event.names.angular)}(detail: ${event.detail}) {`,
166
430
  ' console.log(detail);',
167
431
  ' }',
168
432
  ]);
169
- const classMembers = [...referenceFields, ...body];
433
+ const classMembers = [
434
+ ...(contract.name === 'popover' ? [' readonly info = info;'] : []),
435
+ ...referenceFields,
436
+ ...body,
437
+ ];
438
+ const components = `${component}${contract.name === 'popover' ? ', CosmIconButtonComponent, CosmIconComponent' : ''}`;
170
439
  return [
171
440
  "import { Component } from '@angular/core';",
172
- `import { ${component} } from '@joralvmel/cosmos-angular';`,
173
- ...referenceImports,
441
+ `import { ${components} } from '@joralvmel/cosmos-angular';`,
442
+ ...(contract.name === 'popover'
443
+ ? ["import { info } from '@joralvmel/cosmos-icons/info';"]
444
+ : []),
445
+ ...referenceImports(props),
174
446
  ...(detailTypes.length > 0
175
447
  ? [`import type { ${detailTypes.join(', ')} } from '@joralvmel/cosmos-core';`]
176
448
  : []),
177
449
  '',
178
450
  '@Component({',
179
451
  " selector: 'app-example',",
180
- ` imports: [${component}],`,
452
+ ` imports: [${components}],`,
181
453
  ` template: \`${template.replaceAll('`', '\\`')}\`,`,
182
454
  '})',
183
455
  classMembers.length > 0
@@ -191,13 +463,19 @@ const colorToken = (value) => typeof value === 'string'
191
463
  : undefined;
192
464
  const themeColor = (token) => /^[a-z][a-z0-9]*$/.test(token) ? `theme.colors.${token}` : `theme.colors['${token}']`;
193
465
  /** Paint props whose Cosmos color tokens become `useTheme()` colors; other strings stay literal. */
194
- const themedPaintProps = { icon: ['stroke', 'fill'] };
466
+ const themedPaintProps = { icon: ['color'] };
195
467
  function native(contract, values, content) {
196
468
  content = contentFor(contract, content);
197
469
  const component = pascalCase(contract.name);
198
470
  const props = activeProps(contract, values, 'native');
471
+ const isPopover = contract.name === 'popover';
199
472
  let themed = false;
200
473
  const attributes = [
474
+ ...(isPopover
475
+ ? [
476
+ 'trigger={props => <IconButton {...props} label="Details" variant="ghost" tone="neutral"><Icon icon={info} /></IconButton>}',
477
+ ]
478
+ : []),
201
479
  ...props.map(prop => {
202
480
  const token = themedPaintProps[contract.name]?.includes(prop.name)
203
481
  ? colorToken(prop.value)
@@ -209,15 +487,20 @@ function native(contract, values, content) {
209
487
  }),
210
488
  ...Object.values(contract.events).map(event => `${event.names.native}={detail => console.log(detail)}`),
211
489
  ];
212
- const referenceImports = props
213
- .filter(({ spec }) => spec.type === 'reference')
214
- .map(({ value }) => {
215
- const slug = referenceSlug(String(value));
216
- const binding = referenceBinding(slug);
217
- return `import { ${binding} } from '@joralvmel/cosmos-icons/${slug}';`;
218
- });
219
- const imports = themed ? `${component}, useTheme` : component;
220
- return example([`import { ${imports} } from '@joralvmel/cosmos-native';`, ...referenceImports].join('\n'), jsxElement(component, attributes, content), themed ? [' const theme = useTheme();'] : []);
490
+ const imports = [
491
+ component,
492
+ ...(isPopover ? ['PopoverProvider', 'IconButton', 'Icon'] : []),
493
+ ...(themed ? ['useTheme'] : []),
494
+ ].join(', ');
495
+ const element = jsxElement(component, attributes, content, '', isPopover);
496
+ return example([
497
+ `import { ${imports} } from '@joralvmel/cosmos-native';`,
498
+ ...(isPopover ? ["import { info } from '@joralvmel/cosmos-icons/info';"] : []),
499
+ ...(isPopover && content ? ["import { Text } from 'react-native';"] : []),
500
+ ...referenceImports(props),
501
+ ].join('\n'), isPopover
502
+ ? [' <PopoverProvider>', ...element.map(line => ` ${line}`), ' </PopoverProvider>']
503
+ : element, themed ? [' const theme = useTheme();'] : []);
221
504
  }
222
505
  /** Whether a code tab applies to the component (no Web Component tabs for native-only ones). */
223
506
  export function supportsTarget(contract, target) {
@@ -228,16 +511,72 @@ export function generateSnippets(contract, values, content) {
228
511
  const builders = { html, react, angular, native };
229
512
  const sampleValues = { ...values };
230
513
  for (const [name, spec] of Object.entries(contract.props)) {
231
- if (spec.type === 'reference' && sampleValues[name] === undefined && spec.example) {
514
+ if (spec.type === 'string' && spec.required && sampleValues[name] === undefined) {
515
+ sampleValues[name] = pascalCase(contract.name);
516
+ }
517
+ if (spec.type === 'reference' && !Object.hasOwn(values, name) && spec.example) {
232
518
  sampleValues[name] = spec.example;
233
519
  }
234
520
  }
235
521
  const result = {};
236
522
  for (const target of snippetTargets) {
237
523
  if (supportsTarget(contract, target)) {
238
- result[target] = builders[target](contract, sampleValues, content);
524
+ result[target] =
525
+ contract.name === 'dialog'
526
+ ? dialogExample(contract, sampleValues, content, target)
527
+ : builders[target](contract, sampleValues, content);
239
528
  }
240
529
  }
241
530
  return result;
242
531
  }
532
+ /** Compose a default-slot child into each platform's generated example. */
533
+ export function generateComposedSnippets(contract, values, content, child) {
534
+ if (!child)
535
+ return generateSnippets(contract, values, content);
536
+ const marker = 'COSMOS_PLAYGROUND_CHILD';
537
+ const parents = generateSnippets(contract, values, marker);
538
+ const children = generateSnippets(child.contract, child.values, '');
539
+ const component = pascalCase(child.contract.name);
540
+ for (const platform of snippetTargets) {
541
+ let parent = parents[platform];
542
+ const source = children[platform];
543
+ if (!parent || !source)
544
+ continue;
545
+ const tag = platform === 'html' || platform === 'angular' ? child.contract.tag : component;
546
+ const start = source.indexOf(`<${tag}`);
547
+ const end = source.indexOf(`</${tag}>`, start) + tag.length + 3;
548
+ if (start < 0 || end < tag.length + 3)
549
+ throw new Error('Missing generated child markup.');
550
+ parent = parent.replace(marker, source.slice(start, end));
551
+ const wrapperImport = /import \{ ([^}]+) \} from '(@joralvmel\/cosmos-(?:react|native|angular))';/;
552
+ const childNames = wrapperImport.exec(source)?.[1];
553
+ if (childNames) {
554
+ parent = parent.replace(wrapperImport, (_, names, module) => {
555
+ const merged = [...new Set([...names.split(', '), ...childNames.split(', ')])];
556
+ return `import { ${merged.join(', ')} } from '${module}';`;
557
+ });
558
+ }
559
+ const imports = source
560
+ .split('\n')
561
+ .filter(line => /^\s*import /.test(line) && !wrapperImport.test(line))
562
+ .filter(line => !parent?.includes(line));
563
+ if (platform === 'html') {
564
+ parent = parent.replace('<script type="module">', `<script type="module">\n${imports.join('\n')}`);
565
+ if (source.includes('\n const element =')) {
566
+ const setup = source.slice(source.indexOf('\n const element ='), source.indexOf('</script>'));
567
+ parent = parent.replace('</script>', `${setup.replaceAll('element', 'childElement')}</script>`);
568
+ }
569
+ }
570
+ else {
571
+ parent = [...imports, parent].join('\n');
572
+ if (platform === 'angular') {
573
+ parent = parent.replace(/imports: \[([^\]]+)\]/, (_, names) => `imports: [${names}, Cosm${component}Component]`);
574
+ const fields = source.split('\n').filter(line => /^\s+readonly /.test(line));
575
+ parent = parent.replace('export class ExampleComponent {', `export class ExampleComponent {\n${fields.join('\n')}`);
576
+ }
577
+ }
578
+ parents[platform] = parent;
579
+ }
580
+ return parents;
581
+ }
243
582
  //# sourceMappingURL=codegen.js.map