@dxos/react-ui-syntax-highlighter 0.8.4-main.b97322e → 0.8.4-main.bc2380dfbc

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 (41) hide show
  1. package/LICENSE +102 -5
  2. package/dist/lib/browser/index.mjs +192 -87
  3. package/dist/lib/browser/index.mjs.map +4 -4
  4. package/dist/lib/browser/meta.json +1 -1
  5. package/dist/lib/node-esm/index.mjs +192 -87
  6. package/dist/lib/node-esm/index.mjs.map +4 -4
  7. package/dist/lib/node-esm/meta.json +1 -1
  8. package/dist/types/src/JsonHighlighter/JsonHighlighter.d.ts +23 -0
  9. package/dist/types/src/JsonHighlighter/JsonHighlighter.d.ts.map +1 -0
  10. package/dist/types/src/JsonHighlighter/JsonHighlighter.stories.d.ts +14 -0
  11. package/dist/types/src/JsonHighlighter/JsonHighlighter.stories.d.ts.map +1 -0
  12. package/dist/types/src/JsonHighlighter/index.d.ts +2 -0
  13. package/dist/types/src/JsonHighlighter/index.d.ts.map +1 -0
  14. package/dist/types/src/Syntax/Syntax.d.ts +49 -0
  15. package/dist/types/src/Syntax/Syntax.d.ts.map +1 -0
  16. package/dist/types/src/Syntax/Syntax.stories.d.ts +23 -0
  17. package/dist/types/src/Syntax/Syntax.stories.d.ts.map +1 -0
  18. package/dist/types/src/Syntax/index.d.ts +2 -0
  19. package/dist/types/src/Syntax/index.d.ts.map +1 -0
  20. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts +17 -5
  21. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.d.ts.map +1 -1
  22. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts +11 -3
  23. package/dist/types/src/SyntaxHighlighter/SyntaxHighlighter.stories.d.ts.map +1 -1
  24. package/dist/types/src/index.d.ts +2 -1
  25. package/dist/types/src/index.d.ts.map +1 -1
  26. package/dist/types/tsconfig.tsbuildinfo +1 -1
  27. package/package.json +27 -22
  28. package/src/JsonHighlighter/JsonHighlighter.stories.tsx +65 -0
  29. package/src/JsonHighlighter/JsonHighlighter.tsx +47 -0
  30. package/src/JsonHighlighter/index.ts +5 -0
  31. package/src/Syntax/Syntax.stories.tsx +99 -0
  32. package/src/Syntax/Syntax.tsx +229 -0
  33. package/src/{Json → Syntax}/index.ts +1 -1
  34. package/src/SyntaxHighlighter/SyntaxHighlighter.stories.tsx +21 -15
  35. package/src/SyntaxHighlighter/SyntaxHighlighter.tsx +102 -37
  36. package/src/index.ts +2 -1
  37. package/dist/types/src/Json/Json.d.ts +0 -9
  38. package/dist/types/src/Json/Json.d.ts.map +0 -1
  39. package/dist/types/src/Json/index.d.ts +0 -2
  40. package/dist/types/src/Json/index.d.ts.map +0 -1
  41. package/src/Json/Json.tsx +0 -57
@@ -2,48 +2,113 @@
2
2
  // Copyright 2024 DXOS.org
3
3
  //
4
4
 
5
- import React from 'react';
6
- import { type SyntaxHighlighterProps as NativeSyntaxHighlighterProps } from 'react-syntax-highlighter';
7
- // Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
8
- // Lightweight version will load specific language parsers asynchronously.
9
- // eslint-disable-next-line no-restricted-imports
10
- import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/light-async';
11
- // eslint-disable-next-line no-restricted-imports
12
- import { github as light, a11yDark as dark } from 'react-syntax-highlighter/dist/esm/styles/hljs';
13
-
14
- import { type ThemedClassName, useThemeContext } from '@dxos/react-ui';
15
- import { mx } from '@dxos/react-ui-theme';
5
+ import React, { Children } from 'react';
6
+ import { type SyntaxHighlighterProps as NaturalSyntaxHighlighterProps } from 'react-syntax-highlighter';
7
+ import NativeSyntaxHighlighter from 'react-syntax-highlighter/dist/esm/prism-async-light';
8
+ import { coldarkDark as dark, coldarkCold as light } from 'react-syntax-highlighter/dist/esm/styles/prism';
9
+
10
+ import { Clipboard, useThemeContext } from '@dxos/react-ui';
11
+ import { composable, composableProps } from '@dxos/ui-theme';
16
12
 
17
13
  const zeroWidthSpace = '\u200b';
18
14
 
19
- export type SyntaxHighlighterProps = ThemedClassName<
20
- NativeSyntaxHighlighterProps & {
21
- fallback?: string;
22
- }
23
- >;
15
+ const languages = {
16
+ js: 'javascript',
17
+ ts: 'typescript',
18
+ };
24
19
 
25
- light.hljs.background = '';
26
- dark.hljs.background = '';
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'];
32
+ fallback?: string;
33
+ copyButton?: boolean;
34
+ };
27
35
 
28
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
+ *
42
+ * NOTE: Using `light-async` version directly from dist to avoid any chance of the heavy one being loaded.
43
+ * The lightweight version will load specific language parsers asynchronously.
44
+ *
29
45
  * https://github.com/react-syntax-highlighter/react-syntax-highlighter
46
+ * https://react-syntax-highlighter.github.io/react-syntax-highlighter/demo/prism.html
30
47
  */
31
- export const SyntaxHighlighter = ({
32
- classNames,
33
- children,
34
- fallback = zeroWidthSpace,
35
- ...props
36
- }: SyntaxHighlighterProps) => {
37
- const { themeMode } = useThemeContext();
38
-
39
- return (
40
- <NativeSyntaxHighlighter
41
- className={mx('w-full p-0 font-thin overflow-auto scrollbar-thin !text-baseText', classNames)}
42
- style={themeMode === 'dark' ? dark : light}
43
- {...props}
44
- >
45
- {/* Non-empty fallback prevents collapse. */}
46
- {children || fallback}
47
- </NativeSyntaxHighlighter>
48
- );
49
- };
48
+ export const SyntaxHighlighter = composable<HTMLDivElement, SyntaxHighlighterProps>(
49
+ (
50
+ {
51
+ classNames,
52
+ className,
53
+ children,
54
+ role,
55
+ style,
56
+ themeStyle,
57
+ language = 'text',
58
+ fallback = zeroWidthSpace,
59
+ copyButton,
60
+ ...nativeProps
61
+ },
62
+ forwardedRef,
63
+ ) => {
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;
69
+
70
+ return (
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 className='pointer-events-none absolute top-1 right-1 z-10 opacity-0 group-hover:opacity-100 focus-within:opacity-100'>
99
+ <Clipboard.Provider>
100
+ <Clipboard.IconButton
101
+ value={source}
102
+ variant='ghost'
103
+ size={4}
104
+ classNames='pointer-events-auto aspect-square rounded-sm'
105
+ />
106
+ </Clipboard.Provider>
107
+ </div>
108
+ )}
109
+ </div>
110
+ );
111
+ },
112
+ );
113
+
114
+ SyntaxHighlighter.displayName = 'SyntaxHighlighter';
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,9 +0,0 @@
1
- import React from 'react';
2
- import { type ThemedClassName } from '@dxos/react-ui';
3
- export type JsonProps = ThemedClassName<{
4
- data?: any;
5
- testId?: string;
6
- }>;
7
- export declare const Json: ({ data, classNames, testId }: JsonProps) => React.JSX.Element;
8
- export declare const JsonFilter: ({ data: initialData, classNames, testId }: JsonProps) => React.JSX.Element;
9
- //# sourceMappingURL=Json.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"Json.d.ts","sourceRoot":"","sources":["../../../../src/Json/Json.tsx"],"names":[],"mappings":"AAMA,OAAO,KAA8B,MAAM,OAAO,CAAC;AAEnD,OAAO,EAAS,KAAK,eAAe,EAAE,MAAM,gBAAgB,CAAC;AAI7D,MAAM,MAAM,SAAS,GAAG,eAAe,CAAC;IAAE,IAAI,CAAC,EAAE,GAAG,CAAC;IAAC,MAAM,CAAC,EAAE,MAAM,CAAA;CAAE,CAAC,CAAC;AAEzE,eAAO,MAAM,IAAI,GAAI,8BAA8B,SAAS,sBAM3D,CAAC;AAEF,eAAO,MAAM,UAAU,GAAI,2CAA2C,SAAS,sBAkC9E,CAAC"}
@@ -1,2 +0,0 @@
1
- export * 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,cAAc,QAAQ,CAAC"}
package/src/Json/Json.tsx DELETED
@@ -1,57 +0,0 @@
1
- //
2
- // Copyright 2025 DXOS.org
3
- //
4
-
5
- // TODO(burdon): Move to jsonpath-plus.
6
- import jp from 'jsonpath';
7
- import React, { useEffect, useState } from 'react';
8
-
9
- import { Input, type ThemedClassName } from '@dxos/react-ui';
10
-
11
- import { SyntaxHighlighter } from '../SyntaxHighlighter';
12
-
13
- export type JsonProps = ThemedClassName<{ data?: any; testId?: string }>;
14
-
15
- export const Json = ({ data, classNames, testId }: JsonProps) => {
16
- return (
17
- <SyntaxHighlighter language='json' classNames={['w-full', classNames]} data-testid={testId}>
18
- {JSON.stringify(data, null, 2)}
19
- </SyntaxHighlighter>
20
- );
21
- };
22
-
23
- export const JsonFilter = ({ data: initialData, classNames, testId }: JsonProps) => {
24
- const [data, setData] = useState(initialData);
25
- const [text, setText] = useState('');
26
- const [error, setError] = useState<Error | null>(null);
27
- useEffect(() => {
28
- if (!initialData || !text.trim().length) {
29
- setData(initialData);
30
- } else {
31
- try {
32
- setData(jp.query(initialData, text));
33
- setError(null);
34
- } catch (err) {
35
- setData(initialData);
36
- setError(err as Error);
37
- }
38
- }
39
- }, [initialData, text]); // TODO(burdon): Need structural diff.
40
-
41
- return (
42
- <div className='flex flex-col grow overflow-hidden'>
43
- <Input.Root validationValence={error ? 'error' : 'success'}>
44
- <Input.TextInput
45
- classNames={['p-1 px-2 font-mono', error && 'border-red-500']}
46
- variant='subdued'
47
- value={text}
48
- onChange={(event) => setText(event.target.value)}
49
- placeholder='JSONPath (e.g., $.graph.nodes)'
50
- />
51
- </Input.Root>
52
- <SyntaxHighlighter language='json' classNames={['grow overflow-y-auto', classNames]} data-testid={testId}>
53
- {JSON.stringify(data, null, 2)}
54
- </SyntaxHighlighter>
55
- </div>
56
- );
57
- };