@openpkg-ts/react 0.2.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.
@@ -0,0 +1,670 @@
1
+ "use client";
2
+ var __create = Object.create;
3
+ var __getProtoOf = Object.getPrototypeOf;
4
+ var __defProp = Object.defineProperty;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
7
+ var __toESM = (mod, isNodeMode, target) => {
8
+ target = mod != null ? __create(__getProtoOf(mod)) : {};
9
+ const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
10
+ for (let key of __getOwnPropNames(mod))
11
+ if (!__hasOwnProp.call(to, key))
12
+ __defProp(to, key, {
13
+ get: () => mod[key],
14
+ enumerable: true
15
+ });
16
+ return to;
17
+ };
18
+ var __commonJS = (cb, mod) => () => (mod || cb((mod = { exports: {} }).exports, mod), mod.exports);
19
+
20
+ // src/components/headless/CollapsibleMethod.tsx
21
+ import { useEffect, useState } from "react";
22
+ import { getMemberBadges, formatReturnType, formatSchema } from "@openpkg-ts/sdk";
23
+ import { jsx, jsxs } from "react/jsx-runtime";
24
+
25
+ function formatParamPreview(params) {
26
+ if (!params || params.length === 0)
27
+ return "";
28
+ if (params.length === 1)
29
+ return params[0].name || "arg";
30
+ return `${params[0].name || "arg"}, ...`;
31
+ }
32
+ function CollapsibleMethod({
33
+ member,
34
+ defaultExpanded = false,
35
+ className,
36
+ renderHeader,
37
+ renderContent
38
+ }) {
39
+ const [expanded, setExpanded] = useState(defaultExpanded);
40
+ const sig = member.signatures?.[0];
41
+ const hasParams = sig?.parameters && sig.parameters.length > 0;
42
+ const returnType = formatReturnType(sig);
43
+ const paramPreview = formatParamPreview(sig?.parameters);
44
+ const badges = getMemberBadges(member);
45
+ const toggle = () => setExpanded(!expanded);
46
+ useEffect(() => {
47
+ if (typeof window !== "undefined" && window.location.hash === `#${member.name}`) {
48
+ setExpanded(true);
49
+ }
50
+ }, [member.name]);
51
+ return /* @__PURE__ */ jsxs("div", {
52
+ id: member.name,
53
+ className,
54
+ "data-expanded": expanded,
55
+ children: [
56
+ renderHeader ? renderHeader(member, expanded, toggle) : /* @__PURE__ */ jsxs("button", {
57
+ type: "button",
58
+ onClick: toggle,
59
+ "data-header": true,
60
+ children: [
61
+ /* @__PURE__ */ jsxs("span", {
62
+ "data-name": true,
63
+ children: [
64
+ member.name,
65
+ /* @__PURE__ */ jsxs("span", {
66
+ "data-params": true,
67
+ children: [
68
+ "(",
69
+ paramPreview,
70
+ ")"
71
+ ]
72
+ })
73
+ ]
74
+ }),
75
+ /* @__PURE__ */ jsx("span", {
76
+ "data-return": true,
77
+ children: returnType
78
+ }),
79
+ badges.length > 0 && /* @__PURE__ */ jsx("span", {
80
+ "data-badges": true,
81
+ children: badges.map((badge) => /* @__PURE__ */ jsx("span", {
82
+ "data-badge": badge,
83
+ children: badge
84
+ }, badge))
85
+ })
86
+ ]
87
+ }),
88
+ expanded && (renderContent ? renderContent(member) : /* @__PURE__ */ jsxs("div", {
89
+ "data-content": true,
90
+ children: [
91
+ member.description && /* @__PURE__ */ jsx("p", {
92
+ children: member.description
93
+ }),
94
+ hasParams && /* @__PURE__ */ jsxs("div", {
95
+ "data-params-section": true,
96
+ children: [
97
+ /* @__PURE__ */ jsx("h4", {
98
+ children: "Parameters"
99
+ }),
100
+ /* @__PURE__ */ jsx("ul", {
101
+ children: sig.parameters.map((param, index) => /* @__PURE__ */ jsxs("li", {
102
+ children: [
103
+ /* @__PURE__ */ jsxs("code", {
104
+ children: [
105
+ param.name,
106
+ param.required === false && "?",
107
+ ": ",
108
+ formatSchema(param.schema)
109
+ ]
110
+ }),
111
+ param.description && /* @__PURE__ */ jsx("span", {
112
+ children: param.description
113
+ })
114
+ ]
115
+ }, param.name ?? index))
116
+ })
117
+ ]
118
+ }),
119
+ sig?.returns && returnType !== "void" && /* @__PURE__ */ jsxs("div", {
120
+ "data-returns-section": true,
121
+ children: [
122
+ /* @__PURE__ */ jsx("h4", {
123
+ children: "Returns"
124
+ }),
125
+ /* @__PURE__ */ jsx("code", {
126
+ children: returnType
127
+ }),
128
+ sig.returns.description && /* @__PURE__ */ jsx("p", {
129
+ children: sig.returns.description
130
+ })
131
+ ]
132
+ })
133
+ ]
134
+ }))
135
+ ]
136
+ });
137
+ }
138
+
139
+ // src/components/headless/ExampleBlock.tsx
140
+ import { useState as useState2 } from "react";
141
+ import { jsx as jsx2, jsxs as jsxs2 } from "react/jsx-runtime";
142
+
143
+ function getExampleCode(example) {
144
+ if (typeof example === "string")
145
+ return example;
146
+ return example.code;
147
+ }
148
+ function getExampleTitle(example) {
149
+ if (typeof example === "string")
150
+ return;
151
+ return example.title;
152
+ }
153
+ function getExampleLanguage(example) {
154
+ if (typeof example === "string")
155
+ return "typescript";
156
+ return example.language ?? "typescript";
157
+ }
158
+ function cleanCode(code) {
159
+ let cleaned = code.trim();
160
+ if (cleaned.startsWith("```")) {
161
+ const lines = cleaned.split(`
162
+ `);
163
+ lines.shift();
164
+ if (lines[lines.length - 1] === "```") {
165
+ lines.pop();
166
+ }
167
+ cleaned = lines.join(`
168
+ `);
169
+ }
170
+ return cleaned;
171
+ }
172
+ function ExampleBlock({
173
+ examples,
174
+ className,
175
+ renderExample
176
+ }) {
177
+ const [activeIndex, setActiveIndex] = useState2(0);
178
+ if (!examples?.length)
179
+ return null;
180
+ const showTabs = examples.length > 1;
181
+ const currentExample = examples[activeIndex];
182
+ const code = cleanCode(getExampleCode(currentExample));
183
+ if (renderExample) {
184
+ return /* @__PURE__ */ jsxs2("div", {
185
+ className,
186
+ children: [
187
+ showTabs && /* @__PURE__ */ jsx2("div", {
188
+ "data-tabs": true,
189
+ children: examples.map((example, index) => /* @__PURE__ */ jsx2("button", {
190
+ type: "button",
191
+ onClick: () => setActiveIndex(index),
192
+ "data-active": activeIndex === index,
193
+ children: getExampleTitle(example) ?? `Example ${index + 1}`
194
+ }, index))
195
+ }),
196
+ renderExample(currentExample, activeIndex)
197
+ ]
198
+ });
199
+ }
200
+ return /* @__PURE__ */ jsxs2("div", {
201
+ className,
202
+ children: [
203
+ showTabs && /* @__PURE__ */ jsx2("div", {
204
+ "data-tabs": true,
205
+ children: examples.map((example, index) => /* @__PURE__ */ jsx2("button", {
206
+ type: "button",
207
+ onClick: () => setActiveIndex(index),
208
+ "data-active": activeIndex === index,
209
+ children: getExampleTitle(example) ?? `Example ${index + 1}`
210
+ }, index))
211
+ }),
212
+ /* @__PURE__ */ jsx2("pre", {
213
+ children: /* @__PURE__ */ jsx2("code", {
214
+ "data-language": getExampleLanguage(currentExample),
215
+ children: code
216
+ })
217
+ })
218
+ ]
219
+ });
220
+ }
221
+
222
+ // src/components/headless/ExpandableProperty.tsx
223
+ import { useState as useState3 } from "react";
224
+ import { formatSchema as formatSchema2 } from "@openpkg-ts/sdk";
225
+ import { jsx as jsx3, jsxs as jsxs3 } from "react/jsx-runtime";
226
+
227
+ function getNestedProperties(schema) {
228
+ if (!schema || typeof schema !== "object")
229
+ return null;
230
+ const s = schema;
231
+ if (s.type === "object" && s.properties && typeof s.properties === "object") {
232
+ return s.properties;
233
+ }
234
+ return null;
235
+ }
236
+ function getRequiredFields(schema) {
237
+ if (!schema || typeof schema !== "object")
238
+ return [];
239
+ const s = schema;
240
+ if (Array.isArray(s.required)) {
241
+ return s.required;
242
+ }
243
+ return [];
244
+ }
245
+ function countProperties(schema) {
246
+ const props = getNestedProperties(schema);
247
+ return props ? Object.keys(props).length : 0;
248
+ }
249
+ function NestedProperty({
250
+ name,
251
+ schema,
252
+ required = false,
253
+ depth = 0
254
+ }) {
255
+ const [expanded, setExpanded] = useState3(false);
256
+ const type = formatSchema2(schema);
257
+ const nestedProps = getNestedProperties(schema);
258
+ const nestedCount = countProperties(schema);
259
+ const hasNested = nestedCount > 0;
260
+ const schemaObj = schema;
261
+ const description = schemaObj?.description;
262
+ return /* @__PURE__ */ jsxs3("div", {
263
+ "data-property": name,
264
+ "data-depth": depth,
265
+ children: [
266
+ /* @__PURE__ */ jsxs3("div", {
267
+ "data-row": true,
268
+ children: [
269
+ /* @__PURE__ */ jsxs3("span", {
270
+ "data-name": true,
271
+ children: [
272
+ name,
273
+ !required && "?",
274
+ ":"
275
+ ]
276
+ }),
277
+ /* @__PURE__ */ jsx3("span", {
278
+ "data-type": true,
279
+ children: hasNested ? "object" : type
280
+ }),
281
+ description && /* @__PURE__ */ jsx3("span", {
282
+ "data-description": true,
283
+ children: description
284
+ }),
285
+ hasNested && /* @__PURE__ */ jsxs3("button", {
286
+ type: "button",
287
+ onClick: () => setExpanded(!expanded),
288
+ "data-expand": true,
289
+ children: [
290
+ nestedCount,
291
+ " properties"
292
+ ]
293
+ })
294
+ ]
295
+ }),
296
+ hasNested && expanded && nestedProps && /* @__PURE__ */ jsx3("div", {
297
+ "data-nested": true,
298
+ children: Object.entries(nestedProps).map(([propName, propSchema]) => /* @__PURE__ */ jsx3(NestedProperty, {
299
+ name: propName,
300
+ schema: propSchema,
301
+ required: getRequiredFields(schema).includes(propName),
302
+ depth: depth + 1
303
+ }, propName))
304
+ })
305
+ ]
306
+ });
307
+ }
308
+ function ExpandableProperty({
309
+ param,
310
+ depth = 0,
311
+ className
312
+ }) {
313
+ const [expanded, setExpanded] = useState3(false);
314
+ const type = formatSchema2(param.schema);
315
+ const isOptional = param.required === false;
316
+ const nestedProps = getNestedProperties(param.schema);
317
+ const nestedCount = countProperties(param.schema);
318
+ const hasNested = nestedCount > 0;
319
+ return /* @__PURE__ */ jsxs3("div", {
320
+ className,
321
+ "data-param": param.name,
322
+ "data-depth": depth,
323
+ children: [
324
+ /* @__PURE__ */ jsxs3("div", {
325
+ "data-row": true,
326
+ children: [
327
+ /* @__PURE__ */ jsxs3("span", {
328
+ "data-name": true,
329
+ children: [
330
+ param.name,
331
+ isOptional && "?",
332
+ ":"
333
+ ]
334
+ }),
335
+ /* @__PURE__ */ jsx3("span", {
336
+ "data-type": true,
337
+ children: hasNested ? "object" : type
338
+ }),
339
+ param.description && /* @__PURE__ */ jsx3("span", {
340
+ "data-description": true,
341
+ children: param.description
342
+ }),
343
+ hasNested && /* @__PURE__ */ jsxs3("button", {
344
+ type: "button",
345
+ onClick: () => setExpanded(!expanded),
346
+ "data-expand": true,
347
+ children: [
348
+ nestedCount,
349
+ " properties"
350
+ ]
351
+ })
352
+ ]
353
+ }),
354
+ hasNested && expanded && nestedProps && /* @__PURE__ */ jsx3("div", {
355
+ "data-nested": true,
356
+ children: Object.entries(nestedProps).map(([propName, propSchema]) => /* @__PURE__ */ jsx3(NestedProperty, {
357
+ name: propName,
358
+ schema: propSchema,
359
+ required: getRequiredFields(param.schema).includes(propName),
360
+ depth: depth + 1
361
+ }, propName))
362
+ })
363
+ ]
364
+ });
365
+ }
366
+
367
+ // src/components/headless/MembersTable.tsx
368
+ import { getMemberBadges as getMemberBadges2, formatSchema as formatSchema3 } from "@openpkg-ts/sdk";
369
+ import { jsx as jsx4, jsxs as jsxs4 } from "react/jsx-runtime";
370
+
371
+ function groupMembersByKind(members) {
372
+ const groups = {
373
+ constructors: [],
374
+ properties: [],
375
+ methods: [],
376
+ accessors: [],
377
+ other: []
378
+ };
379
+ for (const member of members) {
380
+ const kind = member.kind?.toLowerCase() ?? "other";
381
+ if (kind === "constructor") {
382
+ groups.constructors.push(member);
383
+ } else if (kind === "property" || kind === "field") {
384
+ groups.properties.push(member);
385
+ } else if (kind === "method" || kind === "function") {
386
+ groups.methods.push(member);
387
+ } else if (kind === "getter" || kind === "setter" || kind === "accessor") {
388
+ groups.accessors.push(member);
389
+ } else {
390
+ groups.other.push(member);
391
+ }
392
+ }
393
+ return groups;
394
+ }
395
+ function MemberRow({ member }) {
396
+ const type = formatSchema3(member.schema);
397
+ const sig = member.signatures?.[0];
398
+ let signature = "";
399
+ if (sig) {
400
+ const params = sig.parameters?.map((p) => {
401
+ const optional = p.required === false ? "?" : "";
402
+ return `${p.name}${optional}: ${formatSchema3(p.schema)}`;
403
+ }) ?? [];
404
+ const returnType = formatSchema3(sig.returns?.schema) ?? "void";
405
+ signature = `(${params.join(", ")}): ${returnType}`;
406
+ }
407
+ const badges = getMemberBadges2(member);
408
+ return /* @__PURE__ */ jsxs4("div", {
409
+ "data-member": member.name,
410
+ children: [
411
+ /* @__PURE__ */ jsxs4("div", {
412
+ children: [
413
+ /* @__PURE__ */ jsxs4("code", {
414
+ children: [
415
+ member.name,
416
+ signature
417
+ ]
418
+ }),
419
+ badges.length > 0 && /* @__PURE__ */ jsx4("span", {
420
+ children: badges.map((badge) => /* @__PURE__ */ jsx4("span", {
421
+ "data-badge": badge,
422
+ children: badge
423
+ }, badge))
424
+ })
425
+ ]
426
+ }),
427
+ !signature && type !== "unknown" && /* @__PURE__ */ jsx4("code", {
428
+ children: type
429
+ }),
430
+ member.description && /* @__PURE__ */ jsx4("p", {
431
+ children: member.description
432
+ })
433
+ ]
434
+ });
435
+ }
436
+ function MembersTable({
437
+ members,
438
+ className,
439
+ groupByKind = false,
440
+ renderMember
441
+ }) {
442
+ if (!members?.length)
443
+ return null;
444
+ if (!groupByKind) {
445
+ return /* @__PURE__ */ jsx4("div", {
446
+ className,
447
+ children: members.map((member, index) => renderMember ? renderMember(member, index) : /* @__PURE__ */ jsx4(MemberRow, {
448
+ member
449
+ }, member.name ?? index))
450
+ });
451
+ }
452
+ const groups = groupMembersByKind(members);
453
+ return /* @__PURE__ */ jsxs4("div", {
454
+ className,
455
+ children: [
456
+ groups.constructors.length > 0 && /* @__PURE__ */ jsxs4("section", {
457
+ "data-group": "constructors",
458
+ children: [
459
+ /* @__PURE__ */ jsx4("h4", {
460
+ children: "Constructor"
461
+ }),
462
+ groups.constructors.map((member, index) => renderMember ? renderMember(member, index) : /* @__PURE__ */ jsx4(MemberRow, {
463
+ member
464
+ }, member.name ?? index))
465
+ ]
466
+ }),
467
+ groups.properties.length > 0 && /* @__PURE__ */ jsxs4("section", {
468
+ "data-group": "properties",
469
+ children: [
470
+ /* @__PURE__ */ jsx4("h4", {
471
+ children: "Properties"
472
+ }),
473
+ groups.properties.map((member, index) => renderMember ? renderMember(member, index) : /* @__PURE__ */ jsx4(MemberRow, {
474
+ member
475
+ }, member.name ?? index))
476
+ ]
477
+ }),
478
+ groups.methods.length > 0 && /* @__PURE__ */ jsxs4("section", {
479
+ "data-group": "methods",
480
+ children: [
481
+ /* @__PURE__ */ jsx4("h4", {
482
+ children: "Methods"
483
+ }),
484
+ groups.methods.map((member, index) => renderMember ? renderMember(member, index) : /* @__PURE__ */ jsx4(MemberRow, {
485
+ member
486
+ }, member.name ?? index))
487
+ ]
488
+ }),
489
+ groups.accessors.length > 0 && /* @__PURE__ */ jsxs4("section", {
490
+ "data-group": "accessors",
491
+ children: [
492
+ /* @__PURE__ */ jsx4("h4", {
493
+ children: "Accessors"
494
+ }),
495
+ groups.accessors.map((member, index) => renderMember ? renderMember(member, index) : /* @__PURE__ */ jsx4(MemberRow, {
496
+ member
497
+ }, member.name ?? index))
498
+ ]
499
+ })
500
+ ]
501
+ });
502
+ }
503
+
504
+ // src/components/headless/ParamTable.tsx
505
+ import { formatSchema as formatSchema4 } from "@openpkg-ts/sdk";
506
+ import { jsx as jsx5, jsxs as jsxs5 } from "react/jsx-runtime";
507
+
508
+ function isParameter(item) {
509
+ return "required" in item;
510
+ }
511
+ function ParamRow({ item, showRequired = true }) {
512
+ const name = item.name ?? "arg";
513
+ const type = formatSchema4(item.schema);
514
+ const description = item.description ?? "";
515
+ const required = isParameter(item) ? item.required : true;
516
+ return /* @__PURE__ */ jsxs5("tr", {
517
+ children: [
518
+ /* @__PURE__ */ jsxs5("td", {
519
+ children: [
520
+ /* @__PURE__ */ jsx5("code", {
521
+ children: name
522
+ }),
523
+ showRequired && required && /* @__PURE__ */ jsx5("span", {
524
+ children: "*"
525
+ }),
526
+ showRequired && !required && /* @__PURE__ */ jsx5("span", {
527
+ children: "?"
528
+ })
529
+ ]
530
+ }),
531
+ /* @__PURE__ */ jsx5("td", {
532
+ children: /* @__PURE__ */ jsx5("code", {
533
+ children: type
534
+ })
535
+ }),
536
+ /* @__PURE__ */ jsx5("td", {
537
+ children: description
538
+ })
539
+ ]
540
+ });
541
+ }
542
+ function ParamTable({
543
+ items,
544
+ showRequired = true,
545
+ className,
546
+ renderRow
547
+ }) {
548
+ if (!items?.length)
549
+ return null;
550
+ return /* @__PURE__ */ jsxs5("table", {
551
+ className,
552
+ children: [
553
+ /* @__PURE__ */ jsx5("thead", {
554
+ children: /* @__PURE__ */ jsxs5("tr", {
555
+ children: [
556
+ /* @__PURE__ */ jsx5("th", {
557
+ children: "Name"
558
+ }),
559
+ /* @__PURE__ */ jsx5("th", {
560
+ children: "Type"
561
+ }),
562
+ /* @__PURE__ */ jsx5("th", {
563
+ children: "Description"
564
+ })
565
+ ]
566
+ })
567
+ }),
568
+ /* @__PURE__ */ jsx5("tbody", {
569
+ children: items.map((item, index) => renderRow ? renderRow(item, index) : /* @__PURE__ */ jsx5(ParamRow, {
570
+ item,
571
+ showRequired
572
+ }, item.name ?? index))
573
+ })
574
+ ]
575
+ });
576
+ }
577
+
578
+ // src/components/headless/Signature.tsx
579
+ import { buildSignatureString } from "@openpkg-ts/sdk";
580
+ import { jsx as jsx6 } from "react/jsx-runtime";
581
+
582
+ function Signature({
583
+ export: exp,
584
+ signatureIndex = 0,
585
+ className,
586
+ children
587
+ }) {
588
+ const signature = buildSignatureString(exp, signatureIndex);
589
+ if (children) {
590
+ return children(signature);
591
+ }
592
+ return /* @__PURE__ */ jsx6("code", {
593
+ className,
594
+ children: signature
595
+ });
596
+ }
597
+
598
+ // src/components/headless/TypeTable.tsx
599
+ import { formatSchema as formatSchema5 } from "@openpkg-ts/sdk";
600
+ import { jsx as jsx7, jsxs as jsxs6 } from "react/jsx-runtime";
601
+
602
+ function isParameter2(item) {
603
+ return "required" in item;
604
+ }
605
+ function TypeTable({
606
+ items,
607
+ showRequired = true,
608
+ className,
609
+ renderRow
610
+ }) {
611
+ if (!items?.length)
612
+ return null;
613
+ return /* @__PURE__ */ jsxs6("table", {
614
+ className,
615
+ children: [
616
+ /* @__PURE__ */ jsx7("thead", {
617
+ children: /* @__PURE__ */ jsxs6("tr", {
618
+ children: [
619
+ /* @__PURE__ */ jsx7("th", {
620
+ children: "Name"
621
+ }),
622
+ /* @__PURE__ */ jsx7("th", {
623
+ children: "Type"
624
+ }),
625
+ /* @__PURE__ */ jsx7("th", {
626
+ children: "Description"
627
+ })
628
+ ]
629
+ })
630
+ }),
631
+ /* @__PURE__ */ jsx7("tbody", {
632
+ children: items.map((item, index) => {
633
+ if (renderRow) {
634
+ return renderRow(item, index);
635
+ }
636
+ const name = item.name ?? `arg${index}`;
637
+ const type = formatSchema5(item.schema);
638
+ const description = item.description ?? "";
639
+ const required = isParameter2(item) ? item.required : true;
640
+ return /* @__PURE__ */ jsxs6("tr", {
641
+ children: [
642
+ /* @__PURE__ */ jsxs6("td", {
643
+ children: [
644
+ /* @__PURE__ */ jsx7("code", {
645
+ children: name
646
+ }),
647
+ showRequired && required && /* @__PURE__ */ jsx7("span", {
648
+ children: "*"
649
+ }),
650
+ showRequired && !required && /* @__PURE__ */ jsx7("span", {
651
+ children: "?"
652
+ })
653
+ ]
654
+ }),
655
+ /* @__PURE__ */ jsx7("td", {
656
+ children: /* @__PURE__ */ jsx7("code", {
657
+ children: type
658
+ })
659
+ }),
660
+ /* @__PURE__ */ jsx7("td", {
661
+ children: description
662
+ })
663
+ ]
664
+ }, name);
665
+ })
666
+ })
667
+ ]
668
+ });
669
+ }
670
+ export { __toESM, __commonJS, CollapsibleMethod, getExampleCode, getExampleTitle, getExampleLanguage, cleanCode, ExampleBlock, NestedProperty, ExpandableProperty, groupMembersByKind, MemberRow, MembersTable, ParamRow, ParamTable, Signature, TypeTable };