@dxos/react-ui-syntax-highlighter 0.8.4-main.9be5663bfe → 0.8.4-main.abd8ff62ef

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 (43) hide show
  1. package/dist/lib/browser/index.mjs +133 -84
  2. package/dist/lib/browser/index.mjs.map +4 -4
  3. package/dist/lib/browser/meta.json +1 -1
  4. package/dist/lib/node-esm/index.mjs +133 -84
  5. package/dist/lib/node-esm/index.mjs.map +4 -4
  6. package/dist/lib/node-esm/meta.json +1 -1
  7. package/dist/types/src/JsonHighlighter/JsonHighlighter.d.ts +23 -0
  8. package/dist/types/src/JsonHighlighter/JsonHighlighter.d.ts.map +1 -0
  9. package/dist/types/src/JsonHighlighter/JsonHighlighter.stories.d.ts +14 -0
  10. package/dist/types/src/JsonHighlighter/JsonHighlighter.stories.d.ts.map +1 -0
  11. package/dist/types/src/JsonHighlighter/index.d.ts +2 -0
  12. package/dist/types/src/JsonHighlighter/index.d.ts.map +1 -0
  13. package/dist/types/src/Syntax/Syntax.d.ts +49 -0
  14. package/dist/types/src/Syntax/Syntax.d.ts.map +1 -0
  15. package/dist/types/src/Syntax/Syntax.stories.d.ts +23 -0
  16. package/dist/types/src/Syntax/Syntax.stories.d.ts.map +1 -0
  17. package/dist/types/src/Syntax/index.d.ts +2 -0
  18. package/dist/types/src/Syntax/index.d.ts.map +1 -0
  19. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts +9 -2
  20. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts.map +1 -1
  21. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts +1 -1
  22. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts.map +1 -1
  23. package/dist/types/src/index.d.ts +2 -1
  24. package/dist/types/src/index.d.ts.map +1 -1
  25. package/dist/types/tsconfig.tsbuildinfo +1 -1
  26. package/package.json +12 -14
  27. package/src/JsonHighlighter/JsonHighlighter.stories.tsx +65 -0
  28. package/src/JsonHighlighter/JsonHighlighter.tsx +47 -0
  29. package/src/JsonHighlighter/index.ts +5 -0
  30. package/src/{Json/Json.stories.tsx → Syntax/Syntax.stories.tsx} +37 -44
  31. package/src/Syntax/Syntax.tsx +229 -0
  32. package/src/Syntax/index.ts +5 -0
  33. package/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx +2 -2
  34. package/src/SyntaxHighlighter/SyntaxHighlighter.tsx +77 -25
  35. package/src/index.ts +2 -1
  36. package/dist/types/src/Json/Json.d.ts +0 -37
  37. package/dist/types/src/Json/Json.d.ts.map +0 -1
  38. package/dist/types/src/Json/Json.stories.d.ts +0 -21
  39. package/dist/types/src/Json/Json.stories.d.ts.map +0 -1
  40. package/dist/types/src/Json/index.d.ts +0 -2
  41. package/dist/types/src/Json/index.d.ts.map +0 -1
  42. package/src/Json/Json.tsx +0 -190
  43. package/src/Json/index.ts +0 -5
@@ -2,12 +2,12 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React, { CSSProperties } from 'react';
5
+ import React, { Children } 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 { ScrollArea, useThemeContext } from '@dxos/react-ui';
10
+ import { Clipboard, useThemeContext } from '@dxos/react-ui';
11
11
  import { composable, composableProps } from '@dxos/ui-theme';
12
12
 
13
13
  const zeroWidthSpace = '\u200b';
@@ -17,11 +17,28 @@ const languages = {
17
17
  ts: 'typescript',
18
18
  };
19
19
 
20
- export type SyntaxHighlighterProps = NaturalSyntaxHighlighterProps & {
20
+ export type SyntaxHighlighterProps = Pick<
21
+ NaturalSyntaxHighlighterProps,
22
+ | 'language'
23
+ | 'renderer'
24
+ | 'showLineNumbers'
25
+ | 'showInlineLineNumbers'
26
+ | 'startingLineNumber'
27
+ | 'wrapLines'
28
+ | 'wrapLongLines'
29
+ | 'PreTag'
30
+ > & {
31
+ themeStyle?: NaturalSyntaxHighlighterProps['style'];
21
32
  fallback?: string;
33
+ copyButton?: boolean;
22
34
  };
23
35
 
24
36
  /**
37
+ * Inline, non-scrolling wrapper around `react-syntax-highlighter`.
38
+ *
39
+ * Use directly for small snippets (e.g. inside markdown code blocks).
40
+ * For scrollable panels, compose with `Syntax.Viewport`.
41
+ *
25
42
  * NOTE: Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
26
43
  * The lightweight version will load specific language parsers asynchronously.
27
44
  *
@@ -30,34 +47,69 @@ export type SyntaxHighlighterProps = NaturalSyntaxHighlighterProps & {
30
47
  */
31
48
  export const SyntaxHighlighter = composable<HTMLDivElement, SyntaxHighlighterProps>(
32
49
  (
33
- { children, language = 'text', fallback = zeroWidthSpace, classNames, className, style, ...nativeProps },
50
+ {
51
+ classNames,
52
+ className,
53
+ children,
54
+ role,
55
+ style,
56
+ themeStyle,
57
+ language = 'text',
58
+ fallback = zeroWidthSpace,
59
+ copyButton,
60
+ ...nativeProps
61
+ },
34
62
  forwardedRef,
35
63
  ) => {
36
64
  const { themeMode } = useThemeContext();
65
+ const source = Children.toArray(children).join('') || fallback;
66
+
67
+ const hasCustomTheme = themeStyle && typeof themeStyle === 'object' && Object.keys(themeStyle).length > 0;
68
+ const prismTheme = hasCustomTheme ? themeStyle : themeMode === 'dark' ? dark : light;
37
69
 
38
70
  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>
71
+ <div
72
+ {...composableProps(
73
+ { classNames, className, role, style },
74
+ {
75
+ role: 'none',
76
+ classNames: copyButton && 'relative group',
77
+ },
78
+ )}
79
+ ref={forwardedRef}
80
+ >
81
+ <NativeSyntaxHighlighter
82
+ language={languages[language as keyof typeof languages] || language}
83
+ style={prismTheme}
84
+ customStyle={{
85
+ background: 'unset',
86
+ border: 'none',
87
+ boxShadow: 'none',
88
+ padding: 0,
89
+ margin: 0,
90
+ }}
91
+ {...nativeProps}
92
+ >
93
+ {/* Non-empty fallback prevents collapse. */}
94
+ {source}
95
+ </NativeSyntaxHighlighter>
96
+
97
+ {copyButton && (
98
+ <div
99
+ role='none'
100
+ className='pointer-events-none absolute top-1 right-1 z-10 opacity-0 group-hover:opacity-100 focus-within:opacity-100'
101
+ >
102
+ <Clipboard.Provider>
103
+ <Clipboard.IconButton
104
+ value={source}
105
+ variant='ghost'
106
+ size={4}
107
+ classNames='pointer-events-auto aspect-square rounded-sm'
108
+ />
109
+ </Clipboard.Provider>
58
110
  </div>
59
- </ScrollArea.Viewport>
60
- </ScrollArea.Root>
111
+ )}
112
+ </div>
61
113
  );
62
114
  },
63
115
  );
package/src/index.ts CHANGED
@@ -2,5 +2,6 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- export * from './Json';
5
+ export * from './JsonHighlighter';
6
+ export * from './Syntax';
6
7
  export * from './SyntaxHighlighter';
@@ -1,37 +0,0 @@
1
- import React, { type PropsWithChildren } from 'react';
2
- import { type ComposableProps } from '@dxos/ui-types';
3
- import { type CreateReplacerProps } from '@dxos/util';
4
- type JsonRootProps = PropsWithChildren<{
5
- data?: any;
6
- replacer?: CreateReplacerProps;
7
- }>;
8
- type JsonContentProps = ComposableProps;
9
- type JsonFilterProps = ComposableProps<{
10
- placeholder?: string;
11
- }>;
12
- type JsonDataProps = ComposableProps<{
13
- data?: any;
14
- replacer?: CreateReplacerProps;
15
- testId?: string;
16
- }>;
17
- export declare const Json: {
18
- Root: React.ForwardRefExoticComponent<{
19
- data?: any;
20
- replacer?: CreateReplacerProps;
21
- } & {
22
- children?: React.ReactNode | undefined;
23
- } & React.RefAttributes<HTMLDivElement>>;
24
- Content: React.ForwardRefExoticComponent<Omit<JsonContentProps, "className"> & {
25
- classNames?: import("@dxos/ui-types").ClassNameValue;
26
- } & Pick<React.HTMLAttributes<Element>, "className" | "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>;
27
- Filter: React.ForwardRefExoticComponent<Omit<{
28
- placeholder?: string;
29
- }, "className"> & {
30
- classNames?: import("@dxos/ui-types").ClassNameValue;
31
- } & Pick<React.HTMLAttributes<Element>, "className" | "children" | "role" | "style"> & React.RefAttributes<HTMLInputElement>>;
32
- Data: React.ForwardRefExoticComponent<Omit<JsonDataProps, "className"> & {
33
- classNames?: import("@dxos/ui-types").ClassNameValue;
34
- } & Pick<React.HTMLAttributes<Element>, "className" | "children" | "role" | "style"> & React.RefAttributes<HTMLDivElement>>;
35
- };
36
- export type { JsonRootProps, JsonContentProps, JsonFilterProps, JsonDataProps };
37
- //# sourceMappingURL=Json.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Json.d.ts","sourceRoot":"","sources":["../../../../src/Json/Json.tsx"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,EAEZ,KAAK,iBAAiB,EAMvB,MAAM,OAAO,CAAC;AAIf,OAAO,EAAE,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,EAAE,KAAK,mBAAmB,EAAiC,MAAM,YAAY,CAAC;AAuCrF,KAAK,aAAa,GAAG,iBAAiB,CAAC;IACrC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,QAAQ,CAAC,EAAE,mBAAmB,CAAC;CAChC,CAAC,CAAC;AAuCH,KAAK,gBAAgB,GAAG,eAAe,CAAC;AAmBxC,KAAK,eAAe,GAAG,eAAe,CAAC;IACrC,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB,CAAC,CAAC;AA8BH,KAAK,aAAa,GAAG,eAAe,CAAC;IACnC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,QAAQ,CAAC,EAAE,mBAAmB,CAAC;IAC/B,MAAM,CAAC,EAAE,MAAM,CAAC;CACjB,CAAC,CAAC;AA4BH,eAAO,MAAM,IAAI;;eA5HR,GAAG;mBACC,mBAAmB;;;;;;;;sBA4DhB,MAAM;;;;;;;CAoErB,CAAC;AAEF,YAAY,EAAE,aAAa,EAAE,gBAAgB,EAAE,eAAe,EAAE,aAAa,EAAE,CAAC"}
@@ -1,21 +0,0 @@
1
- import { type StoryObj } from '@storybook/react-vite';
2
- import React from 'react';
3
- declare const meta: {
4
- title: string;
5
- component: React.ForwardRefExoticComponent<{
6
- data?: any;
7
- replacer?: import("@dxos/util").CreateReplacerProps;
8
- } & {
9
- children?: React.ReactNode | undefined;
10
- } & React.RefAttributes<HTMLDivElement>>;
11
- decorators: import("@storybook/react").Decorator[];
12
- };
13
- export default meta;
14
- type Story = StoryObj<typeof meta>;
15
- /** Standalone Json.Data — simplest usage. */
16
- export declare const Default: Story;
17
- /** Circular reference handling. */
18
- export declare const Cycle: Story;
19
- /** Large dataset with replacer. */
20
- export declare const Filter: Story;
21
- //# sourceMappingURL=Json.stories.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Json.stories.d.ts","sourceRoot":"","sources":["../../../../src/Json/Json.stories.tsx"],"names":[],"mappings":"AAIA,OAAO,EAAa,KAAK,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjE,OAAO,KAAK,MAAM,OAAO,CAAC;AA4D1B,QAAA,MAAM,IAAI;;;;;;;;;CAIM,CAAC;AAEjB,eAAe,IAAI,CAAC;AAEpB,KAAK,KAAK,GAAG,QAAQ,CAAC,OAAO,IAAI,CAAC,CAAC;AAEnC,6CAA6C;AAC7C,eAAO,MAAM,OAAO,EAAE,KAKrB,CAAC;AAEF,mCAAmC;AACnC,eAAO,MAAM,KAAK,EAAE,KAKnB,CAAC;AAEF,mCAAmC;AACnC,eAAO,MAAM,MAAM,EAAE,KAapB,CAAC"}
@@ -1,2 +0,0 @@
1
- export { Json, type JsonRootProps, type JsonContentProps, type JsonFilterProps, type JsonDataProps } from './Json';
2
- //# sourceMappingURL=index.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../../../src/Json/index.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,IAAI,EAAE,KAAK,aAAa,EAAE,KAAK,gBAAgB,EAAE,KAAK,eAAe,EAAE,KAAK,aAAa,EAAE,MAAM,QAAQ,CAAC"}
package/src/Json/Json.tsx DELETED
@@ -1,190 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
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';
15
-
16
- import { Input } from '@dxos/react-ui';
17
- import { composable, composableProps } from '@dxos/ui-theme';
18
- import { type ComposableProps } from '@dxos/ui-types';
19
- import { type CreateReplacerProps, createReplacer, safeStringify } from '@dxos/util';
20
-
21
- import { SyntaxHighlighter } from '../SyntaxHighlighter';
22
-
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<{
59
- data?: any;
60
- replacer?: CreateReplacerProps;
61
- }>;
62
-
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);
68
-
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) => {
104
- return (
105
- <div {...composableProps(props, { classNames: 'flex flex-col h-full min-h-0 overflow-hidden' })} ref={forwardedRef}>
106
- {children}
107
- </div>
108
- );
109
- });
110
-
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;
163
-
164
- return (
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>
173
- );
174
- },
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 DELETED
@@ -1,5 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- export { Json, type JsonRootProps, type JsonContentProps, type JsonFilterProps, type JsonDataProps } from './Json';