@xyd-js/atlas 0.0.0-build-099b7bb-20250918135344 → 0.0.0-build-9f87f13-20250930210637

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.
@@ -1,69 +1,125 @@
1
- import React, { useState, useMemo } from "react";
1
+ import React, { useState, useMemo, useContext } from "react";
2
2
  import { UXNode } from "openux-js";
3
3
 
4
- import { ExampleRoot } from "@xyd-js/uniform";
4
+ import { Example, ExampleRoot } from "@xyd-js/uniform";
5
5
  import { CodeSample, type CodeThemeBlockProps } from "@xyd-js/components/coder";
6
6
 
7
7
  import { CodeExampleButtons } from "@/components/Code";
8
- import { useSyntaxHighlight } from "@/components/Atlas/AtlasContext";
8
+ import {
9
+ AtlasContext,
10
+ useSyntaxHighlight,
11
+ } from "@/components/Atlas/AtlasContext";
9
12
 
10
13
  import * as cn from "./ApiRefSamples.styles";
11
14
 
12
15
  export interface ApiRefSamplesProps {
13
- examples: ExampleRoot
16
+ examples: ExampleRoot;
14
17
  }
15
18
 
16
19
  export function ApiRefSamples({ examples }: ApiRefSamplesProps) {
17
- const syntaxHighlight = useSyntaxHighlight()
18
- const [activeExampleIndices, setActiveExampleIndices] = useState<Record<number, number>>({})
19
-
20
- const handleExampleChange = (groupIndex: number, exampleIndex: number) => {
21
- setActiveExampleIndices(prev => ({
22
- ...prev,
23
- [groupIndex]: exampleIndex
24
- }))
25
- }
26
-
27
- return <atlas-apiref-samples className={cn.ApiRefSamplesContainerHost}>
28
- {
29
- examples.groups?.map(({ description, examples: example }, i) => {
30
- const activeExampleIndex = activeExampleIndices[i] || 0
31
- const activeExample = example[activeExampleIndex]
32
-
33
- const codeblocks = activeExample?.codeblock?.tabs?.map(tab => ({
34
- value: String(tab.code || ""),
35
- lang: String(tab.language || ""),
36
- meta: String(tab.context || ""),
37
- highlighted: tab.highlighted
38
- } as CodeThemeBlockProps)) || []
39
-
40
- return <UXNode
41
- name="APIRefSample"
42
- props={activeExample}
43
- >
44
- <div key={i} className={cn.ApiRefSamplesGroupHost}>
45
- {
46
- example?.length > 1
47
- ? <CodeExampleButtons
48
- activeExample={activeExample}
49
- examples={example}
50
- onClick={(ex) => {
51
- const index = example.findIndex(e => e === ex)
52
- handleExampleChange(i, index)
53
- }}
54
- />
55
- : null
56
- }
57
- <CodeSample
58
- name={String(i)}
59
- description={description || ""}
60
- codeblocks={codeblocks}
61
- theme={syntaxHighlight || undefined}
62
- // controlByMeta
63
- />
64
- </div>
65
- </UXNode>
66
- })
67
- }
20
+ const [activeExampleIndices, setActiveExampleIndices] = useState<
21
+ Record<number, number>
22
+ >({});
23
+
24
+ const handleExampleChange = (groupIndex: number, exampleIndex: number) => {
25
+ setActiveExampleIndices((prev) => ({
26
+ ...prev,
27
+ [groupIndex]: exampleIndex,
28
+ }));
29
+ };
30
+
31
+ return (
32
+ <atlas-apiref-samples className={cn.ApiRefSamplesContainerHost}>
33
+ {examples.groups?.map(({ description, examples: samples }, i) => {
34
+ const activeExampleIndex = activeExampleIndices[i] || 0;
35
+ const activeExample = samples[activeExampleIndex];
36
+
37
+ return (
38
+ <CodeSampleItem
39
+ key={i}
40
+ currentExample={activeExample}
41
+ examples={samples}
42
+ name={String(i)}
43
+ description={description}
44
+ onExampleChange={(ex) => {
45
+ const index = samples.findIndex((e) => e === ex);
46
+ handleExampleChange(i, index);
47
+ }}
48
+ />
49
+ );
50
+ })}
68
51
  </atlas-apiref-samples>
69
- }
52
+ );
53
+ }
54
+
55
+ interface CodeSampleItemProps {
56
+ currentExample: Example;
57
+ examples: Example[];
58
+ name: string;
59
+ description?: string;
60
+ onExampleChange: (example: Example) => void;
61
+ }
62
+
63
+ function CodeSampleItem(props: CodeSampleItemProps) {
64
+ const { currentExample, examples, name, description, onExampleChange } =
65
+ props;
66
+ const { markdownFormat } = useContext(AtlasContext);
67
+ const syntaxHighlight = useSyntaxHighlight();
68
+
69
+ const shouldRenderAllExamples = markdownFormat;
70
+
71
+ // example tabs
72
+ const buttons = examples?.length > 1 && (
73
+ <CodeExampleButtons
74
+ activeExample={currentExample}
75
+ examples={examples}
76
+ onClick={onExampleChange}
77
+ />
78
+ );
79
+
80
+ const singleCodeSample = (
81
+ <UXNode name="APIRefSample" props={currentExample}>
82
+ <CodeSample
83
+ name={name}
84
+ description={description || ""}
85
+ codeblocks={createCodeblocks(currentExample)}
86
+ theme={syntaxHighlight || undefined}
87
+ markdownFormat={markdownFormat}
88
+ />
89
+ </UXNode>
90
+ );
91
+
92
+ const allCodeSamples = examples.map((example, index) => (
93
+ <UXNode key={index} name="APIRefSample" props={example}>
94
+ <CodeSample
95
+ name={`${name}-${example.codeblock?.title || index}`}
96
+ description={example.codeblock?.title || description || ""}
97
+ codeblocks={createCodeblocks(example)}
98
+ theme={syntaxHighlight || undefined}
99
+ markdownFormat={markdownFormat}
100
+ />
101
+ </UXNode>
102
+ ));
103
+
104
+ return (
105
+ <div key={name} className={cn.ApiRefSamplesGroupHost}>
106
+ {buttons}
107
+ {shouldRenderAllExamples ? allCodeSamples : singleCodeSample}
108
+ </div>
109
+ );
110
+ }
111
+
112
+ // Helper function to create codeblocks from an example
113
+ function createCodeblocks(example: Example): CodeThemeBlockProps[] {
114
+ return (
115
+ example?.codeblock?.tabs?.map(
116
+ (tab) =>
117
+ ({
118
+ value: String(tab.code || ""),
119
+ lang: String(tab.language || ""),
120
+ meta: String(tab.context || ""),
121
+ highlighted: tab.highlighted,
122
+ }) as CodeThemeBlockProps
123
+ ) || []
124
+ );
125
+ }
@@ -6,38 +6,52 @@ import { AtlasPrimary } from "./AtlasPrimary";
6
6
  import { AtlasSecondary } from "./AtlasSecondary";
7
7
 
8
8
  import * as cn from "./Atlas.styles";
9
+ import { UXAnalytics } from "openux-js";
9
10
 
10
11
  interface AtlasProps<T> extends MDXCommonAtlasProps<T> {
11
- kind: "secondary" | "primary" | undefined | null
12
+ kind: "secondary" | "primary" | undefined | null;
13
+ analytics?: boolean;
12
14
  }
13
15
 
16
+ const noopAnalytics = {
17
+ track: () => {},
18
+ };
19
+
14
20
  export function Atlas<T>(props: AtlasProps<T>) {
15
- let AtlasComponent: React.FC<MDXCommonAtlasProps<T>>;
21
+ let AtlasComponent: React.FC<MDXCommonAtlasProps<T>>;
16
22
 
17
- if (props.kind === "secondary") {
18
- AtlasComponent = AtlasSecondary;
19
- } else {
20
- AtlasComponent = AtlasPrimary;
21
- }
23
+ if (props.kind === "secondary") {
24
+ AtlasComponent = AtlasSecondary;
25
+ } else {
26
+ AtlasComponent = AtlasPrimary;
27
+ }
22
28
 
23
- let references = props.references
24
- {
25
- // TODO: find better solution - if we pass from md then its string
26
- if (references && typeof references === "string") { // TODO: DO IT BETTER
27
- try {
28
- references = JSON.parse(references)
29
- } catch (error) {
30
- console.error("Error parsing references", error)
31
- }
32
- }
29
+ let references = props.references;
30
+ {
31
+ // TODO: find better solution - if we pass from md then its string
32
+ if (references && typeof references === "string") {
33
+ // TODO: DO IT BETTER
34
+ try {
35
+ references = JSON.parse(references);
36
+ } catch (error) {
37
+ console.error("Error parsing references", error);
38
+ }
33
39
  }
40
+ }
34
41
 
35
- return <AtlasDecorator>
42
+ // TODO: temporary, remove when we will have proper analytics
43
+ const Wrapper = props.analytics ? UXAnalytics : React.Fragment;
44
+
45
+ return (
46
+ <Wrapper analytics={noopAnalytics}>
47
+ <AtlasDecorator>
36
48
  <div className={cn.AtlasHost}>
37
- <AtlasComponent
38
- references={references}
39
- apiRefItemKind={props.apiRefItemKind}
40
- />
49
+ <AtlasComponent
50
+ references={references}
51
+ apiRefItemKind={props.apiRefItemKind}
52
+ />
41
53
  </div>
42
- </AtlasDecorator>
43
- }
54
+ </AtlasDecorator>
55
+ </Wrapper>
56
+ );
57
+ }
@@ -9,11 +9,13 @@ export interface VariantToggleConfig {
9
9
 
10
10
  export const AtlasContext = createContext<{
11
11
  syntaxHighlight: Theme | null,
12
+ markdownFormat?: boolean,
12
13
  baseMatch?: string,
13
14
  variantToggles?: VariantToggleConfig[], // Array of toggle configurations
14
15
  Link?: any
15
16
  }>({
16
- syntaxHighlight: null
17
+ syntaxHighlight: null,
18
+ markdownFormat: false
17
19
  })
18
20
 
19
21
  export function useSyntaxHighlight() {
@@ -38,7 +38,7 @@ export function CodeExampleButtons({examples, activeExample, onClick}: CodeExamp
38
38
  }
39
39
 
40
40
  return (
41
- <div className={cn.CodeSampleButtonsHost}>
41
+ <atlas-sample-buttons className={cn.CodeSampleButtonsHost}>
42
42
  <div className={cn.CodeSampleButtonsContainer}>
43
43
  {showLeftArrow && (
44
44
  <button
@@ -75,7 +75,7 @@ export function CodeExampleButtons({examples, activeExample, onClick}: CodeExamp
75
75
  </button>
76
76
  )}
77
77
  </div>
78
- </div>
78
+ </atlas-sample-buttons>
79
79
  )
80
80
  }
81
81
 
@@ -89,6 +89,7 @@ function SampleButton({onClick, children, activeExample, example}: {
89
89
  (activeExample?.codeblock?.title && activeExample?.codeblock?.title === example.codeblock.title)
90
90
 
91
91
  return <button
92
+ data-sample-button-title={example.codeblock.title}
92
93
  onClick={onClick}
93
94
  className={`${cn.CodeSampleButtonsButtonHost} ${markExampleAsActive && cn.CodeSampleButtonsButtonActive}`}
94
95
  >
package/types.d.ts CHANGED
@@ -12,10 +12,14 @@ declare global {
12
12
  'atlas-apiref-definitions': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
13
13
  'atlas-apiref-properties': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
14
14
  'atlas-apiref-variant': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
15
+ 'atlas-apiref-prop': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
15
16
  'atlas-apiref-propname': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
16
17
  'atlas-apiref-proptype': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
17
18
  'atlas-apiref-propmeta': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
18
19
  'atlas-apiref-meta-info': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
20
+ 'atlas-apiref-propdescription': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
21
+
22
+ 'atlas-sample-buttons': React.DetailedHTMLProps<React.HTMLAttributes<HTMLElement>, HTMLElement>;
19
23
  }
20
24
  }
21
25
  }