@dxos/react-ui-syntax-highlighter 0.8.4-main.bc674ce → 0.8.4-main.bcb3aa67d6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dxos/react-ui-syntax-highlighter",
3
- "version": "0.8.4-main.bc674ce",
3
+ "version": "0.8.4-main.bcb3aa67d6",
4
4
  "description": "A syntax highlighter wrapper.",
5
5
  "homepage": "https://dxos.org",
6
6
  "bugs": "https://github.com/dxos/dxos/issues",
@@ -29,29 +29,29 @@
29
29
  "src"
30
30
  ],
31
31
  "dependencies": {
32
- "jsonpath": "^1.1.1",
32
+ "jsonpath-plus": "^10.3.0",
33
33
  "react-syntax-highlighter": "^15.6.1",
34
- "@dxos/util": "0.8.4-main.bc674ce"
34
+ "@dxos/util": "0.8.4-main.bcb3aa67d6",
35
+ "@dxos/ui-types": "0.8.4-main.bcb3aa67d6"
35
36
  },
36
37
  "devDependencies": {
37
- "@types/jsonpath": "^0.2.4",
38
38
  "@types/react": "~19.2.7",
39
39
  "@types/react-dom": "~19.2.3",
40
40
  "@types/react-syntax-highlighter": "^15.5.13",
41
41
  "react": "~19.2.3",
42
42
  "react-dom": "~19.2.3",
43
- "vite": "7.1.9",
44
- "@dxos/random": "0.8.4-main.bc674ce",
45
- "@dxos/react-ui": "0.8.4-main.bc674ce",
46
- "@dxos/storybook-utils": "0.8.4-main.bc674ce",
47
- "@dxos/util": "0.8.4-main.bc674ce",
48
- "@dxos/ui-theme": "0.8.4-main.bc674ce"
43
+ "vite": "^7.1.11",
44
+ "@dxos/random": "0.8.4-main.bcb3aa67d6",
45
+ "@dxos/react-ui": "0.8.4-main.bcb3aa67d6",
46
+ "@dxos/storybook-utils": "0.8.4-main.bcb3aa67d6",
47
+ "@dxos/util": "0.8.4-main.bcb3aa67d6",
48
+ "@dxos/ui-theme": "0.8.4-main.bcb3aa67d6"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "react": "~19.2.3",
52
52
  "react-dom": "~19.2.3",
53
- "@dxos/react-ui": "0.8.4-main.bc674ce",
54
- "@dxos/ui-theme": "0.8.4-main.bc674ce"
53
+ "@dxos/react-ui": "0.8.4-main.bcb3aa67d6",
54
+ "@dxos/ui-theme": "0.8.4-main.bcb3aa67d6"
55
55
  },
56
56
  "publishConfig": {
57
57
  "access": "public"
@@ -53,54 +53,54 @@ const createData = ({ depth = 2, children = 3 } = {}): any => {
53
53
  return createChildren(createNode());
54
54
  };
55
55
 
56
+ const createCycle = () => {
57
+ const data: any = {
58
+ a: 1,
59
+ b: [],
60
+ };
61
+
62
+ data.b.push(data);
63
+ return data;
64
+ };
65
+
56
66
  const meta = {
57
67
  title: 'ui/react-ui-syntax-highlighter/Json',
58
- component: Json,
59
- decorators: [withTheme, withLayout({ layout: 'column' })],
60
- } satisfies Meta<typeof Json>;
68
+ component: Json.Root,
69
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
70
+ } satisfies Meta;
61
71
 
62
72
  export default meta;
63
73
 
64
- type Story = StoryObj<typeof Json>;
65
-
66
- const data = createData();
74
+ type Story = StoryObj<typeof meta>;
67
75
 
76
+ /** Standalone Json.Data — simplest usage. */
68
77
  export const Default: Story = {
78
+ render: (args) => <Json.Data {...args} />,
69
79
  args: {
70
- classNames: 'text-sm',
71
- data,
80
+ data: createData(),
72
81
  },
73
82
  };
74
83
 
75
- export const Filter: Story = {
84
+ /** Circular reference handling. */
85
+ export const Cycle: Story = {
86
+ render: (args) => <Json.Data {...args} />,
76
87
  args: {
77
- classNames: 'text-sm',
78
- filter: true,
79
- data,
88
+ data: createCycle(),
80
89
  },
81
90
  };
82
91
 
83
- export const Large: Story = {
92
+ /** Large dataset with replacer. */
93
+ export const Filter: Story = {
94
+ render: (args) => (
95
+ <Json.Root {...args}>
96
+ <Json.Content>
97
+ <Json.Filter />
98
+ <Json.Data />
99
+ </Json.Content>
100
+ </Json.Root>
101
+ ),
84
102
  args: {
85
- classNames: 'text-sm',
86
- filter: true,
87
103
  data: createData({ depth: 5 }),
88
- replacer: {
89
- maxDepth: 3,
90
- maxArrayLen: 10,
91
- maxStringLen: 10,
92
- },
104
+ replacer: { maxDepth: 3, maxArrayLen: 10, maxStringLen: 10 },
93
105
  },
94
106
  };
95
-
96
- const cycle: any = {
97
- a: 1,
98
- b: [],
99
- };
100
-
101
- cycle.b.push(cycle);
102
-
103
- // NOTE: Storybook args cannot be circular.
104
- export const Cycle: Story = {
105
- render: () => <Json data={cycle} />,
106
- };
package/src/Json/Json.tsx CHANGED
@@ -2,79 +2,189 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- // TODO(burdon): Use to jsonpath-plus.
6
- import jp from 'jsonpath';
7
- import React, { forwardRef, useEffect, useState } from 'react';
5
+ import { JSONPath } from 'jsonpath-plus';
6
+ import React, {
7
+ createContext,
8
+ type PropsWithChildren,
9
+ forwardRef,
10
+ useContext,
11
+ useEffect,
12
+ useMemo,
13
+ useState,
14
+ } from 'react';
8
15
 
9
- import { Input, type ThemedClassName } from '@dxos/react-ui';
16
+ import { Input } from '@dxos/react-ui';
17
+ import { composable, composableProps } from '@dxos/ui-theme';
18
+ import { type ComposableProps } from '@dxos/ui-types';
10
19
  import { type CreateReplacerProps, createReplacer, safeStringify } from '@dxos/util';
11
20
 
12
21
  import { SyntaxHighlighter } from '../SyntaxHighlighter';
13
22
 
14
- export type JsonProps = ThemedClassName<{
23
+ //
24
+ // Context
25
+ //
26
+
27
+ type JsonContextType = {
28
+ data: any;
29
+ filteredData: any;
30
+ filterText: string;
31
+ setFilterText: (text: string) => void;
32
+ filterError: Error | null;
33
+ replacer?: CreateReplacerProps;
34
+ };
35
+
36
+ const JsonContext = createContext<JsonContextType | null>(null);
37
+
38
+ /** Require Json context (throws if used outside Json.Root). */
39
+ const useJsonContext = (consumerName: string): JsonContextType => {
40
+ const context = useContext(JsonContext);
41
+ if (!context) {
42
+ throw new Error(`\`${consumerName}\` must be used within \`Json.Root\`.`);
43
+ }
44
+ return context;
45
+ };
46
+
47
+ /** Optional Json context (returns null outside Json.Root). */
48
+ const useOptionalJsonContext = (): JsonContextType | null => {
49
+ return useContext(JsonContext);
50
+ };
51
+
52
+ //
53
+ // Root
54
+ //
55
+
56
+ const JSON_ROOT_NAME = 'Json.Root';
57
+
58
+ type JsonRootProps = PropsWithChildren<{
15
59
  data?: any;
16
- filter?: boolean;
17
60
  replacer?: CreateReplacerProps;
18
- testId?: string;
19
61
  }>;
20
62
 
21
- export const Json = forwardRef<HTMLDivElement, JsonProps>((props, forwardedRef) => {
22
- if (props.filter) {
23
- return <JsonFilter {...props} />;
24
- }
63
+ /** Headless context provider for Json composite. */
64
+ const JsonRoot = forwardRef<HTMLDivElement, JsonRootProps>(({ children, data, replacer }, _forwardedRef) => {
65
+ const [filterText, setFilterText] = useState('');
66
+ const [filteredData, setFilteredData] = useState(data);
67
+ const [filterError, setFilterError] = useState<Error | null>(null);
25
68
 
26
- const { classNames, data, replacer, testId } = props;
69
+ useEffect(() => {
70
+ if (!filterText.trim().length) {
71
+ setFilteredData(data);
72
+ setFilterError(null);
73
+ } else {
74
+ try {
75
+ setFilteredData(JSONPath({ path: filterText, json: data }));
76
+ setFilterError(null);
77
+ } catch (err) {
78
+ setFilteredData(data);
79
+ setFilterError(err as Error);
80
+ }
81
+ }
82
+ }, [data, filterText]);
83
+
84
+ const context = useMemo(
85
+ () => ({ data, filteredData, filterText, setFilterText, filterError, replacer }),
86
+ [data, filteredData, filterText, setFilterText, filterError, replacer],
87
+ );
88
+
89
+ return <JsonContext.Provider value={context}>{children}</JsonContext.Provider>;
90
+ });
91
+
92
+ JsonRoot.displayName = JSON_ROOT_NAME;
93
+
94
+ //
95
+ // Content
96
+ //
97
+
98
+ const JSON_CONTENT_NAME = 'Json.Content';
99
+
100
+ type JsonContentProps = ComposableProps;
101
+
102
+ /** Layout container for Json composite parts. */
103
+ const JsonContent = composable<HTMLDivElement, JsonContentProps>(({ children, ...props }, forwardedRef) => {
27
104
  return (
28
- <SyntaxHighlighter
29
- language='json'
30
- classNames={['is-full overflow-y-auto text-sm', classNames]}
31
- data-testid={testId}
32
- ref={forwardedRef}
33
- >
34
- {safeStringify(data, replacer && createReplacer(replacer), 2)}
35
- </SyntaxHighlighter>
105
+ <div {...composableProps(props, { classNames: 'flex flex-col h-full min-h-0 overflow-hidden' })} ref={forwardedRef}>
106
+ {children}
107
+ </div>
36
108
  );
37
109
  });
38
110
 
39
- export const JsonFilter = forwardRef<HTMLDivElement, JsonProps>(
40
- ({ classNames, data: initialData, replacer, testId }, forwardedRef) => {
41
- const [data, setData] = useState(initialData);
42
- const [text, setText] = useState('');
43
- const [error, setError] = useState<Error | null>(null);
44
-
45
- useEffect(() => {
46
- if (!initialData || !text.trim().length) {
47
- setData(initialData);
48
- } else {
49
- try {
50
- setData(jp.query(initialData, text));
51
- setError(null);
52
- } catch (err) {
53
- setData(initialData);
54
- setError(err as Error);
55
- }
56
- }
57
- }, [initialData, text]); // TODO(burdon): Need structural diff.
111
+ JsonContent.displayName = JSON_CONTENT_NAME;
112
+
113
+ //
114
+ // Filter
115
+ //
116
+
117
+ const JSON_FILTER_NAME = 'Json.Filter';
118
+
119
+ type JsonFilterProps = ComposableProps<{
120
+ placeholder?: string;
121
+ }>;
122
+
123
+ /** JSONPath filter input. Must be used within Json.Root. */
124
+ const JsonFilter = forwardRef<HTMLInputElement, JsonFilterProps>(
125
+ ({ classNames, placeholder = 'JSONPath (e.g., $.graph.nodes)' }, forwardedRef) => {
126
+ const { filterText, setFilterText, filterError } = useJsonContext(JSON_FILTER_NAME);
127
+
128
+ return (
129
+ <Input.Root validationValence={filterError ? 'error' : 'success'}>
130
+ <Input.TextInput
131
+ classNames={['p-1 px-2 font-mono', filterError && 'border-rose-500', classNames]}
132
+ variant='subdued'
133
+ value={filterText}
134
+ placeholder={placeholder}
135
+ onChange={(event) => setFilterText(event.target.value)}
136
+ ref={forwardedRef}
137
+ />
138
+ </Input.Root>
139
+ );
140
+ },
141
+ );
142
+
143
+ JsonFilter.displayName = JSON_FILTER_NAME;
144
+
145
+ //
146
+ // Data
147
+ //
148
+
149
+ const JSON_DATA_NAME = 'Json.Data';
150
+
151
+ type JsonDataProps = ComposableProps<{
152
+ data?: any;
153
+ replacer?: CreateReplacerProps;
154
+ testId?: string;
155
+ }>;
156
+
157
+ /** Syntax-highlighted JSON display. Works standalone or within Json.Root. */
158
+ const JsonData = composable<HTMLDivElement, JsonDataProps>(
159
+ ({ data: dataProp, replacer: replacerProp, testId, ...props }, forwardedRef) => {
160
+ const context = useOptionalJsonContext();
161
+ const data = dataProp ?? context?.filteredData;
162
+ const replacer = replacerProp ?? context?.replacer;
58
163
 
59
164
  return (
60
- <div className='flex flex-col bs-full overflow-hidden' ref={forwardedRef}>
61
- <Input.Root validationValence={error ? 'error' : 'success'}>
62
- <Input.TextInput
63
- classNames={['p-1 pli-2 font-mono', error && 'border-rose-500']}
64
- variant='subdued'
65
- value={text}
66
- placeholder='JSONPath (e.g., $.graph.nodes)'
67
- onChange={(event) => setText(event.target.value)}
68
- />
69
- </Input.Root>
70
- <SyntaxHighlighter
71
- language='json'
72
- classNames={['is-full overflow-y-auto text-sm', classNames]}
73
- data-testid={testId}
74
- >
75
- {safeStringify(data, replacer && createReplacer(replacer), 2)}
76
- </SyntaxHighlighter>
77
- </div>
165
+ <SyntaxHighlighter
166
+ {...composableProps(props, { classNames: 'flex-1 min-h-0 w-full py-1 px-2 overflow-y-auto text-sm' })}
167
+ language='json'
168
+ data-testid={testId}
169
+ ref={forwardedRef}
170
+ >
171
+ {safeStringify(data, replacer && createReplacer(replacer), 2)}
172
+ </SyntaxHighlighter>
78
173
  );
79
174
  },
80
175
  );
176
+
177
+ JsonData.displayName = JSON_DATA_NAME;
178
+
179
+ //
180
+ // Json
181
+ //
182
+
183
+ export const Json = {
184
+ Root: JsonRoot,
185
+ Content: JsonContent,
186
+ Filter: JsonFilter,
187
+ Data: JsonData,
188
+ };
189
+
190
+ export type { JsonRootProps, JsonContentProps, JsonFilterProps, JsonDataProps };
package/src/Json/index.ts CHANGED
@@ -2,4 +2,4 @@
2
2
  // Copyright 2025 DXOS.org
3
3
  //
4
4
 
5
- export * from './Json';
5
+ export { Json, type JsonRootProps, type JsonContentProps, type JsonFilterProps, type JsonDataProps } from './Json';
@@ -4,15 +4,21 @@
4
4
 
5
5
  import { type Meta, type StoryObj } from '@storybook/react-vite';
6
6
 
7
- import { withTheme } from '@dxos/react-ui/testing';
7
+ import { withLayout, withTheme } from '@dxos/react-ui/testing';
8
8
  import { trim } from '@dxos/util';
9
9
 
10
+ // @ts-ignore - Vite raw import.
11
+ import TEXT from '../../package.json?raw';
12
+
10
13
  import { SyntaxHighlighter } from './SyntaxHighlighter';
11
14
 
12
15
  const meta = {
13
16
  title: 'ui/react-ui-syntax-highlighter/SyntaxHighlighter',
14
17
  component: SyntaxHighlighter,
15
- decorators: [withTheme],
18
+ decorators: [withTheme(), withLayout({ layout: 'column' })],
19
+ parameters: {
20
+ layout: 'fullscreen',
21
+ },
16
22
  } satisfies Meta<typeof SyntaxHighlighter>;
17
23
 
18
24
  export default meta;
@@ -23,7 +29,7 @@ export const Default: Story = {
23
29
  args: {
24
30
  language: 'json',
25
31
  classNames: 'text-sm',
26
- children: JSON.stringify({ message: 'DXOS', initialized: true }, null, 2),
32
+ children: TEXT,
27
33
  },
28
34
  };
29
35
 
@@ -32,7 +38,7 @@ export const Typescript: Story = {
32
38
  language: 'tsx',
33
39
  children: trim`
34
40
  import React from 'react'
35
-
41
+
36
42
  const Test = () => {
37
43
  return <div>Test</div>
38
44
  }
@@ -40,8 +46,4 @@ export const Typescript: Story = {
40
46
  },
41
47
  };
42
48
 
43
- export const Empty: Story = {
44
- args: {
45
- children: false,
46
- },
47
- };
49
+ export const Empty: Story = {};
@@ -2,21 +2,24 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
5
+ import React, { CSSProperties } from 'react';
6
6
  import { type SyntaxHighlighterProps as NaturalSyntaxHighlighterProps } from 'react-syntax-highlighter';
7
7
  import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-async-light';
8
8
  import { coldarkDark as dark, coldarkCold as light } from 'react-syntax-highlighter/dist/esm/styles/prism';
9
9
 
10
- import { type ThemedClassName, useThemeContext } from '@dxos/react-ui';
11
- import { mx } from '@dxos/ui-theme';
10
+ import { ScrollArea, useThemeContext } from '@dxos/react-ui';
11
+ import { composable, composableProps } from '@dxos/ui-theme';
12
12
 
13
13
  const zeroWidthSpace = '\u200b';
14
14
 
15
- export type SyntaxHighlighterProps = ThemedClassName<
16
- NaturalSyntaxHighlighterProps & {
17
- fallback?: string;
18
- }
19
- >;
15
+ const languages = {
16
+ js: 'javascript',
17
+ ts: 'typescript',
18
+ };
19
+
20
+ export type SyntaxHighlighterProps = NaturalSyntaxHighlighterProps & {
21
+ fallback?: string;
22
+ };
20
23
 
21
24
  /**
22
25
  * NOTE: Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
@@ -25,39 +28,38 @@ export type SyntaxHighlighterProps = ThemedClassName<
25
28
  * https://github.com/react-syntax-highlighter/react-syntax-highlighter
26
29
  * https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/prism.html
27
30
  */
28
- // TODO(burdon): Replace with react-ui-editor (and reuse styles).
29
- export const SyntaxHighlighter = ({
30
- classNames,
31
- children,
32
- language = 'text',
33
- fallback = zeroWidthSpace,
34
- ...props
35
- }: SyntaxHighlighterProps) => {
36
- const { themeMode } = useThemeContext();
37
-
38
- return (
39
- <div className={mx('flex is-full p-1 overflow-hidden', classNames)}>
40
- <NativeSyntaxHighlighter
41
- className='!m-0 is-full overflow-auto scrollbar-thin'
42
- language={languages[language as keyof typeof languages] || language}
43
- style={themeMode === 'dark' ? dark : light}
44
- customStyle={{
45
- background: 'unset',
46
- border: 'none',
47
- boxShadow: 'none',
48
- padding: 0,
49
- margin: 0,
50
- }}
51
- {...props}
52
- >
53
- {/* Non-empty fallback prevents collapse. */}
54
- {children || fallback}
55
- </NativeSyntaxHighlighter>
56
- </div>
57
- );
58
- };
31
+ export const SyntaxHighlighter = composable<HTMLDivElement, SyntaxHighlighterProps>(
32
+ (
33
+ { children, language = 'text', fallback = zeroWidthSpace, classNames, className, style, ...nativeProps },
34
+ forwardedRef,
35
+ ) => {
36
+ const { themeMode } = useThemeContext();
59
37
 
60
- const languages = {
61
- js: 'javascript',
62
- ts: 'typescript',
63
- };
38
+ return (
39
+ <ScrollArea.Root {...composableProps({ classNames, className })} thin ref={forwardedRef}>
40
+ <ScrollArea.Viewport>
41
+ {/* NOTE: The div prevents NativeSyntaxHighlighter from managing scrolling. */}
42
+ <div role='none'>
43
+ <NativeSyntaxHighlighter
44
+ language={languages[language as keyof typeof languages] || language}
45
+ style={(style as { [key: string]: CSSProperties }) ?? (themeMode === 'dark' ? dark : light)}
46
+ customStyle={{
47
+ background: 'unset',
48
+ border: 'none',
49
+ boxShadow: 'none',
50
+ padding: 0,
51
+ margin: 0,
52
+ }}
53
+ {...nativeProps}
54
+ >
55
+ {/* Non-empty fallback prevents collapse. */}
56
+ {children || fallback}
57
+ </NativeSyntaxHighlighter>
58
+ </div>
59
+ </ScrollArea.Viewport>
60
+ </ScrollArea.Root>
61
+ );
62
+ },
63
+ );
64
+
65
+ SyntaxHighlighter.displayName = 'SyntaxHighlighter';